Branch data Line data Source code
1 : : /* Forward propagation of expressions for single use variables.
2 : : Copyright (C) 2004-2025 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 statment. */
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 : 32144586 : fwprop_set_lattice_val (tree name, tree val)
222 : : {
223 : 32144586 : if (TREE_CODE (name) == SSA_NAME)
224 : : {
225 : 32144586 : if (SSA_NAME_VERSION (name) >= lattice.length ())
226 : : {
227 : 1176 : lattice.reserve (num_ssa_names - lattice.length ());
228 : 784 : lattice.quick_grow_cleared (num_ssa_names);
229 : : }
230 : 32144586 : lattice[SSA_NAME_VERSION (name)] = val;
231 : : /* As this now constitutes a copy duplicate points-to
232 : : and range info appropriately. */
233 : 32144586 : if (TREE_CODE (val) == SSA_NAME)
234 : 31688833 : maybe_duplicate_ssa_info_at_copy (name, val);
235 : : }
236 : 32144586 : }
237 : :
238 : : /* Invalidate the lattice entry for NAME, done when releasing SSA names. */
239 : : static void
240 : 879494 : fwprop_invalidate_lattice (tree name)
241 : : {
242 : 879494 : if (name
243 : 877208 : && TREE_CODE (name) == SSA_NAME
244 : 1756595 : && SSA_NAME_VERSION (name) < lattice.length ())
245 : 877072 : lattice[SSA_NAME_VERSION (name)] = NULL_TREE;
246 : 879494 : }
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 : 27103171 : get_prop_source_stmt (tree name, bool single_use_only, bool *single_use_p)
258 : : {
259 : 27103171 : bool single_use = true;
260 : :
261 : 27104143 : do {
262 : 27103657 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
263 : :
264 : 27103657 : if (!has_single_use (name))
265 : : {
266 : 14645480 : single_use = false;
267 : 14645480 : 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 : 27102255 : if (!is_gimple_assign (def_stmt))
273 : : return NULL;
274 : :
275 : : /* If def_stmt is a simple copy, continue looking. */
276 : 18976987 : if (gimple_assign_rhs_code (def_stmt) == SSA_NAME)
277 : 486 : name = gimple_assign_rhs1 (def_stmt);
278 : : else
279 : : {
280 : 18976501 : if (!single_use_only && single_use_p)
281 : 18678846 : *single_use_p = single_use;
282 : :
283 : 18976501 : return def_stmt;
284 : : }
285 : 486 : } 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 : 26900851 : can_propagate_from (gimple *def_stmt)
293 : : {
294 : 26900851 : gcc_assert (is_gimple_assign (def_stmt));
295 : :
296 : : /* If the rhs has side-effects we cannot propagate from it. */
297 : 26900851 : if (gimple_has_volatile_ops (def_stmt))
298 : : return false;
299 : :
300 : : /* If the rhs is a load we cannot propagate from it. */
301 : 26315776 : if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_reference
302 : 26315776 : || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_declaration)
303 : : return false;
304 : :
305 : : /* Constants can be always propagated. */
306 : 13158764 : if (gimple_assign_single_p (def_stmt)
307 : 13158764 : && 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 : 13158764 : 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 : 13158133 : if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
319 : : {
320 : 3143789 : tree rhs = gimple_assign_rhs1 (def_stmt);
321 : 3143789 : 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 : : Returns true if cleanup-cfg has to run. */
334 : :
335 : : static bool
336 : 217068 : remove_prop_source_from_use (tree name)
337 : : {
338 : 217068 : gimple_stmt_iterator gsi;
339 : 217068 : gimple *stmt;
340 : 217068 : bool cfg_changed = false;
341 : :
342 : 275286 : do {
343 : 275286 : basic_block bb;
344 : :
345 : 275286 : if (SSA_NAME_IN_FREE_LIST (name)
346 : 275243 : || SSA_NAME_IS_DEFAULT_DEF (name)
347 : 547197 : || !has_zero_uses (name))
348 : : return cfg_changed;
349 : :
350 : 58668 : stmt = SSA_NAME_DEF_STMT (name);
351 : 58668 : if (gimple_code (stmt) == GIMPLE_PHI
352 : 58668 : || gimple_has_side_effects (stmt))
353 : 0 : return cfg_changed;
354 : :
355 : 58668 : bb = gimple_bb (stmt);
356 : 58668 : gsi = gsi_for_stmt (stmt);
357 : 58668 : unlink_stmt_vdef (stmt);
358 : 58668 : if (gsi_remove (&gsi, true))
359 : 6 : bitmap_set_bit (to_purge, bb->index);
360 : 58668 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
361 : 58668 : release_defs (stmt);
362 : :
363 : 58668 : name = is_gimple_assign (stmt) ? gimple_assign_rhs1 (stmt) : NULL_TREE;
364 : 58668 : } while (name && TREE_CODE (name) == SSA_NAME);
365 : :
366 : : return cfg_changed;
367 : : }
368 : :
369 : : /* Return the rhs of a gassign *STMT in a form of a single tree,
370 : : converted to type TYPE.
371 : :
372 : : This should disappear, but is needed so we can combine expressions and use
373 : : the fold() interfaces. Long term, we need to develop folding and combine
374 : : routines that deal with gimple exclusively . */
375 : :
376 : : static tree
377 : 7072880 : rhs_to_tree (tree type, gimple *stmt)
378 : : {
379 : 7072880 : location_t loc = gimple_location (stmt);
380 : 7072880 : enum tree_code code = gimple_assign_rhs_code (stmt);
381 : 7072880 : switch (get_gimple_rhs_class (code))
382 : : {
383 : 12180 : case GIMPLE_TERNARY_RHS:
384 : 12180 : return fold_build3_loc (loc, code, type, gimple_assign_rhs1 (stmt),
385 : : gimple_assign_rhs2 (stmt),
386 : 12180 : gimple_assign_rhs3 (stmt));
387 : 4934888 : case GIMPLE_BINARY_RHS:
388 : 4934888 : return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (stmt),
389 : 4934888 : gimple_assign_rhs2 (stmt));
390 : 1950327 : case GIMPLE_UNARY_RHS:
391 : 1950327 : return build1 (code, type, gimple_assign_rhs1 (stmt));
392 : 175485 : case GIMPLE_SINGLE_RHS:
393 : 175485 : return gimple_assign_rhs1 (stmt);
394 : 0 : default:
395 : 0 : gcc_unreachable ();
396 : : }
397 : : }
398 : :
399 : : /* Combine OP0 CODE OP1 in the context of a COND_EXPR. Returns
400 : : the folded result in a form suitable for COND_EXPR_COND or
401 : : NULL_TREE, if there is no suitable simplified form. If
402 : : INVARIANT_ONLY is true only gimple_min_invariant results are
403 : : considered simplified. */
404 : :
405 : : static tree
406 : 7974618 : combine_cond_expr_cond (gimple *stmt, enum tree_code code, tree type,
407 : : tree op0, tree op1, bool invariant_only)
408 : : {
409 : 7974618 : tree t;
410 : :
411 : 7974618 : gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
412 : :
413 : 7974618 : fold_defer_overflow_warnings ();
414 : 7974618 : t = fold_binary_loc (gimple_location (stmt), code, type, op0, op1);
415 : 7974618 : if (!t)
416 : : {
417 : 4498965 : fold_undefer_overflow_warnings (false, NULL, 0);
418 : 4498965 : return NULL_TREE;
419 : : }
420 : :
421 : : /* Require that we got a boolean type out if we put one in. */
422 : 3475653 : gcc_assert (TREE_CODE (TREE_TYPE (t)) == TREE_CODE (type));
423 : :
424 : : /* Canonicalize the combined condition for use in a COND_EXPR. */
425 : 3475653 : t = canonicalize_cond_expr_cond (t);
426 : :
427 : : /* Bail out if we required an invariant but didn't get one. */
428 : 3475653 : if (!t || (invariant_only && !is_gimple_min_invariant (t)))
429 : : {
430 : 3260216 : fold_undefer_overflow_warnings (false, NULL, 0);
431 : 3260216 : return NULL_TREE;
432 : : }
433 : :
434 : 215437 : bool nowarn = warning_suppressed_p (stmt, OPT_Wstrict_overflow);
435 : 215437 : fold_undefer_overflow_warnings (!nowarn, stmt, 0);
436 : :
437 : 215437 : return t;
438 : : }
439 : :
440 : : /* Combine the comparison OP0 CODE OP1 at LOC with the defining statements
441 : : of its operand. Return a new comparison tree or NULL_TREE if there
442 : : were no simplifying combines. */
443 : :
444 : : static tree
445 : 21342437 : forward_propagate_into_comparison_1 (gimple *stmt,
446 : : enum tree_code code, tree type,
447 : : tree op0, tree op1)
448 : : {
449 : 21342437 : tree tmp = NULL_TREE;
450 : 21342437 : tree rhs0 = NULL_TREE, rhs1 = NULL_TREE;
451 : 21342437 : bool single_use0_p = false, single_use1_p = false;
452 : :
453 : : /* For comparisons use the first operand, that is likely to
454 : : simplify comparisons against constants. */
455 : 21342437 : if (TREE_CODE (op0) == SSA_NAME)
456 : : {
457 : 21305813 : gimple *def_stmt = get_prop_source_stmt (op0, false, &single_use0_p);
458 : 21305813 : if (def_stmt && can_propagate_from (def_stmt))
459 : : {
460 : 5370381 : enum tree_code def_code = gimple_assign_rhs_code (def_stmt);
461 : 5370381 : bool invariant_only_p = !single_use0_p;
462 : :
463 : 5370381 : rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt);
464 : :
465 : : /* Always combine comparisons or conversions from booleans. */
466 : 5370381 : if (TREE_CODE (op1) == INTEGER_CST
467 : 5370381 : && ((CONVERT_EXPR_CODE_P (def_code)
468 : 822527 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs0, 0)))
469 : : == BOOLEAN_TYPE)
470 : 3507704 : || TREE_CODE_CLASS (def_code) == tcc_comparison))
471 : : invariant_only_p = false;
472 : :
473 : 5370381 : tmp = combine_cond_expr_cond (stmt, code, type,
474 : : rhs0, op1, invariant_only_p);
475 : 5370381 : if (tmp)
476 : : return tmp;
477 : : }
478 : : }
479 : :
480 : : /* If that wasn't successful, try the second operand. */
481 : 21134720 : if (TREE_CODE (op1) == SSA_NAME)
482 : : {
483 : 5264867 : gimple *def_stmt = get_prop_source_stmt (op1, false, &single_use1_p);
484 : 5264867 : if (def_stmt && can_propagate_from (def_stmt))
485 : : {
486 : 1702499 : rhs1 = rhs_to_tree (TREE_TYPE (op0), def_stmt);
487 : 3404998 : tmp = combine_cond_expr_cond (stmt, code, type,
488 : 1702499 : op0, rhs1, !single_use1_p);
489 : 1702499 : if (tmp)
490 : : return tmp;
491 : : }
492 : : }
493 : :
494 : : /* If that wasn't successful either, try both operands. */
495 : 21128940 : if (rhs0 != NULL_TREE
496 : 21128940 : && rhs1 != NULL_TREE)
497 : 901738 : tmp = combine_cond_expr_cond (stmt, code, type,
498 : : rhs0, rhs1,
499 : 901738 : !(single_use0_p && single_use1_p));
500 : :
501 : : return tmp;
502 : : }
503 : :
504 : : /* Propagate from the ssa name definition statements of the assignment
505 : : from a comparison at *GSI into the conditional if that simplifies it.
506 : : Returns 1 if the stmt was modified and 2 if the CFG needs cleanup,
507 : : otherwise returns 0. */
508 : :
509 : : static int
510 : 2500160 : forward_propagate_into_comparison (gimple_stmt_iterator *gsi)
511 : : {
512 : 2500160 : gimple *stmt = gsi_stmt (*gsi);
513 : 2500160 : tree tmp;
514 : 2500160 : bool cfg_changed = false;
515 : 2500160 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
516 : 2500160 : tree rhs1 = gimple_assign_rhs1 (stmt);
517 : 2500160 : tree rhs2 = gimple_assign_rhs2 (stmt);
518 : :
519 : : /* Combine the comparison with defining statements. */
520 : 2500160 : tmp = forward_propagate_into_comparison_1 (stmt,
521 : : gimple_assign_rhs_code (stmt),
522 : : type, rhs1, rhs2);
523 : 2500160 : if (tmp && useless_type_conversion_p (type, TREE_TYPE (tmp)))
524 : : {
525 : 6725 : if (dump_file)
526 : : {
527 : 0 : fprintf (dump_file, " Replaced '");
528 : 0 : print_gimple_expr (dump_file, stmt, 0);
529 : 0 : fprintf (dump_file, "' with '");
530 : 0 : print_generic_expr (dump_file, tmp);
531 : 0 : fprintf (dump_file, "'\n");
532 : : }
533 : 6725 : gimple_assign_set_rhs_from_tree (gsi, tmp);
534 : 6725 : fold_stmt (gsi);
535 : 6725 : update_stmt (gsi_stmt (*gsi));
536 : :
537 : 6725 : if (TREE_CODE (rhs1) == SSA_NAME)
538 : 6725 : cfg_changed |= remove_prop_source_from_use (rhs1);
539 : 6725 : if (TREE_CODE (rhs2) == SSA_NAME)
540 : 2683 : cfg_changed |= remove_prop_source_from_use (rhs2);
541 : 6725 : return cfg_changed ? 2 : 1;
542 : : }
543 : :
544 : : return 0;
545 : : }
546 : :
547 : : /* Propagate from the ssa name definition statements of COND_EXPR
548 : : in GIMPLE_COND statement STMT into the conditional if that simplifies it.
549 : : Returns zero if no statement was changed, one if there were
550 : : changes and two if cfg_cleanup needs to run. */
551 : :
552 : : static int
553 : 18842277 : forward_propagate_into_gimple_cond (gcond *stmt)
554 : : {
555 : 18842277 : tree tmp;
556 : 18842277 : enum tree_code code = gimple_cond_code (stmt);
557 : 18842277 : bool cfg_changed = false;
558 : 18842277 : tree rhs1 = gimple_cond_lhs (stmt);
559 : 18842277 : tree rhs2 = gimple_cond_rhs (stmt);
560 : :
561 : : /* GIMPLE_COND will always be a comparison. */
562 : 18842277 : gcc_assert (TREE_CODE_CLASS (gimple_cond_code (stmt)) == tcc_comparison);
563 : :
564 : 18842277 : tmp = forward_propagate_into_comparison_1 (stmt, code,
565 : : boolean_type_node,
566 : : rhs1, rhs2);
567 : 18842277 : if (tmp
568 : 18842277 : && is_gimple_condexpr_for_cond (tmp))
569 : : {
570 : 202384 : if (dump_file)
571 : : {
572 : 9 : fprintf (dump_file, " Replaced '");
573 : 9 : print_gimple_expr (dump_file, stmt, 0);
574 : 9 : fprintf (dump_file, "' with '");
575 : 9 : print_generic_expr (dump_file, tmp);
576 : 9 : fprintf (dump_file, "'\n");
577 : : }
578 : :
579 : 202384 : gimple_cond_set_condition_from_tree (stmt, unshare_expr (tmp));
580 : 202384 : update_stmt (stmt);
581 : :
582 : 202384 : if (TREE_CODE (rhs1) == SSA_NAME)
583 : 202384 : cfg_changed |= remove_prop_source_from_use (rhs1);
584 : 202384 : if (TREE_CODE (rhs2) == SSA_NAME)
585 : 5275 : cfg_changed |= remove_prop_source_from_use (rhs2);
586 : 202384 : return (cfg_changed || is_gimple_min_invariant (tmp)) ? 2 : 1;
587 : : }
588 : :
589 : 18639893 : if (canonicalize_bool_cond (stmt, gimple_bb (stmt)))
590 : : return 1;
591 : :
592 : : return 0;
593 : : }
594 : :
595 : : /* We've just substituted an ADDR_EXPR into stmt. Update all the
596 : : relevant data structures to match. */
597 : :
598 : : static void
599 : 2120341 : tidy_after_forward_propagate_addr (gimple *stmt)
600 : : {
601 : : /* We may have turned a trapping insn into a non-trapping insn. */
602 : 2120341 : if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
603 : 8 : bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
604 : :
605 : 2120341 : if (TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
606 : 251533 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
607 : 2120341 : }
608 : :
609 : : /* NAME is a SSA_NAME representing DEF_RHS which is of the form
610 : : ADDR_EXPR <whatever>.
611 : :
612 : : Try to forward propagate the ADDR_EXPR into the use USE_STMT.
613 : : Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
614 : : node or for recovery of array indexing from pointer arithmetic.
615 : :
616 : : Return true if the propagation was successful (the propagation can
617 : : be not totally successful, yet things may have been changed). */
618 : :
619 : : static bool
620 : 2950387 : forward_propagate_addr_expr_1 (tree name, tree def_rhs,
621 : : gimple_stmt_iterator *use_stmt_gsi,
622 : : bool single_use_p)
623 : : {
624 : 2950387 : tree lhs, rhs, rhs2, array_ref;
625 : 2950387 : gimple *use_stmt = gsi_stmt (*use_stmt_gsi);
626 : 2950387 : enum tree_code rhs_code;
627 : 2950387 : bool res = true;
628 : :
629 : 2950387 : gcc_assert (TREE_CODE (def_rhs) == ADDR_EXPR);
630 : :
631 : 2950387 : lhs = gimple_assign_lhs (use_stmt);
632 : 2950387 : rhs_code = gimple_assign_rhs_code (use_stmt);
633 : 2950387 : rhs = gimple_assign_rhs1 (use_stmt);
634 : :
635 : : /* Do not perform copy-propagation but recurse through copy chains. */
636 : 2950387 : if (TREE_CODE (lhs) == SSA_NAME
637 : 1357828 : && rhs_code == SSA_NAME)
638 : 6084 : return forward_propagate_addr_expr (lhs, def_rhs, single_use_p);
639 : :
640 : : /* The use statement could be a conversion. Recurse to the uses of the
641 : : lhs as copyprop does not copy through pointer to integer to pointer
642 : : conversions and FRE does not catch all cases either.
643 : : Treat the case of a single-use name and
644 : : a conversion to def_rhs type separate, though. */
645 : 2944303 : if (TREE_CODE (lhs) == SSA_NAME
646 : 1351744 : && CONVERT_EXPR_CODE_P (rhs_code))
647 : : {
648 : : /* If there is a point in a conversion chain where the types match
649 : : so we can remove a conversion re-materialize the address here
650 : : and stop. */
651 : 25651 : if (single_use_p
652 : 25651 : && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs)))
653 : : {
654 : 2 : gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs));
655 : 2 : gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs));
656 : 2 : return true;
657 : : }
658 : :
659 : : /* Else recurse if the conversion preserves the address value. */
660 : 51298 : if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
661 : 1 : || POINTER_TYPE_P (TREE_TYPE (lhs)))
662 : 51298 : && (TYPE_PRECISION (TREE_TYPE (lhs))
663 : 25649 : >= TYPE_PRECISION (TREE_TYPE (def_rhs))))
664 : 25582 : return forward_propagate_addr_expr (lhs, def_rhs, single_use_p);
665 : :
666 : : return false;
667 : : }
668 : :
669 : : /* If this isn't a conversion chain from this on we only can propagate
670 : : into compatible pointer contexts. */
671 : 2918652 : if (!types_compatible_p (TREE_TYPE (name), TREE_TYPE (def_rhs)))
672 : : return false;
673 : :
674 : : /* Propagate through constant pointer adjustments. */
675 : 2896242 : if (TREE_CODE (lhs) == SSA_NAME
676 : 1304865 : && rhs_code == POINTER_PLUS_EXPR
677 : 1304865 : && rhs == name
678 : 3053111 : && TREE_CODE (gimple_assign_rhs2 (use_stmt)) == INTEGER_CST)
679 : : {
680 : 118179 : tree new_def_rhs;
681 : : /* As we come here with non-invariant addresses in def_rhs we need
682 : : to make sure we can build a valid constant offsetted address
683 : : for further propagation. Simply rely on fold building that
684 : : and check after the fact. */
685 : 118179 : new_def_rhs = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (rhs)),
686 : : def_rhs,
687 : : fold_convert (ptr_type_node,
688 : : gimple_assign_rhs2 (use_stmt)));
689 : 118179 : if (TREE_CODE (new_def_rhs) == MEM_REF
690 : 118179 : && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs, 0)))
691 : : return false;
692 : 114690 : new_def_rhs = build1 (ADDR_EXPR, TREE_TYPE (rhs), new_def_rhs);
693 : :
694 : : /* Recurse. If we could propagate into all uses of lhs do not
695 : : bother to replace into the current use but just pretend we did. */
696 : 114690 : if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p))
697 : : return true;
698 : :
699 : 37605 : if (useless_type_conversion_p (TREE_TYPE (lhs),
700 : 37605 : TREE_TYPE (new_def_rhs)))
701 : 37605 : gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs),
702 : : new_def_rhs);
703 : 0 : else if (is_gimple_min_invariant (new_def_rhs))
704 : 0 : gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR, new_def_rhs);
705 : : else
706 : : return false;
707 : 37605 : gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt);
708 : 37605 : update_stmt (use_stmt);
709 : 37605 : return true;
710 : : }
711 : :
712 : : /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
713 : : ADDR_EXPR will not appear on the LHS. */
714 : 2778063 : tree *lhsp = gimple_assign_lhs_ptr (use_stmt);
715 : 4139251 : while (handled_component_p (*lhsp))
716 : 1361188 : lhsp = &TREE_OPERAND (*lhsp, 0);
717 : 2778063 : lhs = *lhsp;
718 : :
719 : : /* Now see if the LHS node is a MEM_REF using NAME. If so,
720 : : propagate the ADDR_EXPR into the use of NAME and fold the result. */
721 : 2778063 : if (TREE_CODE (lhs) == MEM_REF
722 : 2778063 : && TREE_OPERAND (lhs, 0) == name)
723 : : {
724 : 1043477 : tree def_rhs_base;
725 : 1043477 : poly_int64 def_rhs_offset;
726 : : /* If the address is invariant we can always fold it. */
727 : 1043477 : if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
728 : : &def_rhs_offset)))
729 : : {
730 : 1003278 : poly_offset_int off = mem_ref_offset (lhs);
731 : 1003278 : tree new_ptr;
732 : 1003278 : off += def_rhs_offset;
733 : 1003278 : if (TREE_CODE (def_rhs_base) == MEM_REF)
734 : : {
735 : 978332 : off += mem_ref_offset (def_rhs_base);
736 : 978332 : new_ptr = TREE_OPERAND (def_rhs_base, 0);
737 : : }
738 : : else
739 : 24946 : new_ptr = build_fold_addr_expr (def_rhs_base);
740 : 1003278 : TREE_OPERAND (lhs, 0) = new_ptr;
741 : 1003278 : TREE_OPERAND (lhs, 1)
742 : 1003278 : = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs, 1)), off);
743 : 1003278 : tidy_after_forward_propagate_addr (use_stmt);
744 : : /* Continue propagating into the RHS if this was not the only use. */
745 : 1003278 : if (single_use_p)
746 : 192267 : return true;
747 : : }
748 : : /* If the LHS is a plain dereference and the value type is the same as
749 : : that of the pointed-to type of the address we can put the
750 : : dereferenced address on the LHS preserving the original alias-type. */
751 : 40199 : else if (integer_zerop (TREE_OPERAND (lhs, 1))
752 : 16068 : && ((gimple_assign_lhs (use_stmt) == lhs
753 : 12753 : && useless_type_conversion_p
754 : 12753 : (TREE_TYPE (TREE_OPERAND (def_rhs, 0)),
755 : 12753 : TREE_TYPE (gimple_assign_rhs1 (use_stmt))))
756 : 12279 : || types_compatible_p (TREE_TYPE (lhs),
757 : 12279 : TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
758 : : /* Don't forward anything into clobber stmts if it would result
759 : : in the lhs no longer being a MEM_REF. */
760 : 46948 : && (!gimple_clobber_p (use_stmt)
761 : 320 : || TREE_CODE (TREE_OPERAND (def_rhs, 0)) == MEM_REF))
762 : : {
763 : 6429 : tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
764 : 6429 : tree new_offset, new_base, saved, new_lhs;
765 : 22448 : while (handled_component_p (*def_rhs_basep))
766 : 9590 : def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
767 : 6429 : saved = *def_rhs_basep;
768 : 6429 : if (TREE_CODE (*def_rhs_basep) == MEM_REF)
769 : : {
770 : 3561 : new_base = TREE_OPERAND (*def_rhs_basep, 0);
771 : 3561 : new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (lhs, 1)),
772 : : TREE_OPERAND (*def_rhs_basep, 1));
773 : : }
774 : : else
775 : : {
776 : 2868 : new_base = build_fold_addr_expr (*def_rhs_basep);
777 : 2868 : new_offset = TREE_OPERAND (lhs, 1);
778 : : }
779 : 6429 : *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep),
780 : : new_base, new_offset);
781 : 6429 : TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (lhs);
782 : 6429 : TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (lhs);
783 : 6429 : TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (lhs);
784 : 6429 : new_lhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
785 : 6429 : *lhsp = new_lhs;
786 : 6429 : TREE_THIS_VOLATILE (new_lhs) = TREE_THIS_VOLATILE (lhs);
787 : 6429 : TREE_SIDE_EFFECTS (new_lhs) = TREE_SIDE_EFFECTS (lhs);
788 : 6429 : *def_rhs_basep = saved;
789 : 6429 : tidy_after_forward_propagate_addr (use_stmt);
790 : : /* Continue propagating into the RHS if this was not the
791 : : only use. */
792 : 6429 : if (single_use_p)
793 : : return true;
794 : : }
795 : : else
796 : : /* We can have a struct assignment dereferencing our name twice.
797 : : Note that we didn't propagate into the lhs to not falsely
798 : : claim we did when propagating into the rhs. */
799 : : res = false;
800 : : }
801 : :
802 : : /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
803 : : nodes from the RHS. */
804 : 2583311 : tree *rhsp = gimple_assign_rhs1_ptr (use_stmt);
805 : 2583311 : if (TREE_CODE (*rhsp) == ADDR_EXPR)
806 : 240712 : rhsp = &TREE_OPERAND (*rhsp, 0);
807 : 3576459 : while (handled_component_p (*rhsp))
808 : 993148 : rhsp = &TREE_OPERAND (*rhsp, 0);
809 : 2583311 : rhs = *rhsp;
810 : :
811 : : /* Now see if the RHS node is a MEM_REF using NAME. If so,
812 : : propagate the ADDR_EXPR into the use of NAME and fold the result. */
813 : 2583311 : if (TREE_CODE (rhs) == MEM_REF
814 : 2583311 : && TREE_OPERAND (rhs, 0) == name)
815 : : {
816 : 1119927 : tree def_rhs_base;
817 : 1119927 : poly_int64 def_rhs_offset;
818 : 1119927 : if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
819 : : &def_rhs_offset)))
820 : : {
821 : 1096310 : poly_offset_int off = mem_ref_offset (rhs);
822 : 1096310 : tree new_ptr;
823 : 1096310 : off += def_rhs_offset;
824 : 1096310 : if (TREE_CODE (def_rhs_base) == MEM_REF)
825 : : {
826 : 1063912 : off += mem_ref_offset (def_rhs_base);
827 : 1063912 : new_ptr = TREE_OPERAND (def_rhs_base, 0);
828 : : }
829 : : else
830 : 32398 : new_ptr = build_fold_addr_expr (def_rhs_base);
831 : 1096310 : TREE_OPERAND (rhs, 0) = new_ptr;
832 : 1096310 : TREE_OPERAND (rhs, 1)
833 : 1096310 : = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs, 1)), off);
834 : 1096310 : fold_stmt_inplace (use_stmt_gsi);
835 : 1096310 : tidy_after_forward_propagate_addr (use_stmt);
836 : 1096310 : return res;
837 : : }
838 : : /* If the RHS is a plain dereference and the value type is the same as
839 : : that of the pointed-to type of the address we can put the
840 : : dereferenced address on the RHS preserving the original alias-type. */
841 : 23617 : else if (integer_zerop (TREE_OPERAND (rhs, 1))
842 : 23617 : && ((gimple_assign_rhs1 (use_stmt) == rhs
843 : 10056 : && useless_type_conversion_p
844 : 10056 : (TREE_TYPE (gimple_assign_lhs (use_stmt)),
845 : 10056 : TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
846 : 12893 : || types_compatible_p (TREE_TYPE (rhs),
847 : 12893 : TREE_TYPE (TREE_OPERAND (def_rhs, 0)))))
848 : : {
849 : 14324 : tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
850 : 14324 : tree new_offset, new_base, saved, new_rhs;
851 : 49915 : while (handled_component_p (*def_rhs_basep))
852 : 21267 : def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
853 : 14324 : saved = *def_rhs_basep;
854 : 14324 : if (TREE_CODE (*def_rhs_basep) == MEM_REF)
855 : : {
856 : 7099 : new_base = TREE_OPERAND (*def_rhs_basep, 0);
857 : 7099 : new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (rhs, 1)),
858 : : TREE_OPERAND (*def_rhs_basep, 1));
859 : : }
860 : : else
861 : : {
862 : 7225 : new_base = build_fold_addr_expr (*def_rhs_basep);
863 : 7225 : new_offset = TREE_OPERAND (rhs, 1);
864 : : }
865 : 14324 : *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep),
866 : : new_base, new_offset);
867 : 14324 : TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (rhs);
868 : 14324 : TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (rhs);
869 : 14324 : TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (rhs);
870 : 14324 : new_rhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
871 : 14324 : *rhsp = new_rhs;
872 : 14324 : TREE_THIS_VOLATILE (new_rhs) = TREE_THIS_VOLATILE (rhs);
873 : 14324 : TREE_SIDE_EFFECTS (new_rhs) = TREE_SIDE_EFFECTS (rhs);
874 : 14324 : *def_rhs_basep = saved;
875 : 14324 : fold_stmt_inplace (use_stmt_gsi);
876 : 14324 : tidy_after_forward_propagate_addr (use_stmt);
877 : 14324 : return res;
878 : : }
879 : : }
880 : :
881 : : /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there
882 : : is nothing to do. */
883 : 1472677 : if (gimple_assign_rhs_code (use_stmt) != POINTER_PLUS_EXPR
884 : 1472677 : || gimple_assign_rhs1 (use_stmt) != name)
885 : : return false;
886 : :
887 : : /* The remaining cases are all for turning pointer arithmetic into
888 : : array indexing. They only apply when we have the address of
889 : : element zero in an array. If that is not the case then there
890 : : is nothing to do. */
891 : 38690 : array_ref = TREE_OPERAND (def_rhs, 0);
892 : 38690 : if ((TREE_CODE (array_ref) != ARRAY_REF
893 : 4640 : || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
894 : 4640 : || TREE_CODE (TREE_OPERAND (array_ref, 1)) != INTEGER_CST)
895 : 40123 : && TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE)
896 : : return false;
897 : :
898 : 15414 : rhs2 = gimple_assign_rhs2 (use_stmt);
899 : : /* Optimize &x[C1] p+ C2 to &x p+ C3 with C3 = C1 * element_size + C2. */
900 : 15414 : if (TREE_CODE (rhs2) == INTEGER_CST)
901 : : {
902 : 0 : tree new_rhs = build1_loc (gimple_location (use_stmt),
903 : 0 : ADDR_EXPR, TREE_TYPE (def_rhs),
904 : 0 : fold_build2 (MEM_REF,
905 : : TREE_TYPE (TREE_TYPE (def_rhs)),
906 : : unshare_expr (def_rhs),
907 : : fold_convert (ptr_type_node,
908 : : rhs2)));
909 : 0 : gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs);
910 : 0 : use_stmt = gsi_stmt (*use_stmt_gsi);
911 : 0 : update_stmt (use_stmt);
912 : 0 : tidy_after_forward_propagate_addr (use_stmt);
913 : 0 : return true;
914 : : }
915 : :
916 : : return false;
917 : : }
918 : :
919 : : /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
920 : :
921 : : Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
922 : : Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
923 : : node or for recovery of array indexing from pointer arithmetic.
924 : :
925 : : PARENT_SINGLE_USE_P tells if, when in a recursive invocation, NAME was
926 : : the single use in the previous invocation. Pass true when calling
927 : : this as toplevel.
928 : :
929 : : Returns true, if all uses have been propagated into. */
930 : :
931 : : static bool
932 : 3262620 : forward_propagate_addr_expr (tree name, tree rhs, bool parent_single_use_p)
933 : : {
934 : 3262620 : imm_use_iterator iter;
935 : 3262620 : gimple *use_stmt;
936 : 3262620 : bool all = true;
937 : 3262620 : bool single_use_p = parent_single_use_p && has_single_use (name);
938 : :
939 : 10863230 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, name)
940 : : {
941 : 7600610 : bool result;
942 : 7600610 : tree use_rhs;
943 : :
944 : : /* If the use is not in a simple assignment statement, then
945 : : there is nothing we can do. */
946 : 7600610 : if (!is_gimple_assign (use_stmt))
947 : : {
948 : 4650223 : if (!is_gimple_debug (use_stmt))
949 : 1896535 : all = false;
950 : 4650223 : continue;
951 : : }
952 : :
953 : 2950387 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
954 : 2950387 : result = forward_propagate_addr_expr_1 (name, rhs, &gsi,
955 : : single_use_p);
956 : : /* If the use has moved to a different statement adjust
957 : : the update machinery for the old statement too. */
958 : 2950387 : if (use_stmt != gsi_stmt (gsi))
959 : : {
960 : 0 : update_stmt (use_stmt);
961 : 0 : use_stmt = gsi_stmt (gsi);
962 : : }
963 : 2950387 : update_stmt (use_stmt);
964 : 2950387 : all &= result;
965 : :
966 : : /* Remove intermediate now unused copy and conversion chains. */
967 : 2950387 : use_rhs = gimple_assign_rhs1 (use_stmt);
968 : 2950387 : if (result
969 : 1422092 : && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
970 : 1214942 : && TREE_CODE (use_rhs) == SSA_NAME
971 : 3029486 : && has_zero_uses (gimple_assign_lhs (use_stmt)))
972 : : {
973 : 79099 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
974 : 79099 : fwprop_invalidate_lattice (gimple_get_lhs (use_stmt));
975 : 79099 : release_defs (use_stmt);
976 : 79099 : gsi_remove (&gsi, true);
977 : : }
978 : 3262620 : }
979 : :
980 : 3262620 : return all && has_zero_uses (name);
981 : : }
982 : :
983 : :
984 : : /* Helper function for simplify_gimple_switch. Remove case labels that
985 : : have values outside the range of the new type. */
986 : :
987 : : static void
988 : 11535 : simplify_gimple_switch_label_vec (gswitch *stmt, tree index_type,
989 : : vec<std::pair<int, int> > &edges_to_remove)
990 : : {
991 : 11535 : unsigned int branch_num = gimple_switch_num_labels (stmt);
992 : 11535 : auto_vec<tree> labels (branch_num);
993 : 11535 : unsigned int i, len;
994 : :
995 : : /* Collect the existing case labels in a VEC, and preprocess it as if
996 : : we are gimplifying a GENERIC SWITCH_EXPR. */
997 : 74211 : for (i = 1; i < branch_num; i++)
998 : 51141 : labels.quick_push (gimple_switch_label (stmt, i));
999 : 11535 : preprocess_case_label_vec_for_gimple (labels, index_type, NULL);
1000 : :
1001 : : /* If any labels were removed, replace the existing case labels
1002 : : in the GIMPLE_SWITCH statement with the correct ones.
1003 : : Note that the type updates were done in-place on the case labels,
1004 : : so we only have to replace the case labels in the GIMPLE_SWITCH
1005 : : if the number of labels changed. */
1006 : 11535 : len = labels.length ();
1007 : 11535 : if (len < branch_num - 1)
1008 : : {
1009 : 0 : bitmap target_blocks;
1010 : 0 : edge_iterator ei;
1011 : 0 : edge e;
1012 : :
1013 : : /* Corner case: *all* case labels have been removed as being
1014 : : out-of-range for INDEX_TYPE. Push one label and let the
1015 : : CFG cleanups deal with this further. */
1016 : 0 : if (len == 0)
1017 : : {
1018 : 0 : tree label, elt;
1019 : :
1020 : 0 : label = CASE_LABEL (gimple_switch_default_label (stmt));
1021 : 0 : elt = build_case_label (build_int_cst (index_type, 0), NULL, label);
1022 : 0 : labels.quick_push (elt);
1023 : 0 : len = 1;
1024 : : }
1025 : :
1026 : 0 : for (i = 0; i < labels.length (); i++)
1027 : 0 : gimple_switch_set_label (stmt, i + 1, labels[i]);
1028 : 0 : for (i++ ; i < branch_num; i++)
1029 : 0 : gimple_switch_set_label (stmt, i, NULL_TREE);
1030 : 0 : gimple_switch_set_num_labels (stmt, len + 1);
1031 : :
1032 : : /* Cleanup any edges that are now dead. */
1033 : 0 : target_blocks = BITMAP_ALLOC (NULL);
1034 : 0 : for (i = 0; i < gimple_switch_num_labels (stmt); i++)
1035 : : {
1036 : 0 : tree elt = gimple_switch_label (stmt, i);
1037 : 0 : basic_block target = label_to_block (cfun, CASE_LABEL (elt));
1038 : 0 : bitmap_set_bit (target_blocks, target->index);
1039 : : }
1040 : 0 : for (ei = ei_start (gimple_bb (stmt)->succs); (e = ei_safe_edge (ei)); )
1041 : : {
1042 : 0 : if (! bitmap_bit_p (target_blocks, e->dest->index))
1043 : 0 : edges_to_remove.safe_push (std::make_pair (e->src->index,
1044 : 0 : e->dest->index));
1045 : : else
1046 : 0 : ei_next (&ei);
1047 : : }
1048 : 0 : BITMAP_FREE (target_blocks);
1049 : : }
1050 : 11535 : }
1051 : :
1052 : : /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
1053 : : the condition which we may be able to optimize better. */
1054 : :
1055 : : static bool
1056 : 115097 : simplify_gimple_switch (gswitch *stmt,
1057 : : vec<std::pair<int, int> > &edges_to_remove)
1058 : : {
1059 : : /* The optimization that we really care about is removing unnecessary
1060 : : casts. That will let us do much better in propagating the inferred
1061 : : constant at the switch target. */
1062 : 115097 : tree cond = gimple_switch_index (stmt);
1063 : 115097 : if (TREE_CODE (cond) == SSA_NAME)
1064 : : {
1065 : 115096 : gimple *def_stmt = SSA_NAME_DEF_STMT (cond);
1066 : 115096 : if (gimple_assign_cast_p (def_stmt))
1067 : : {
1068 : 12030 : tree def = gimple_assign_rhs1 (def_stmt);
1069 : 12030 : if (TREE_CODE (def) != SSA_NAME)
1070 : : return false;
1071 : :
1072 : : /* If we have an extension or sign-change that preserves the
1073 : : values we check against then we can copy the source value into
1074 : : the switch. */
1075 : 12030 : tree ti = TREE_TYPE (def);
1076 : 12030 : if (INTEGRAL_TYPE_P (ti)
1077 : 12030 : && TYPE_PRECISION (ti) <= TYPE_PRECISION (TREE_TYPE (cond)))
1078 : : {
1079 : 11786 : size_t n = gimple_switch_num_labels (stmt);
1080 : 11786 : tree min = NULL_TREE, max = NULL_TREE;
1081 : 11786 : if (n > 1)
1082 : : {
1083 : 11786 : min = CASE_LOW (gimple_switch_label (stmt, 1));
1084 : 11786 : if (CASE_HIGH (gimple_switch_label (stmt, n - 1)))
1085 : 149 : max = CASE_HIGH (gimple_switch_label (stmt, n - 1));
1086 : : else
1087 : 11637 : max = CASE_LOW (gimple_switch_label (stmt, n - 1));
1088 : : }
1089 : 11786 : if ((!min || int_fits_type_p (min, ti))
1090 : 11782 : && (!max || int_fits_type_p (max, ti)))
1091 : : {
1092 : 11535 : gimple_switch_set_index (stmt, def);
1093 : 11535 : simplify_gimple_switch_label_vec (stmt, ti,
1094 : : edges_to_remove);
1095 : 11535 : update_stmt (stmt);
1096 : 11535 : return true;
1097 : : }
1098 : : }
1099 : : }
1100 : : }
1101 : :
1102 : : return false;
1103 : : }
1104 : :
1105 : : /* For pointers p2 and p1 return p2 - p1 if the
1106 : : difference is known and constant, otherwise return NULL. */
1107 : :
1108 : : static tree
1109 : 5036 : constant_pointer_difference (tree p1, tree p2)
1110 : : {
1111 : 5036 : int i, j;
1112 : : #define CPD_ITERATIONS 5
1113 : 5036 : tree exps[2][CPD_ITERATIONS];
1114 : 5036 : tree offs[2][CPD_ITERATIONS];
1115 : 5036 : int cnt[2];
1116 : :
1117 : 15108 : for (i = 0; i < 2; i++)
1118 : : {
1119 : 10072 : tree p = i ? p1 : p2;
1120 : 10072 : tree off = size_zero_node;
1121 : 10072 : gimple *stmt;
1122 : 10072 : enum tree_code code;
1123 : :
1124 : : /* For each of p1 and p2 we need to iterate at least
1125 : : twice, to handle ADDR_EXPR directly in p1/p2,
1126 : : SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc.
1127 : : on definition's stmt RHS. Iterate a few extra times. */
1128 : 10072 : j = 0;
1129 : 11851 : do
1130 : : {
1131 : 11851 : if (!POINTER_TYPE_P (TREE_TYPE (p)))
1132 : : break;
1133 : 11845 : if (TREE_CODE (p) == ADDR_EXPR)
1134 : : {
1135 : 8672 : tree q = TREE_OPERAND (p, 0);
1136 : 8672 : poly_int64 offset;
1137 : 8672 : tree base = get_addr_base_and_unit_offset (q, &offset);
1138 : 8672 : if (base)
1139 : : {
1140 : 7942 : q = base;
1141 : 7942 : if (maybe_ne (offset, 0))
1142 : 3306 : off = size_binop (PLUS_EXPR, off, size_int (offset));
1143 : : }
1144 : 8672 : if (TREE_CODE (q) == MEM_REF
1145 : 8672 : && TREE_CODE (TREE_OPERAND (q, 0)) == SSA_NAME)
1146 : : {
1147 : 213 : p = TREE_OPERAND (q, 0);
1148 : 213 : off = size_binop (PLUS_EXPR, off,
1149 : : wide_int_to_tree (sizetype,
1150 : : mem_ref_offset (q)));
1151 : : }
1152 : : else
1153 : : {
1154 : 8459 : exps[i][j] = q;
1155 : 8459 : offs[i][j++] = off;
1156 : 8459 : break;
1157 : : }
1158 : : }
1159 : 3386 : if (TREE_CODE (p) != SSA_NAME)
1160 : : break;
1161 : 3386 : exps[i][j] = p;
1162 : 3386 : offs[i][j++] = off;
1163 : 3386 : if (j == CPD_ITERATIONS)
1164 : : break;
1165 : 3386 : stmt = SSA_NAME_DEF_STMT (p);
1166 : 3386 : if (!is_gimple_assign (stmt) || gimple_assign_lhs (stmt) != p)
1167 : : break;
1168 : 2583 : code = gimple_assign_rhs_code (stmt);
1169 : 2583 : if (code == POINTER_PLUS_EXPR)
1170 : : {
1171 : 1335 : if (TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
1172 : : break;
1173 : 880 : off = size_binop (PLUS_EXPR, off, gimple_assign_rhs2 (stmt));
1174 : 880 : p = gimple_assign_rhs1 (stmt);
1175 : : }
1176 : 1248 : else if (code == ADDR_EXPR || CONVERT_EXPR_CODE_P (code))
1177 : 899 : p = gimple_assign_rhs1 (stmt);
1178 : : else
1179 : : break;
1180 : : }
1181 : : while (1);
1182 : 10072 : cnt[i] = j;
1183 : : }
1184 : :
1185 : 6944 : for (i = 0; i < cnt[0]; i++)
1186 : 9128 : for (j = 0; j < cnt[1]; j++)
1187 : 7220 : if (exps[0][i] == exps[1][j])
1188 : 4200 : return size_binop (MINUS_EXPR, offs[0][i], offs[1][j]);
1189 : :
1190 : : return NULL_TREE;
1191 : : }
1192 : :
1193 : :
1194 : : /* Optimize
1195 : : a = {};
1196 : : b = a;
1197 : : into
1198 : : a = {};
1199 : : b = {};
1200 : : Similarly for memset (&a, ..., sizeof (a)); instead of a = {};
1201 : : and/or memcpy (&b, &a, sizeof (a)); instead of b = a; */
1202 : :
1203 : : static bool
1204 : 4484038 : optimize_memcpy_to_memset (gimple_stmt_iterator *gsip, tree dest, tree src, tree len)
1205 : : {
1206 : 4484038 : ao_ref read;
1207 : 4484038 : gimple *stmt = gsi_stmt (*gsip);
1208 : 8968076 : if (gimple_has_volatile_ops (stmt))
1209 : : return false;
1210 : :
1211 : 4482908 : tree src2 = NULL_TREE, len2 = NULL_TREE;
1212 : 4482908 : poly_int64 offset, offset2;
1213 : 4482908 : tree val = integer_zero_node;
1214 : 4482908 : bool len_was_null = len == NULL_TREE;
1215 : 4482908 : if (len == NULL_TREE)
1216 : 4444590 : len = (TREE_CODE (src) == COMPONENT_REF
1217 : 4444590 : ? DECL_SIZE_UNIT (TREE_OPERAND (src, 1))
1218 : 3761997 : : TYPE_SIZE_UNIT (TREE_TYPE (src)));
1219 : 4444590 : if (len == NULL_TREE
1220 : 4482908 : || !poly_int_tree_p (len))
1221 : : return false;
1222 : :
1223 : 4482908 : ao_ref_init (&read, src);
1224 : 4482908 : tree vuse = gimple_vuse (stmt);
1225 : 4482908 : gimple *defstmt;
1226 : 4482908 : unsigned limit = param_sccvn_max_alias_queries_per_access;
1227 : 19072445 : do {
1228 : : /* If the vuse is the default definition, then there is no stores beforhand. */
1229 : 19072445 : if (SSA_NAME_IS_DEFAULT_DEF (vuse))
1230 : : return false;
1231 : 18611285 : defstmt = SSA_NAME_DEF_STMT (vuse);
1232 : 18611285 : if (is_a <gphi*>(defstmt))
1233 : : return false;
1234 : 17843663 : if (limit-- == 0)
1235 : : return false;
1236 : : /* If the len was null, then we can use TBBA. */
1237 : 17842626 : if (stmt_may_clobber_ref_p_1 (defstmt, &read,
1238 : : /* tbaa_p = */ len_was_null))
1239 : : break;
1240 : 33661982 : vuse = gimple_vuse (defstmt);
1241 : : } while (true);
1242 : :
1243 : 3253089 : if (gimple_store_p (defstmt)
1244 : 2992577 : && gimple_assign_single_p (defstmt)
1245 : 2180546 : && TREE_CODE (gimple_assign_rhs1 (defstmt)) == STRING_CST
1246 : 3254169 : && !gimple_clobber_p (defstmt))
1247 : : {
1248 : 1080 : tree str = gimple_assign_rhs1 (defstmt);
1249 : 1080 : src2 = gimple_assign_lhs (defstmt);
1250 : : /* The string must contain all null char's for now. */
1251 : 1250 : for (int i = 0; i < TREE_STRING_LENGTH (str); i++)
1252 : : {
1253 : 1243 : if (TREE_STRING_POINTER (str)[i] != 0)
1254 : : {
1255 : : src2 = NULL_TREE;
1256 : : break;
1257 : : }
1258 : : }
1259 : : }
1260 : 3252009 : else if (gimple_store_p (defstmt)
1261 : 2991497 : && gimple_assign_single_p (defstmt)
1262 : 2179466 : && TREE_CODE (gimple_assign_rhs1 (defstmt)) == CONSTRUCTOR
1263 : 3290129 : && !gimple_clobber_p (defstmt))
1264 : 15616 : src2 = gimple_assign_lhs (defstmt);
1265 : 3236393 : else if (gimple_call_builtin_p (defstmt, BUILT_IN_MEMSET)
1266 : 517 : && TREE_CODE (gimple_call_arg (defstmt, 0)) == ADDR_EXPR
1267 : 3236771 : && TREE_CODE (gimple_call_arg (defstmt, 1)) == INTEGER_CST)
1268 : : {
1269 : 378 : src2 = TREE_OPERAND (gimple_call_arg (defstmt, 0), 0);
1270 : 378 : len2 = gimple_call_arg (defstmt, 2);
1271 : 378 : val = gimple_call_arg (defstmt, 1);
1272 : : /* For non-0 val, we'd have to transform stmt from assignment
1273 : : into memset (only if dest is addressable). */
1274 : 378 : if (!integer_zerop (val) && is_gimple_assign (stmt))
1275 : : src2 = NULL_TREE;
1276 : : }
1277 : :
1278 : 16877 : if (src2 == NULL_TREE)
1279 : 3237285 : return false;
1280 : :
1281 : 15804 : if (len2 == NULL_TREE)
1282 : 15623 : len2 = (TREE_CODE (src2) == COMPONENT_REF
1283 : 15623 : ? DECL_SIZE_UNIT (TREE_OPERAND (src2, 1))
1284 : 13457 : : TYPE_SIZE_UNIT (TREE_TYPE (src2)));
1285 : 15623 : if (len2 == NULL_TREE
1286 : 15804 : || !poly_int_tree_p (len2))
1287 : : return false;
1288 : :
1289 : 15804 : src = get_addr_base_and_unit_offset (src, &offset);
1290 : 15804 : src2 = get_addr_base_and_unit_offset (src2, &offset2);
1291 : 15804 : if (src == NULL_TREE
1292 : 15804 : || src2 == NULL_TREE
1293 : 15804 : || maybe_lt (offset, offset2))
1294 : : return false;
1295 : :
1296 : 15454 : if (!operand_equal_p (src, src2, 0))
1297 : : return false;
1298 : :
1299 : : /* [ src + offset2, src + offset2 + len2 - 1 ] is set to val.
1300 : : Make sure that
1301 : : [ src + offset, src + offset + len - 1 ] is a subset of that. */
1302 : 10466 : if (maybe_gt (wi::to_poly_offset (len) + (offset - offset2),
1303 : : wi::to_poly_offset (len2)))
1304 : : return false;
1305 : :
1306 : 10212 : if (dump_file && (dump_flags & TDF_DETAILS))
1307 : : {
1308 : 19 : fprintf (dump_file, "Simplified\n ");
1309 : 19 : print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1310 : 19 : fprintf (dump_file, "after previous\n ");
1311 : 19 : print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
1312 : : }
1313 : :
1314 : : /* For simplicity, don't change the kind of the stmt,
1315 : : turn dest = src; into dest = {}; and memcpy (&dest, &src, len);
1316 : : into memset (&dest, val, len);
1317 : : In theory we could change dest = src into memset if dest
1318 : : is addressable (maybe beneficial if val is not 0), or
1319 : : memcpy (&dest, &src, len) into dest = {} if len is the size
1320 : : of dest, dest isn't volatile. */
1321 : 10212 : if (is_gimple_assign (stmt))
1322 : : {
1323 : 10210 : tree ctor = build_constructor (TREE_TYPE (dest), NULL);
1324 : 10210 : gimple_assign_set_rhs_from_tree (gsip, ctor);
1325 : 10210 : update_stmt (stmt);
1326 : 10210 : statistics_counter_event (cfun, "copy zeroing propagation of aggregate", 1);
1327 : : }
1328 : : else /* If stmt is memcpy, transform it into memset. */
1329 : : {
1330 : 2 : gcall *call = as_a <gcall *> (stmt);
1331 : 2 : tree fndecl = builtin_decl_implicit (BUILT_IN_MEMSET);
1332 : 2 : gimple_call_set_fndecl (call, fndecl);
1333 : 2 : gimple_call_set_fntype (call, TREE_TYPE (fndecl));
1334 : 2 : gimple_call_set_arg (call, 1, val);
1335 : 2 : update_stmt (stmt);
1336 : 2 : statistics_counter_event (cfun, "memcpy to memset changed", 1);
1337 : : }
1338 : :
1339 : 10212 : if (dump_file && (dump_flags & TDF_DETAILS))
1340 : : {
1341 : 19 : fprintf (dump_file, "into\n ");
1342 : 19 : print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1343 : : }
1344 : : return true;
1345 : : }
1346 : : /* Optimizes
1347 : : a = c;
1348 : : b = a;
1349 : : into
1350 : : a = c;
1351 : : b = c;
1352 : : GSIP is the second statement and SRC is the common
1353 : : between the statements.
1354 : : */
1355 : : static bool
1356 : 4435510 : optimize_agr_copyprop (gimple_stmt_iterator *gsip)
1357 : : {
1358 : 4435510 : gimple *stmt = gsi_stmt (*gsip);
1359 : 8871020 : if (gimple_has_volatile_ops (stmt))
1360 : : return false;
1361 : :
1362 : 4434380 : tree dest = gimple_assign_lhs (stmt);
1363 : 4434380 : tree src = gimple_assign_rhs1 (stmt);
1364 : : /* If the statement is `src = src;` then ignore it. */
1365 : 4434380 : if (operand_equal_p (dest, src, 0))
1366 : : return false;
1367 : :
1368 : 4432016 : tree vuse = gimple_vuse (stmt);
1369 : : /* If the vuse is the default definition, then there is no store beforehand. */
1370 : 4432016 : if (SSA_NAME_IS_DEFAULT_DEF (vuse))
1371 : : return false;
1372 : 4224728 : gimple *defstmt = SSA_NAME_DEF_STMT (vuse);
1373 : 4224728 : if (!gimple_assign_load_p (defstmt)
1374 : 4224728 : || !gimple_store_p (defstmt))
1375 : 2546514 : return false;
1376 : 3356428 : if (gimple_has_volatile_ops (defstmt))
1377 : : return false;
1378 : :
1379 : 1678154 : tree dest2 = gimple_assign_lhs (defstmt);
1380 : 1678154 : tree src2 = gimple_assign_rhs1 (defstmt);
1381 : :
1382 : : /* If the original store is `src2 = src2;` skip over it. */
1383 : 1678154 : if (operand_equal_p (src2, dest2, 0))
1384 : : return false;
1385 : 1677169 : if (!operand_equal_p (src, dest2, 0))
1386 : : return false;
1387 : :
1388 : :
1389 : : /* For 2 memory refences and using a temporary to do the copy,
1390 : : don't remove the temporary as the 2 memory references might overlap.
1391 : : Note t does not need to be decl as it could be field.
1392 : : See PR 22237 for full details.
1393 : : E.g.
1394 : : t = *a;
1395 : : *b = t;
1396 : : Cannot be convert into
1397 : : t = *a;
1398 : : *b = *a;
1399 : : Though the following is allowed to be done:
1400 : : t = *a;
1401 : : *a = t;
1402 : : And convert it into:
1403 : : t = *a;
1404 : : *a = *a;
1405 : : */
1406 : 484060 : if (!operand_equal_p (src2, dest, 0)
1407 : 484060 : && !DECL_P (dest) && !DECL_P (src2))
1408 : : return false;
1409 : :
1410 : 423376 : if (dump_file && (dump_flags & TDF_DETAILS))
1411 : : {
1412 : 4 : fprintf (dump_file, "Simplified\n ");
1413 : 4 : print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1414 : 4 : fprintf (dump_file, "after previous\n ");
1415 : 4 : print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
1416 : : }
1417 : 423376 : gimple_assign_set_rhs_from_tree (gsip, unshare_expr (src2));
1418 : 423376 : update_stmt (stmt);
1419 : :
1420 : 423376 : if (dump_file && (dump_flags & TDF_DETAILS))
1421 : : {
1422 : 4 : fprintf (dump_file, "into\n ");
1423 : 4 : print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1424 : : }
1425 : 423376 : statistics_counter_event (cfun, "copy prop for aggregate", 1);
1426 : 423376 : return true;
1427 : : }
1428 : :
1429 : : /* *GSI_P is a GIMPLE_CALL to a builtin function.
1430 : : Optimize
1431 : : memcpy (p, "abcd", 4);
1432 : : memset (p + 4, ' ', 3);
1433 : : into
1434 : : memcpy (p, "abcd ", 7);
1435 : : call if the latter can be stored by pieces during expansion.
1436 : :
1437 : : Optimize
1438 : : memchr ("abcd", a, 4) == 0;
1439 : : or
1440 : : memchr ("abcd", a, 4) != 0;
1441 : : to
1442 : : (a == 'a' || a == 'b' || a == 'c' || a == 'd') == 0
1443 : : or
1444 : : (a == 'a' || a == 'b' || a == 'c' || a == 'd') != 0
1445 : :
1446 : : Also canonicalize __atomic_fetch_op (p, x, y) op x
1447 : : to __atomic_op_fetch (p, x, y) or
1448 : : __atomic_op_fetch (p, x, y) iop x
1449 : : to __atomic_fetch_op (p, x, y) when possible (also __sync). */
1450 : :
1451 : : static bool
1452 : 5936594 : simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2)
1453 : : {
1454 : 5936594 : gimple *stmt1, *stmt2 = gsi_stmt (*gsi_p);
1455 : 5936594 : enum built_in_function other_atomic = END_BUILTINS;
1456 : 5936594 : enum tree_code atomic_op = ERROR_MARK;
1457 : 11870720 : tree vuse = gimple_vuse (stmt2);
1458 : 5936594 : if (vuse == NULL)
1459 : : return false;
1460 : 4869489 : stmt1 = SSA_NAME_DEF_STMT (vuse);
1461 : :
1462 : 4869489 : tree res;
1463 : :
1464 : 4869489 : switch (DECL_FUNCTION_CODE (callee2))
1465 : : {
1466 : 291912 : case BUILT_IN_MEMCPY:
1467 : 291912 : if (gimple_call_num_args (stmt2) == 3)
1468 : : {
1469 : 291900 : tree dest = gimple_call_arg (stmt2, 0);
1470 : 291900 : tree src = gimple_call_arg (stmt2, 1);
1471 : 291900 : tree len = gimple_call_arg (stmt2, 2);
1472 : : /* Try to optimize the memcpy to memset if src
1473 : : and dest are addresses. */
1474 : 291900 : if (TREE_CODE (dest) == ADDR_EXPR
1475 : 49495 : && TREE_CODE (src) == ADDR_EXPR
1476 : 42755 : && TREE_CODE (len) == INTEGER_CST
1477 : 330218 : && optimize_memcpy_to_memset (gsi_p, TREE_OPERAND (dest, 0),
1478 : 38318 : TREE_OPERAND (src, 0), len))
1479 : : return true;
1480 : : }
1481 : : break;
1482 : 10665 : case BUILT_IN_MEMCHR:
1483 : 10665 : if (gimple_call_num_args (stmt2) == 3
1484 : 10665 : && (res = gimple_call_lhs (stmt2)) != nullptr
1485 : 10665 : && use_in_zero_equality (res) != nullptr
1486 : : && CHAR_BIT == 8
1487 : 10665 : && BITS_PER_UNIT == 8)
1488 : : {
1489 : 1053 : tree ptr = gimple_call_arg (stmt2, 0);
1490 : 1053 : if (TREE_CODE (ptr) != ADDR_EXPR
1491 : 1053 : || TREE_CODE (TREE_OPERAND (ptr, 0)) != STRING_CST)
1492 : : break;
1493 : 145 : unsigned HOST_WIDE_INT slen
1494 : 145 : = TREE_STRING_LENGTH (TREE_OPERAND (ptr, 0));
1495 : : /* It must be a non-empty string constant. */
1496 : 145 : if (slen < 2)
1497 : : break;
1498 : : /* For -Os, only simplify strings with a single character. */
1499 : 141 : if (!optimize_bb_for_speed_p (gimple_bb (stmt2))
1500 : 141 : && slen > 2)
1501 : : break;
1502 : 125 : tree size = gimple_call_arg (stmt2, 2);
1503 : : /* Size must be a constant which is <= UNITS_PER_WORD and
1504 : : <= the string length. */
1505 : 125 : if (TREE_CODE (size) != INTEGER_CST)
1506 : : break;
1507 : :
1508 : 125 : if (!tree_fits_uhwi_p (size))
1509 : : break;
1510 : :
1511 : 125 : unsigned HOST_WIDE_INT sz = tree_to_uhwi (size);
1512 : 126 : if (sz == 0 || sz > UNITS_PER_WORD || sz >= slen)
1513 : : break;
1514 : :
1515 : 73 : tree ch = gimple_call_arg (stmt2, 1);
1516 : 73 : location_t loc = gimple_location (stmt2);
1517 : 73 : if (!useless_type_conversion_p (char_type_node,
1518 : 73 : TREE_TYPE (ch)))
1519 : 73 : ch = fold_convert_loc (loc, char_type_node, ch);
1520 : 73 : const char *p = TREE_STRING_POINTER (TREE_OPERAND (ptr, 0));
1521 : 73 : unsigned int isize = sz;
1522 : 73 : tree *op = XALLOCAVEC (tree, isize);
1523 : 315 : for (unsigned int i = 0; i < isize; i++)
1524 : : {
1525 : 242 : op[i] = build_int_cst (char_type_node, p[i]);
1526 : 242 : op[i] = fold_build2_loc (loc, EQ_EXPR, boolean_type_node,
1527 : : op[i], ch);
1528 : : }
1529 : 242 : for (unsigned int i = isize - 1; i >= 1; i--)
1530 : 169 : op[i - 1] = fold_convert_loc (loc, boolean_type_node,
1531 : : fold_build2_loc (loc,
1532 : : BIT_IOR_EXPR,
1533 : : boolean_type_node,
1534 : 169 : op[i - 1],
1535 : 169 : op[i]));
1536 : 73 : res = fold_convert_loc (loc, TREE_TYPE (res), op[0]);
1537 : 73 : gimplify_and_update_call_from_tree (gsi_p, res);
1538 : 73 : return true;
1539 : : }
1540 : : break;
1541 : :
1542 : 108439 : case BUILT_IN_MEMSET:
1543 : 108439 : if (gimple_call_num_args (stmt2) != 3
1544 : 108439 : || gimple_call_lhs (stmt2)
1545 : : || CHAR_BIT != 8
1546 : 216878 : || BITS_PER_UNIT != 8)
1547 : : break;
1548 : : else
1549 : : {
1550 : 101631 : tree callee1;
1551 : 101631 : tree ptr1, src1, str1, off1, len1, lhs1;
1552 : 101631 : tree ptr2 = gimple_call_arg (stmt2, 0);
1553 : 101631 : tree val2 = gimple_call_arg (stmt2, 1);
1554 : 101631 : tree len2 = gimple_call_arg (stmt2, 2);
1555 : 101631 : tree diff, vdef, new_str_cst;
1556 : 101631 : gimple *use_stmt;
1557 : 101631 : unsigned int ptr1_align;
1558 : 101631 : unsigned HOST_WIDE_INT src_len;
1559 : 101631 : char *src_buf;
1560 : 101631 : use_operand_p use_p;
1561 : :
1562 : 101631 : if (!tree_fits_shwi_p (val2)
1563 : 97570 : || !tree_fits_uhwi_p (len2)
1564 : 160971 : || compare_tree_int (len2, 1024) == 1)
1565 : : break;
1566 : 54441 : if (is_gimple_call (stmt1))
1567 : : {
1568 : : /* If first stmt is a call, it needs to be memcpy
1569 : : or mempcpy, with string literal as second argument and
1570 : : constant length. */
1571 : 28363 : callee1 = gimple_call_fndecl (stmt1);
1572 : 28363 : if (callee1 == NULL_TREE
1573 : 28256 : || !fndecl_built_in_p (callee1, BUILT_IN_NORMAL)
1574 : 53299 : || gimple_call_num_args (stmt1) != 3)
1575 : : break;
1576 : 23686 : if (DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMCPY
1577 : 23686 : && DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMPCPY)
1578 : : break;
1579 : 10511 : ptr1 = gimple_call_arg (stmt1, 0);
1580 : 10511 : src1 = gimple_call_arg (stmt1, 1);
1581 : 10511 : len1 = gimple_call_arg (stmt1, 2);
1582 : 10511 : lhs1 = gimple_call_lhs (stmt1);
1583 : 10511 : if (!tree_fits_uhwi_p (len1))
1584 : : break;
1585 : 10426 : str1 = string_constant (src1, &off1, NULL, NULL);
1586 : 10426 : if (str1 == NULL_TREE)
1587 : : break;
1588 : 4691 : if (!tree_fits_uhwi_p (off1)
1589 : 4691 : || compare_tree_int (off1, TREE_STRING_LENGTH (str1) - 1) > 0
1590 : 4691 : || compare_tree_int (len1, TREE_STRING_LENGTH (str1)
1591 : 4691 : - tree_to_uhwi (off1)) > 0
1592 : 4691 : || TREE_CODE (TREE_TYPE (str1)) != ARRAY_TYPE
1593 : 14073 : || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1)))
1594 : 4691 : != TYPE_MODE (char_type_node))
1595 : : break;
1596 : : }
1597 : 26078 : else if (gimple_assign_single_p (stmt1))
1598 : : {
1599 : : /* Otherwise look for length 1 memcpy optimized into
1600 : : assignment. */
1601 : 16035 : ptr1 = gimple_assign_lhs (stmt1);
1602 : 16035 : src1 = gimple_assign_rhs1 (stmt1);
1603 : 16035 : if (TREE_CODE (ptr1) != MEM_REF
1604 : 2732 : || TYPE_MODE (TREE_TYPE (ptr1)) != TYPE_MODE (char_type_node)
1605 : 16779 : || !tree_fits_shwi_p (src1))
1606 : : break;
1607 : 338 : ptr1 = build_fold_addr_expr (ptr1);
1608 : 338 : STRIP_USELESS_TYPE_CONVERSION (ptr1);
1609 : 338 : callee1 = NULL_TREE;
1610 : 338 : len1 = size_one_node;
1611 : 338 : lhs1 = NULL_TREE;
1612 : 338 : off1 = size_zero_node;
1613 : 338 : str1 = NULL_TREE;
1614 : : }
1615 : : else
1616 : : break;
1617 : :
1618 : 5029 : diff = constant_pointer_difference (ptr1, ptr2);
1619 : 5029 : if (diff == NULL && lhs1 != NULL)
1620 : : {
1621 : 7 : diff = constant_pointer_difference (lhs1, ptr2);
1622 : 7 : if (DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
1623 : 7 : && diff != NULL)
1624 : 7 : diff = size_binop (PLUS_EXPR, diff,
1625 : : fold_convert (sizetype, len1));
1626 : : }
1627 : : /* If the difference between the second and first destination pointer
1628 : : is not constant, or is bigger than memcpy length, bail out. */
1629 : 5029 : if (diff == NULL
1630 : 4200 : || !tree_fits_uhwi_p (diff)
1631 : 4200 : || tree_int_cst_lt (len1, diff)
1632 : 8991 : || compare_tree_int (diff, 1024) == 1)
1633 : : break;
1634 : :
1635 : : /* Use maximum of difference plus memset length and memcpy length
1636 : : as the new memcpy length, if it is too big, bail out. */
1637 : 3962 : src_len = tree_to_uhwi (diff);
1638 : 3962 : src_len += tree_to_uhwi (len2);
1639 : 3962 : if (src_len < tree_to_uhwi (len1))
1640 : : src_len = tree_to_uhwi (len1);
1641 : 3962 : if (src_len > 1024)
1642 : : break;
1643 : :
1644 : : /* If mempcpy value is used elsewhere, bail out, as mempcpy
1645 : : with bigger length will return different result. */
1646 : 3962 : if (lhs1 != NULL_TREE
1647 : 187 : && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
1648 : 3969 : && (TREE_CODE (lhs1) != SSA_NAME
1649 : 7 : || !single_imm_use (lhs1, &use_p, &use_stmt)
1650 : 7 : || use_stmt != stmt2))
1651 : : break;
1652 : :
1653 : : /* If anything reads memory in between memcpy and memset
1654 : : call, the modified memcpy call might change it. */
1655 : 3962 : vdef = gimple_vdef (stmt1);
1656 : 3962 : if (vdef != NULL
1657 : 3962 : && (!single_imm_use (vdef, &use_p, &use_stmt)
1658 : 3233 : || use_stmt != stmt2))
1659 : : break;
1660 : :
1661 : 3233 : ptr1_align = get_pointer_alignment (ptr1);
1662 : : /* Construct the new source string literal. */
1663 : 3233 : src_buf = XALLOCAVEC (char, src_len + 1);
1664 : 3233 : if (callee1)
1665 : 3108 : memcpy (src_buf,
1666 : 3108 : TREE_STRING_POINTER (str1) + tree_to_uhwi (off1),
1667 : : tree_to_uhwi (len1));
1668 : : else
1669 : 125 : src_buf[0] = tree_to_shwi (src1);
1670 : 3233 : memset (src_buf + tree_to_uhwi (diff),
1671 : 3233 : tree_to_shwi (val2), tree_to_uhwi (len2));
1672 : 3233 : src_buf[src_len] = '\0';
1673 : : /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
1674 : : handle embedded '\0's. */
1675 : 3233 : if (strlen (src_buf) != src_len)
1676 : : break;
1677 : 3159 : rtl_profile_for_bb (gimple_bb (stmt2));
1678 : : /* If the new memcpy wouldn't be emitted by storing the literal
1679 : : by pieces, this optimization might enlarge .rodata too much,
1680 : : as commonly used string literals couldn't be shared any
1681 : : longer. */
1682 : 3159 : if (!can_store_by_pieces (src_len,
1683 : : builtin_strncpy_read_str,
1684 : : src_buf, ptr1_align, false))
1685 : : break;
1686 : :
1687 : 2393 : new_str_cst = build_string_literal (src_len, src_buf);
1688 : 2393 : if (callee1)
1689 : : {
1690 : : /* If STMT1 is a mem{,p}cpy call, adjust it and remove
1691 : : memset call. */
1692 : 2286 : if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
1693 : 7 : gimple_call_set_lhs (stmt1, NULL_TREE);
1694 : 2286 : gimple_call_set_arg (stmt1, 1, new_str_cst);
1695 : 2286 : gimple_call_set_arg (stmt1, 2,
1696 : 2286 : build_int_cst (TREE_TYPE (len1), src_len));
1697 : 2286 : update_stmt (stmt1);
1698 : 2286 : unlink_stmt_vdef (stmt2);
1699 : 2286 : gsi_replace (gsi_p, gimple_build_nop (), false);
1700 : 2286 : fwprop_invalidate_lattice (gimple_get_lhs (stmt2));
1701 : 2286 : release_defs (stmt2);
1702 : 2286 : if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
1703 : : {
1704 : 7 : fwprop_invalidate_lattice (lhs1);
1705 : 7 : release_ssa_name (lhs1);
1706 : : }
1707 : 2393 : return true;
1708 : : }
1709 : : else
1710 : : {
1711 : : /* Otherwise, if STMT1 is length 1 memcpy optimized into
1712 : : assignment, remove STMT1 and change memset call into
1713 : : memcpy call. */
1714 : 107 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt1);
1715 : :
1716 : 107 : if (!is_gimple_val (ptr1))
1717 : 12 : ptr1 = force_gimple_operand_gsi (gsi_p, ptr1, true, NULL_TREE,
1718 : : true, GSI_SAME_STMT);
1719 : 107 : tree fndecl = builtin_decl_explicit (BUILT_IN_MEMCPY);
1720 : 107 : gimple_call_set_fndecl (stmt2, fndecl);
1721 : 107 : gimple_call_set_fntype (as_a <gcall *> (stmt2),
1722 : 107 : TREE_TYPE (fndecl));
1723 : 107 : gimple_call_set_arg (stmt2, 0, ptr1);
1724 : 107 : gimple_call_set_arg (stmt2, 1, new_str_cst);
1725 : 107 : gimple_call_set_arg (stmt2, 2,
1726 : 107 : build_int_cst (TREE_TYPE (len2), src_len));
1727 : 107 : unlink_stmt_vdef (stmt1);
1728 : 107 : gsi_remove (&gsi, true);
1729 : 107 : fwprop_invalidate_lattice (gimple_get_lhs (stmt1));
1730 : 107 : release_defs (stmt1);
1731 : 107 : update_stmt (stmt2);
1732 : 107 : return false;
1733 : : }
1734 : : }
1735 : : break;
1736 : :
1737 : : #define CASE_ATOMIC(NAME, OTHER, OP) \
1738 : : case BUILT_IN_##NAME##_1: \
1739 : : case BUILT_IN_##NAME##_2: \
1740 : : case BUILT_IN_##NAME##_4: \
1741 : : case BUILT_IN_##NAME##_8: \
1742 : : case BUILT_IN_##NAME##_16: \
1743 : : atomic_op = OP; \
1744 : : other_atomic \
1745 : : = (enum built_in_function) (BUILT_IN_##OTHER##_1 \
1746 : : + (DECL_FUNCTION_CODE (callee2) \
1747 : : - BUILT_IN_##NAME##_1)); \
1748 : : goto handle_atomic_fetch_op;
1749 : :
1750 : 51521 : CASE_ATOMIC (ATOMIC_FETCH_ADD, ATOMIC_ADD_FETCH, PLUS_EXPR)
1751 : 8153 : CASE_ATOMIC (ATOMIC_FETCH_SUB, ATOMIC_SUB_FETCH, MINUS_EXPR)
1752 : 2856 : CASE_ATOMIC (ATOMIC_FETCH_AND, ATOMIC_AND_FETCH, BIT_AND_EXPR)
1753 : 2875 : CASE_ATOMIC (ATOMIC_FETCH_XOR, ATOMIC_XOR_FETCH, BIT_XOR_EXPR)
1754 : 3889 : CASE_ATOMIC (ATOMIC_FETCH_OR, ATOMIC_OR_FETCH, BIT_IOR_EXPR)
1755 : :
1756 : 2355 : CASE_ATOMIC (SYNC_FETCH_AND_ADD, SYNC_ADD_AND_FETCH, PLUS_EXPR)
1757 : 1996 : CASE_ATOMIC (SYNC_FETCH_AND_SUB, SYNC_SUB_AND_FETCH, MINUS_EXPR)
1758 : 1868 : CASE_ATOMIC (SYNC_FETCH_AND_AND, SYNC_AND_AND_FETCH, BIT_AND_EXPR)
1759 : 2136 : CASE_ATOMIC (SYNC_FETCH_AND_XOR, SYNC_XOR_AND_FETCH, BIT_XOR_EXPR)
1760 : 1979 : CASE_ATOMIC (SYNC_FETCH_AND_OR, SYNC_OR_AND_FETCH, BIT_IOR_EXPR)
1761 : :
1762 : 14061 : CASE_ATOMIC (ATOMIC_ADD_FETCH, ATOMIC_FETCH_ADD, MINUS_EXPR)
1763 : 9113 : CASE_ATOMIC (ATOMIC_SUB_FETCH, ATOMIC_FETCH_SUB, PLUS_EXPR)
1764 : 2366 : CASE_ATOMIC (ATOMIC_XOR_FETCH, ATOMIC_FETCH_XOR, BIT_XOR_EXPR)
1765 : :
1766 : 821 : CASE_ATOMIC (SYNC_ADD_AND_FETCH, SYNC_FETCH_AND_ADD, MINUS_EXPR)
1767 : 732 : CASE_ATOMIC (SYNC_SUB_AND_FETCH, SYNC_FETCH_AND_SUB, PLUS_EXPR)
1768 : 800 : CASE_ATOMIC (SYNC_XOR_AND_FETCH, SYNC_FETCH_AND_XOR, BIT_XOR_EXPR)
1769 : :
1770 : : #undef CASE_ATOMIC
1771 : :
1772 : 107521 : handle_atomic_fetch_op:
1773 : 107521 : if (gimple_call_num_args (stmt2) >= 2 && gimple_call_lhs (stmt2))
1774 : : {
1775 : 61226 : tree lhs2 = gimple_call_lhs (stmt2), lhsc = lhs2;
1776 : 61226 : tree arg = gimple_call_arg (stmt2, 1);
1777 : 61226 : gimple *use_stmt, *cast_stmt = NULL;
1778 : 61226 : use_operand_p use_p;
1779 : 61226 : tree ndecl = builtin_decl_explicit (other_atomic);
1780 : :
1781 : 61226 : if (ndecl == NULL_TREE || !single_imm_use (lhs2, &use_p, &use_stmt))
1782 : : break;
1783 : :
1784 : 58444 : if (gimple_assign_cast_p (use_stmt))
1785 : : {
1786 : 30550 : cast_stmt = use_stmt;
1787 : 30550 : lhsc = gimple_assign_lhs (cast_stmt);
1788 : 30550 : if (lhsc == NULL_TREE
1789 : 30550 : || !INTEGRAL_TYPE_P (TREE_TYPE (lhsc))
1790 : 29935 : || (TYPE_PRECISION (TREE_TYPE (lhsc))
1791 : 29935 : != TYPE_PRECISION (TREE_TYPE (lhs2)))
1792 : 58959 : || !single_imm_use (lhsc, &use_p, &use_stmt))
1793 : : {
1794 : 2725 : use_stmt = cast_stmt;
1795 : 2725 : cast_stmt = NULL;
1796 : 2725 : lhsc = lhs2;
1797 : : }
1798 : : }
1799 : :
1800 : 58444 : bool ok = false;
1801 : 58444 : tree oarg = NULL_TREE;
1802 : 58444 : enum tree_code ccode = ERROR_MARK;
1803 : 58444 : tree crhs1 = NULL_TREE, crhs2 = NULL_TREE;
1804 : 58444 : if (is_gimple_assign (use_stmt)
1805 : 58444 : && gimple_assign_rhs_code (use_stmt) == atomic_op)
1806 : : {
1807 : 1416 : if (gimple_assign_rhs1 (use_stmt) == lhsc)
1808 : 1025 : oarg = gimple_assign_rhs2 (use_stmt);
1809 : 391 : else if (atomic_op != MINUS_EXPR)
1810 : : oarg = gimple_assign_rhs1 (use_stmt);
1811 : : }
1812 : 57028 : else if (atomic_op == MINUS_EXPR
1813 : 12968 : && is_gimple_assign (use_stmt)
1814 : 3626 : && gimple_assign_rhs_code (use_stmt) == PLUS_EXPR
1815 : 195 : && TREE_CODE (arg) == INTEGER_CST
1816 : 57223 : && (TREE_CODE (gimple_assign_rhs2 (use_stmt))
1817 : : == INTEGER_CST))
1818 : : {
1819 : 179 : tree a = fold_convert (TREE_TYPE (lhs2), arg);
1820 : 179 : tree o = fold_convert (TREE_TYPE (lhs2),
1821 : : gimple_assign_rhs2 (use_stmt));
1822 : 179 : if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
1823 : : ok = true;
1824 : : }
1825 : 56849 : else if (atomic_op == BIT_AND_EXPR || atomic_op == BIT_IOR_EXPR)
1826 : : ;
1827 : 51663 : else if (gimple_code (use_stmt) == GIMPLE_COND)
1828 : : {
1829 : 19663 : ccode = gimple_cond_code (use_stmt);
1830 : 19663 : crhs1 = gimple_cond_lhs (use_stmt);
1831 : 19663 : crhs2 = gimple_cond_rhs (use_stmt);
1832 : : }
1833 : 32000 : else if (is_gimple_assign (use_stmt))
1834 : : {
1835 : 9568 : if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
1836 : : {
1837 : 4063 : ccode = gimple_assign_rhs_code (use_stmt);
1838 : 4063 : crhs1 = gimple_assign_rhs1 (use_stmt);
1839 : 4063 : crhs2 = gimple_assign_rhs2 (use_stmt);
1840 : : }
1841 : 5505 : else if (gimple_assign_rhs_code (use_stmt) == COND_EXPR)
1842 : : {
1843 : 0 : tree cond = gimple_assign_rhs1 (use_stmt);
1844 : 0 : if (COMPARISON_CLASS_P (cond))
1845 : : {
1846 : 0 : ccode = TREE_CODE (cond);
1847 : 0 : crhs1 = TREE_OPERAND (cond, 0);
1848 : 0 : crhs2 = TREE_OPERAND (cond, 1);
1849 : : }
1850 : : }
1851 : : }
1852 : 24751 : if (ccode == EQ_EXPR || ccode == NE_EXPR)
1853 : : {
1854 : : /* Deal with x - y == 0 or x ^ y == 0
1855 : : being optimized into x == y and x + cst == 0
1856 : : into x == -cst. */
1857 : 22518 : tree o = NULL_TREE;
1858 : 22518 : if (crhs1 == lhsc)
1859 : : o = crhs2;
1860 : 133 : else if (crhs2 == lhsc)
1861 : 133 : o = crhs1;
1862 : 22518 : if (o && atomic_op != PLUS_EXPR)
1863 : : oarg = o;
1864 : 10569 : else if (o
1865 : 10569 : && TREE_CODE (o) == INTEGER_CST
1866 : 10569 : && TREE_CODE (arg) == INTEGER_CST)
1867 : : {
1868 : 9837 : tree a = fold_convert (TREE_TYPE (lhs2), arg);
1869 : 9837 : o = fold_convert (TREE_TYPE (lhs2), o);
1870 : 9837 : if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
1871 : 58444 : ok = true;
1872 : : }
1873 : : }
1874 : 58444 : if (oarg && !ok)
1875 : : {
1876 : 13365 : if (operand_equal_p (arg, oarg, 0))
1877 : : ok = true;
1878 : 12019 : else if (TREE_CODE (arg) == SSA_NAME
1879 : 2179 : && TREE_CODE (oarg) == SSA_NAME)
1880 : : {
1881 : 745 : tree oarg2 = oarg;
1882 : 745 : if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (oarg)))
1883 : : {
1884 : 104 : gimple *g = SSA_NAME_DEF_STMT (oarg);
1885 : 104 : oarg2 = gimple_assign_rhs1 (g);
1886 : 104 : if (TREE_CODE (oarg2) != SSA_NAME
1887 : 104 : || !INTEGRAL_TYPE_P (TREE_TYPE (oarg2))
1888 : 208 : || (TYPE_PRECISION (TREE_TYPE (oarg2))
1889 : 104 : != TYPE_PRECISION (TREE_TYPE (oarg))))
1890 : : oarg2 = oarg;
1891 : : }
1892 : 745 : if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg)))
1893 : : {
1894 : 544 : gimple *g = SSA_NAME_DEF_STMT (arg);
1895 : 544 : tree rhs1 = gimple_assign_rhs1 (g);
1896 : : /* Handle e.g.
1897 : : x.0_1 = (long unsigned int) x_4(D);
1898 : : _2 = __atomic_fetch_add_8 (&vlong, x.0_1, 0);
1899 : : _3 = (long int) _2;
1900 : : _7 = x_4(D) + _3; */
1901 : 544 : if (rhs1 == oarg || rhs1 == oarg2)
1902 : : ok = true;
1903 : : /* Handle e.g.
1904 : : x.18_1 = (short unsigned int) x_5(D);
1905 : : _2 = (int) x.18_1;
1906 : : _3 = __atomic_fetch_xor_2 (&vshort, _2, 0);
1907 : : _4 = (short int) _3;
1908 : : _8 = x_5(D) ^ _4;
1909 : : This happens only for char/short. */
1910 : 160 : else if (TREE_CODE (rhs1) == SSA_NAME
1911 : 160 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1912 : 320 : && (TYPE_PRECISION (TREE_TYPE (rhs1))
1913 : 160 : == TYPE_PRECISION (TREE_TYPE (lhs2))))
1914 : : {
1915 : 160 : g = SSA_NAME_DEF_STMT (rhs1);
1916 : 160 : if (gimple_assign_cast_p (g)
1917 : 160 : && (gimple_assign_rhs1 (g) == oarg
1918 : 0 : || gimple_assign_rhs1 (g) == oarg2))
1919 : : ok = true;
1920 : : }
1921 : : }
1922 : 745 : if (!ok && arg == oarg2)
1923 : : /* Handle e.g.
1924 : : _1 = __sync_fetch_and_add_4 (&v, x_5(D));
1925 : : _2 = (int) _1;
1926 : : x.0_3 = (int) x_5(D);
1927 : : _7 = _2 + x.0_3; */
1928 : : ok = true;
1929 : : }
1930 : : }
1931 : :
1932 : 57098 : if (ok)
1933 : : {
1934 : 2276 : tree new_lhs = make_ssa_name (TREE_TYPE (lhs2));
1935 : 2276 : gimple_call_set_lhs (stmt2, new_lhs);
1936 : 2276 : gimple_call_set_fndecl (stmt2, ndecl);
1937 : 2276 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1938 : 2276 : if (ccode == ERROR_MARK)
1939 : 1992 : gimple_assign_set_rhs_with_ops (&gsi, cast_stmt
1940 : : ? NOP_EXPR : SSA_NAME,
1941 : : new_lhs);
1942 : : else
1943 : : {
1944 : 1057 : crhs1 = new_lhs;
1945 : 1057 : crhs2 = build_zero_cst (TREE_TYPE (lhs2));
1946 : 1057 : if (gimple_code (use_stmt) == GIMPLE_COND)
1947 : : {
1948 : 704 : gcond *cond_stmt = as_a <gcond *> (use_stmt);
1949 : 704 : gimple_cond_set_lhs (cond_stmt, crhs1);
1950 : 704 : gimple_cond_set_rhs (cond_stmt, crhs2);
1951 : : }
1952 : 353 : else if (gimple_assign_rhs_class (use_stmt)
1953 : : == GIMPLE_BINARY_RHS)
1954 : : {
1955 : 353 : gimple_assign_set_rhs1 (use_stmt, crhs1);
1956 : 353 : gimple_assign_set_rhs2 (use_stmt, crhs2);
1957 : : }
1958 : : else
1959 : : {
1960 : 0 : gcc_checking_assert (gimple_assign_rhs_code (use_stmt)
1961 : : == COND_EXPR);
1962 : 0 : tree cond = build2 (ccode, boolean_type_node,
1963 : : crhs1, crhs2);
1964 : 0 : gimple_assign_set_rhs1 (use_stmt, cond);
1965 : : }
1966 : : }
1967 : 2276 : update_stmt (use_stmt);
1968 : 2276 : if (atomic_op != BIT_AND_EXPR
1969 : 2276 : && atomic_op != BIT_IOR_EXPR
1970 : 2276 : && !stmt_ends_bb_p (stmt2))
1971 : : {
1972 : : /* For the benefit of debug stmts, emit stmt(s) to set
1973 : : lhs2 to the value it had from the new builtin.
1974 : : E.g. if it was previously:
1975 : : lhs2 = __atomic_fetch_add_8 (ptr, arg, 0);
1976 : : emit:
1977 : : new_lhs = __atomic_add_fetch_8 (ptr, arg, 0);
1978 : : lhs2 = new_lhs - arg;
1979 : : We also keep cast_stmt if any in the IL for
1980 : : the same reasons.
1981 : : These stmts will be DCEd later and proper debug info
1982 : : will be emitted.
1983 : : This is only possible for reversible operations
1984 : : (+/-/^) and without -fnon-call-exceptions. */
1985 : 1935 : gsi = gsi_for_stmt (stmt2);
1986 : 1935 : tree type = TREE_TYPE (lhs2);
1987 : 1935 : if (TREE_CODE (arg) == INTEGER_CST)
1988 : 1353 : arg = fold_convert (type, arg);
1989 : 582 : else if (!useless_type_conversion_p (type, TREE_TYPE (arg)))
1990 : : {
1991 : 0 : tree narg = make_ssa_name (type);
1992 : 0 : gimple *g = gimple_build_assign (narg, NOP_EXPR, arg);
1993 : 0 : gsi_insert_after (&gsi, g, GSI_NEW_STMT);
1994 : 0 : arg = narg;
1995 : : }
1996 : 1935 : enum tree_code rcode;
1997 : 1935 : switch (atomic_op)
1998 : : {
1999 : : case PLUS_EXPR: rcode = MINUS_EXPR; break;
2000 : 740 : case MINUS_EXPR: rcode = PLUS_EXPR; break;
2001 : 492 : case BIT_XOR_EXPR: rcode = atomic_op; break;
2002 : 0 : default: gcc_unreachable ();
2003 : : }
2004 : 1935 : gimple *g = gimple_build_assign (lhs2, rcode, new_lhs, arg);
2005 : 1935 : gsi_insert_after (&gsi, g, GSI_NEW_STMT);
2006 : 1935 : update_stmt (stmt2);
2007 : : }
2008 : : else
2009 : : {
2010 : : /* For e.g.
2011 : : lhs2 = __atomic_fetch_or_8 (ptr, arg, 0);
2012 : : after we change it to
2013 : : new_lhs = __atomic_or_fetch_8 (ptr, arg, 0);
2014 : : there is no way to find out the lhs2 value (i.e.
2015 : : what the atomic memory contained before the operation),
2016 : : values of some bits are lost. We have checked earlier
2017 : : that we don't have any non-debug users except for what
2018 : : we are already changing, so we need to reset the
2019 : : debug stmts and remove the cast_stmt if any. */
2020 : 341 : imm_use_iterator iter;
2021 : 676 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs2)
2022 : 335 : if (use_stmt != cast_stmt)
2023 : : {
2024 : 168 : gcc_assert (is_gimple_debug (use_stmt));
2025 : 168 : gimple_debug_bind_reset_value (use_stmt);
2026 : 168 : update_stmt (use_stmt);
2027 : 341 : }
2028 : 341 : if (cast_stmt)
2029 : : {
2030 : 167 : gsi = gsi_for_stmt (cast_stmt);
2031 : 167 : gsi_remove (&gsi, true);
2032 : : }
2033 : 341 : update_stmt (stmt2);
2034 : 341 : release_ssa_name (lhs2);
2035 : : }
2036 : : }
2037 : : }
2038 : : break;
2039 : :
2040 : : default:
2041 : : break;
2042 : : }
2043 : : return false;
2044 : : }
2045 : :
2046 : : /* Given a ssa_name in NAME see if it was defined by an assignment and
2047 : : set CODE to be the code and ARG1 to the first operand on the rhs and ARG2
2048 : : to the second operand on the rhs. */
2049 : :
2050 : : static inline void
2051 : 17059056 : defcodefor_name (tree name, enum tree_code *code, tree *arg1, tree *arg2)
2052 : : {
2053 : 17059056 : gimple *def;
2054 : 17059056 : enum tree_code code1;
2055 : 17059056 : tree arg11;
2056 : 17059056 : tree arg21;
2057 : 17059056 : tree arg31;
2058 : 17059056 : enum gimple_rhs_class grhs_class;
2059 : :
2060 : 17059056 : code1 = TREE_CODE (name);
2061 : 17059056 : arg11 = name;
2062 : 17059056 : arg21 = NULL_TREE;
2063 : 17059056 : arg31 = NULL_TREE;
2064 : 17059056 : grhs_class = get_gimple_rhs_class (code1);
2065 : :
2066 : 17059056 : if (code1 == SSA_NAME)
2067 : : {
2068 : 11452545 : def = SSA_NAME_DEF_STMT (name);
2069 : :
2070 : 11452545 : if (def && is_gimple_assign (def)
2071 : 18557987 : && can_propagate_from (def))
2072 : : {
2073 : 4948291 : code1 = gimple_assign_rhs_code (def);
2074 : 4948291 : arg11 = gimple_assign_rhs1 (def);
2075 : 4948291 : arg21 = gimple_assign_rhs2 (def);
2076 : 4948291 : arg31 = gimple_assign_rhs3 (def);
2077 : : }
2078 : : }
2079 : 5606511 : else if (grhs_class != GIMPLE_SINGLE_RHS)
2080 : 0 : code1 = ERROR_MARK;
2081 : :
2082 : 17059056 : *code = code1;
2083 : 17059056 : *arg1 = arg11;
2084 : 17059056 : if (arg2)
2085 : 17040094 : *arg2 = arg21;
2086 : 17059056 : if (arg31)
2087 : 2595 : *code = ERROR_MARK;
2088 : 17059056 : }
2089 : :
2090 : :
2091 : : /* Recognize rotation patterns. Return true if a transformation
2092 : : applied, otherwise return false.
2093 : :
2094 : : We are looking for X with unsigned type T with bitsize B, OP being
2095 : : +, | or ^, some type T2 wider than T. For:
2096 : : (X << CNT1) OP (X >> CNT2) iff CNT1 + CNT2 == B
2097 : : ((T) ((T2) X << CNT1)) OP ((T) ((T2) X >> CNT2)) iff CNT1 + CNT2 == B
2098 : :
2099 : : transform these into:
2100 : : X r<< CNT1
2101 : :
2102 : : Or for:
2103 : : (X << Y) OP (X >> (B - Y))
2104 : : (X << (int) Y) OP (X >> (int) (B - Y))
2105 : : ((T) ((T2) X << Y)) OP ((T) ((T2) X >> (B - Y)))
2106 : : ((T) ((T2) X << (int) Y)) OP ((T) ((T2) X >> (int) (B - Y)))
2107 : : (X << Y) | (X >> ((-Y) & (B - 1)))
2108 : : (X << (int) Y) | (X >> (int) ((-Y) & (B - 1)))
2109 : : ((T) ((T2) X << Y)) | ((T) ((T2) X >> ((-Y) & (B - 1))))
2110 : : ((T) ((T2) X << (int) Y)) | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
2111 : :
2112 : : transform these into (last 2 only if ranger can prove Y < B
2113 : : or Y = N * B):
2114 : : X r<< Y
2115 : : or
2116 : : X r<< (& & (B - 1))
2117 : : The latter for the forms with T2 wider than T if ranger can't prove Y < B.
2118 : :
2119 : : Or for:
2120 : : (X << (Y & (B - 1))) | (X >> ((-Y) & (B - 1)))
2121 : : (X << (int) (Y & (B - 1))) | (X >> (int) ((-Y) & (B - 1)))
2122 : : ((T) ((T2) X << (Y & (B - 1)))) | ((T) ((T2) X >> ((-Y) & (B - 1))))
2123 : : ((T) ((T2) X << (int) (Y & (B - 1)))) \
2124 : : | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
2125 : :
2126 : : transform these into:
2127 : : X r<< (Y & (B - 1))
2128 : :
2129 : : Note, in the patterns with T2 type, the type of OP operands
2130 : : might be even a signed type, but should have precision B.
2131 : : Expressions with & (B - 1) should be recognized only if B is
2132 : : a power of 2. */
2133 : :
2134 : : static bool
2135 : 10101606 : simplify_rotate (gimple_stmt_iterator *gsi)
2136 : : {
2137 : 10101606 : gimple *stmt = gsi_stmt (*gsi);
2138 : 10101606 : tree arg[2], rtype, rotcnt = NULL_TREE;
2139 : 10101606 : tree def_arg1[2], def_arg2[2];
2140 : 10101606 : enum tree_code def_code[2];
2141 : 10101606 : tree lhs;
2142 : 10101606 : int i;
2143 : 10101606 : bool swapped_p = false;
2144 : 10101606 : gimple *g;
2145 : 10101606 : gimple *def_arg_stmt[2] = { NULL, NULL };
2146 : 10101606 : int wider_prec = 0;
2147 : 10101606 : bool add_masking = false;
2148 : :
2149 : 10101606 : arg[0] = gimple_assign_rhs1 (stmt);
2150 : 10101606 : arg[1] = gimple_assign_rhs2 (stmt);
2151 : 10101606 : rtype = TREE_TYPE (arg[0]);
2152 : :
2153 : : /* Only create rotates in complete modes. Other cases are not
2154 : : expanded properly. */
2155 : 10101606 : if (!INTEGRAL_TYPE_P (rtype)
2156 : 10101606 : || !type_has_mode_precision_p (rtype))
2157 : 1621755 : return false;
2158 : :
2159 : 25439553 : for (i = 0; i < 2; i++)
2160 : : {
2161 : 16959702 : defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
2162 : 16959702 : if (TREE_CODE (arg[i]) == SSA_NAME)
2163 : 11353191 : def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
2164 : : }
2165 : :
2166 : : /* Look through narrowing (or same precision) conversions. */
2167 : 7518486 : if (CONVERT_EXPR_CODE_P (def_code[0])
2168 : 961365 : && CONVERT_EXPR_CODE_P (def_code[1])
2169 : 137769 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[0]))
2170 : 114096 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[1]))
2171 : 108295 : && TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
2172 : 108295 : == TYPE_PRECISION (TREE_TYPE (def_arg1[1]))
2173 : 59154 : && TYPE_PRECISION (TREE_TYPE (def_arg1[0])) >= TYPE_PRECISION (rtype)
2174 : 38756 : && has_single_use (arg[0])
2175 : 8510401 : && has_single_use (arg[1]))
2176 : : {
2177 : 26414 : wider_prec = TYPE_PRECISION (TREE_TYPE (def_arg1[0]));
2178 : 79242 : for (i = 0; i < 2; i++)
2179 : : {
2180 : 52828 : arg[i] = def_arg1[i];
2181 : 52828 : defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
2182 : 52828 : if (TREE_CODE (arg[i]) == SSA_NAME)
2183 : 52828 : def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
2184 : : }
2185 : : }
2186 : : else
2187 : : {
2188 : : /* Handle signed rotate; the RSHIFT_EXPR has to be done
2189 : : in unsigned type but LSHIFT_EXPR could be signed. */
2190 : 8453437 : i = (def_code[0] == LSHIFT_EXPR || def_code[0] == RSHIFT_EXPR);
2191 : 7500974 : if (CONVERT_EXPR_CODE_P (def_code[i])
2192 : 952463 : && (def_code[1 - i] == LSHIFT_EXPR || def_code[1 - i] == RSHIFT_EXPR)
2193 : 29768 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[i]))
2194 : 28557 : && TYPE_PRECISION (rtype) == TYPE_PRECISION (TREE_TYPE (def_arg1[i]))
2195 : 8456954 : && has_single_use (arg[i]))
2196 : : {
2197 : 2099 : arg[i] = def_arg1[i];
2198 : 2099 : defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
2199 : 2099 : if (TREE_CODE (arg[i]) == SSA_NAME)
2200 : 2099 : def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
2201 : : }
2202 : : }
2203 : :
2204 : : /* One operand has to be LSHIFT_EXPR and one RSHIFT_EXPR. */
2205 : 8689732 : for (i = 0; i < 2; i++)
2206 : 8659818 : if (def_code[i] != LSHIFT_EXPR && def_code[i] != RSHIFT_EXPR)
2207 : : return false;
2208 : 253742 : else if (!has_single_use (arg[i]))
2209 : : return false;
2210 : 29914 : if (def_code[0] == def_code[1])
2211 : : return false;
2212 : :
2213 : : /* If we've looked through narrowing conversions before, look through
2214 : : widening conversions from unsigned type with the same precision
2215 : : as rtype here. */
2216 : 23313 : if (TYPE_PRECISION (TREE_TYPE (def_arg1[0])) != TYPE_PRECISION (rtype))
2217 : 21597 : for (i = 0; i < 2; i++)
2218 : : {
2219 : 14400 : tree tem;
2220 : 14400 : enum tree_code code;
2221 : 14400 : defcodefor_name (def_arg1[i], &code, &tem, NULL);
2222 : 6 : if (!CONVERT_EXPR_CODE_P (code)
2223 : 14394 : || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
2224 : 28794 : || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
2225 : 6 : return false;
2226 : 14394 : def_arg1[i] = tem;
2227 : : }
2228 : : /* Both shifts have to use the same first operand. */
2229 : 23307 : if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
2230 : 36698 : || !types_compatible_p (TREE_TYPE (def_arg1[0]),
2231 : 13391 : TREE_TYPE (def_arg1[1])))
2232 : : {
2233 : 9916 : if ((TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
2234 : 9916 : != TYPE_PRECISION (TREE_TYPE (def_arg1[1])))
2235 : 9916 : || (TYPE_UNSIGNED (TREE_TYPE (def_arg1[0]))
2236 : 9916 : == TYPE_UNSIGNED (TREE_TYPE (def_arg1[1]))))
2237 : 9894 : return false;
2238 : :
2239 : : /* Handle signed rotate; the RSHIFT_EXPR has to be done
2240 : : in unsigned type but LSHIFT_EXPR could be signed. */
2241 : 551 : i = def_code[0] != RSHIFT_EXPR;
2242 : 551 : if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[i])))
2243 : : return false;
2244 : :
2245 : 502 : tree tem;
2246 : 502 : enum tree_code code;
2247 : 502 : defcodefor_name (def_arg1[i], &code, &tem, NULL);
2248 : 271 : if (!CONVERT_EXPR_CODE_P (code)
2249 : 231 : || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
2250 : 733 : || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
2251 : : return false;
2252 : 222 : def_arg1[i] = tem;
2253 : 222 : if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
2254 : 244 : || !types_compatible_p (TREE_TYPE (def_arg1[0]),
2255 : 22 : TREE_TYPE (def_arg1[1])))
2256 : 200 : return false;
2257 : : }
2258 : 13391 : else if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[0])))
2259 : : return false;
2260 : :
2261 : : /* CNT1 + CNT2 == B case above. */
2262 : 11696 : if (tree_fits_uhwi_p (def_arg2[0])
2263 : 1238 : && tree_fits_uhwi_p (def_arg2[1])
2264 : 11696 : && tree_to_uhwi (def_arg2[0])
2265 : 1238 : + tree_to_uhwi (def_arg2[1]) == TYPE_PRECISION (rtype))
2266 : : rotcnt = def_arg2[0];
2267 : 10898 : else if (TREE_CODE (def_arg2[0]) != SSA_NAME
2268 : 10458 : || TREE_CODE (def_arg2[1]) != SSA_NAME)
2269 : : return false;
2270 : : else
2271 : : {
2272 : 10458 : tree cdef_arg1[2], cdef_arg2[2], def_arg2_alt[2];
2273 : 10458 : enum tree_code cdef_code[2];
2274 : 10458 : gimple *def_arg_alt_stmt[2] = { NULL, NULL };
2275 : 10458 : int check_range = 0;
2276 : 10458 : gimple *check_range_stmt = NULL;
2277 : : /* Look through conversion of the shift count argument.
2278 : : The C/C++ FE cast any shift count argument to integer_type_node.
2279 : : The only problem might be if the shift count type maximum value
2280 : : is equal or smaller than number of bits in rtype. */
2281 : 31374 : for (i = 0; i < 2; i++)
2282 : : {
2283 : 20916 : def_arg2_alt[i] = def_arg2[i];
2284 : 20916 : defcodefor_name (def_arg2[i], &cdef_code[i],
2285 : : &cdef_arg1[i], &cdef_arg2[i]);
2286 : 16367 : if (CONVERT_EXPR_CODE_P (cdef_code[i])
2287 : 4549 : && INTEGRAL_TYPE_P (TREE_TYPE (cdef_arg1[i]))
2288 : 4549 : && TYPE_PRECISION (TREE_TYPE (cdef_arg1[i]))
2289 : 9098 : > floor_log2 (TYPE_PRECISION (rtype))
2290 : 25465 : && type_has_mode_precision_p (TREE_TYPE (cdef_arg1[i])))
2291 : : {
2292 : 4549 : def_arg2_alt[i] = cdef_arg1[i];
2293 : 4549 : if (TREE_CODE (def_arg2[i]) == SSA_NAME)
2294 : 4549 : def_arg_alt_stmt[i] = SSA_NAME_DEF_STMT (def_arg2[i]);
2295 : 4549 : defcodefor_name (def_arg2_alt[i], &cdef_code[i],
2296 : : &cdef_arg1[i], &cdef_arg2[i]);
2297 : : }
2298 : : else
2299 : 16367 : def_arg_alt_stmt[i] = def_arg_stmt[i];
2300 : : }
2301 : 28568 : for (i = 0; i < 2; i++)
2302 : : /* Check for one shift count being Y and the other B - Y,
2303 : : with optional casts. */
2304 : 20570 : if (cdef_code[i] == MINUS_EXPR
2305 : 878 : && tree_fits_shwi_p (cdef_arg1[i])
2306 : 878 : && tree_to_shwi (cdef_arg1[i]) == TYPE_PRECISION (rtype)
2307 : 21390 : && TREE_CODE (cdef_arg2[i]) == SSA_NAME)
2308 : : {
2309 : 820 : tree tem;
2310 : 820 : enum tree_code code;
2311 : :
2312 : 820 : if (cdef_arg2[i] == def_arg2[1 - i]
2313 : 472 : || cdef_arg2[i] == def_arg2_alt[1 - i])
2314 : : {
2315 : 348 : rotcnt = cdef_arg2[i];
2316 : 348 : check_range = -1;
2317 : 348 : if (cdef_arg2[i] == def_arg2[1 - i])
2318 : 348 : check_range_stmt = def_arg_stmt[1 - i];
2319 : : else
2320 : 0 : check_range_stmt = def_arg_alt_stmt[1 - i];
2321 : 804 : break;
2322 : : }
2323 : 472 : defcodefor_name (cdef_arg2[i], &code, &tem, NULL);
2324 : 16 : if (CONVERT_EXPR_CODE_P (code)
2325 : 456 : && INTEGRAL_TYPE_P (TREE_TYPE (tem))
2326 : 456 : && TYPE_PRECISION (TREE_TYPE (tem))
2327 : 912 : > floor_log2 (TYPE_PRECISION (rtype))
2328 : 456 : && type_has_mode_precision_p (TREE_TYPE (tem))
2329 : 928 : && (tem == def_arg2[1 - i]
2330 : 288 : || tem == def_arg2_alt[1 - i]))
2331 : : {
2332 : 456 : rotcnt = tem;
2333 : 456 : check_range = -1;
2334 : 456 : if (tem == def_arg2[1 - i])
2335 : 168 : check_range_stmt = def_arg_stmt[1 - i];
2336 : : else
2337 : 288 : check_range_stmt = def_arg_alt_stmt[1 - i];
2338 : : break;
2339 : : }
2340 : : }
2341 : : /* The above sequence isn't safe for Y being 0,
2342 : : because then one of the shifts triggers undefined behavior.
2343 : : This alternative is safe even for rotation count of 0.
2344 : : One shift count is Y and the other (-Y) & (B - 1).
2345 : : Or one shift count is Y & (B - 1) and the other (-Y) & (B - 1). */
2346 : 19750 : else if (cdef_code[i] == BIT_AND_EXPR
2347 : 31873 : && pow2p_hwi (TYPE_PRECISION (rtype))
2348 : 13763 : && tree_fits_shwi_p (cdef_arg2[i])
2349 : 27526 : && tree_to_shwi (cdef_arg2[i])
2350 : 13763 : == TYPE_PRECISION (rtype) - 1
2351 : 13682 : && TREE_CODE (cdef_arg1[i]) == SSA_NAME
2352 : 33432 : && gimple_assign_rhs_code (stmt) == BIT_IOR_EXPR)
2353 : : {
2354 : 2526 : tree tem;
2355 : 2526 : enum tree_code code;
2356 : :
2357 : 2526 : defcodefor_name (cdef_arg1[i], &code, &tem, NULL);
2358 : 2328 : if (CONVERT_EXPR_CODE_P (code)
2359 : 198 : && INTEGRAL_TYPE_P (TREE_TYPE (tem))
2360 : 198 : && TYPE_PRECISION (TREE_TYPE (tem))
2361 : 396 : > floor_log2 (TYPE_PRECISION (rtype))
2362 : 2724 : && type_has_mode_precision_p (TREE_TYPE (tem)))
2363 : 198 : defcodefor_name (tem, &code, &tem, NULL);
2364 : :
2365 : 2526 : if (code == NEGATE_EXPR)
2366 : : {
2367 : 1670 : if (tem == def_arg2[1 - i] || tem == def_arg2_alt[1 - i])
2368 : : {
2369 : 999 : rotcnt = tem;
2370 : 999 : check_range = 1;
2371 : 999 : if (tem == def_arg2[1 - i])
2372 : 991 : check_range_stmt = def_arg_stmt[1 - i];
2373 : : else
2374 : 8 : check_range_stmt = def_arg_alt_stmt[1 - i];
2375 : 1656 : break;
2376 : : }
2377 : 671 : tree tem2;
2378 : 671 : defcodefor_name (tem, &code, &tem2, NULL);
2379 : 237 : if (CONVERT_EXPR_CODE_P (code)
2380 : 434 : && INTEGRAL_TYPE_P (TREE_TYPE (tem2))
2381 : 434 : && TYPE_PRECISION (TREE_TYPE (tem2))
2382 : 868 : > floor_log2 (TYPE_PRECISION (rtype))
2383 : 1105 : && type_has_mode_precision_p (TREE_TYPE (tem2)))
2384 : : {
2385 : 434 : if (tem2 == def_arg2[1 - i]
2386 : 434 : || tem2 == def_arg2_alt[1 - i])
2387 : : {
2388 : 228 : rotcnt = tem2;
2389 : 228 : check_range = 1;
2390 : 228 : if (tem2 == def_arg2[1 - i])
2391 : 0 : check_range_stmt = def_arg_stmt[1 - i];
2392 : : else
2393 : 228 : check_range_stmt = def_arg_alt_stmt[1 - i];
2394 : : break;
2395 : : }
2396 : : }
2397 : : else
2398 : 237 : tem2 = NULL_TREE;
2399 : :
2400 : 443 : if (cdef_code[1 - i] == BIT_AND_EXPR
2401 : 430 : && tree_fits_shwi_p (cdef_arg2[1 - i])
2402 : 860 : && tree_to_shwi (cdef_arg2[1 - i])
2403 : 430 : == TYPE_PRECISION (rtype) - 1
2404 : 873 : && TREE_CODE (cdef_arg1[1 - i]) == SSA_NAME)
2405 : : {
2406 : 430 : if (tem == cdef_arg1[1 - i]
2407 : 205 : || tem2 == cdef_arg1[1 - i])
2408 : : {
2409 : : rotcnt = def_arg2[1 - i];
2410 : 429 : break;
2411 : : }
2412 : 193 : tree tem3;
2413 : 193 : defcodefor_name (cdef_arg1[1 - i], &code, &tem3, NULL);
2414 : 0 : if (CONVERT_EXPR_CODE_P (code)
2415 : 193 : && INTEGRAL_TYPE_P (TREE_TYPE (tem3))
2416 : 193 : && TYPE_PRECISION (TREE_TYPE (tem3))
2417 : 386 : > floor_log2 (TYPE_PRECISION (rtype))
2418 : 386 : && type_has_mode_precision_p (TREE_TYPE (tem3)))
2419 : : {
2420 : 193 : if (tem == tem3 || tem2 == tem3)
2421 : : {
2422 : : rotcnt = def_arg2[1 - i];
2423 : : break;
2424 : : }
2425 : : }
2426 : : }
2427 : : }
2428 : : }
2429 : 2460 : if (check_range && wider_prec > TYPE_PRECISION (rtype))
2430 : : {
2431 : 1676 : if (TREE_CODE (rotcnt) != SSA_NAME)
2432 : 718 : return false;
2433 : 1676 : int_range_max r;
2434 : 1676 : range_query *q = get_range_query (cfun);
2435 : 1676 : if (q == get_global_range_query ())
2436 : 1520 : q = enable_ranger (cfun);
2437 : 1676 : if (!q->range_of_expr (r, rotcnt, check_range_stmt))
2438 : : {
2439 : 0 : if (check_range > 0)
2440 : : return false;
2441 : 0 : r.set_varying (TREE_TYPE (rotcnt));
2442 : : }
2443 : 1676 : int prec = TYPE_PRECISION (TREE_TYPE (rotcnt));
2444 : 1676 : signop sign = TYPE_SIGN (TREE_TYPE (rotcnt));
2445 : 1676 : wide_int min = wide_int::from (TYPE_PRECISION (rtype), prec, sign);
2446 : 1676 : wide_int max = wide_int::from (wider_prec - 1, prec, sign);
2447 : 1676 : if (check_range < 0)
2448 : 614 : max = min;
2449 : 1676 : int_range<1> r2 (TREE_TYPE (rotcnt), min, max);
2450 : 1676 : r.intersect (r2);
2451 : 1676 : if (!r.undefined_p ())
2452 : : {
2453 : 1324 : if (check_range > 0)
2454 : : {
2455 : 734 : int_range_max r3;
2456 : 2296 : for (int i = TYPE_PRECISION (rtype) + 1; i < wider_prec;
2457 : 1562 : i += TYPE_PRECISION (rtype))
2458 : : {
2459 : 1562 : int j = i + TYPE_PRECISION (rtype) - 2;
2460 : 1562 : min = wide_int::from (i, prec, sign);
2461 : 1562 : max = wide_int::from (MIN (j, wider_prec - 1),
2462 : 1562 : prec, sign);
2463 : 1562 : int_range<1> r4 (TREE_TYPE (rotcnt), min, max);
2464 : 1562 : r3.union_ (r4);
2465 : 1562 : }
2466 : 734 : r.intersect (r3);
2467 : 734 : if (!r.undefined_p ())
2468 : 718 : return false;
2469 : 734 : }
2470 : : add_masking = true;
2471 : : }
2472 : 1676 : }
2473 : 9740 : if (rotcnt == NULL_TREE)
2474 : : return false;
2475 : 1742 : swapped_p = i != 1;
2476 : : }
2477 : :
2478 : 2540 : if (!useless_type_conversion_p (TREE_TYPE (def_arg2[0]),
2479 : 2540 : TREE_TYPE (rotcnt)))
2480 : : {
2481 : 496 : g = gimple_build_assign (make_ssa_name (TREE_TYPE (def_arg2[0])),
2482 : : NOP_EXPR, rotcnt);
2483 : 496 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
2484 : 496 : rotcnt = gimple_assign_lhs (g);
2485 : : }
2486 : 2540 : if (add_masking)
2487 : : {
2488 : 606 : g = gimple_build_assign (make_ssa_name (TREE_TYPE (rotcnt)),
2489 : : BIT_AND_EXPR, rotcnt,
2490 : 606 : build_int_cst (TREE_TYPE (rotcnt),
2491 : 606 : TYPE_PRECISION (rtype) - 1));
2492 : 606 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
2493 : 606 : rotcnt = gimple_assign_lhs (g);
2494 : : }
2495 : 2540 : lhs = gimple_assign_lhs (stmt);
2496 : 2540 : if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0])))
2497 : 1014 : lhs = make_ssa_name (TREE_TYPE (def_arg1[0]));
2498 : 2540 : g = gimple_build_assign (lhs,
2499 : 2540 : ((def_code[0] == LSHIFT_EXPR) ^ swapped_p)
2500 : : ? LROTATE_EXPR : RROTATE_EXPR, def_arg1[0], rotcnt);
2501 : 2540 : if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0])))
2502 : : {
2503 : 1014 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
2504 : 1014 : g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, lhs);
2505 : : }
2506 : 2540 : gsi_replace (gsi, g, false);
2507 : 2540 : return true;
2508 : : }
2509 : :
2510 : :
2511 : : /* Check whether an array contains a valid table according to VALIDATE_FN. */
2512 : : template<typename ValidateFn>
2513 : : static bool
2514 : 8 : check_table_array (tree ctor, HOST_WIDE_INT &zero_val, unsigned bits,
2515 : : ValidateFn validate_fn)
2516 : : {
2517 : : tree elt, idx;
2518 : 8 : unsigned HOST_WIDE_INT i, raw_idx = 0;
2519 : 8 : unsigned matched = 0;
2520 : :
2521 : 8 : zero_val = 0;
2522 : :
2523 : 350 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, elt)
2524 : : {
2525 : 350 : if (!tree_fits_shwi_p (idx))
2526 : : return false;
2527 : 350 : if (!tree_fits_shwi_p (elt) && TREE_CODE (elt) != RAW_DATA_CST)
2528 : : return false;
2529 : :
2530 : 350 : unsigned HOST_WIDE_INT index = tree_to_shwi (idx);
2531 : : HOST_WIDE_INT val;
2532 : :
2533 : 350 : if (TREE_CODE (elt) == INTEGER_CST)
2534 : 286 : val = tree_to_shwi (elt);
2535 : : else
2536 : : {
2537 : 64 : if (raw_idx == (unsigned) RAW_DATA_LENGTH (elt))
2538 : : {
2539 : 0 : raw_idx = 0;
2540 : 0 : continue;
2541 : : }
2542 : 64 : if (TYPE_UNSIGNED (TREE_TYPE (elt)))
2543 : 0 : val = RAW_DATA_UCHAR_ELT (elt, raw_idx);
2544 : : else
2545 : 64 : val = RAW_DATA_SCHAR_ELT (elt, raw_idx);
2546 : 64 : index += raw_idx;
2547 : 64 : raw_idx++;
2548 : 64 : i--;
2549 : : }
2550 : :
2551 : 350 : if (index > bits * 2)
2552 : : return false;
2553 : :
2554 : 350 : if (index == 0)
2555 : : {
2556 : 8 : zero_val = val;
2557 : 8 : matched++;
2558 : : }
2559 : :
2560 : 350 : if (val >= 0 && val < bits && validate_fn (val, index))
2561 : 288 : matched++;
2562 : :
2563 : 350 : if (matched > bits)
2564 : : return true;
2565 : : }
2566 : :
2567 : : return false;
2568 : : }
2569 : :
2570 : : /* Check whether a string contains a valid table according to VALIDATE_FN. */
2571 : : template<typename ValidateFn>
2572 : : static bool
2573 : 4 : check_table_string (tree string, HOST_WIDE_INT &zero_val,unsigned bits,
2574 : : ValidateFn validate_fn)
2575 : : {
2576 : 4 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (string);
2577 : 4 : unsigned matched = 0;
2578 : 4 : const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (string);
2579 : :
2580 : 4 : if (len < bits || len > bits * 2)
2581 : : return false;
2582 : :
2583 : 4 : zero_val = p[0];
2584 : :
2585 : 164 : for (unsigned i = 0; i < len; i++)
2586 : 160 : if (p[i] < bits && validate_fn (p[i], i))
2587 : 160 : matched++;
2588 : :
2589 : 4 : return matched == bits;
2590 : : }
2591 : :
2592 : : /* Check whether CTOR contains a valid table according to VALIDATE_FN. */
2593 : : template<typename ValidateFn>
2594 : : static bool
2595 : 20 : check_table (tree ctor, tree type, HOST_WIDE_INT &zero_val, unsigned bits,
2596 : : ValidateFn validate_fn)
2597 : : {
2598 : 20 : if (TREE_CODE (ctor) == CONSTRUCTOR)
2599 : 8 : return check_table_array (ctor, zero_val, bits, validate_fn);
2600 : : else if (TREE_CODE (ctor) == STRING_CST
2601 : 12 : && TYPE_PRECISION (type) == CHAR_TYPE_SIZE)
2602 : 4 : return check_table_string (ctor, zero_val, bits, validate_fn);
2603 : : return false;
2604 : : }
2605 : :
2606 : : /* Match.pd function to match the ctz expression. */
2607 : : extern bool gimple_ctz_table_index (tree, tree *, tree (*)(tree));
2608 : : extern bool gimple_clz_table_index (tree, tree *, tree (*)(tree));
2609 : :
2610 : : /* Recognize count leading and trailing zeroes idioms.
2611 : : The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
2612 : : constant which when multiplied by a power of 2 creates a unique value
2613 : : in the top 5 or 6 bits. This is then indexed into a table which maps it
2614 : : to the number of trailing zeroes. Array[0] is returned so the caller can
2615 : : emit an appropriate sequence depending on whether ctz (0) is defined on
2616 : : the target. */
2617 : :
2618 : : static bool
2619 : 1993267 : simplify_count_zeroes (gimple_stmt_iterator *gsi)
2620 : : {
2621 : 1993267 : gimple *stmt = gsi_stmt (*gsi);
2622 : 1993267 : tree array_ref = gimple_assign_rhs1 (stmt);
2623 : 1993267 : tree res_ops[3];
2624 : :
2625 : 1993267 : gcc_checking_assert (TREE_CODE (array_ref) == ARRAY_REF);
2626 : :
2627 : 1993267 : internal_fn fn = IFN_LAST;
2628 : : /* For CTZ we recognize ((x & -x) * C) >> SHIFT where the array data
2629 : : represents the number of trailing zeros. */
2630 : 1993267 : if (gimple_ctz_table_index (TREE_OPERAND (array_ref, 1), &res_ops[0], NULL))
2631 : : fn = IFN_CTZ;
2632 : : /* For CLZ we recognize
2633 : : x |= x >> 1;
2634 : : x |= x >> 2;
2635 : : x |= x >> 4;
2636 : : x |= x >> 8;
2637 : : x |= x >> 16;
2638 : : (x * C) >> SHIFT
2639 : : where 31 minus the array data represents the number of leading zeros. */
2640 : 1993245 : else if (gimple_clz_table_index (TREE_OPERAND (array_ref, 1), &res_ops[0],
2641 : : NULL))
2642 : : fn = IFN_CLZ;
2643 : : else
2644 : : return false;
2645 : :
2646 : 25 : HOST_WIDE_INT zero_val;
2647 : 25 : tree type = TREE_TYPE (array_ref);
2648 : 25 : tree array = TREE_OPERAND (array_ref, 0);
2649 : 25 : tree input_type = TREE_TYPE (res_ops[0]);
2650 : 25 : unsigned input_bits = tree_to_shwi (TYPE_SIZE (input_type));
2651 : :
2652 : : /* Check the array element type is not wider than 32 bits and the input is
2653 : : an unsigned 32-bit or 64-bit type. */
2654 : 25 : if (TYPE_PRECISION (type) > 32 || !TYPE_UNSIGNED (input_type))
2655 : : return false;
2656 : 21 : if (input_bits != 32 && input_bits != 64)
2657 : : return false;
2658 : :
2659 : 21 : if (!direct_internal_fn_supported_p (fn, input_type, OPTIMIZE_FOR_BOTH))
2660 : : return false;
2661 : :
2662 : : /* Check the lower bound of the array is zero. */
2663 : 21 : tree low = array_ref_low_bound (array_ref);
2664 : 21 : if (!low || !integer_zerop (low))
2665 : 0 : return false;
2666 : :
2667 : : /* Check the shift extracts the top 5..7 bits. */
2668 : 21 : unsigned shiftval = tree_to_shwi (res_ops[2]);
2669 : 21 : if (shiftval < input_bits - 7 || shiftval > input_bits - 5)
2670 : : return false;
2671 : :
2672 : 20 : tree ctor = ctor_for_folding (array);
2673 : 20 : if (!ctor)
2674 : : return false;
2675 : 20 : unsigned HOST_WIDE_INT mulval = tree_to_uhwi (res_ops[1]);
2676 : 20 : if (fn == IFN_CTZ)
2677 : : {
2678 : 429 : auto checkfn = [&](unsigned data, unsigned i) -> bool
2679 : : {
2680 : 412 : unsigned HOST_WIDE_INT mask
2681 : 412 : = ((HOST_WIDE_INT_1U << (input_bits - shiftval)) - 1) << shiftval;
2682 : 412 : return (((mulval << data) & mask) >> shiftval) == i;
2683 : 17 : };
2684 : 17 : if (!check_table (ctor, type, zero_val, input_bits, checkfn))
2685 : 8 : return false;
2686 : : }
2687 : 3 : else if (fn == IFN_CLZ)
2688 : : {
2689 : 99 : auto checkfn = [&](unsigned data, unsigned i) -> bool
2690 : : {
2691 : 96 : unsigned HOST_WIDE_INT mask
2692 : 96 : = ((HOST_WIDE_INT_1U << (input_bits - shiftval)) - 1) << shiftval;
2693 : 96 : return (((((HOST_WIDE_INT_1U << (data + 1)) - 1) * mulval) & mask)
2694 : 96 : >> shiftval) == i;
2695 : 3 : };
2696 : 3 : if (!check_table (ctor, type, zero_val, input_bits, checkfn))
2697 : 0 : return false;
2698 : : }
2699 : :
2700 : 12 : HOST_WIDE_INT ctz_val = -1;
2701 : 12 : bool zero_ok;
2702 : 12 : if (fn == IFN_CTZ)
2703 : : {
2704 : 9 : ctz_val = 0;
2705 : 18 : zero_ok = CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (input_type),
2706 : : ctz_val) == 2;
2707 : : }
2708 : 3 : else if (fn == IFN_CLZ)
2709 : : {
2710 : 3 : ctz_val = 32;
2711 : 3 : zero_ok = CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (input_type),
2712 : : ctz_val) == 2;
2713 : 3 : zero_val = input_bits - 1 - zero_val;
2714 : : }
2715 : 12 : int nargs = 2;
2716 : :
2717 : : /* If the input value can't be zero, don't special case ctz (0). */
2718 : 12 : range_query *q = get_range_query (cfun);
2719 : 12 : if (q == get_global_range_query ())
2720 : 12 : q = enable_ranger (cfun);
2721 : 12 : int_range_max vr;
2722 : 12 : if (q->range_of_expr (vr, res_ops[0], stmt)
2723 : 12 : && !range_includes_zero_p (vr))
2724 : : {
2725 : 4 : zero_ok = true;
2726 : 4 : zero_val = 0;
2727 : 4 : ctz_val = 0;
2728 : 4 : nargs = 1;
2729 : : }
2730 : :
2731 : 12 : gimple_seq seq = NULL;
2732 : 12 : gimple *g;
2733 : 12 : gcall *call = gimple_build_call_internal (fn, nargs, res_ops[0],
2734 : : nargs == 1 ? NULL_TREE
2735 : 20 : : build_int_cst (integer_type_node,
2736 : 8 : ctz_val));
2737 : 12 : gimple_set_location (call, gimple_location (stmt));
2738 : 12 : gimple_set_lhs (call, make_ssa_name (integer_type_node));
2739 : 12 : gimple_seq_add_stmt (&seq, call);
2740 : :
2741 : 12 : tree prev_lhs = gimple_call_lhs (call);
2742 : 12 : if (fn == IFN_CLZ)
2743 : : {
2744 : 3 : g = gimple_build_assign (make_ssa_name (integer_type_node),
2745 : : MINUS_EXPR,
2746 : : build_int_cst (integer_type_node,
2747 : 3 : input_bits - 1),
2748 : : prev_lhs);
2749 : 3 : gimple_set_location (g, gimple_location (stmt));
2750 : 3 : gimple_seq_add_stmt (&seq, g);
2751 : 3 : prev_lhs = gimple_assign_lhs (g);
2752 : : }
2753 : :
2754 : 12 : if (zero_ok && zero_val == ctz_val)
2755 : : ;
2756 : : /* Emit ctz (x) & 31 if ctz (0) is 32 but we need to return 0. */
2757 : 6 : else if (zero_ok && zero_val == 0 && ctz_val == input_bits)
2758 : : {
2759 : 5 : g = gimple_build_assign (make_ssa_name (integer_type_node),
2760 : : BIT_AND_EXPR, prev_lhs,
2761 : : build_int_cst (integer_type_node,
2762 : 5 : input_bits - 1));
2763 : 5 : gimple_set_location (g, gimple_location (stmt));
2764 : 5 : gimple_seq_add_stmt (&seq, g);
2765 : 5 : prev_lhs = gimple_assign_lhs (g);
2766 : : }
2767 : : /* As fallback emit a conditional move. */
2768 : : else
2769 : : {
2770 : 1 : g = gimple_build_assign (make_ssa_name (boolean_type_node), EQ_EXPR,
2771 : : res_ops[0], build_zero_cst (input_type));
2772 : 1 : gimple_set_location (g, gimple_location (stmt));
2773 : 1 : gimple_seq_add_stmt (&seq, g);
2774 : 1 : tree cond = gimple_assign_lhs (g);
2775 : 1 : g = gimple_build_assign (make_ssa_name (integer_type_node),
2776 : : COND_EXPR, cond,
2777 : 1 : build_int_cst (integer_type_node, zero_val),
2778 : : prev_lhs);
2779 : 1 : gimple_set_location (g, gimple_location (stmt));
2780 : 1 : gimple_seq_add_stmt (&seq, g);
2781 : 1 : prev_lhs = gimple_assign_lhs (g);
2782 : : }
2783 : :
2784 : 12 : g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, prev_lhs);
2785 : 12 : gimple_seq_add_stmt (&seq, g);
2786 : 12 : gsi_replace_with_seq (gsi, seq, true);
2787 : 12 : return true;
2788 : 12 : }
2789 : :
2790 : :
2791 : : /* Determine whether applying the 2 permutations (mask1 then mask2)
2792 : : gives back one of the input. */
2793 : :
2794 : : static int
2795 : 34 : is_combined_permutation_identity (tree mask1, tree mask2)
2796 : : {
2797 : 34 : tree mask;
2798 : 34 : unsigned HOST_WIDE_INT nelts, i, j;
2799 : 34 : bool maybe_identity1 = true;
2800 : 34 : bool maybe_identity2 = true;
2801 : :
2802 : 34 : gcc_checking_assert (TREE_CODE (mask1) == VECTOR_CST
2803 : : && TREE_CODE (mask2) == VECTOR_CST);
2804 : :
2805 : : /* For VLA masks, check for the following pattern:
2806 : : v1 = VEC_PERM_EXPR (v0, ..., mask1)
2807 : : v2 = VEC_PERM_EXPR (v1, ..., mask2)
2808 : : -->
2809 : : v2 = v0
2810 : : if mask1 == mask2 == {nelts - 1, nelts - 2, ...}. */
2811 : :
2812 : 34 : if (operand_equal_p (mask1, mask2, 0)
2813 : 34 : && !VECTOR_CST_NELTS (mask1).is_constant ())
2814 : : {
2815 : : vec_perm_builder builder;
2816 : : if (tree_to_vec_perm_builder (&builder, mask1))
2817 : : {
2818 : : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask1));
2819 : : vec_perm_indices sel (builder, 1, nelts);
2820 : : if (sel.series_p (0, 1, nelts - 1, -1))
2821 : : return 1;
2822 : : }
2823 : : }
2824 : :
2825 : 34 : mask = fold_ternary (VEC_PERM_EXPR, TREE_TYPE (mask1), mask1, mask1, mask2);
2826 : 34 : if (mask == NULL_TREE || TREE_CODE (mask) != VECTOR_CST)
2827 : : return 0;
2828 : :
2829 : 34 : if (!VECTOR_CST_NELTS (mask).is_constant (&nelts))
2830 : : return 0;
2831 : 60 : for (i = 0; i < nelts; i++)
2832 : : {
2833 : 60 : tree val = VECTOR_CST_ELT (mask, i);
2834 : 60 : gcc_assert (TREE_CODE (val) == INTEGER_CST);
2835 : 60 : j = TREE_INT_CST_LOW (val) & (2 * nelts - 1);
2836 : 60 : if (j == i)
2837 : : maybe_identity2 = false;
2838 : 47 : else if (j == i + nelts)
2839 : : maybe_identity1 = false;
2840 : : else
2841 : : return 0;
2842 : : }
2843 : 0 : return maybe_identity1 ? 1 : maybe_identity2 ? 2 : 0;
2844 : : }
2845 : :
2846 : : /* Combine a shuffle with its arguments. Returns 1 if there were any
2847 : : changes made, 2 if cfg-cleanup needs to run. Else it returns 0. */
2848 : :
2849 : : static int
2850 : 173905 : simplify_permutation (gimple_stmt_iterator *gsi)
2851 : : {
2852 : 173905 : gimple *stmt = gsi_stmt (*gsi);
2853 : 173905 : gimple *def_stmt = NULL;
2854 : 173905 : tree op0, op1, op2, op3, arg0, arg1;
2855 : 173905 : enum tree_code code, code2 = ERROR_MARK;
2856 : 173905 : bool single_use_op0 = false;
2857 : :
2858 : 173905 : gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
2859 : :
2860 : 173905 : op0 = gimple_assign_rhs1 (stmt);
2861 : 173905 : op1 = gimple_assign_rhs2 (stmt);
2862 : 173905 : op2 = gimple_assign_rhs3 (stmt);
2863 : :
2864 : 173905 : if (TREE_CODE (op2) != VECTOR_CST)
2865 : : return 0;
2866 : :
2867 : 171187 : if (TREE_CODE (op0) == VECTOR_CST)
2868 : : {
2869 : : code = VECTOR_CST;
2870 : : arg0 = op0;
2871 : : }
2872 : 169347 : else if (TREE_CODE (op0) == SSA_NAME)
2873 : : {
2874 : 169347 : def_stmt = get_prop_source_stmt (op0, false, &single_use_op0);
2875 : 169347 : if (!def_stmt)
2876 : : return 0;
2877 : 161512 : code = gimple_assign_rhs_code (def_stmt);
2878 : 161512 : if (code == VIEW_CONVERT_EXPR)
2879 : : {
2880 : 1267 : tree rhs = gimple_assign_rhs1 (def_stmt);
2881 : 1267 : tree name = TREE_OPERAND (rhs, 0);
2882 : 1267 : if (TREE_CODE (name) != SSA_NAME)
2883 : : return 0;
2884 : 1267 : if (!has_single_use (name))
2885 : 206 : single_use_op0 = false;
2886 : : /* Here we update the def_stmt through this VIEW_CONVERT_EXPR,
2887 : : but still keep the code to indicate it comes from
2888 : : VIEW_CONVERT_EXPR. */
2889 : 1267 : def_stmt = SSA_NAME_DEF_STMT (name);
2890 : 1267 : if (!def_stmt || !is_gimple_assign (def_stmt))
2891 : : return 0;
2892 : 483 : if (gimple_assign_rhs_code (def_stmt) != CONSTRUCTOR)
2893 : : return 0;
2894 : : }
2895 : 160344 : if (!can_propagate_from (def_stmt))
2896 : : return 0;
2897 : 18765 : arg0 = gimple_assign_rhs1 (def_stmt);
2898 : : }
2899 : : else
2900 : : return 0;
2901 : :
2902 : : /* Two consecutive shuffles. */
2903 : 18765 : if (code == VEC_PERM_EXPR)
2904 : : {
2905 : 6136 : tree orig;
2906 : 6136 : int ident;
2907 : :
2908 : 6136 : if (op0 != op1)
2909 : : return 0;
2910 : 34 : op3 = gimple_assign_rhs3 (def_stmt);
2911 : 34 : if (TREE_CODE (op3) != VECTOR_CST)
2912 : : return 0;
2913 : 34 : ident = is_combined_permutation_identity (op3, op2);
2914 : 34 : if (!ident)
2915 : : return 0;
2916 : 0 : orig = (ident == 1) ? gimple_assign_rhs1 (def_stmt)
2917 : 0 : : gimple_assign_rhs2 (def_stmt);
2918 : 0 : gimple_assign_set_rhs1 (stmt, unshare_expr (orig));
2919 : 0 : gimple_assign_set_rhs_code (stmt, TREE_CODE (orig));
2920 : 0 : gimple_set_num_ops (stmt, 2);
2921 : 0 : update_stmt (stmt);
2922 : 0 : return remove_prop_source_from_use (op0) ? 2 : 1;
2923 : : }
2924 : 14469 : else if (code == CONSTRUCTOR
2925 : 14469 : || code == VECTOR_CST
2926 : : || code == VIEW_CONVERT_EXPR)
2927 : : {
2928 : 2500 : if (op0 != op1)
2929 : : {
2930 : 2350 : if (TREE_CODE (op0) == SSA_NAME && !single_use_op0)
2931 : : return 0;
2932 : :
2933 : 2039 : if (TREE_CODE (op1) == VECTOR_CST)
2934 : : arg1 = op1;
2935 : 1569 : else if (TREE_CODE (op1) == SSA_NAME)
2936 : : {
2937 : 1569 : gimple *def_stmt2 = get_prop_source_stmt (op1, true, NULL);
2938 : 1569 : if (!def_stmt2)
2939 : : return 0;
2940 : 159 : code2 = gimple_assign_rhs_code (def_stmt2);
2941 : 159 : if (code2 == VIEW_CONVERT_EXPR)
2942 : : {
2943 : 0 : tree rhs = gimple_assign_rhs1 (def_stmt2);
2944 : 0 : tree name = TREE_OPERAND (rhs, 0);
2945 : 0 : if (TREE_CODE (name) != SSA_NAME)
2946 : : return 0;
2947 : 0 : if (!has_single_use (name))
2948 : : return 0;
2949 : 0 : def_stmt2 = SSA_NAME_DEF_STMT (name);
2950 : 0 : if (!def_stmt2 || !is_gimple_assign (def_stmt2))
2951 : : return 0;
2952 : 0 : if (gimple_assign_rhs_code (def_stmt2) != CONSTRUCTOR)
2953 : : return 0;
2954 : : }
2955 : 159 : else if (code2 != CONSTRUCTOR && code2 != VECTOR_CST)
2956 : : return 0;
2957 : 49 : if (!can_propagate_from (def_stmt2))
2958 : : return 0;
2959 : 49 : arg1 = gimple_assign_rhs1 (def_stmt2);
2960 : : }
2961 : : else
2962 : : return 0;
2963 : : }
2964 : : else
2965 : : {
2966 : : /* Already used twice in this statement. */
2967 : 150 : if (TREE_CODE (op0) == SSA_NAME && num_imm_uses (op0) > 2)
2968 : : return 0;
2969 : : arg1 = arg0;
2970 : : }
2971 : :
2972 : : /* If there are any VIEW_CONVERT_EXPRs found when finding permutation
2973 : : operands source, check whether it's valid to transform and prepare
2974 : : the required new operands. */
2975 : 582 : if (code == VIEW_CONVERT_EXPR || code2 == VIEW_CONVERT_EXPR)
2976 : : {
2977 : : /* Figure out the target vector type to which operands should be
2978 : : converted. If both are CONSTRUCTOR, the types should be the
2979 : : same, otherwise, use the one of CONSTRUCTOR. */
2980 : 18 : tree tgt_type = NULL_TREE;
2981 : 18 : if (code == VIEW_CONVERT_EXPR)
2982 : : {
2983 : 18 : gcc_assert (gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR);
2984 : 18 : code = CONSTRUCTOR;
2985 : 18 : tgt_type = TREE_TYPE (arg0);
2986 : : }
2987 : 18 : if (code2 == VIEW_CONVERT_EXPR)
2988 : : {
2989 : 0 : tree arg1_type = TREE_TYPE (arg1);
2990 : 0 : if (tgt_type == NULL_TREE)
2991 : : tgt_type = arg1_type;
2992 : 0 : else if (tgt_type != arg1_type)
2993 : 17 : return 0;
2994 : : }
2995 : :
2996 : 18 : if (!VECTOR_TYPE_P (tgt_type))
2997 : : return 0;
2998 : 18 : tree op2_type = TREE_TYPE (op2);
2999 : :
3000 : : /* Figure out the shrunk factor. */
3001 : 18 : poly_uint64 tgt_units = TYPE_VECTOR_SUBPARTS (tgt_type);
3002 : 18 : poly_uint64 op2_units = TYPE_VECTOR_SUBPARTS (op2_type);
3003 : 18 : if (maybe_gt (tgt_units, op2_units))
3004 : : return 0;
3005 : 18 : unsigned int factor;
3006 : 35 : if (!constant_multiple_p (op2_units, tgt_units, &factor))
3007 : : return 0;
3008 : :
3009 : : /* Build the new permutation control vector as target vector. */
3010 : 18 : vec_perm_builder builder;
3011 : 18 : if (!tree_to_vec_perm_builder (&builder, op2))
3012 : : return 0;
3013 : 18 : vec_perm_indices indices (builder, 2, op2_units);
3014 : 18 : vec_perm_indices new_indices;
3015 : 18 : if (new_indices.new_shrunk_vector (indices, factor))
3016 : : {
3017 : 1 : tree mask_type = tgt_type;
3018 : 1 : if (!VECTOR_INTEGER_TYPE_P (mask_type))
3019 : : {
3020 : 0 : tree elem_type = TREE_TYPE (mask_type);
3021 : 0 : unsigned elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type));
3022 : 0 : tree int_type = build_nonstandard_integer_type (elem_size, 0);
3023 : 0 : mask_type = build_vector_type (int_type, tgt_units);
3024 : : }
3025 : 1 : op2 = vec_perm_indices_to_tree (mask_type, new_indices);
3026 : : }
3027 : : else
3028 : 17 : return 0;
3029 : :
3030 : : /* Convert the VECTOR_CST to the appropriate vector type. */
3031 : 1 : if (tgt_type != TREE_TYPE (arg0))
3032 : 0 : arg0 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg0);
3033 : 1 : else if (tgt_type != TREE_TYPE (arg1))
3034 : 0 : arg1 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg1);
3035 : 35 : }
3036 : :
3037 : : /* VIEW_CONVERT_EXPR should be updated to CONSTRUCTOR before. */
3038 : 565 : gcc_assert (code == CONSTRUCTOR || code == VECTOR_CST);
3039 : :
3040 : : /* Shuffle of a constructor. */
3041 : 565 : bool ret = false;
3042 : 565 : tree res_type
3043 : 565 : = build_vector_type (TREE_TYPE (TREE_TYPE (arg0)),
3044 : 565 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (op2)));
3045 : 565 : tree opt = fold_ternary (VEC_PERM_EXPR, res_type, arg0, arg1, op2);
3046 : 565 : if (!opt
3047 : 280 : || (TREE_CODE (opt) != CONSTRUCTOR && TREE_CODE (opt) != VECTOR_CST))
3048 : : return 0;
3049 : : /* Found VIEW_CONVERT_EXPR before, need one explicit conversion. */
3050 : 280 : if (res_type != TREE_TYPE (op0))
3051 : : {
3052 : 1 : tree name = make_ssa_name (TREE_TYPE (opt));
3053 : 1 : gimple *ass_stmt = gimple_build_assign (name, opt);
3054 : 1 : gsi_insert_before (gsi, ass_stmt, GSI_SAME_STMT);
3055 : 1 : opt = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op0), name);
3056 : : }
3057 : 280 : gimple_assign_set_rhs_from_tree (gsi, opt);
3058 : 280 : update_stmt (gsi_stmt (*gsi));
3059 : 280 : if (TREE_CODE (op0) == SSA_NAME)
3060 : 1 : ret = remove_prop_source_from_use (op0);
3061 : 280 : if (op0 != op1 && TREE_CODE (op1) == SSA_NAME)
3062 : 0 : ret |= remove_prop_source_from_use (op1);
3063 : 280 : return ret ? 2 : 1;
3064 : : }
3065 : :
3066 : : return 0;
3067 : : }
3068 : :
3069 : : /* Get the BIT_FIELD_REF definition of VAL, if any, looking through
3070 : : conversions with code CONV_CODE or update it if still ERROR_MARK.
3071 : : Return NULL_TREE if no such matching def was found. */
3072 : :
3073 : : static tree
3074 : 387449 : get_bit_field_ref_def (tree val, enum tree_code &conv_code)
3075 : : {
3076 : 387449 : if (TREE_CODE (val) != SSA_NAME)
3077 : : return NULL_TREE ;
3078 : 361575 : gimple *def_stmt = get_prop_source_stmt (val, false, NULL);
3079 : 361575 : if (!def_stmt)
3080 : : return NULL_TREE;
3081 : 297496 : enum tree_code code = gimple_assign_rhs_code (def_stmt);
3082 : 297496 : if (code == FLOAT_EXPR
3083 : 297496 : || code == FIX_TRUNC_EXPR
3084 : : || CONVERT_EXPR_CODE_P (code))
3085 : : {
3086 : 181976 : tree op1 = gimple_assign_rhs1 (def_stmt);
3087 : 181976 : if (conv_code == ERROR_MARK)
3088 : 88878 : conv_code = code;
3089 : 93098 : else if (conv_code != code)
3090 : : return NULL_TREE;
3091 : 181950 : if (TREE_CODE (op1) != SSA_NAME)
3092 : : return NULL_TREE;
3093 : 72204 : def_stmt = SSA_NAME_DEF_STMT (op1);
3094 : 72204 : if (! is_gimple_assign (def_stmt))
3095 : : return NULL_TREE;
3096 : 57662 : code = gimple_assign_rhs_code (def_stmt);
3097 : : }
3098 : 173182 : if (code != BIT_FIELD_REF)
3099 : : return NULL_TREE;
3100 : 21922 : return gimple_assign_rhs1 (def_stmt);
3101 : : }
3102 : :
3103 : : /* Recognize a VEC_PERM_EXPR. Returns true if there were any changes. */
3104 : :
3105 : : static bool
3106 : 161325 : simplify_vector_constructor (gimple_stmt_iterator *gsi)
3107 : : {
3108 : 161325 : gimple *stmt = gsi_stmt (*gsi);
3109 : 161325 : tree op, orig[2], type, elem_type;
3110 : 161325 : unsigned elem_size, i;
3111 : 161325 : unsigned HOST_WIDE_INT nelts;
3112 : 161325 : unsigned HOST_WIDE_INT refnelts;
3113 : 161325 : enum tree_code conv_code;
3114 : 161325 : constructor_elt *elt;
3115 : :
3116 : 161325 : op = gimple_assign_rhs1 (stmt);
3117 : 161325 : type = TREE_TYPE (op);
3118 : 161325 : gcc_checking_assert (TREE_CODE (op) == CONSTRUCTOR
3119 : : && TREE_CODE (type) == VECTOR_TYPE);
3120 : :
3121 : 161325 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
3122 : : return false;
3123 : 161325 : elem_type = TREE_TYPE (type);
3124 : 161325 : elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type));
3125 : :
3126 : 161325 : orig[0] = NULL;
3127 : 161325 : orig[1] = NULL;
3128 : 161325 : conv_code = ERROR_MARK;
3129 : 161325 : bool maybe_ident = true;
3130 : 161325 : bool maybe_blend[2] = { true, true };
3131 : 161325 : tree one_constant = NULL_TREE;
3132 : 161325 : tree one_nonconstant = NULL_TREE;
3133 : 161325 : auto_vec<tree> constants;
3134 : 161325 : constants.safe_grow_cleared (nelts, true);
3135 : 161325 : auto_vec<std::pair<unsigned, unsigned>, 64> elts;
3136 : 430330 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (op), i, elt)
3137 : : {
3138 : 387449 : tree ref, op1;
3139 : 387449 : unsigned int elem;
3140 : :
3141 : 387449 : if (i >= nelts)
3142 : 161325 : return false;
3143 : :
3144 : : /* Look for elements extracted and possibly converted from
3145 : : another vector. */
3146 : 387449 : op1 = get_bit_field_ref_def (elt->value, conv_code);
3147 : 387449 : if (op1
3148 : 21922 : && TREE_CODE ((ref = TREE_OPERAND (op1, 0))) == SSA_NAME
3149 : 4049 : && VECTOR_TYPE_P (TREE_TYPE (ref))
3150 : 4046 : && useless_type_conversion_p (TREE_TYPE (op1),
3151 : 4046 : TREE_TYPE (TREE_TYPE (ref)))
3152 : 3661 : && constant_multiple_p (bit_field_offset (op1),
3153 : 391110 : bit_field_size (op1), &elem)
3154 : 391110 : && TYPE_VECTOR_SUBPARTS (TREE_TYPE (ref)).is_constant (&refnelts))
3155 : : {
3156 : : unsigned int j;
3157 : 3818 : for (j = 0; j < 2; ++j)
3158 : : {
3159 : 3807 : if (!orig[j])
3160 : : {
3161 : 1842 : if (j == 0
3162 : 1948 : || useless_type_conversion_p (TREE_TYPE (orig[0]),
3163 : 106 : TREE_TYPE (ref)))
3164 : : break;
3165 : : }
3166 : 1965 : else if (ref == orig[j])
3167 : : break;
3168 : : }
3169 : : /* Found a suitable vector element. */
3170 : 3661 : if (j < 2)
3171 : : {
3172 : 3650 : orig[j] = ref;
3173 : 3650 : if (elem != i || j != 0)
3174 : 1661 : maybe_ident = false;
3175 : 3650 : if (elem != i)
3176 : 1589 : maybe_blend[j] = false;
3177 : 3650 : elts.safe_push (std::make_pair (j, elem));
3178 : 3650 : continue;
3179 : : }
3180 : : /* Else fallthru. */
3181 : : }
3182 : : /* Handle elements not extracted from a vector.
3183 : : 1. constants by permuting with constant vector
3184 : : 2. a unique non-constant element by permuting with a splat vector */
3185 : 383799 : if (orig[1]
3186 : 236245 : && orig[1] != error_mark_node)
3187 : : return false;
3188 : 383787 : orig[1] = error_mark_node;
3189 : 383787 : if (CONSTANT_CLASS_P (elt->value))
3190 : : {
3191 : 25870 : if (one_nonconstant)
3192 : : return false;
3193 : 17961 : if (!one_constant)
3194 : 8916 : one_constant = elt->value;
3195 : 17961 : constants[i] = elt->value;
3196 : : }
3197 : : else
3198 : : {
3199 : 357917 : if (one_constant)
3200 : : return false;
3201 : 349257 : if (!one_nonconstant)
3202 : : one_nonconstant = elt->value;
3203 : 210619 : else if (!operand_equal_p (one_nonconstant, elt->value, 0))
3204 : : return false;
3205 : : }
3206 : 265355 : elts.safe_push (std::make_pair (1, i));
3207 : 265355 : maybe_ident = false;
3208 : : }
3209 : 42881 : if (i < nelts)
3210 : : return false;
3211 : :
3212 : 29195 : if (! orig[0]
3213 : 29195 : || ! VECTOR_TYPE_P (TREE_TYPE (orig[0])))
3214 : : return false;
3215 : 1309 : refnelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (orig[0])).to_constant ();
3216 : : /* We currently do not handle larger destination vectors. */
3217 : 1309 : if (refnelts < nelts)
3218 : : return false;
3219 : :
3220 : 1204 : if (maybe_ident)
3221 : : {
3222 : 389 : tree conv_src_type
3223 : : = (nelts != refnelts
3224 : 389 : ? (conv_code != ERROR_MARK
3225 : 2 : ? build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])), nelts)
3226 : : : type)
3227 : 387 : : TREE_TYPE (orig[0]));
3228 : 389 : if (conv_code != ERROR_MARK
3229 : 389 : && !supportable_convert_operation (conv_code, type, conv_src_type,
3230 : : &conv_code))
3231 : : {
3232 : : /* Only few targets implement direct conversion patterns so try
3233 : : some simple special cases via VEC_[UN]PACK[_FLOAT]_LO_EXPR. */
3234 : 4 : optab optab;
3235 : 4 : insn_code icode;
3236 : 4 : tree halfvectype, dblvectype;
3237 : 4 : enum tree_code unpack_op;
3238 : :
3239 : 4 : if (!BYTES_BIG_ENDIAN)
3240 : 4 : unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
3241 : 4 : ? VEC_UNPACK_FLOAT_LO_EXPR
3242 : : : VEC_UNPACK_LO_EXPR);
3243 : : else
3244 : : unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
3245 : : ? VEC_UNPACK_FLOAT_HI_EXPR
3246 : : : VEC_UNPACK_HI_EXPR);
3247 : :
3248 : : /* Conversions between DFP and FP have no special tree code
3249 : : but we cannot handle those since all relevant vector conversion
3250 : : optabs only have a single mode. */
3251 : 2 : if (CONVERT_EXPR_CODE_P (conv_code)
3252 : 2 : && FLOAT_TYPE_P (TREE_TYPE (type))
3253 : 8 : && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (type))
3254 : 2 : != DECIMAL_FLOAT_TYPE_P (TREE_TYPE (conv_src_type))))
3255 : : return false;
3256 : :
3257 : 2 : if (CONVERT_EXPR_CODE_P (conv_code)
3258 : 1 : && (2 * TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
3259 : 1 : == TYPE_PRECISION (TREE_TYPE (type)))
3260 : 0 : && mode_for_vector (as_a <scalar_mode>
3261 : 0 : (TYPE_MODE (TREE_TYPE (TREE_TYPE (orig[0])))),
3262 : 0 : nelts * 2).exists ()
3263 : 0 : && (dblvectype
3264 : 0 : = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
3265 : 0 : nelts * 2))
3266 : : /* Only use it for vector modes or for vector booleans
3267 : : represented as scalar bitmasks. See PR95528. */
3268 : 0 : && (VECTOR_MODE_P (TYPE_MODE (dblvectype))
3269 : 0 : || VECTOR_BOOLEAN_TYPE_P (dblvectype))
3270 : 0 : && (optab = optab_for_tree_code (unpack_op,
3271 : : dblvectype,
3272 : : optab_default))
3273 : 0 : && ((icode = optab_handler (optab, TYPE_MODE (dblvectype)))
3274 : : != CODE_FOR_nothing)
3275 : 3 : && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
3276 : : {
3277 : 0 : gimple_seq stmts = NULL;
3278 : 0 : tree dbl;
3279 : 0 : if (refnelts == nelts)
3280 : : {
3281 : : /* ??? Paradoxical subregs don't exist, so insert into
3282 : : the lower half of a wider zero vector. */
3283 : 0 : dbl = gimple_build (&stmts, BIT_INSERT_EXPR, dblvectype,
3284 : : build_zero_cst (dblvectype), orig[0],
3285 : 0 : bitsize_zero_node);
3286 : : }
3287 : 0 : else if (refnelts == 2 * nelts)
3288 : : dbl = orig[0];
3289 : : else
3290 : 0 : dbl = gimple_build (&stmts, BIT_FIELD_REF, dblvectype,
3291 : 0 : orig[0], TYPE_SIZE (dblvectype),
3292 : 0 : bitsize_zero_node);
3293 : 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3294 : 0 : gimple_assign_set_rhs_with_ops (gsi, unpack_op, dbl);
3295 : : }
3296 : 2 : else if (CONVERT_EXPR_CODE_P (conv_code)
3297 : 1 : && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
3298 : 1 : == 2 * TYPE_PRECISION (TREE_TYPE (type)))
3299 : 1 : && mode_for_vector (as_a <scalar_mode>
3300 : 1 : (TYPE_MODE
3301 : : (TREE_TYPE (TREE_TYPE (orig[0])))),
3302 : 2 : nelts / 2).exists ()
3303 : 1 : && (halfvectype
3304 : 1 : = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
3305 : 1 : nelts / 2))
3306 : : /* Only use it for vector modes or for vector booleans
3307 : : represented as scalar bitmasks. See PR95528. */
3308 : 1 : && (VECTOR_MODE_P (TYPE_MODE (halfvectype))
3309 : 0 : || VECTOR_BOOLEAN_TYPE_P (halfvectype))
3310 : 1 : && (optab = optab_for_tree_code (VEC_PACK_TRUNC_EXPR,
3311 : : halfvectype,
3312 : : optab_default))
3313 : 1 : && ((icode = optab_handler (optab, TYPE_MODE (halfvectype)))
3314 : : != CODE_FOR_nothing)
3315 : 4 : && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
3316 : : {
3317 : 0 : gimple_seq stmts = NULL;
3318 : 0 : tree low = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
3319 : 0 : orig[0], TYPE_SIZE (halfvectype),
3320 : 0 : bitsize_zero_node);
3321 : 0 : tree hig = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
3322 : 0 : orig[0], TYPE_SIZE (halfvectype),
3323 : 0 : TYPE_SIZE (halfvectype));
3324 : 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3325 : 0 : gimple_assign_set_rhs_with_ops (gsi, VEC_PACK_TRUNC_EXPR,
3326 : : low, hig);
3327 : : }
3328 : : else
3329 : 3 : return false;
3330 : 0 : update_stmt (gsi_stmt (*gsi));
3331 : 0 : return true;
3332 : : }
3333 : 385 : if (nelts != refnelts)
3334 : : {
3335 : 2 : gassign *lowpart
3336 : 2 : = gimple_build_assign (make_ssa_name (conv_src_type),
3337 : : build3 (BIT_FIELD_REF, conv_src_type,
3338 : 2 : orig[0], TYPE_SIZE (conv_src_type),
3339 : : bitsize_zero_node));
3340 : 2 : gsi_insert_before (gsi, lowpart, GSI_SAME_STMT);
3341 : 2 : orig[0] = gimple_assign_lhs (lowpart);
3342 : : }
3343 : 385 : if (conv_code == ERROR_MARK)
3344 : : {
3345 : 368 : tree src_type = TREE_TYPE (orig[0]);
3346 : 368 : if (!useless_type_conversion_p (type, src_type))
3347 : : {
3348 : 0 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
3349 : : TYPE_VECTOR_SUBPARTS (src_type))
3350 : : && useless_type_conversion_p (TREE_TYPE (type),
3351 : : TREE_TYPE (src_type)));
3352 : 0 : tree rhs = build1 (VIEW_CONVERT_EXPR, type, orig[0]);
3353 : 0 : orig[0] = make_ssa_name (type);
3354 : 0 : gassign *assign = gimple_build_assign (orig[0], rhs);
3355 : 0 : gsi_insert_before (gsi, assign, GSI_SAME_STMT);
3356 : : }
3357 : 368 : gimple_assign_set_rhs_from_tree (gsi, orig[0]);
3358 : : }
3359 : : else
3360 : 17 : gimple_assign_set_rhs_with_ops (gsi, conv_code, orig[0],
3361 : : NULL_TREE, NULL_TREE);
3362 : : }
3363 : : else
3364 : : {
3365 : : /* If we combine a vector with a non-vector avoid cases where
3366 : : we'll obviously end up with more GIMPLE stmts which is when
3367 : : we'll later not fold this to a single insert into the vector
3368 : : and we had a single extract originally. See PR92819. */
3369 : 815 : if (nelts == 2
3370 : 654 : && refnelts > 2
3371 : 47 : && orig[1] == error_mark_node
3372 : 14 : && !maybe_blend[0])
3373 : 413 : return false;
3374 : 803 : tree mask_type, perm_type, conv_src_type;
3375 : 803 : perm_type = TREE_TYPE (orig[0]);
3376 : 803 : conv_src_type = (nelts == refnelts
3377 : 803 : ? perm_type
3378 : 55 : : build_vector_type (TREE_TYPE (perm_type), nelts));
3379 : 803 : if (conv_code != ERROR_MARK
3380 : 803 : && !supportable_convert_operation (conv_code, type, conv_src_type,
3381 : : &conv_code))
3382 : : return false;
3383 : :
3384 : : /* Now that we know the number of elements of the source build the
3385 : : permute vector.
3386 : : ??? When the second vector has constant values we can shuffle
3387 : : it and its source indexes to make the permutation supported.
3388 : : For now it mimics a blend. */
3389 : 518 : vec_perm_builder sel (refnelts, refnelts, 1);
3390 : 518 : bool all_same_p = true;
3391 : 4648 : for (i = 0; i < elts.length (); ++i)
3392 : : {
3393 : 1806 : sel.quick_push (elts[i].second + elts[i].first * refnelts);
3394 : 1806 : all_same_p &= known_eq (sel[i], sel[0]);
3395 : : }
3396 : : /* And fill the tail with "something". It's really don't care,
3397 : : and ideally we'd allow VEC_PERM to have a smaller destination
3398 : : vector. As a heuristic:
3399 : :
3400 : : (a) if what we have so far duplicates a single element, make the
3401 : : tail do the same
3402 : :
3403 : : (b) otherwise preserve a uniform orig[0]. This facilitates
3404 : : later pattern-matching of VEC_PERM_EXPR to a BIT_INSERT_EXPR. */
3405 : 840 : for (; i < refnelts; ++i)
3406 : 644 : sel.quick_push (all_same_p
3407 : 966 : ? sel[0]
3408 : 28 : : (elts[0].second == 0 && elts[0].first == 0
3409 : 244 : ? 0 : refnelts) + i);
3410 : 652 : vec_perm_indices indices (sel, orig[1] ? 2 : 1, refnelts);
3411 : 518 : machine_mode vmode = TYPE_MODE (perm_type);
3412 : 518 : if (!can_vec_perm_const_p (vmode, vmode, indices))
3413 : : return false;
3414 : 402 : mask_type
3415 : 402 : = build_vector_type (build_nonstandard_integer_type (elem_size, 1),
3416 : 402 : refnelts);
3417 : 402 : if (GET_MODE_CLASS (TYPE_MODE (mask_type)) != MODE_VECTOR_INT
3418 : 1206 : || maybe_ne (GET_MODE_SIZE (TYPE_MODE (mask_type)),
3419 : 804 : GET_MODE_SIZE (TYPE_MODE (perm_type))))
3420 : 0 : return false;
3421 : 402 : tree op2 = vec_perm_indices_to_tree (mask_type, indices);
3422 : 402 : bool converted_orig1 = false;
3423 : 402 : gimple_seq stmts = NULL;
3424 : 402 : if (!orig[1])
3425 : 79 : orig[1] = orig[0];
3426 : 323 : else if (orig[1] == error_mark_node
3427 : 234 : && one_nonconstant)
3428 : : {
3429 : : /* ??? We can see if we can safely convert to the original
3430 : : element type. */
3431 : 154 : converted_orig1 = conv_code != ERROR_MARK;
3432 : 307 : orig[1] = gimple_build_vector_from_val (&stmts, UNKNOWN_LOCATION,
3433 : : converted_orig1
3434 : : ? type : perm_type,
3435 : : one_nonconstant);
3436 : : }
3437 : 169 : else if (orig[1] == error_mark_node)
3438 : : {
3439 : : /* ??? See if we can convert the vector to the original type. */
3440 : 80 : converted_orig1 = conv_code != ERROR_MARK;
3441 : 80 : unsigned n = converted_orig1 ? nelts : refnelts;
3442 : 66 : tree_vector_builder vec (converted_orig1
3443 : 80 : ? type : perm_type, n, 1);
3444 : 454 : for (unsigned i = 0; i < n; ++i)
3445 : 744 : if (i < nelts && constants[i])
3446 : 187 : vec.quick_push (constants[i]);
3447 : : else
3448 : : /* ??? Push a don't-care value. */
3449 : 187 : vec.quick_push (one_constant);
3450 : 80 : orig[1] = vec.build ();
3451 : 80 : }
3452 : 313 : tree blend_op2 = NULL_TREE;
3453 : 313 : if (converted_orig1)
3454 : : {
3455 : : /* Make sure we can do a blend in the target type. */
3456 : 15 : vec_perm_builder sel (nelts, nelts, 1);
3457 : 75 : for (i = 0; i < elts.length (); ++i)
3458 : 60 : sel.quick_push (elts[i].first
3459 : 60 : ? elts[i].second + nelts : i);
3460 : 15 : vec_perm_indices indices (sel, 2, nelts);
3461 : 15 : machine_mode vmode = TYPE_MODE (type);
3462 : 15 : if (!can_vec_perm_const_p (vmode, vmode, indices))
3463 : : return false;
3464 : 15 : mask_type
3465 : 15 : = build_vector_type (build_nonstandard_integer_type (elem_size, 1),
3466 : 15 : nelts);
3467 : 15 : if (GET_MODE_CLASS (TYPE_MODE (mask_type)) != MODE_VECTOR_INT
3468 : 45 : || maybe_ne (GET_MODE_SIZE (TYPE_MODE (mask_type)),
3469 : 30 : GET_MODE_SIZE (TYPE_MODE (type))))
3470 : 0 : return false;
3471 : 15 : blend_op2 = vec_perm_indices_to_tree (mask_type, indices);
3472 : 15 : }
3473 : 402 : tree orig1_for_perm
3474 : 402 : = converted_orig1 ? build_zero_cst (perm_type) : orig[1];
3475 : 402 : tree res = gimple_build (&stmts, VEC_PERM_EXPR, perm_type,
3476 : : orig[0], orig1_for_perm, op2);
3477 : 402 : if (nelts != refnelts)
3478 : 40 : res = gimple_build (&stmts, BIT_FIELD_REF,
3479 : 40 : conv_code != ERROR_MARK ? conv_src_type : type,
3480 : 40 : res, TYPE_SIZE (type), bitsize_zero_node);
3481 : 402 : if (conv_code != ERROR_MARK)
3482 : 22 : res = gimple_build (&stmts, conv_code, type, res);
3483 : 380 : else if (!useless_type_conversion_p (type, TREE_TYPE (res)))
3484 : : {
3485 : 0 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
3486 : : TYPE_VECTOR_SUBPARTS (perm_type))
3487 : : && useless_type_conversion_p (TREE_TYPE (type),
3488 : : TREE_TYPE (perm_type)));
3489 : 0 : res = gimple_build (&stmts, VIEW_CONVERT_EXPR, type, res);
3490 : : }
3491 : : /* Blend in the actual constant. */
3492 : 402 : if (converted_orig1)
3493 : 15 : res = gimple_build (&stmts, VEC_PERM_EXPR, type,
3494 : 15 : res, orig[1], blend_op2);
3495 : 402 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3496 : 402 : gimple_assign_set_rhs_with_ops (gsi, SSA_NAME, res);
3497 : 518 : }
3498 : 787 : update_stmt (gsi_stmt (*gsi));
3499 : 787 : return true;
3500 : 161325 : }
3501 : :
3502 : : /* Prepare a TARGET_MEM_REF ref so that it can be subsetted as
3503 : : lvalue. This splits out an address computation stmt before *GSI
3504 : : and returns a MEM_REF wrapping the address. */
3505 : :
3506 : : static tree
3507 : 1192 : prepare_target_mem_ref_lvalue (tree ref, gimple_stmt_iterator *gsi)
3508 : : {
3509 : 1192 : if (TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR)
3510 : 343 : mark_addressable (TREE_OPERAND (TREE_OPERAND (ref, 0), 0));
3511 : 1192 : tree ptrtype = build_pointer_type (TREE_TYPE (ref));
3512 : 1192 : tree tem = make_ssa_name (ptrtype);
3513 : 1192 : gimple *new_stmt
3514 : 1192 : = gimple_build_assign (tem, build1 (ADDR_EXPR, TREE_TYPE (tem),
3515 : : unshare_expr (ref)));
3516 : 1192 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
3517 : 2384 : ref = build2_loc (EXPR_LOCATION (ref),
3518 : 1192 : MEM_REF, TREE_TYPE (ref), tem,
3519 : 1192 : build_int_cst (TREE_TYPE (TREE_OPERAND (ref, 1)), 0));
3520 : 1192 : return ref;
3521 : : }
3522 : :
3523 : : /* Rewrite the vector load at *GSI to component-wise loads if the load
3524 : : is only used in BIT_FIELD_REF extractions with eventual intermediate
3525 : : widening. */
3526 : :
3527 : : static void
3528 : 275197 : optimize_vector_load (gimple_stmt_iterator *gsi)
3529 : : {
3530 : 275197 : gimple *stmt = gsi_stmt (*gsi);
3531 : 275197 : tree lhs = gimple_assign_lhs (stmt);
3532 : 275197 : tree rhs = gimple_assign_rhs1 (stmt);
3533 : 275197 : tree vuse = gimple_vuse (stmt);
3534 : :
3535 : : /* Gather BIT_FIELD_REFs to rewrite, looking through
3536 : : VEC_UNPACK_{LO,HI}_EXPR. */
3537 : 275197 : use_operand_p use_p;
3538 : 275197 : imm_use_iterator iter;
3539 : 275197 : bool rewrite = true;
3540 : 275197 : auto_vec<gimple *, 8> bf_stmts;
3541 : 275197 : auto_vec<tree, 8> worklist;
3542 : 275197 : worklist.quick_push (lhs);
3543 : 276900 : do
3544 : : {
3545 : 276900 : tree def = worklist.pop ();
3546 : 276900 : unsigned HOST_WIDE_INT def_eltsize
3547 : 276900 : = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (def))));
3548 : 350762 : FOR_EACH_IMM_USE_FAST (use_p, iter, def)
3549 : : {
3550 : 330717 : gimple *use_stmt = USE_STMT (use_p);
3551 : 330717 : if (is_gimple_debug (use_stmt))
3552 : 73862 : continue;
3553 : 330561 : if (!is_gimple_assign (use_stmt))
3554 : : {
3555 : : rewrite = false;
3556 : 256855 : break;
3557 : : }
3558 : 297619 : enum tree_code use_code = gimple_assign_rhs_code (use_stmt);
3559 : 297619 : tree use_rhs = gimple_assign_rhs1 (use_stmt);
3560 : 367910 : if (use_code == BIT_FIELD_REF
3561 : 70294 : && TREE_OPERAND (use_rhs, 0) == def
3562 : : /* If its on the VEC_UNPACK_{HI,LO}_EXPR
3563 : : def need to verify it is element aligned. */
3564 : 367913 : && (def == lhs
3565 : 87 : || (known_eq (bit_field_size (use_rhs), def_eltsize)
3566 : 87 : && constant_multiple_p (bit_field_offset (use_rhs),
3567 : : def_eltsize)
3568 : : /* We can simulate the VEC_UNPACK_{HI,LO}_EXPR
3569 : : via a NOP_EXPR only for integral types.
3570 : : ??? Support VEC_UNPACK_FLOAT_{HI,LO}_EXPR. */
3571 : 87 : && INTEGRAL_TYPE_P (TREE_TYPE (use_rhs)))))
3572 : : {
3573 : 70291 : bf_stmts.safe_push (use_stmt);
3574 : 70291 : continue;
3575 : : }
3576 : : /* Walk through one level of VEC_UNPACK_{LO,HI}_EXPR. */
3577 : 227328 : if (def == lhs
3578 : 225672 : && (use_code == VEC_UNPACK_HI_EXPR
3579 : 225672 : || use_code == VEC_UNPACK_LO_EXPR)
3580 : 3415 : && use_rhs == lhs)
3581 : : {
3582 : 3415 : worklist.safe_push (gimple_assign_lhs (use_stmt));
3583 : 3415 : continue;
3584 : : }
3585 : : rewrite = false;
3586 : : break;
3587 : : }
3588 : 256855 : if (!rewrite)
3589 : : break;
3590 : : }
3591 : 40090 : while (!worklist.is_empty ());
3592 : :
3593 : 275197 : if (!rewrite)
3594 : : {
3595 : 256855 : gsi_next (gsi);
3596 : 256855 : return;
3597 : : }
3598 : : /* We now have all ultimate uses of the load to rewrite in bf_stmts. */
3599 : :
3600 : : /* Prepare the original ref to be wrapped in adjusted BIT_FIELD_REFs.
3601 : : For TARGET_MEM_REFs we have to separate the LEA from the reference. */
3602 : 18342 : tree load_rhs = rhs;
3603 : 18342 : if (TREE_CODE (load_rhs) == TARGET_MEM_REF)
3604 : 1191 : load_rhs = prepare_target_mem_ref_lvalue (load_rhs, gsi);
3605 : :
3606 : : /* Rewrite the BIT_FIELD_REFs to be actual loads, re-emitting them at
3607 : : the place of the original load. */
3608 : 120618 : for (gimple *use_stmt : bf_stmts)
3609 : : {
3610 : 65592 : tree bfr = gimple_assign_rhs1 (use_stmt);
3611 : 65592 : tree new_rhs = unshare_expr (load_rhs);
3612 : 65592 : if (TREE_OPERAND (bfr, 0) != lhs)
3613 : : {
3614 : : /* When the BIT_FIELD_REF is on the promoted vector we have to
3615 : : adjust it and emit a conversion afterwards. */
3616 : 84 : gimple *def_stmt
3617 : 84 : = SSA_NAME_DEF_STMT (TREE_OPERAND (bfr, 0));
3618 : 84 : enum tree_code def_code
3619 : 84 : = gimple_assign_rhs_code (def_stmt);
3620 : :
3621 : : /* The adjusted BIT_FIELD_REF is of the promotion source
3622 : : vector size and at half of the offset... */
3623 : 84 : new_rhs = fold_build3 (BIT_FIELD_REF,
3624 : : TREE_TYPE (TREE_TYPE (lhs)),
3625 : : new_rhs,
3626 : : TYPE_SIZE (TREE_TYPE (TREE_TYPE (lhs))),
3627 : : size_binop (EXACT_DIV_EXPR,
3628 : : TREE_OPERAND (bfr, 2),
3629 : : bitsize_int (2)));
3630 : : /* ... and offsetted by half of the vector if VEC_UNPACK_HI_EXPR. */
3631 : 84 : if (def_code == (!BYTES_BIG_ENDIAN
3632 : : ? VEC_UNPACK_HI_EXPR : VEC_UNPACK_LO_EXPR))
3633 : 42 : TREE_OPERAND (new_rhs, 2)
3634 : 84 : = size_binop (PLUS_EXPR, TREE_OPERAND (new_rhs, 2),
3635 : : size_binop (EXACT_DIV_EXPR,
3636 : : TYPE_SIZE (TREE_TYPE (lhs)),
3637 : : bitsize_int (2)));
3638 : 84 : tree tem = make_ssa_name (TREE_TYPE (TREE_TYPE (lhs)));
3639 : 84 : gimple *new_stmt = gimple_build_assign (tem, new_rhs);
3640 : 84 : location_t loc = gimple_location (use_stmt);
3641 : 84 : gimple_set_location (new_stmt, loc);
3642 : 84 : gimple_set_vuse (new_stmt, vuse);
3643 : 84 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
3644 : : /* Perform scalar promotion. */
3645 : 84 : new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
3646 : : NOP_EXPR, tem);
3647 : 84 : gimple_set_location (new_stmt, loc);
3648 : 84 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
3649 : : }
3650 : : else
3651 : : {
3652 : : /* When the BIT_FIELD_REF is on the original load result
3653 : : we can just wrap that. */
3654 : 65508 : tree new_rhs = fold_build3 (BIT_FIELD_REF, TREE_TYPE (bfr),
3655 : : unshare_expr (load_rhs),
3656 : : TREE_OPERAND (bfr, 1),
3657 : : TREE_OPERAND (bfr, 2));
3658 : 65508 : gimple *new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
3659 : : new_rhs);
3660 : 65508 : location_t loc = gimple_location (use_stmt);
3661 : 65508 : gimple_set_location (new_stmt, loc);
3662 : 65508 : gimple_set_vuse (new_stmt, vuse);
3663 : 65508 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
3664 : : }
3665 : 65592 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
3666 : 65592 : unlink_stmt_vdef (use_stmt);
3667 : 65592 : gsi_remove (&gsi2, true);
3668 : : }
3669 : :
3670 : : /* Finally get rid of the intermediate stmts. */
3671 : 18342 : gimple *use_stmt;
3672 : 18442 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
3673 : : {
3674 : 100 : if (is_gimple_debug (use_stmt))
3675 : : {
3676 : 72 : if (gimple_debug_bind_p (use_stmt))
3677 : : {
3678 : 72 : gimple_debug_bind_reset_value (use_stmt);
3679 : 72 : update_stmt (use_stmt);
3680 : : }
3681 : 72 : continue;
3682 : : }
3683 : 28 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
3684 : 28 : unlink_stmt_vdef (use_stmt);
3685 : 28 : release_defs (use_stmt);
3686 : 28 : gsi_remove (&gsi2, true);
3687 : 18342 : }
3688 : : /* And the original load. */
3689 : 18342 : release_defs (stmt);
3690 : 18342 : gsi_remove (gsi, true);
3691 : 275197 : }
3692 : :
3693 : :
3694 : : /* Primitive "lattice" function for gimple_simplify. */
3695 : :
3696 : : static tree
3697 : 1470953611 : fwprop_ssa_val (tree name)
3698 : : {
3699 : : /* First valueize NAME. */
3700 : 1470953611 : if (TREE_CODE (name) == SSA_NAME
3701 : 1470953611 : && SSA_NAME_VERSION (name) < lattice.length ())
3702 : : {
3703 : 1470294458 : tree val = lattice[SSA_NAME_VERSION (name)];
3704 : 1470294458 : if (val)
3705 : 1470953611 : name = val;
3706 : : }
3707 : : /* We continue matching along SSA use-def edges for SSA names
3708 : : that are not single-use. Currently there are no patterns
3709 : : that would cause any issues with that. */
3710 : 1470953611 : return name;
3711 : : }
3712 : :
3713 : : /* Search for opportunities to free half of the lanes in the following pattern:
3714 : :
3715 : : v_in = {e0, e1, e2, e3}
3716 : : v_1 = VEC_PERM <v_in, v_in, {0, 2, 0, 2}>
3717 : : // v_1 = {e0, e2, e0, e2}
3718 : : v_2 = VEC_PERM <v_in, v_in, {1, 3, 1, 3}>
3719 : : // v_2 = {e1, e3, e1, e3}
3720 : :
3721 : : v_x = v_1 + v_2
3722 : : // v_x = {e0+e1, e2+e3, e0+e1, e2+e3}
3723 : : v_y = v_1 - v_2
3724 : : // v_y = {e0-e1, e2-e3, e0-e1, e2-e3}
3725 : :
3726 : : v_out = VEC_PERM <v_x, v_y, {0, 1, 6, 7}>
3727 : : // v_out = {e0+e1, e2+e3, e0-e1, e2-e3}
3728 : :
3729 : : The last statement could be simplified to:
3730 : : v_out' = VEC_PERM <v_x, v_y, {0, 1, 4, 5}>
3731 : : // v_out' = {e0+e1, e2+e3, e0-e1, e2-e3}
3732 : :
3733 : : Characteristic properties:
3734 : : - v_1 and v_2 are created from the same input vector v_in and introduce the
3735 : : lane duplication (in the selection operand) that we can eliminate.
3736 : : - v_x and v_y are results from lane-preserving operations that use v_1 and
3737 : : v_2 as inputs.
3738 : : - v_out is created by selecting from duplicated lanes. */
3739 : :
3740 : : static bool
3741 : 173679 : recognise_vec_perm_simplify_seq (gassign *stmt, vec_perm_simplify_seq *seq)
3742 : : {
3743 : 173679 : unsigned HOST_WIDE_INT nelts;
3744 : :
3745 : 173679 : gcc_checking_assert (stmt);
3746 : 173679 : gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
3747 : 173679 : basic_block bb = gimple_bb (stmt);
3748 : :
3749 : : /* Decompose the final vec permute statement. */
3750 : 173679 : tree v_x = gimple_assign_rhs1 (stmt);
3751 : 173679 : tree v_y = gimple_assign_rhs2 (stmt);
3752 : 173679 : tree sel = gimple_assign_rhs3 (stmt);
3753 : :
3754 : 173679 : if (TREE_CODE (sel) != VECTOR_CST
3755 : 170961 : || !VECTOR_CST_NELTS (sel).is_constant (&nelts)
3756 : 170961 : || TREE_CODE (v_x) != SSA_NAME
3757 : 169133 : || TREE_CODE (v_y) != SSA_NAME
3758 : 166772 : || !has_single_use (v_x)
3759 : 280605 : || !has_single_use (v_y))
3760 : 68376 : return false;
3761 : :
3762 : : /* Don't analyse sequences with many lanes. */
3763 : 105303 : if (nelts > 4)
3764 : : return false;
3765 : :
3766 : : /* Lookup the definition of v_x and v_y. */
3767 : 103347 : gassign *v_x_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x));
3768 : 103347 : gassign *v_y_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_y));
3769 : 102979 : if (!v_x_stmt || gimple_bb (v_x_stmt) != bb
3770 : 206326 : || !v_y_stmt || gimple_bb (v_y_stmt) != bb)
3771 : : return false;
3772 : :
3773 : : /* Check the operations that define v_x and v_y. */
3774 : 102978 : if (TREE_CODE_CLASS (gimple_assign_rhs_code (v_x_stmt)) != tcc_binary
3775 : 104724 : || TREE_CODE_CLASS (gimple_assign_rhs_code (v_y_stmt)) != tcc_binary)
3776 : : return false;
3777 : :
3778 : 1746 : tree v_x_1 = gimple_assign_rhs1 (v_x_stmt);
3779 : 1746 : tree v_x_2 = gimple_assign_rhs2 (v_x_stmt);
3780 : 1746 : tree v_y_1 = gimple_assign_rhs1 (v_y_stmt);
3781 : 1746 : tree v_y_2 = gimple_assign_rhs2 (v_y_stmt);
3782 : :
3783 : 1746 : if (v_x_stmt == v_y_stmt
3784 : 1746 : || TREE_CODE (v_x_1) != SSA_NAME
3785 : 1743 : || TREE_CODE (v_x_2) != SSA_NAME
3786 : 1741 : || num_imm_uses (v_x_1) != 2
3787 : 3413 : || num_imm_uses (v_x_2) != 2)
3788 : : return false;
3789 : :
3790 : 1570 : if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
3791 : : {
3792 : : /* Allow operands of commutative operators to swap. */
3793 : 532 : if (commutative_tree_code (gimple_assign_rhs_code (v_x_stmt)))
3794 : : {
3795 : : /* Keep v_x_1 the first operand for non-commutative operators. */
3796 : 239 : v_x_1 = gimple_assign_rhs2 (v_x_stmt);
3797 : 239 : v_x_2 = gimple_assign_rhs1 (v_x_stmt);
3798 : 239 : if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
3799 : : return false;
3800 : : }
3801 : 293 : else if (commutative_tree_code (gimple_assign_rhs_code (v_y_stmt)))
3802 : : {
3803 : 293 : if (v_x_1 != v_y_2 || v_x_2 != v_y_1)
3804 : : return false;
3805 : : }
3806 : : else
3807 : : return false;
3808 : : }
3809 : 1570 : gassign *v_1_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_1));
3810 : 1570 : gassign *v_2_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_2));
3811 : 1506 : if (!v_1_stmt || gimple_bb (v_1_stmt) != bb
3812 : 3076 : || !v_2_stmt || gimple_bb (v_2_stmt) != bb)
3813 : : return false;
3814 : :
3815 : 1502 : if (gimple_assign_rhs_code (v_1_stmt) != VEC_PERM_EXPR
3816 : 1612 : || gimple_assign_rhs_code (v_2_stmt) != VEC_PERM_EXPR)
3817 : : return false;
3818 : :
3819 : : /* Decompose initial VEC_PERM_EXPRs. */
3820 : 106 : tree v_in = gimple_assign_rhs1 (v_1_stmt);
3821 : 106 : tree v_1_sel = gimple_assign_rhs3 (v_1_stmt);
3822 : 106 : tree v_2_sel = gimple_assign_rhs3 (v_2_stmt);
3823 : 106 : if (v_in != gimple_assign_rhs2 (v_1_stmt)
3824 : 101 : || v_in != gimple_assign_rhs1 (v_2_stmt)
3825 : 205 : || v_in != gimple_assign_rhs2 (v_2_stmt))
3826 : : return false;
3827 : :
3828 : 99 : unsigned HOST_WIDE_INT v_1_nelts, v_2_nelts;
3829 : 99 : if (TREE_CODE (v_1_sel) != VECTOR_CST
3830 : 99 : || !VECTOR_CST_NELTS (v_1_sel).is_constant (&v_1_nelts)
3831 : 99 : || TREE_CODE (v_2_sel) != VECTOR_CST
3832 : 198 : || !VECTOR_CST_NELTS (v_2_sel).is_constant (&v_2_nelts))
3833 : 0 : return false;
3834 : :
3835 : 99 : if (nelts != v_1_nelts || nelts != v_2_nelts)
3836 : : return false;
3837 : :
3838 : : /* Create the new selector. */
3839 : 99 : vec_perm_builder new_sel_perm (nelts, nelts, 1);
3840 : 99 : auto_vec<unsigned int> lanes (nelts);
3841 : 99 : lanes.quick_grow_cleared (nelts);
3842 : 495 : for (unsigned int i = 0; i < nelts; i++)
3843 : : {
3844 : : /* Extract the i-th value from the selector. */
3845 : 396 : unsigned int sel_cst = TREE_INT_CST_LOW (VECTOR_CST_ELT (sel, i));
3846 : 396 : unsigned int lane = sel_cst % nelts;
3847 : 396 : unsigned int offs = sel_cst / nelts;
3848 : :
3849 : : /* Check what's in the lane. */
3850 : 396 : unsigned int e_1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, lane));
3851 : 396 : unsigned int e_2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, lane));
3852 : :
3853 : : /* Reuse previous lane (if any). */
3854 : 396 : unsigned int l = 0;
3855 : 675 : for (; l < lane; l++)
3856 : : {
3857 : 477 : if ((TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, l)) == e_1)
3858 : 477 : && (TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, l)) == e_2))
3859 : : break;
3860 : : }
3861 : :
3862 : : /* Add to narrowed selector. */
3863 : 396 : new_sel_perm.quick_push (l + offs * nelts);
3864 : :
3865 : : /* Mark lane as used. */
3866 : 396 : lanes[l] = 1;
3867 : : }
3868 : :
3869 : : /* Count how many lanes are need. */
3870 : : unsigned int cnt = 0;
3871 : 495 : for (unsigned int i = 0; i < nelts; i++)
3872 : 396 : cnt += lanes[i];
3873 : :
3874 : : /* If more than (nelts/2) lanes are needed, skip the sequence. */
3875 : 99 : if (cnt > nelts / 2)
3876 : : return false;
3877 : :
3878 : : /* Check if the resulting permuation is cheap. */
3879 : 99 : vec_perm_indices new_indices (new_sel_perm, 2, nelts);
3880 : 99 : tree vectype = TREE_TYPE (gimple_assign_lhs (stmt));
3881 : 99 : machine_mode vmode = TYPE_MODE (vectype);
3882 : 99 : if (!can_vec_perm_const_p (vmode, vmode, new_indices, false))
3883 : : return false;
3884 : :
3885 : 99 : *seq = XNEW (struct _vec_perm_simplify_seq);
3886 : 99 : (*seq)->stmt = stmt;
3887 : 99 : (*seq)->v_1_stmt = v_1_stmt;
3888 : 99 : (*seq)->v_2_stmt = v_2_stmt;
3889 : 99 : (*seq)->v_x_stmt = v_x_stmt;
3890 : 99 : (*seq)->v_y_stmt = v_y_stmt;
3891 : 99 : (*seq)->nelts = nelts;
3892 : 99 : (*seq)->new_sel = vect_gen_perm_mask_checked (vectype, new_indices);
3893 : :
3894 : 99 : if (dump_file)
3895 : : {
3896 : 26 : fprintf (dump_file, "Found vec perm simplify sequence ending with:\n\t");
3897 : 26 : print_gimple_stmt (dump_file, stmt, 0);
3898 : :
3899 : 26 : if (dump_flags & TDF_DETAILS)
3900 : : {
3901 : 26 : fprintf (dump_file, "\tNarrowed vec_perm selector: ");
3902 : 26 : print_generic_expr (dump_file, (*seq)->new_sel);
3903 : 26 : fprintf (dump_file, "\n");
3904 : : }
3905 : : }
3906 : :
3907 : : return true;
3908 : 198 : }
3909 : :
3910 : : /* Reduce the lane consumption of a simplifiable vec perm sequence. */
3911 : :
3912 : : static void
3913 : 72 : narrow_vec_perm_simplify_seq (const vec_perm_simplify_seq &seq)
3914 : : {
3915 : 72 : gassign *stmt = seq->stmt;
3916 : 72 : if (dump_file && (dump_flags & TDF_DETAILS))
3917 : : {
3918 : 20 : fprintf (dump_file, "Updating VEC_PERM statment:\n");
3919 : 20 : fprintf (dump_file, "Old stmt: ");
3920 : 20 : print_gimple_stmt (dump_file, stmt, 0);
3921 : : }
3922 : :
3923 : : /* Update the last VEC_PERM statement. */
3924 : 72 : gimple_assign_set_rhs3 (stmt, seq->new_sel);
3925 : 72 : update_stmt (stmt);
3926 : :
3927 : 72 : if (dump_file && (dump_flags & TDF_DETAILS))
3928 : : {
3929 : 20 : fprintf (dump_file, "New stmt: ");
3930 : 20 : print_gimple_stmt (dump_file, stmt, 0);
3931 : : }
3932 : 72 : }
3933 : :
3934 : : /* Test if we can blend two simplifiable vec permute sequences.
3935 : : NEED_SWAP will be set, if sequences must be swapped for blending. */
3936 : :
3937 : : static bool
3938 : 46 : can_blend_vec_perm_simplify_seqs_p (vec_perm_simplify_seq seq1,
3939 : : vec_perm_simplify_seq seq2,
3940 : : bool *need_swap)
3941 : : {
3942 : 46 : unsigned int nelts = seq1->nelts;
3943 : 46 : basic_block bb = gimple_bb (seq1->stmt);
3944 : :
3945 : 46 : gcc_assert (gimple_bb (seq2->stmt) == bb);
3946 : :
3947 : : /* BBs and number of elements must be equal. */
3948 : 46 : if (gimple_bb (seq2->stmt) != bb || seq2->nelts != nelts)
3949 : : return false;
3950 : :
3951 : : /* We need vectors of the same type. */
3952 : 46 : if (TREE_TYPE (gimple_assign_lhs (seq1->stmt))
3953 : 46 : != TREE_TYPE (gimple_assign_lhs (seq2->stmt)))
3954 : : return false;
3955 : :
3956 : : /* We require isomorphic operators. */
3957 : 40 : if (((gimple_assign_rhs_code (seq1->v_x_stmt)
3958 : 40 : != gimple_assign_rhs_code (seq2->v_x_stmt))
3959 : 40 : || (gimple_assign_rhs_code (seq1->v_y_stmt)
3960 : 40 : != gimple_assign_rhs_code (seq2->v_y_stmt))))
3961 : : return false;
3962 : :
3963 : : /* We cannot have any dependencies between the sequences.
3964 : :
3965 : : For merging, we will reuse seq1->v_1_stmt and seq1->v_2_stmt.
3966 : : seq1's v_in is defined before these statements, but we need
3967 : : to check if seq2's v_in is defined before them as well.
3968 : :
3969 : : Further, we will reuse seq2->stmt. We need to ensure that
3970 : : seq1->v_x_stmt and seq1->v_y_stmt are before it.
3971 : :
3972 : : Note, that we don't need to check the BBs here, because all
3973 : : statements of both sequences have to be in the same BB.
3974 : : */
3975 : :
3976 : 40 : tree seq2_v_in = gimple_assign_rhs1 (seq2->v_1_stmt);
3977 : 40 : if (TREE_CODE (seq2_v_in) != SSA_NAME)
3978 : : return false;
3979 : :
3980 : 40 : gassign *seq2_v_in_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq2_v_in));
3981 : 40 : if (!seq2_v_in_stmt || gimple_bb (seq2_v_in_stmt) != bb
3982 : 40 : || (gimple_uid (seq2_v_in_stmt) > gimple_uid (seq1->v_1_stmt))
3983 : 36 : || (gimple_uid (seq1->v_x_stmt) > gimple_uid (seq2->stmt))
3984 : 36 : || (gimple_uid (seq1->v_y_stmt) > gimple_uid (seq2->stmt)))
3985 : : {
3986 : 4 : tree seq1_v_in = gimple_assign_rhs1 (seq1->v_1_stmt);
3987 : 4 : if (TREE_CODE (seq1_v_in) != SSA_NAME)
3988 : : return false;
3989 : :
3990 : 4 : gassign *seq1_v_in_stmt
3991 : 4 : = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq1_v_in));
3992 : : /* Let's try to see if we succeed when swapping the sequences. */
3993 : 4 : if (!seq1_v_in_stmt || gimple_bb (seq1_v_in_stmt)
3994 : 0 : || (gimple_uid (seq1_v_in_stmt) > gimple_uid (seq2->v_1_stmt))
3995 : 0 : || (gimple_uid (seq2->v_x_stmt) > gimple_uid (seq1->stmt))
3996 : 0 : || (gimple_uid (seq2->v_y_stmt) > gimple_uid (seq1->stmt)))
3997 : : return false;
3998 : 0 : *need_swap = true;
3999 : : }
4000 : : else
4001 : 36 : *need_swap = false;
4002 : :
4003 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4004 : 10 : fprintf (dump_file, "Found vec perm simplify sequence pair.\n");
4005 : :
4006 : : return true;
4007 : : }
4008 : :
4009 : : /* Calculate the permutations for blending the two given vec permute
4010 : : sequences. This may fail if the resulting permutation is not
4011 : : supported. */
4012 : :
4013 : : static bool
4014 : 36 : calc_perm_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
4015 : : vec_perm_simplify_seq seq2,
4016 : : vec_perm_indices *seq2_stmt_indices,
4017 : : vec_perm_indices *seq1_v_1_stmt_indices,
4018 : : vec_perm_indices *seq1_v_2_stmt_indices)
4019 : : {
4020 : 36 : unsigned int i;
4021 : 36 : unsigned int nelts = seq1->nelts;
4022 : 36 : auto_vec<int> lane_assignment;
4023 : 36 : lane_assignment.create (nelts);
4024 : :
4025 : : /* Mark all lanes as free. */
4026 : 36 : lane_assignment.quick_grow_cleared (nelts);
4027 : :
4028 : : /* Allocate lanes for seq1. */
4029 : 180 : for (i = 0; i < nelts; i++)
4030 : : {
4031 : 144 : unsigned int l = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq1->new_sel, i));
4032 : 144 : l %= nelts;
4033 : 144 : lane_assignment[l] = 1;
4034 : : }
4035 : :
4036 : : /* Allocate lanes for seq2 and calculate selector for seq2->stmt. */
4037 : 36 : vec_perm_builder seq2_stmt_sel_perm (nelts, nelts, 1);
4038 : 180 : for (i = 0; i < nelts; i++)
4039 : : {
4040 : 144 : unsigned int sel = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, i));
4041 : 144 : unsigned int lane = sel % nelts;
4042 : 144 : unsigned int offs = sel / nelts;
4043 : 144 : unsigned int new_sel;
4044 : :
4045 : : /* Check if we already allocated the lane for seq2. */
4046 : 144 : unsigned int j = 0;
4047 : 255 : for (; j < i; j++)
4048 : : {
4049 : 183 : unsigned int sel_old;
4050 : 183 : sel_old = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, j));
4051 : 183 : unsigned int lane_old = sel_old % nelts;
4052 : 183 : if (lane == lane_old)
4053 : : {
4054 : 72 : new_sel = seq2_stmt_sel_perm[j].to_constant ();
4055 : 72 : new_sel = (new_sel % nelts) + offs * nelts;
4056 : 72 : break;
4057 : : }
4058 : : }
4059 : :
4060 : : /* If the lane is not allocated, we need to do that now. */
4061 : 144 : if (j == i)
4062 : : {
4063 : : unsigned int l_orig = lane;
4064 : 176 : while (lane_assignment[lane] != 0)
4065 : : {
4066 : 104 : lane = (lane + 1) % nelts;
4067 : :
4068 : : /* This should not happen if both sequences utilize no more than
4069 : : half of the lanes. Test anyway to guarantee termination. */
4070 : 104 : if (lane == l_orig)
4071 : 0 : return false;
4072 : : }
4073 : :
4074 : : /* Allocate lane. */
4075 : 72 : lane_assignment[lane] = 2;
4076 : 72 : new_sel = lane + offs * nelts;
4077 : : }
4078 : :
4079 : 144 : seq2_stmt_sel_perm.quick_push (new_sel);
4080 : : }
4081 : :
4082 : : /* Check if the resulting permuation is cheap. */
4083 : 36 : seq2_stmt_indices->new_vector (seq2_stmt_sel_perm, 2, nelts);
4084 : 36 : tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
4085 : 36 : machine_mode vmode = TYPE_MODE (vectype);
4086 : 36 : if (!can_vec_perm_const_p (vmode, vmode, *seq2_stmt_indices, false))
4087 : : return false;
4088 : :
4089 : : /* Calculate selectors for seq1->v_1_stmt and seq1->v_2_stmt. */
4090 : 36 : vec_perm_builder seq1_v_1_stmt_sel_perm (nelts, nelts, 1);
4091 : 36 : vec_perm_builder seq1_v_2_stmt_sel_perm (nelts, nelts, 1);
4092 : 180 : for (i = 0; i < nelts; i++)
4093 : : {
4094 : 144 : bool use_seq1 = lane_assignment[i] != 2;
4095 : 144 : unsigned int l1, l2;
4096 : :
4097 : 144 : if (use_seq1)
4098 : : {
4099 : : /* Just reuse the selector indices. */
4100 : 72 : tree s1 = gimple_assign_rhs3 (seq1->v_1_stmt);
4101 : 72 : tree s2 = gimple_assign_rhs3 (seq1->v_2_stmt);
4102 : 72 : l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, i));
4103 : 72 : l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, i));
4104 : : }
4105 : : else
4106 : : {
4107 : : /* We moved the lanes for seq2, so we need to adjust for that. */
4108 : 72 : tree s1 = gimple_assign_rhs3 (seq2->v_1_stmt);
4109 : 72 : tree s2 = gimple_assign_rhs3 (seq2->v_2_stmt);
4110 : :
4111 : 72 : unsigned int j = 0;
4112 : 128 : for (; j < i; j++)
4113 : : {
4114 : 128 : unsigned int sel_new;
4115 : 128 : sel_new = seq2_stmt_sel_perm[j].to_constant ();
4116 : 128 : sel_new %= nelts;
4117 : 128 : if (sel_new == i)
4118 : : break;
4119 : : }
4120 : :
4121 : : /* This should not happen. Test anyway to guarantee correctness. */
4122 : 72 : if (j == i)
4123 : : return false;
4124 : :
4125 : 72 : l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, j));
4126 : 72 : l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, j));
4127 : : }
4128 : :
4129 : 216 : seq1_v_1_stmt_sel_perm.quick_push (l1 + (use_seq1 ? 0 : nelts));
4130 : 216 : seq1_v_2_stmt_sel_perm.quick_push (l2 + (use_seq1 ? 0 : nelts));
4131 : : }
4132 : :
4133 : 36 : seq1_v_1_stmt_indices->new_vector (seq1_v_1_stmt_sel_perm, 2, nelts);
4134 : 36 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
4135 : 36 : vmode = TYPE_MODE (vectype);
4136 : 36 : if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_1_stmt_indices, false))
4137 : : return false;
4138 : :
4139 : 36 : seq1_v_2_stmt_indices->new_vector (seq1_v_2_stmt_sel_perm, 2, nelts);
4140 : 36 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
4141 : 36 : vmode = TYPE_MODE (vectype);
4142 : 36 : if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_2_stmt_indices, false))
4143 : : return false;
4144 : :
4145 : : return true;
4146 : 72 : }
4147 : :
4148 : : /* Blend the two given simplifiable vec permute sequences using the
4149 : : given permutations. */
4150 : :
4151 : : static void
4152 : 36 : blend_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
4153 : : vec_perm_simplify_seq seq2,
4154 : : const vec_perm_indices &seq2_stmt_indices,
4155 : : const vec_perm_indices &seq1_v_1_stmt_indices,
4156 : : const vec_perm_indices &seq1_v_2_stmt_indices)
4157 : : {
4158 : : /* We don't need to adjust seq1->stmt because its lanes consumption
4159 : : was already narrowed before entering this function. */
4160 : :
4161 : : /* Adjust seq2->stmt: copy RHS1/RHS2 from seq1->stmt and set new sel. */
4162 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4163 : : {
4164 : 10 : fprintf (dump_file, "Updating VEC_PERM statment:\n");
4165 : 10 : fprintf (dump_file, "Old stmt: ");
4166 : 10 : print_gimple_stmt (dump_file, seq2->stmt, 0);
4167 : : }
4168 : :
4169 : 36 : gimple_assign_set_rhs1 (seq2->stmt, gimple_assign_rhs1 (seq1->stmt));
4170 : 72 : gimple_assign_set_rhs2 (seq2->stmt, gimple_assign_rhs2 (seq1->stmt));
4171 : 36 : tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
4172 : 36 : tree sel = vect_gen_perm_mask_checked (vectype, seq2_stmt_indices);
4173 : 36 : gimple_assign_set_rhs3 (seq2->stmt, sel);
4174 : 36 : update_stmt (seq2->stmt);
4175 : :
4176 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4177 : : {
4178 : 10 : fprintf (dump_file, "New stmt: ");
4179 : 10 : print_gimple_stmt (dump_file, seq2->stmt, 0);
4180 : : }
4181 : :
4182 : : /* Adjust seq1->v_1_stmt: copy RHS2 from seq2->v_1_stmt and set new sel. */
4183 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4184 : : {
4185 : 10 : fprintf (dump_file, "Updating VEC_PERM statment:\n");
4186 : 10 : fprintf (dump_file, "Old stmt: ");
4187 : 10 : print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
4188 : : }
4189 : :
4190 : 36 : gimple_assign_set_rhs2 (seq1->v_1_stmt, gimple_assign_rhs1 (seq2->v_1_stmt));
4191 : 36 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
4192 : 36 : sel = vect_gen_perm_mask_checked (vectype, seq1_v_1_stmt_indices);
4193 : 36 : gimple_assign_set_rhs3 (seq1->v_1_stmt, sel);
4194 : 36 : update_stmt (seq1->v_1_stmt);
4195 : :
4196 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4197 : : {
4198 : 10 : fprintf (dump_file, "New stmt: ");
4199 : 10 : print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
4200 : : }
4201 : :
4202 : : /* Adjust seq1->v_2_stmt: copy RHS2 from seq2->v_2_stmt and set new sel. */
4203 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4204 : : {
4205 : 10 : fprintf (dump_file, "Updating VEC_PERM statment:\n");
4206 : 10 : fprintf (dump_file, "Old stmt: ");
4207 : 10 : print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
4208 : : }
4209 : :
4210 : 36 : gimple_assign_set_rhs2 (seq1->v_2_stmt, gimple_assign_rhs1 (seq2->v_2_stmt));
4211 : 36 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
4212 : 36 : sel = vect_gen_perm_mask_checked (vectype, seq1_v_2_stmt_indices);
4213 : 36 : gimple_assign_set_rhs3 (seq1->v_2_stmt, sel);
4214 : 36 : update_stmt (seq1->v_2_stmt);
4215 : :
4216 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4217 : : {
4218 : 10 : fprintf (dump_file, "New stmt: ");
4219 : 10 : print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
4220 : : }
4221 : :
4222 : : /* At this point, we have four unmodified seq2 stmts, which will be
4223 : : eliminated by DCE. */
4224 : :
4225 : 36 : if (dump_file)
4226 : 10 : fprintf (dump_file, "Vec perm simplify sequences have been blended.\n\n");
4227 : 36 : }
4228 : :
4229 : : /* Try to blend narrowed vec_perm_simplify_seqs pairwise.
4230 : : The provided list will be empty after this call. */
4231 : :
4232 : : static void
4233 : 309058035 : process_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l)
4234 : : {
4235 : 309058035 : unsigned int i, j;
4236 : 309058035 : vec_perm_simplify_seq seq1, seq2;
4237 : :
4238 : 309058035 : if (l->is_empty ())
4239 : 309057991 : return;
4240 : :
4241 : 44 : if (dump_file && (dump_flags & TDF_DETAILS))
4242 : 12 : fprintf (dump_file, "\nProcessing %u vec perm simplify sequences.\n",
4243 : : l->length ());
4244 : :
4245 : 107 : FOR_EACH_VEC_ELT (*l, i, seq1)
4246 : : {
4247 : 63 : if (i + 1 < l->length ())
4248 : : {
4249 : 50 : FOR_EACH_VEC_ELT_FROM (*l, j, seq2, i + 1)
4250 : : {
4251 : 46 : bool swap = false;
4252 : 46 : if (can_blend_vec_perm_simplify_seqs_p (seq1, seq2, &swap))
4253 : : {
4254 : 36 : vec_perm_indices seq2_stmt_indices;
4255 : 36 : vec_perm_indices seq1_v_1_stmt_indices;
4256 : 36 : vec_perm_indices seq1_v_2_stmt_indices;
4257 : 108 : if (calc_perm_vec_perm_simplify_seqs (swap ? seq2 : seq1,
4258 : : swap ? seq1 : seq2,
4259 : : &seq2_stmt_indices,
4260 : : &seq1_v_1_stmt_indices,
4261 : : &seq1_v_2_stmt_indices))
4262 : : {
4263 : : /* Narrow lane usage. */
4264 : 36 : narrow_vec_perm_simplify_seq (seq1);
4265 : 36 : narrow_vec_perm_simplify_seq (seq2);
4266 : :
4267 : : /* Blend sequences. */
4268 : 36 : blend_vec_perm_simplify_seqs (swap ? seq2 : seq1,
4269 : : swap ? seq1 : seq2,
4270 : : seq2_stmt_indices,
4271 : : seq1_v_1_stmt_indices,
4272 : : seq1_v_2_stmt_indices);
4273 : :
4274 : : /* We can use unordered_remove as we break the loop. */
4275 : 36 : l->unordered_remove (j);
4276 : 36 : XDELETE (seq2);
4277 : 36 : break;
4278 : : }
4279 : 36 : }
4280 : : }
4281 : : }
4282 : :
4283 : : /* We don't need to call l->remove for seq1. */
4284 : 63 : XDELETE (seq1);
4285 : : }
4286 : :
4287 : 44 : l->truncate (0);
4288 : : }
4289 : :
4290 : : static void
4291 : 99 : append_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l,
4292 : : const vec_perm_simplify_seq &seq)
4293 : : {
4294 : : /* If no space on list left, then process the list. */
4295 : 99 : if (!l->space (1))
4296 : 0 : process_vec_perm_simplify_seq_list (l);
4297 : :
4298 : 99 : l->quick_push (seq);
4299 : 99 : }
4300 : :
4301 : : /* Main entry point for the forward propagation and statement combine
4302 : : optimizer. */
4303 : :
4304 : : namespace {
4305 : :
4306 : : const pass_data pass_data_forwprop =
4307 : : {
4308 : : GIMPLE_PASS, /* type */
4309 : : "forwprop", /* name */
4310 : : OPTGROUP_NONE, /* optinfo_flags */
4311 : : TV_TREE_FORWPROP, /* tv_id */
4312 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
4313 : : 0, /* properties_provided */
4314 : : 0, /* properties_destroyed */
4315 : : 0, /* todo_flags_start */
4316 : : 0, /* todo_flags_finish */
4317 : : };
4318 : :
4319 : : class pass_forwprop : public gimple_opt_pass
4320 : : {
4321 : : public:
4322 : 1138692 : pass_forwprop (gcc::context *ctxt)
4323 : 2277384 : : gimple_opt_pass (pass_data_forwprop, ctxt), last_p (false)
4324 : : {}
4325 : :
4326 : : /* opt_pass methods: */
4327 : 854019 : opt_pass * clone () final override { return new pass_forwprop (m_ctxt); }
4328 : 1138692 : void set_pass_param (unsigned int n, bool param) final override
4329 : : {
4330 : 1138692 : gcc_assert (n == 0);
4331 : 1138692 : last_p = param;
4332 : 1138692 : }
4333 : 5520325 : bool gate (function *) final override { return flag_tree_forwprop; }
4334 : : unsigned int execute (function *) final override;
4335 : :
4336 : : private:
4337 : : /* Determines whether the pass instance should set PROP_last_full_fold. */
4338 : : bool last_p;
4339 : : }; // class pass_forwprop
4340 : :
4341 : : unsigned int
4342 : 5517869 : pass_forwprop::execute (function *fun)
4343 : : {
4344 : 5517869 : unsigned int todoflags = 0;
4345 : :
4346 : 5517869 : cfg_changed = false;
4347 : 5517869 : if (last_p)
4348 : 1023681 : fun->curr_properties |= PROP_last_full_fold;
4349 : :
4350 : 5517869 : calculate_dominance_info (CDI_DOMINATORS);
4351 : :
4352 : : /* Combine stmts with the stmts defining their operands. Do that
4353 : : in an order that guarantees visiting SSA defs before SSA uses. */
4354 : 11035738 : lattice.create (num_ssa_names);
4355 : 11035738 : lattice.quick_grow_cleared (num_ssa_names);
4356 : 5517869 : int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (fun));
4357 : 5517869 : int postorder_num = pre_and_rev_post_order_compute_fn (fun, NULL,
4358 : : postorder, false);
4359 : 5517869 : int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
4360 : 50246057 : for (int i = 0; i < postorder_num; ++i)
4361 : : {
4362 : 44728188 : bb_to_rpo[postorder[i]] = i;
4363 : 44728188 : edge_iterator ei;
4364 : 44728188 : edge e;
4365 : 107681761 : FOR_EACH_EDGE (e, ei, BASIC_BLOCK_FOR_FN (fun, postorder[i])->succs)
4366 : 62953573 : e->flags &= ~EDGE_EXECUTABLE;
4367 : : }
4368 : 5517869 : single_succ_edge (BASIC_BLOCK_FOR_FN (fun, ENTRY_BLOCK))->flags
4369 : 5517869 : |= EDGE_EXECUTABLE;
4370 : 5517869 : auto_vec<gimple *, 4> to_fixup;
4371 : 5517869 : auto_vec<gimple *, 32> to_remove;
4372 : 5517869 : auto_vec<unsigned, 32> to_remove_defs;
4373 : 5517869 : auto_vec<std::pair<int, int>, 10> edges_to_remove;
4374 : 5517869 : auto_bitmap simple_dce_worklist;
4375 : 5517869 : auto_bitmap need_ab_cleanup;
4376 : 5517869 : to_purge = BITMAP_ALLOC (NULL);
4377 : 5517869 : auto_vec<vec_perm_simplify_seq, 8> vec_perm_simplify_seq_list;
4378 : 50246057 : for (int i = 0; i < postorder_num; ++i)
4379 : : {
4380 : 44728188 : gimple_stmt_iterator gsi;
4381 : 44728188 : basic_block bb = BASIC_BLOCK_FOR_FN (fun, postorder[i]);
4382 : 44728188 : edge_iterator ei;
4383 : 44728188 : edge e;
4384 : :
4385 : : /* Skip processing not executable blocks. We could improve
4386 : : single_use tracking by at least unlinking uses from unreachable
4387 : : blocks but since blocks with uses are not processed in a
4388 : : meaningful order this is probably not worth it. */
4389 : 44728188 : bool any = false;
4390 : 45845820 : FOR_EACH_EDGE (e, ei, bb->preds)
4391 : : {
4392 : 45833473 : if ((e->flags & EDGE_EXECUTABLE)
4393 : : /* We can handle backedges in natural loops correctly but
4394 : : for irreducible regions we have to take all backedges
4395 : : conservatively when we did not visit the source yet. */
4396 : 45833473 : || (bb_to_rpo[e->src->index] > i
4397 : 656458 : && !dominated_by_p (CDI_DOMINATORS, e->src, e->dest)))
4398 : : {
4399 : : any = true;
4400 : : break;
4401 : : }
4402 : : }
4403 : 44728188 : if (!any)
4404 : 12347 : continue;
4405 : :
4406 : : /* Record degenerate PHIs in the lattice. */
4407 : 61063595 : for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
4408 : 16347754 : gsi_next (&si))
4409 : : {
4410 : 16347754 : gphi *phi = si.phi ();
4411 : 16347754 : tree res = gimple_phi_result (phi);
4412 : 32695508 : if (virtual_operand_p (res))
4413 : 7574608 : continue;
4414 : :
4415 : 8773146 : tree first = NULL_TREE;
4416 : 8773146 : bool all_same = true;
4417 : 8773146 : edge_iterator ei;
4418 : 8773146 : edge e;
4419 : 18038056 : FOR_EACH_EDGE (e, ei, bb->preds)
4420 : : {
4421 : : /* Ignore not executable forward edges. */
4422 : 17788695 : if (!(e->flags & EDGE_EXECUTABLE))
4423 : : {
4424 : 4007722 : if (bb_to_rpo[e->src->index] < i)
4425 : 5231 : continue;
4426 : : /* Avoid equivalences from backedges - while we might
4427 : : be able to make irreducible regions reducible and
4428 : : thus turning a back into a forward edge we do not
4429 : : want to deal with the intermediate SSA issues that
4430 : : exposes. */
4431 : : all_same = false;
4432 : : }
4433 : 17783464 : tree use = PHI_ARG_DEF_FROM_EDGE (phi, e);
4434 : 17783464 : if (use == res)
4435 : : /* The PHI result can also appear on a backedge, if so
4436 : : we can ignore this case for the purpose of determining
4437 : : the singular value. */
4438 : : ;
4439 : 17770057 : else if (! first)
4440 : : first = use;
4441 : 8996911 : else if (! operand_equal_p (first, use, 0))
4442 : : {
4443 : : all_same = false;
4444 : : break;
4445 : : }
4446 : : }
4447 : 8773146 : if (all_same)
4448 : : {
4449 : 244316 : if (may_propagate_copy (res, first))
4450 : 243846 : to_remove_defs.safe_push (SSA_NAME_VERSION (res));
4451 : 244316 : fwprop_set_lattice_val (res, first);
4452 : : }
4453 : : }
4454 : :
4455 : : /* Apply forward propagation to all stmts in the basic-block.
4456 : : Note we update GSI within the loop as necessary. */
4457 : 44715841 : unsigned int uid = 1;
4458 : 419243264 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
4459 : : {
4460 : 329811582 : gimple *stmt = gsi_stmt (gsi);
4461 : 329811582 : tree lhs, rhs;
4462 : 329811582 : enum tree_code code;
4463 : :
4464 : 329811582 : gimple_set_uid (stmt, uid++);
4465 : :
4466 : 329811582 : if (!is_gimple_assign (stmt))
4467 : : {
4468 : 224896079 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
4469 : 224896079 : gsi_next (&gsi);
4470 : 224896079 : continue;
4471 : : }
4472 : :
4473 : 104915503 : lhs = gimple_assign_lhs (stmt);
4474 : 104915503 : rhs = gimple_assign_rhs1 (stmt);
4475 : 104915503 : code = gimple_assign_rhs_code (stmt);
4476 : :
4477 : 144361618 : if (TREE_CODE (lhs) != SSA_NAME
4478 : 104915503 : || has_zero_uses (lhs))
4479 : : {
4480 : 39446115 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
4481 : 39446115 : gsi_next (&gsi);
4482 : 39446115 : continue;
4483 : : }
4484 : :
4485 : : /* If this statement sets an SSA_NAME to an address,
4486 : : try to propagate the address into the uses of the SSA_NAME. */
4487 : 65469388 : if ((code == ADDR_EXPR
4488 : : /* Handle pointer conversions on invariant addresses
4489 : : as well, as this is valid gimple. */
4490 : 63187529 : || (CONVERT_EXPR_CODE_P (code)
4491 : 8698510 : && TREE_CODE (rhs) == ADDR_EXPR
4492 : 356246 : && POINTER_TYPE_P (TREE_TYPE (lhs))))
4493 : 65469612 : && TREE_CODE (TREE_OPERAND (rhs, 0)) != TARGET_MEM_REF)
4494 : : {
4495 : 2281656 : tree base = get_base_address (TREE_OPERAND (rhs, 0));
4496 : 2281656 : if ((!base
4497 : 2281656 : || !DECL_P (base)
4498 : 137564 : || decl_address_invariant_p (base))
4499 : 2281656 : && !stmt_references_abnormal_ssa_name (stmt)
4500 : 4563296 : && forward_propagate_addr_expr (lhs, rhs, true))
4501 : : {
4502 : 444119 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
4503 : 444119 : release_defs (stmt);
4504 : 444119 : gsi_remove (&gsi, true);
4505 : : }
4506 : : else
4507 : 1837537 : gsi_next (&gsi);
4508 : : }
4509 : 63187732 : else if (code == POINTER_PLUS_EXPR)
4510 : : {
4511 : 3580832 : tree off = gimple_assign_rhs2 (stmt);
4512 : 3580832 : if (TREE_CODE (off) == INTEGER_CST
4513 : 1117682 : && can_propagate_from (stmt)
4514 : 1117329 : && !simple_iv_increment_p (stmt)
4515 : : /* ??? Better adjust the interface to that function
4516 : : instead of building new trees here. */
4517 : 4415456 : && forward_propagate_addr_expr
4518 : 2503872 : (lhs,
4519 : : build1_loc (gimple_location (stmt),
4520 : 834624 : ADDR_EXPR, TREE_TYPE (rhs),
4521 : 834624 : fold_build2 (MEM_REF,
4522 : : TREE_TYPE (TREE_TYPE (rhs)),
4523 : : rhs,
4524 : : fold_convert (ptr_type_node,
4525 : : off))), true))
4526 : : {
4527 : 295208 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
4528 : 295208 : release_defs (stmt);
4529 : 295208 : gsi_remove (&gsi, true);
4530 : : }
4531 : 3285624 : else if (is_gimple_min_invariant (rhs))
4532 : : {
4533 : : /* Make sure to fold &a[0] + off_1 here. */
4534 : 433390 : fold_stmt_inplace (&gsi);
4535 : 433390 : update_stmt (stmt);
4536 : 433390 : if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
4537 : 433372 : gsi_next (&gsi);
4538 : : }
4539 : : else
4540 : 2852234 : gsi_next (&gsi);
4541 : : }
4542 : 59606900 : else if (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE
4543 : 210892 : && gimple_assign_load_p (stmt)
4544 : 133939 : && !gimple_has_volatile_ops (stmt)
4545 : 39870 : && TREE_CODE (rhs) != TARGET_MEM_REF
4546 : 39845 : && TREE_CODE (rhs) != BIT_FIELD_REF
4547 : 59646741 : && !stmt_can_throw_internal (fun, stmt))
4548 : : {
4549 : : /* Rewrite loads used only in real/imagpart extractions to
4550 : : component-wise loads. */
4551 : 39716 : use_operand_p use_p;
4552 : 39716 : imm_use_iterator iter;
4553 : 39716 : tree vuse = gimple_vuse (stmt);
4554 : 39716 : bool rewrite = true;
4555 : 44218 : FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
4556 : : {
4557 : 42256 : gimple *use_stmt = USE_STMT (use_p);
4558 : 42256 : if (is_gimple_debug (use_stmt))
4559 : 692 : continue;
4560 : 41564 : if (!is_gimple_assign (use_stmt)
4561 : 27212 : || (gimple_assign_rhs_code (use_stmt) != REALPART_EXPR
4562 : 25292 : && gimple_assign_rhs_code (use_stmt) != IMAGPART_EXPR)
4563 : 45374 : || TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) != lhs)
4564 : : {
4565 : : rewrite = false;
4566 : : break;
4567 : : }
4568 : : }
4569 : 39716 : if (rewrite)
4570 : : {
4571 : 1962 : gimple *use_stmt;
4572 : 6206 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
4573 : : {
4574 : 4244 : if (is_gimple_debug (use_stmt))
4575 : : {
4576 : 453 : if (gimple_debug_bind_p (use_stmt))
4577 : : {
4578 : 453 : gimple_debug_bind_reset_value (use_stmt);
4579 : 453 : update_stmt (use_stmt);
4580 : : }
4581 : 453 : continue;
4582 : : }
4583 : :
4584 : 7582 : tree new_rhs = build1 (gimple_assign_rhs_code (use_stmt),
4585 : 3791 : TREE_TYPE (TREE_TYPE (rhs)),
4586 : : unshare_expr (rhs));
4587 : 3791 : gimple *new_stmt
4588 : 3791 : = gimple_build_assign (gimple_assign_lhs (use_stmt),
4589 : : new_rhs);
4590 : :
4591 : 3791 : location_t loc = gimple_location (use_stmt);
4592 : 3791 : gimple_set_location (new_stmt, loc);
4593 : 3791 : gimple_set_vuse (new_stmt, vuse);
4594 : 3791 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
4595 : 3791 : unlink_stmt_vdef (use_stmt);
4596 : 3791 : gsi_remove (&gsi2, true);
4597 : :
4598 : 3791 : gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
4599 : 1962 : }
4600 : :
4601 : 1962 : release_defs (stmt);
4602 : 1962 : gsi_remove (&gsi, true);
4603 : : }
4604 : : else
4605 : 37754 : gsi_next (&gsi);
4606 : : }
4607 : 59567184 : else if (TREE_CODE (TREE_TYPE (lhs)) == VECTOR_TYPE
4608 : 1563323 : && (TYPE_MODE (TREE_TYPE (lhs)) == BLKmode
4609 : : /* After vector lowering rewrite all loads, but
4610 : : initially do not since this conflicts with
4611 : : vector CONSTRUCTOR to shuffle optimization. */
4612 : 1542606 : || (fun->curr_properties & PROP_gimple_lvec))
4613 : 850237 : && gimple_assign_load_p (stmt)
4614 : 289544 : && !gimple_has_volatile_ops (stmt)
4615 : 275699 : && !stmt_can_throw_internal (fun, stmt)
4616 : 59842883 : && (!VAR_P (rhs) || !DECL_HARD_REGISTER (rhs)))
4617 : 275197 : optimize_vector_load (&gsi);
4618 : :
4619 : 59291987 : else if (code == COMPLEX_EXPR)
4620 : : {
4621 : : /* Rewrite stores of a single-use complex build expression
4622 : : to component-wise stores. */
4623 : 36342 : use_operand_p use_p;
4624 : 36342 : gimple *use_stmt, *def1, *def2;
4625 : 36342 : tree rhs2;
4626 : 36342 : if (single_imm_use (lhs, &use_p, &use_stmt)
4627 : 34210 : && gimple_store_p (use_stmt)
4628 : 40998 : && !gimple_has_volatile_ops (use_stmt)
4629 : 2583 : && is_gimple_assign (use_stmt)
4630 : 2579 : && (TREE_CODE (TREE_TYPE (gimple_assign_lhs (use_stmt)))
4631 : : == COMPLEX_TYPE)
4632 : 38916 : && (TREE_CODE (gimple_assign_lhs (use_stmt))
4633 : : != TARGET_MEM_REF))
4634 : : {
4635 : 2570 : tree use_lhs = gimple_assign_lhs (use_stmt);
4636 : 2570 : if (auto_var_p (use_lhs))
4637 : 600 : DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
4638 : 5140 : tree new_lhs = build1 (REALPART_EXPR,
4639 : 2570 : TREE_TYPE (TREE_TYPE (use_lhs)),
4640 : : unshare_expr (use_lhs));
4641 : 2570 : gimple *new_stmt = gimple_build_assign (new_lhs, rhs);
4642 : 2570 : location_t loc = gimple_location (use_stmt);
4643 : 2570 : gimple_set_location (new_stmt, loc);
4644 : 5140 : gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
4645 : 2570 : gimple_set_vdef (new_stmt, make_ssa_name (gimple_vop (fun)));
4646 : 5140 : SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
4647 : 5140 : gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
4648 : 2570 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
4649 : 2570 : gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
4650 : :
4651 : 5140 : new_lhs = build1 (IMAGPART_EXPR,
4652 : 2570 : TREE_TYPE (TREE_TYPE (use_lhs)),
4653 : : unshare_expr (use_lhs));
4654 : 2570 : gimple_assign_set_lhs (use_stmt, new_lhs);
4655 : 2570 : gimple_assign_set_rhs1 (use_stmt, gimple_assign_rhs2 (stmt));
4656 : 2570 : update_stmt (use_stmt);
4657 : :
4658 : 2570 : release_defs (stmt);
4659 : 2570 : gsi_remove (&gsi, true);
4660 : : }
4661 : : /* Rewrite a component-wise load of a complex to a complex
4662 : : load if the components are not used separately. */
4663 : 33772 : else if (TREE_CODE (rhs) == SSA_NAME
4664 : 33331 : && has_single_use (rhs)
4665 : 29895 : && ((rhs2 = gimple_assign_rhs2 (stmt)), true)
4666 : 29895 : && TREE_CODE (rhs2) == SSA_NAME
4667 : 28206 : && has_single_use (rhs2)
4668 : 27785 : && (def1 = SSA_NAME_DEF_STMT (rhs),
4669 : 27785 : gimple_assign_load_p (def1))
4670 : 1089 : && (def2 = SSA_NAME_DEF_STMT (rhs2),
4671 : 1089 : gimple_assign_load_p (def2))
4672 : 1594 : && (gimple_vuse (def1) == gimple_vuse (def2))
4673 : 794 : && !gimple_has_volatile_ops (def1)
4674 : 794 : && !gimple_has_volatile_ops (def2)
4675 : 794 : && !stmt_can_throw_internal (fun, def1)
4676 : 794 : && !stmt_can_throw_internal (fun, def2)
4677 : 794 : && gimple_assign_rhs_code (def1) == REALPART_EXPR
4678 : 539 : && gimple_assign_rhs_code (def2) == IMAGPART_EXPR
4679 : 34311 : && operand_equal_p (TREE_OPERAND (gimple_assign_rhs1
4680 : : (def1), 0),
4681 : 539 : TREE_OPERAND (gimple_assign_rhs1
4682 : : (def2), 0)))
4683 : : {
4684 : 539 : tree cl = TREE_OPERAND (gimple_assign_rhs1 (def1), 0);
4685 : 539 : gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (cl));
4686 : 539 : gcc_assert (gsi_stmt (gsi) == stmt);
4687 : 1078 : gimple_set_vuse (stmt, gimple_vuse (def1));
4688 : 539 : gimple_set_modified (stmt, true);
4689 : 539 : gimple_stmt_iterator gsi2 = gsi_for_stmt (def1);
4690 : 539 : gsi_remove (&gsi, false);
4691 : 539 : gsi_insert_after (&gsi2, stmt, GSI_SAME_STMT);
4692 : : }
4693 : : else
4694 : 33233 : gsi_next (&gsi);
4695 : : }
4696 : 59255645 : else if (code == CONSTRUCTOR
4697 : 150505 : && VECTOR_TYPE_P (TREE_TYPE (rhs))
4698 : 150505 : && TYPE_MODE (TREE_TYPE (rhs)) == BLKmode
4699 : 2815 : && CONSTRUCTOR_NELTS (rhs) > 0
4700 : 59258460 : && (!VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
4701 : 498 : || (TYPE_MODE (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
4702 : : != BLKmode)))
4703 : : {
4704 : : /* Rewrite stores of a single-use vector constructors
4705 : : to component-wise stores if the mode isn't supported. */
4706 : 2814 : use_operand_p use_p;
4707 : 2814 : gimple *use_stmt;
4708 : 2814 : if (single_imm_use (lhs, &use_p, &use_stmt)
4709 : 2403 : && gimple_store_p (use_stmt)
4710 : 2898 : && !gimple_has_volatile_ops (use_stmt)
4711 : 1443 : && !stmt_can_throw_internal (fun, use_stmt)
4712 : 4247 : && is_gimple_assign (use_stmt))
4713 : : {
4714 : 1433 : tree elt_t = TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value);
4715 : 1433 : unsigned HOST_WIDE_INT elt_w
4716 : 1433 : = tree_to_uhwi (TYPE_SIZE (elt_t));
4717 : 1433 : unsigned HOST_WIDE_INT n
4718 : 1433 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs)));
4719 : 1433 : tree use_lhs = gimple_assign_lhs (use_stmt);
4720 : 1433 : if (auto_var_p (use_lhs))
4721 : 534 : DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
4722 : 899 : else if (TREE_CODE (use_lhs) == TARGET_MEM_REF)
4723 : : {
4724 : 1 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
4725 : 1 : use_lhs = prepare_target_mem_ref_lvalue (use_lhs, &gsi2);
4726 : : }
4727 : 32664 : for (unsigned HOST_WIDE_INT bi = 0; bi < n; bi += elt_w)
4728 : : {
4729 : 31231 : unsigned HOST_WIDE_INT ci = bi / elt_w;
4730 : 31231 : tree new_rhs;
4731 : 31231 : if (ci < CONSTRUCTOR_NELTS (rhs))
4732 : 30613 : new_rhs = CONSTRUCTOR_ELT (rhs, ci)->value;
4733 : : else
4734 : 618 : new_rhs = build_zero_cst (elt_t);
4735 : 31231 : tree new_lhs = build3 (BIT_FIELD_REF,
4736 : : elt_t,
4737 : : unshare_expr (use_lhs),
4738 : 31231 : bitsize_int (elt_w),
4739 : 31231 : bitsize_int (bi));
4740 : 31231 : gimple *new_stmt = gimple_build_assign (new_lhs, new_rhs);
4741 : 31231 : location_t loc = gimple_location (use_stmt);
4742 : 31231 : gimple_set_location (new_stmt, loc);
4743 : 62462 : gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
4744 : 31231 : gimple_set_vdef (new_stmt,
4745 : : make_ssa_name (gimple_vop (fun)));
4746 : 62462 : SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
4747 : 62462 : gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
4748 : 31231 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
4749 : 31231 : gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
4750 : : }
4751 : 1433 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
4752 : 1433 : unlink_stmt_vdef (use_stmt);
4753 : 1433 : release_defs (use_stmt);
4754 : 1433 : gsi_remove (&gsi2, true);
4755 : 1433 : release_defs (stmt);
4756 : 1433 : gsi_remove (&gsi, true);
4757 : : }
4758 : : else
4759 : 1381 : gsi_next (&gsi);
4760 : : }
4761 : 59252831 : else if (code == VEC_PERM_EXPR)
4762 : : {
4763 : : /* Find vectorized sequences, where we can reduce the lane
4764 : : utilization. The narrowing will be donw later and only
4765 : : if we find a pair of sequences that can be blended. */
4766 : 173679 : gassign *assign = dyn_cast <gassign *> (stmt);
4767 : 173679 : vec_perm_simplify_seq seq;
4768 : 173679 : if (recognise_vec_perm_simplify_seq (assign, &seq))
4769 : 99 : append_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list,
4770 : : seq);
4771 : :
4772 : 173679 : gsi_next (&gsi);
4773 : : }
4774 : : else
4775 : 59079152 : gsi_next (&gsi);
4776 : : }
4777 : :
4778 : 44715841 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
4779 : :
4780 : : /* Combine stmts with the stmts defining their operands.
4781 : : Note we update GSI within the loop as necessary. */
4782 : 418858892 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4783 : : {
4784 : 329427210 : gimple *stmt = gsi_stmt (gsi);
4785 : :
4786 : : /* Mark stmt as potentially needing revisiting. */
4787 : 329427210 : gimple_set_plf (stmt, GF_PLF_1, false);
4788 : :
4789 : 329427210 : bool can_make_abnormal_goto = (is_gimple_call (stmt)
4790 : 329427210 : && stmt_can_make_abnormal_goto (stmt));
4791 : :
4792 : : /* Substitute from our lattice. We need to do so only once. */
4793 : 329427210 : bool substituted_p = false;
4794 : 329427210 : use_operand_p usep;
4795 : 329427210 : ssa_op_iter iter;
4796 : 486669971 : FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
4797 : : {
4798 : 157242761 : tree use = USE_FROM_PTR (usep);
4799 : 157242761 : tree val = fwprop_ssa_val (use);
4800 : 157242761 : if (val && val != use)
4801 : : {
4802 : 1855818 : if (!is_gimple_debug (stmt))
4803 : 1521474 : bitmap_set_bit (simple_dce_worklist, SSA_NAME_VERSION (use));
4804 : 1855818 : if (may_propagate_copy (use, val))
4805 : : {
4806 : 1852701 : propagate_value (usep, val);
4807 : 1852701 : substituted_p = true;
4808 : : }
4809 : : }
4810 : : }
4811 : 329427210 : if (substituted_p)
4812 : 1800855 : update_stmt (stmt);
4813 : 1800855 : if (substituted_p
4814 : 1800855 : && is_gimple_assign (stmt)
4815 : 1084263 : && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
4816 : 22363 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
4817 : 329427210 : if (substituted_p
4818 : 329427210 : && can_make_abnormal_goto
4819 : 329427210 : && !stmt_can_make_abnormal_goto (stmt))
4820 : 3 : bitmap_set_bit (need_ab_cleanup, bb->index);
4821 : :
4822 : 332770499 : bool changed;
4823 : 665540998 : do
4824 : : {
4825 : 332770499 : gimple *orig_stmt = stmt = gsi_stmt (gsi);
4826 : 332770499 : bool was_noreturn = (is_gimple_call (stmt)
4827 : 332770499 : && gimple_call_noreturn_p (stmt));
4828 : 332770499 : changed = false;
4829 : :
4830 : 332770499 : auto_vec<tree, 8> uses;
4831 : 493490957 : FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
4832 : 160720458 : if (uses.space (1))
4833 : 160338496 : uses.quick_push (USE_FROM_PTR (usep));
4834 : :
4835 : 332770499 : if (fold_stmt (&gsi, fwprop_ssa_val, simple_dce_worklist))
4836 : : {
4837 : 2724961 : changed = true;
4838 : 2724961 : stmt = gsi_stmt (gsi);
4839 : : /* Cleanup the CFG if we simplified a condition to
4840 : : true or false. */
4841 : 2724961 : if (gcond *cond = dyn_cast <gcond *> (stmt))
4842 : 933349 : if (gimple_cond_true_p (cond)
4843 : 933349 : || gimple_cond_false_p (cond))
4844 : 14107 : cfg_changed = true;
4845 : : /* Queue old uses for simple DCE if not debug statement. */
4846 : 2724961 : if (!is_gimple_debug (stmt))
4847 : 11783195 : for (tree use : uses)
4848 : 3639563 : if (TREE_CODE (use) == SSA_NAME
4849 : 3639563 : && !SSA_NAME_IS_DEFAULT_DEF (use))
4850 : 3439881 : bitmap_set_bit (simple_dce_worklist,
4851 : 3439881 : SSA_NAME_VERSION (use));
4852 : 2724961 : update_stmt (stmt);
4853 : : }
4854 : :
4855 : 332770499 : switch (gimple_code (stmt))
4856 : : {
4857 : 106590250 : case GIMPLE_ASSIGN:
4858 : 106590250 : {
4859 : 106590250 : tree rhs1 = gimple_assign_rhs1 (stmt);
4860 : 106590250 : enum tree_code code = gimple_assign_rhs_code (stmt);
4861 : 106590250 : if (gimple_assign_load_p (stmt) && gimple_store_p (stmt))
4862 : : {
4863 : 4445720 : if (optimize_memcpy_to_memset (&gsi,
4864 : : gimple_assign_lhs (stmt),
4865 : : gimple_assign_rhs1 (stmt),
4866 : : /* len = */NULL_TREE))
4867 : : {
4868 : : changed = true;
4869 : : break;
4870 : : }
4871 : 4435510 : if (optimize_agr_copyprop (&gsi))
4872 : : {
4873 : : changed = true;
4874 : : break;
4875 : : }
4876 : : }
4877 : :
4878 : 106156664 : if (TREE_CODE_CLASS (code) == tcc_comparison)
4879 : : {
4880 : 2500160 : int did_something;
4881 : 2500160 : did_something = forward_propagate_into_comparison (&gsi);
4882 : 2500160 : if (did_something == 2)
4883 : 0 : cfg_changed = true;
4884 : 2500160 : changed |= did_something != 0;
4885 : : }
4886 : 103656504 : else if ((code == PLUS_EXPR
4887 : 103656504 : || code == BIT_IOR_EXPR
4888 : 93692421 : || code == BIT_XOR_EXPR)
4889 : 103794027 : && simplify_rotate (&gsi))
4890 : : changed = true;
4891 : 103653964 : else if (code == VEC_PERM_EXPR)
4892 : : {
4893 : 173905 : int did_something = simplify_permutation (&gsi);
4894 : 173905 : if (did_something == 2)
4895 : 0 : cfg_changed = true;
4896 : 173905 : changed = did_something != 0;
4897 : : }
4898 : 103480059 : else if (code == CONSTRUCTOR
4899 : 103480059 : && TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
4900 : 161325 : changed |= simplify_vector_constructor (&gsi);
4901 : 103318734 : else if (code == ARRAY_REF)
4902 : 1993267 : changed |= simplify_count_zeroes (&gsi);
4903 : : break;
4904 : : }
4905 : :
4906 : 115097 : case GIMPLE_SWITCH:
4907 : 115097 : changed |= simplify_gimple_switch (as_a <gswitch *> (stmt),
4908 : : edges_to_remove);
4909 : 115097 : break;
4910 : :
4911 : 18842277 : case GIMPLE_COND:
4912 : 18842277 : {
4913 : 18842277 : int did_something = forward_propagate_into_gimple_cond
4914 : 18842277 : (as_a <gcond *> (stmt));
4915 : 18842277 : if (did_something == 2)
4916 : 1768 : cfg_changed = true;
4917 : 18842277 : changed |= did_something != 0;
4918 : 18842277 : break;
4919 : : }
4920 : :
4921 : 22577262 : case GIMPLE_CALL:
4922 : 22577262 : {
4923 : 22577262 : tree callee = gimple_call_fndecl (stmt);
4924 : 22577262 : if (callee != NULL_TREE
4925 : 22577262 : && fndecl_built_in_p (callee, BUILT_IN_NORMAL))
4926 : 5936594 : changed |= simplify_builtin_call (&gsi, callee);
4927 : : break;
4928 : : }
4929 : :
4930 : 332334373 : default:;
4931 : : }
4932 : :
4933 : 332334373 : if (changed || substituted_p)
4934 : : {
4935 : 4646812 : substituted_p = false;
4936 : 4646812 : stmt = gsi_stmt (gsi);
4937 : 4646812 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
4938 : 75 : bitmap_set_bit (to_purge, bb->index);
4939 : 4646812 : if (!was_noreturn
4940 : 4646812 : && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
4941 : 12 : to_fixup.safe_push (stmt);
4942 : : }
4943 : 4646812 : if (changed)
4944 : : {
4945 : : /* If the stmt changed then re-visit it and the statements
4946 : : inserted before it. */
4947 : 10284157 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
4948 : 6552458 : if (gimple_plf (gsi_stmt (gsi), GF_PLF_1))
4949 : : break;
4950 : 3343289 : if (gsi_end_p (gsi))
4951 : 522530 : gsi = gsi_start_bb (bb);
4952 : : else
4953 : 3082024 : gsi_next (&gsi);
4954 : : }
4955 : 332770499 : }
4956 : : while (changed);
4957 : :
4958 : : /* Stmt no longer needs to be revisited. */
4959 : 329427210 : stmt = gsi_stmt (gsi);
4960 : 329427210 : gcc_checking_assert (!gimple_plf (stmt, GF_PLF_1));
4961 : 329427210 : gimple_set_plf (stmt, GF_PLF_1, true);
4962 : :
4963 : : /* Fill up the lattice. */
4964 : 329427210 : if (gimple_assign_single_p (stmt))
4965 : : {
4966 : 70742295 : tree lhs = gimple_assign_lhs (stmt);
4967 : 70742295 : tree rhs = gimple_assign_rhs1 (stmt);
4968 : 70742295 : if (TREE_CODE (lhs) == SSA_NAME)
4969 : : {
4970 : 31900270 : tree val = lhs;
4971 : 31900270 : if (TREE_CODE (rhs) == SSA_NAME)
4972 : 760844 : val = fwprop_ssa_val (rhs);
4973 : 31139426 : else if (is_gimple_min_invariant (rhs))
4974 : 434399 : val = rhs;
4975 : : /* If we can propagate the lattice-value mark the
4976 : : stmt for removal. */
4977 : 31900270 : if (val != lhs
4978 : 31900270 : && may_propagate_copy (lhs, val))
4979 : 1191937 : to_remove_defs.safe_push (SSA_NAME_VERSION (lhs));
4980 : 31900270 : fwprop_set_lattice_val (lhs, val);
4981 : : }
4982 : : }
4983 : 258684915 : else if (gimple_nop_p (stmt))
4984 : 82091 : to_remove.safe_push (stmt);
4985 : : }
4986 : :
4987 : : /* Substitute in destination PHI arguments. */
4988 : 107659485 : FOR_EACH_EDGE (e, ei, bb->succs)
4989 : 62943644 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
4990 : 104560392 : !gsi_end_p (gsi); gsi_next (&gsi))
4991 : : {
4992 : 41616748 : gphi *phi = gsi.phi ();
4993 : 41616748 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
4994 : 41616748 : tree arg = USE_FROM_PTR (use_p);
4995 : 68775301 : if (TREE_CODE (arg) != SSA_NAME
4996 : 41616748 : || virtual_operand_p (arg))
4997 : 27158553 : continue;
4998 : 14458195 : tree val = fwprop_ssa_val (arg);
4999 : 14458195 : if (val != arg
5000 : 14458195 : && may_propagate_copy (arg, val, !(e->flags & EDGE_ABNORMAL)))
5001 : 261834 : propagate_value (use_p, val);
5002 : : }
5003 : :
5004 : : /* Mark outgoing exectuable edges. */
5005 : 44715841 : if (edge e = find_taken_edge (bb, NULL))
5006 : : {
5007 : 19275694 : e->flags |= EDGE_EXECUTABLE;
5008 : 44735922 : if (EDGE_COUNT (bb->succs) > 1)
5009 : 20081 : cfg_changed = true;
5010 : : }
5011 : : else
5012 : : {
5013 : 69088015 : FOR_EACH_EDGE (e, ei, bb->succs)
5014 : 43647868 : e->flags |= EDGE_EXECUTABLE;
5015 : : }
5016 : : }
5017 : 5517869 : free (postorder);
5018 : 5517869 : free (bb_to_rpo);
5019 : 5517869 : lattice.release ();
5020 : :
5021 : : /* First remove chains of stmts where we check no uses remain. */
5022 : 5517869 : simple_dce_from_worklist (simple_dce_worklist, to_purge);
5023 : :
5024 : 5868423 : auto remove = [](gimple *stmt)
5025 : : {
5026 : 350554 : if (dump_file && (dump_flags & TDF_DETAILS))
5027 : : {
5028 : 1 : fprintf (dump_file, "Removing dead stmt ");
5029 : 1 : print_gimple_stmt (dump_file, stmt, 0);
5030 : 1 : fprintf (dump_file, "\n");
5031 : : }
5032 : 350554 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
5033 : 350554 : if (gimple_code (stmt) == GIMPLE_PHI)
5034 : 111634 : remove_phi_node (&gsi, true);
5035 : : else
5036 : : {
5037 : 238920 : unlink_stmt_vdef (stmt);
5038 : 238920 : gsi_remove (&gsi, true);
5039 : 238920 : release_defs (stmt);
5040 : : }
5041 : 350554 : };
5042 : :
5043 : : /* Then remove stmts we know we can remove even though we did not
5044 : : substitute in dead code regions, so uses can remain. Do so in reverse
5045 : : order to make debug stmt creation possible. */
5046 : 12471521 : while (!to_remove_defs.is_empty())
5047 : : {
5048 : 1435783 : tree def = ssa_name (to_remove_defs.pop ());
5049 : : /* For example remove_prop_source_from_use can remove stmts queued
5050 : : for removal. Deal with this gracefully. */
5051 : 1435783 : if (!def)
5052 : 1167320 : continue;
5053 : 268463 : gimple *stmt = SSA_NAME_DEF_STMT (def);
5054 : 268463 : remove (stmt);
5055 : : }
5056 : :
5057 : : /* Wipe other queued stmts that do not have SSA defs. */
5058 : 5599960 : while (!to_remove.is_empty())
5059 : : {
5060 : 82091 : gimple *stmt = to_remove.pop ();
5061 : 82091 : remove (stmt);
5062 : : }
5063 : :
5064 : : /* Fixup stmts that became noreturn calls. This may require splitting
5065 : : blocks and thus isn't possible during the walk. Do this
5066 : : in reverse order so we don't inadvertedly remove a stmt we want to
5067 : : fixup by visiting a dominating now noreturn call first. */
5068 : 5517881 : while (!to_fixup.is_empty ())
5069 : : {
5070 : 12 : gimple *stmt = to_fixup.pop ();
5071 : 12 : if (dump_file && dump_flags & TDF_DETAILS)
5072 : : {
5073 : 0 : fprintf (dump_file, "Fixing up noreturn call ");
5074 : 0 : print_gimple_stmt (dump_file, stmt, 0);
5075 : 0 : fprintf (dump_file, "\n");
5076 : : }
5077 : 12 : cfg_changed |= fixup_noreturn_call (stmt);
5078 : : }
5079 : :
5080 : 5517869 : cfg_changed |= gimple_purge_all_dead_eh_edges (to_purge);
5081 : 5517869 : cfg_changed |= gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
5082 : 5517869 : BITMAP_FREE (to_purge);
5083 : :
5084 : : /* Remove edges queued from switch stmt simplification. */
5085 : 16553607 : for (auto ep : edges_to_remove)
5086 : : {
5087 : 0 : basic_block src = BASIC_BLOCK_FOR_FN (fun, ep.first);
5088 : 0 : basic_block dest = BASIC_BLOCK_FOR_FN (fun, ep.second);
5089 : 0 : edge e;
5090 : 0 : if (src && dest && (e = find_edge (src, dest)))
5091 : : {
5092 : 0 : free_dominance_info (CDI_DOMINATORS);
5093 : 0 : remove_edge (e);
5094 : 0 : cfg_changed = true;
5095 : : }
5096 : : }
5097 : :
5098 : 11034206 : if (get_range_query (fun) != get_global_range_query ())
5099 : 1532 : disable_ranger (fun);
5100 : :
5101 : 5517869 : if (cfg_changed)
5102 : 9237 : todoflags |= TODO_cleanup_cfg;
5103 : :
5104 : 5517869 : return todoflags;
5105 : 5517869 : }
5106 : :
5107 : : } // anon namespace
5108 : :
5109 : : gimple_opt_pass *
5110 : 284673 : make_pass_forwprop (gcc::context *ctxt)
5111 : : {
5112 : 284673 : return new pass_forwprop (ctxt);
5113 : : }
|