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 : 32895017 : fwprop_set_lattice_val (tree name, tree val)
222 : : {
223 : 32895017 : if (TREE_CODE (name) == SSA_NAME)
224 : : {
225 : 32895017 : if (SSA_NAME_VERSION (name) >= lattice.length ())
226 : : {
227 : 32157 : lattice.reserve (num_ssa_names - lattice.length ());
228 : 21438 : lattice.quick_grow_cleared (num_ssa_names);
229 : : }
230 : 32895017 : lattice[SSA_NAME_VERSION (name)] = val;
231 : : /* As this now constitutes a copy duplicate points-to
232 : : and range info appropriately. */
233 : 32895017 : if (TREE_CODE (val) == SSA_NAME)
234 : 32429576 : maybe_duplicate_ssa_info_at_copy (name, val);
235 : : }
236 : 32895017 : }
237 : :
238 : : /* Invalidate the lattice entry for NAME, done when releasing SSA names. */
239 : : static void
240 : 887310 : fwprop_invalidate_lattice (tree name)
241 : : {
242 : 887310 : if (name
243 : 884977 : && TREE_CODE (name) == SSA_NAME
244 : 1772180 : && SSA_NAME_VERSION (name) < lattice.length ())
245 : 884841 : lattice[SSA_NAME_VERSION (name)] = NULL_TREE;
246 : 887310 : }
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 : 27680783 : get_prop_source_stmt (tree name, bool single_use_only, bool *single_use_p)
258 : : {
259 : 27680783 : bool single_use = true;
260 : :
261 : 27681755 : do {
262 : 27681269 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
263 : :
264 : 27681269 : if (!has_single_use (name))
265 : : {
266 : 15026270 : single_use = false;
267 : 15026270 : 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 : 27679859 : if (!is_gimple_assign (def_stmt))
273 : : return NULL;
274 : :
275 : : /* If def_stmt is a simple copy, continue looking. */
276 : 19474239 : if (gimple_assign_rhs_code (def_stmt) == SSA_NAME)
277 : 486 : name = gimple_assign_rhs1 (def_stmt);
278 : : else
279 : : {
280 : 19473753 : if (!single_use_only && single_use_p)
281 : 19171496 : *single_use_p = single_use;
282 : :
283 : 19473753 : 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 : 27538669 : can_propagate_from (gimple *def_stmt)
293 : : {
294 : 27538669 : gcc_assert (is_gimple_assign (def_stmt));
295 : :
296 : : /* If the rhs has side-effects we cannot propagate from it. */
297 : 27538669 : if (gimple_has_volatile_ops (def_stmt))
298 : : return false;
299 : :
300 : : /* If the rhs is a load we cannot propagate from it. */
301 : 26949563 : if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_reference
302 : 26949563 : || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_declaration)
303 : : return false;
304 : :
305 : : /* Constants can be always propagated. */
306 : 13344448 : if (gimple_assign_single_p (def_stmt)
307 : 13344448 : && 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 : 13344448 : 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 : 13343817 : if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
319 : : {
320 : 3223289 : tree rhs = gimple_assign_rhs1 (def_stmt);
321 : 3223289 : if (FUNCTION_POINTER_TYPE_P (TREE_TYPE (rhs)))
322 : : return false;
323 : : }
324 : :
325 : : return true;
326 : : }
327 : :
328 : : /* Remove a chain of dead statements starting at the definition of
329 : : NAME. The chain is linked via the first operand of the defining statements.
330 : : If NAME was replaced in its only use then this function can be used
331 : : to clean up dead stmts. The function handles already released SSA
332 : : names gracefully. */
333 : :
334 : : static void
335 : 218738 : remove_prop_source_from_use (tree name)
336 : : {
337 : 277181 : gimple_stmt_iterator gsi;
338 : 277181 : gimple *stmt;
339 : :
340 : 277181 : do {
341 : 277181 : basic_block bb;
342 : :
343 : 277181 : if (SSA_NAME_IN_FREE_LIST (name)
344 : 277138 : || SSA_NAME_IS_DEFAULT_DEF (name)
345 : 550945 : || !has_zero_uses (name))
346 : : break;
347 : :
348 : 58891 : stmt = SSA_NAME_DEF_STMT (name);
349 : 58891 : if (gimple_code (stmt) == GIMPLE_PHI
350 : 58891 : || gimple_has_side_effects (stmt))
351 : : break;
352 : :
353 : 58891 : bb = gimple_bb (stmt);
354 : 58891 : gsi = gsi_for_stmt (stmt);
355 : 58891 : unlink_stmt_vdef (stmt);
356 : 58891 : if (gsi_remove (&gsi, true))
357 : 6 : bitmap_set_bit (to_purge, bb->index);
358 : 58891 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
359 : 58891 : release_defs (stmt);
360 : :
361 : 58891 : name = is_gimple_assign (stmt) ? gimple_assign_rhs1 (stmt) : NULL_TREE;
362 : 58891 : } while (name && TREE_CODE (name) == SSA_NAME);
363 : :
364 : 218738 : }
365 : :
366 : : /* Return the rhs of a gassign *STMT in a form of a single tree,
367 : : converted to type TYPE.
368 : :
369 : : This should disappear, but is needed so we can combine expressions and use
370 : : the fold() interfaces. Long term, we need to develop folding and combine
371 : : routines that deal with gimple exclusively . */
372 : :
373 : : static tree
374 : 7164975 : rhs_to_tree (tree type, gimple *stmt)
375 : : {
376 : 7164975 : location_t loc = gimple_location (stmt);
377 : 7164975 : enum tree_code code = gimple_assign_rhs_code (stmt);
378 : 7164975 : switch (get_gimple_rhs_class (code))
379 : : {
380 : 12225 : case GIMPLE_TERNARY_RHS:
381 : 12225 : return fold_build3_loc (loc, code, type, gimple_assign_rhs1 (stmt),
382 : : gimple_assign_rhs2 (stmt),
383 : 12225 : gimple_assign_rhs3 (stmt));
384 : 4988225 : case GIMPLE_BINARY_RHS:
385 : 4988225 : return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (stmt),
386 : 4988225 : gimple_assign_rhs2 (stmt));
387 : 1972003 : case GIMPLE_UNARY_RHS:
388 : 1972003 : return build1 (code, type, gimple_assign_rhs1 (stmt));
389 : 192522 : case GIMPLE_SINGLE_RHS:
390 : 192522 : return gimple_assign_rhs1 (stmt);
391 : 0 : default:
392 : 0 : gcc_unreachable ();
393 : : }
394 : : }
395 : :
396 : : /* Combine OP0 CODE OP1 in the context of a COND_EXPR. Returns
397 : : the folded result in a form suitable for COND_EXPR_COND or
398 : : NULL_TREE, if there is no suitable simplified form. If
399 : : INVARIANT_ONLY is true only gimple_min_invariant results are
400 : : considered simplified. */
401 : :
402 : : static tree
403 : 8068223 : combine_cond_expr_cond (gimple *stmt, enum tree_code code, tree type,
404 : : tree op0, tree op1, bool invariant_only)
405 : : {
406 : 8068223 : tree t;
407 : :
408 : 8068223 : gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
409 : :
410 : 8068223 : fold_defer_overflow_warnings ();
411 : 8068223 : t = fold_binary_loc (gimple_location (stmt), code, type, op0, op1);
412 : 8068223 : if (!t)
413 : : {
414 : 4562191 : fold_undefer_overflow_warnings (false, NULL, 0);
415 : 4562191 : return NULL_TREE;
416 : : }
417 : :
418 : : /* Require that we got a boolean type out if we put one in. */
419 : 3506032 : gcc_assert (TREE_CODE (TREE_TYPE (t)) == TREE_CODE (type));
420 : :
421 : : /* Canonicalize the combined condition for use in a COND_EXPR. */
422 : 3506032 : t = canonicalize_cond_expr_cond (t);
423 : :
424 : : /* Bail out if we required an invariant but didn't get one. */
425 : 3506032 : if (!t || (invariant_only && !is_gimple_min_invariant (t)))
426 : : {
427 : 3288957 : fold_undefer_overflow_warnings (false, NULL, 0);
428 : 3288957 : return NULL_TREE;
429 : : }
430 : :
431 : 217075 : bool nowarn = warning_suppressed_p (stmt, OPT_Wstrict_overflow);
432 : 217075 : fold_undefer_overflow_warnings (!nowarn, stmt, 0);
433 : :
434 : 217075 : return t;
435 : : }
436 : :
437 : : /* Combine the comparison OP0 CODE OP1 at LOC with the defining statements
438 : : of its operand. Return a new comparison tree or NULL_TREE if there
439 : : were no simplifying combines. */
440 : :
441 : : static tree
442 : 21764016 : forward_propagate_into_comparison_1 (gimple *stmt,
443 : : enum tree_code code, tree type,
444 : : tree op0, tree op1)
445 : : {
446 : 21764016 : tree tmp = NULL_TREE;
447 : 21764016 : tree rhs0 = NULL_TREE, rhs1 = NULL_TREE;
448 : 21764016 : bool single_use0_p = false, single_use1_p = false;
449 : :
450 : : /* For comparisons use the first operand, that is likely to
451 : : simplify comparisons against constants. */
452 : 21764016 : if (TREE_CODE (op0) == SSA_NAME)
453 : : {
454 : 21727296 : gimple *def_stmt = get_prop_source_stmt (op0, false, &single_use0_p);
455 : 21727296 : if (def_stmt && can_propagate_from (def_stmt))
456 : : {
457 : 5435552 : enum tree_code def_code = gimple_assign_rhs_code (def_stmt);
458 : 5435552 : bool invariant_only_p = !single_use0_p;
459 : :
460 : 5435552 : rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt);
461 : :
462 : : /* Always combine comparisons or conversions from booleans. */
463 : 5435552 : if (TREE_CODE (op1) == INTEGER_CST
464 : 5435552 : && ((CONVERT_EXPR_CODE_P (def_code)
465 : 836546 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs0, 0)))
466 : : == BOOLEAN_TYPE)
467 : 3552426 : || TREE_CODE_CLASS (def_code) == tcc_comparison))
468 : : invariant_only_p = false;
469 : :
470 : 5435552 : tmp = combine_cond_expr_cond (stmt, code, type,
471 : : rhs0, op1, invariant_only_p);
472 : 5435552 : if (tmp)
473 : : return tmp;
474 : : }
475 : : }
476 : :
477 : : /* If that wasn't successful, try the second operand. */
478 : 21554722 : if (TREE_CODE (op1) == SSA_NAME)
479 : : {
480 : 5406866 : gimple *def_stmt = get_prop_source_stmt (op1, false, &single_use1_p);
481 : 5406866 : if (def_stmt && can_propagate_from (def_stmt))
482 : : {
483 : 1729423 : rhs1 = rhs_to_tree (TREE_TYPE (op0), def_stmt);
484 : 3458846 : tmp = combine_cond_expr_cond (stmt, code, type,
485 : 1729423 : op0, rhs1, !single_use1_p);
486 : 1729423 : if (tmp)
487 : : return tmp;
488 : : }
489 : : }
490 : :
491 : : /* If that wasn't successful either, try both operands. */
492 : 21548879 : if (rhs0 != NULL_TREE
493 : 21548879 : && rhs1 != NULL_TREE)
494 : 903248 : tmp = combine_cond_expr_cond (stmt, code, type,
495 : : rhs0, rhs1,
496 : 903248 : !(single_use0_p && single_use1_p));
497 : :
498 : : return tmp;
499 : : }
500 : :
501 : : /* Propagate from the ssa name definition statements of the assignment
502 : : from a comparison at *GSI into the conditional if that simplifies it.
503 : : Returns true if the stmt was modified. */
504 : :
505 : : static bool
506 : 2542166 : forward_propagate_into_comparison (gimple_stmt_iterator *gsi)
507 : : {
508 : 2542166 : gimple *stmt = gsi_stmt (*gsi);
509 : 2542166 : tree tmp;
510 : 2542166 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
511 : 2542166 : tree rhs1 = gimple_assign_rhs1 (stmt);
512 : 2542166 : tree rhs2 = gimple_assign_rhs2 (stmt);
513 : :
514 : : /* Combine the comparison with defining statements. */
515 : 2542166 : tmp = forward_propagate_into_comparison_1 (stmt,
516 : : gimple_assign_rhs_code (stmt),
517 : : type, rhs1, rhs2);
518 : 2542166 : if (tmp && useless_type_conversion_p (type, TREE_TYPE (tmp)))
519 : : {
520 : 6882 : if (dump_file)
521 : : {
522 : 0 : fprintf (dump_file, " Replaced '");
523 : 0 : print_gimple_expr (dump_file, stmt, 0);
524 : 0 : fprintf (dump_file, "' with '");
525 : 0 : print_generic_expr (dump_file, tmp);
526 : 0 : fprintf (dump_file, "'\n");
527 : : }
528 : 6882 : gimple_assign_set_rhs_from_tree (gsi, tmp);
529 : 6882 : fold_stmt (gsi);
530 : 6882 : update_stmt (gsi_stmt (*gsi));
531 : :
532 : 6882 : if (TREE_CODE (rhs1) == SSA_NAME)
533 : 6882 : remove_prop_source_from_use (rhs1);
534 : 6882 : if (TREE_CODE (rhs2) == SSA_NAME)
535 : 2685 : remove_prop_source_from_use (rhs2);
536 : 6882 : return true;
537 : : }
538 : :
539 : : return false;
540 : : }
541 : :
542 : : /* Propagate from the ssa name definition statements of COND_EXPR
543 : : in GIMPLE_COND statement STMT into the conditional if that simplifies it.
544 : : Returns zero if no statement was changed, one if there were
545 : : changes and two if cfg_cleanup needs to run. */
546 : :
547 : : static int
548 : 19221850 : forward_propagate_into_gimple_cond (gcond *stmt)
549 : : {
550 : 19221850 : tree tmp;
551 : 19221850 : enum tree_code code = gimple_cond_code (stmt);
552 : 19221850 : tree rhs1 = gimple_cond_lhs (stmt);
553 : 19221850 : tree rhs2 = gimple_cond_rhs (stmt);
554 : :
555 : : /* GIMPLE_COND will always be a comparison. */
556 : 19221850 : gcc_assert (TREE_CODE_CLASS (gimple_cond_code (stmt)) == tcc_comparison);
557 : :
558 : 19221850 : tmp = forward_propagate_into_comparison_1 (stmt, code,
559 : : boolean_type_node,
560 : : rhs1, rhs2);
561 : 19221850 : if (tmp
562 : 19221850 : && is_gimple_condexpr_for_cond (tmp))
563 : : {
564 : 203862 : if (dump_file)
565 : : {
566 : 9 : fprintf (dump_file, " Replaced '");
567 : 9 : print_gimple_expr (dump_file, stmt, 0);
568 : 9 : fprintf (dump_file, "' with '");
569 : 9 : print_generic_expr (dump_file, tmp);
570 : 9 : fprintf (dump_file, "'\n");
571 : : }
572 : :
573 : 203862 : gimple_cond_set_condition_from_tree (stmt, unshare_expr (tmp));
574 : 203862 : update_stmt (stmt);
575 : :
576 : 203862 : if (TREE_CODE (rhs1) == SSA_NAME)
577 : 203862 : remove_prop_source_from_use (rhs1);
578 : 203862 : if (TREE_CODE (rhs2) == SSA_NAME)
579 : 5308 : remove_prop_source_from_use (rhs2);
580 : 203862 : return is_gimple_min_invariant (tmp) ? 2 : 1;
581 : : }
582 : :
583 : 19017988 : if (canonicalize_bool_cond (stmt, gimple_bb (stmt)))
584 : : return 1;
585 : :
586 : : return 0;
587 : : }
588 : :
589 : : /* We've just substituted an ADDR_EXPR into stmt. Update all the
590 : : relevant data structures to match. */
591 : :
592 : : static void
593 : 2197066 : tidy_after_forward_propagate_addr (gimple *stmt)
594 : : {
595 : : /* We may have turned a trapping insn into a non-trapping insn. */
596 : 2197066 : if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
597 : 131 : bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
598 : :
599 : 2197066 : if (TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
600 : 265961 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
601 : 2197066 : }
602 : :
603 : : /* NAME is a SSA_NAME representing DEF_RHS which is of the form
604 : : ADDR_EXPR <whatever>.
605 : :
606 : : Try to forward propagate the ADDR_EXPR into the use USE_STMT.
607 : : Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
608 : : node or for recovery of array indexing from pointer arithmetic.
609 : :
610 : : Return true if the propagation was successful (the propagation can
611 : : be not totally successful, yet things may have been changed). */
612 : :
613 : : static bool
614 : 3063367 : forward_propagate_addr_expr_1 (tree name, tree def_rhs,
615 : : gimple_stmt_iterator *use_stmt_gsi,
616 : : bool single_use_p)
617 : : {
618 : 3063367 : tree lhs, rhs, rhs2, array_ref;
619 : 3063367 : gimple *use_stmt = gsi_stmt (*use_stmt_gsi);
620 : 3063367 : enum tree_code rhs_code;
621 : 3063367 : bool res = true;
622 : :
623 : 3063367 : gcc_assert (TREE_CODE (def_rhs) == ADDR_EXPR);
624 : :
625 : 3063367 : lhs = gimple_assign_lhs (use_stmt);
626 : 3063367 : rhs_code = gimple_assign_rhs_code (use_stmt);
627 : 3063367 : rhs = gimple_assign_rhs1 (use_stmt);
628 : :
629 : : /* Do not perform copy-propagation but recurse through copy chains. */
630 : 3063367 : if (TREE_CODE (lhs) == SSA_NAME
631 : 1407619 : && rhs_code == SSA_NAME)
632 : 6056 : return forward_propagate_addr_expr (lhs, def_rhs, single_use_p);
633 : :
634 : : /* The use statement could be a conversion. Recurse to the uses of the
635 : : lhs as copyprop does not copy through pointer to integer to pointer
636 : : conversions and FRE does not catch all cases either.
637 : : Treat the case of a single-use name and
638 : : a conversion to def_rhs type separate, though. */
639 : 3057311 : if (TREE_CODE (lhs) == SSA_NAME
640 : 1401563 : && CONVERT_EXPR_CODE_P (rhs_code))
641 : : {
642 : : /* If there is a point in a conversion chain where the types match
643 : : so we can remove a conversion re-materialize the address here
644 : : and stop. */
645 : 25626 : if (single_use_p
646 : 25626 : && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs)))
647 : : {
648 : 2 : gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs));
649 : 2 : gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs));
650 : 2 : return true;
651 : : }
652 : :
653 : : /* Else recurse if the conversion preserves the address value. */
654 : 51248 : if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
655 : 1 : || POINTER_TYPE_P (TREE_TYPE (lhs)))
656 : 51248 : && (TYPE_PRECISION (TREE_TYPE (lhs))
657 : 25624 : >= TYPE_PRECISION (TREE_TYPE (def_rhs))))
658 : 25557 : return forward_propagate_addr_expr (lhs, def_rhs, single_use_p);
659 : :
660 : : return false;
661 : : }
662 : :
663 : : /* If this isn't a conversion chain from this on we only can propagate
664 : : into compatible pointer contexts. */
665 : 3031685 : if (!types_compatible_p (TREE_TYPE (name), TREE_TYPE (def_rhs)))
666 : : return false;
667 : :
668 : : /* Propagate through constant pointer adjustments. */
669 : 3009324 : if (TREE_CODE (lhs) == SSA_NAME
670 : 1354763 : && rhs_code == POINTER_PLUS_EXPR
671 : 1354763 : && rhs == name
672 : 3166423 : && TREE_CODE (gimple_assign_rhs2 (use_stmt)) == INTEGER_CST)
673 : : {
674 : 116869 : tree new_def_rhs;
675 : : /* As we come here with non-invariant addresses in def_rhs we need
676 : : to make sure we can build a valid constant offsetted address
677 : : for further propagation. Simply rely on fold building that
678 : : and check after the fact. */
679 : 116869 : new_def_rhs = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (rhs)),
680 : : def_rhs,
681 : : fold_convert (ptr_type_node,
682 : : gimple_assign_rhs2 (use_stmt)));
683 : 116869 : if (TREE_CODE (new_def_rhs) == MEM_REF
684 : 116869 : && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs, 0)))
685 : : return false;
686 : 113340 : new_def_rhs = build1 (ADDR_EXPR, TREE_TYPE (rhs), new_def_rhs);
687 : :
688 : : /* Recurse. If we could propagate into all uses of lhs do not
689 : : bother to replace into the current use but just pretend we did. */
690 : 113340 : if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p))
691 : : return true;
692 : :
693 : 37453 : if (useless_type_conversion_p (TREE_TYPE (lhs),
694 : 37453 : TREE_TYPE (new_def_rhs)))
695 : 37453 : gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs),
696 : : new_def_rhs);
697 : 0 : else if (is_gimple_min_invariant (new_def_rhs))
698 : 0 : gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR, new_def_rhs);
699 : : else
700 : : return false;
701 : 37453 : gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt);
702 : 37453 : update_stmt (use_stmt);
703 : 37453 : return true;
704 : : }
705 : :
706 : : /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
707 : : ADDR_EXPR will not appear on the LHS. */
708 : 2892455 : tree *lhsp = gimple_assign_lhs_ptr (use_stmt);
709 : 4303011 : while (handled_component_p (*lhsp))
710 : 1410556 : lhsp = &TREE_OPERAND (*lhsp, 0);
711 : 2892455 : lhs = *lhsp;
712 : :
713 : : /* Now see if the LHS node is a MEM_REF using NAME. If so,
714 : : propagate the ADDR_EXPR into the use of NAME and fold the result. */
715 : 2892455 : if (TREE_CODE (lhs) == MEM_REF
716 : 2892455 : && TREE_OPERAND (lhs, 0) == name)
717 : : {
718 : 1082908 : tree def_rhs_base;
719 : 1082908 : poly_int64 def_rhs_offset;
720 : : /* If the address is invariant we can always fold it. */
721 : 1082908 : if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
722 : : &def_rhs_offset)))
723 : : {
724 : 1042393 : poly_offset_int off = mem_ref_offset (lhs);
725 : 1042393 : tree new_ptr;
726 : 1042393 : off += def_rhs_offset;
727 : 1042393 : if (TREE_CODE (def_rhs_base) == MEM_REF)
728 : : {
729 : 1017288 : off += mem_ref_offset (def_rhs_base);
730 : 1017288 : new_ptr = TREE_OPERAND (def_rhs_base, 0);
731 : : }
732 : : else
733 : 25105 : new_ptr = build_fold_addr_expr (def_rhs_base);
734 : 1042393 : TREE_OPERAND (lhs, 0) = new_ptr;
735 : 1042393 : TREE_OPERAND (lhs, 1)
736 : 1042393 : = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs, 1)), off);
737 : 1042393 : tidy_after_forward_propagate_addr (use_stmt);
738 : : /* Continue propagating into the RHS if this was not the only use. */
739 : 1042393 : if (single_use_p)
740 : 186435 : return true;
741 : : }
742 : : /* If the LHS is a plain dereference and the value type is the same as
743 : : that of the pointed-to type of the address we can put the
744 : : dereferenced address on the LHS preserving the original alias-type. */
745 : 40515 : else if (integer_zerop (TREE_OPERAND (lhs, 1))
746 : 16417 : && ((gimple_assign_lhs (use_stmt) == lhs
747 : 13070 : && useless_type_conversion_p
748 : 13070 : (TREE_TYPE (TREE_OPERAND (def_rhs, 0)),
749 : 13070 : TREE_TYPE (gimple_assign_rhs1 (use_stmt))))
750 : 12416 : || types_compatible_p (TREE_TYPE (lhs),
751 : 12416 : TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
752 : : /* Don't forward anything into clobber stmts if it would result
753 : : in the lhs no longer being a MEM_REF. */
754 : 47508 : && (!gimple_clobber_p (use_stmt)
755 : 321 : || TREE_CODE (TREE_OPERAND (def_rhs, 0)) == MEM_REF))
756 : : {
757 : 6672 : tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
758 : 6672 : tree new_offset, new_base, saved, new_lhs;
759 : 23792 : while (handled_component_p (*def_rhs_basep))
760 : 10448 : def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
761 : 6672 : saved = *def_rhs_basep;
762 : 6672 : if (TREE_CODE (*def_rhs_basep) == MEM_REF)
763 : : {
764 : 3783 : new_base = TREE_OPERAND (*def_rhs_basep, 0);
765 : 3783 : new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (lhs, 1)),
766 : : TREE_OPERAND (*def_rhs_basep, 1));
767 : : }
768 : : else
769 : : {
770 : 2889 : new_base = build_fold_addr_expr (*def_rhs_basep);
771 : 2889 : new_offset = TREE_OPERAND (lhs, 1);
772 : : }
773 : 6672 : *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep),
774 : : new_base, new_offset);
775 : 6672 : TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (lhs);
776 : 6672 : TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (lhs);
777 : 6672 : TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (lhs);
778 : 6672 : new_lhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
779 : 6672 : *lhsp = new_lhs;
780 : 6672 : TREE_THIS_VOLATILE (new_lhs) = TREE_THIS_VOLATILE (lhs);
781 : 6672 : TREE_SIDE_EFFECTS (new_lhs) = TREE_SIDE_EFFECTS (lhs);
782 : 6672 : *def_rhs_basep = saved;
783 : 6672 : tidy_after_forward_propagate_addr (use_stmt);
784 : : /* Continue propagating into the RHS if this was not the
785 : : only use. */
786 : 6672 : if (single_use_p)
787 : : return true;
788 : : }
789 : : else
790 : : /* We can have a struct assignment dereferencing our name twice.
791 : : Note that we didn't propagate into the lhs to not falsely
792 : : claim we did when propagating into the rhs. */
793 : : res = false;
794 : : }
795 : :
796 : : /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
797 : : nodes from the RHS. */
798 : 2703350 : tree *rhsp = gimple_assign_rhs1_ptr (use_stmt);
799 : 2703350 : if (TREE_CODE (*rhsp) == ADDR_EXPR)
800 : 254732 : rhsp = &TREE_OPERAND (*rhsp, 0);
801 : 3740905 : while (handled_component_p (*rhsp))
802 : 1037555 : rhsp = &TREE_OPERAND (*rhsp, 0);
803 : 2703350 : rhs = *rhsp;
804 : :
805 : : /* Now see if the RHS node is a MEM_REF using NAME. If so,
806 : : propagate the ADDR_EXPR into the use of NAME and fold the result. */
807 : 2703350 : if (TREE_CODE (rhs) == MEM_REF
808 : 2703350 : && TREE_OPERAND (rhs, 0) == name)
809 : : {
810 : 1166655 : tree def_rhs_base;
811 : 1166655 : poly_int64 def_rhs_offset;
812 : 1166655 : if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
813 : : &def_rhs_offset)))
814 : : {
815 : 1133351 : poly_offset_int off = mem_ref_offset (rhs);
816 : 1133351 : tree new_ptr;
817 : 1133351 : off += def_rhs_offset;
818 : 1133351 : if (TREE_CODE (def_rhs_base) == MEM_REF)
819 : : {
820 : 1101513 : off += mem_ref_offset (def_rhs_base);
821 : 1101513 : new_ptr = TREE_OPERAND (def_rhs_base, 0);
822 : : }
823 : : else
824 : 31838 : new_ptr = build_fold_addr_expr (def_rhs_base);
825 : 1133351 : TREE_OPERAND (rhs, 0) = new_ptr;
826 : 1133351 : TREE_OPERAND (rhs, 1)
827 : 1133351 : = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs, 1)), off);
828 : 1133351 : fold_stmt_inplace (use_stmt_gsi);
829 : 1133351 : tidy_after_forward_propagate_addr (use_stmt);
830 : 1133351 : return res;
831 : : }
832 : : /* If the RHS is a plain dereference and the value type is the same as
833 : : that of the pointed-to type of the address we can put the
834 : : dereferenced address on the RHS preserving the original alias-type. */
835 : 33304 : else if (integer_zerop (TREE_OPERAND (rhs, 1))
836 : 33304 : && ((gimple_assign_rhs1 (use_stmt) == rhs
837 : 19471 : && useless_type_conversion_p
838 : 19471 : (TREE_TYPE (gimple_assign_lhs (use_stmt)),
839 : 19471 : TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
840 : 21986 : || types_compatible_p (TREE_TYPE (rhs),
841 : 21986 : TREE_TYPE (TREE_OPERAND (def_rhs, 0)))))
842 : : {
843 : 14650 : tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
844 : 14650 : tree new_offset, new_base, saved, new_rhs;
845 : 51636 : while (handled_component_p (*def_rhs_basep))
846 : 22336 : def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
847 : 14650 : saved = *def_rhs_basep;
848 : 14650 : if (TREE_CODE (*def_rhs_basep) == MEM_REF)
849 : : {
850 : 7393 : new_base = TREE_OPERAND (*def_rhs_basep, 0);
851 : 7393 : new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (rhs, 1)),
852 : : TREE_OPERAND (*def_rhs_basep, 1));
853 : : }
854 : : else
855 : : {
856 : 7257 : new_base = build_fold_addr_expr (*def_rhs_basep);
857 : 7257 : new_offset = TREE_OPERAND (rhs, 1);
858 : : }
859 : 14650 : *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep),
860 : : new_base, new_offset);
861 : 14650 : TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (rhs);
862 : 14650 : TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (rhs);
863 : 14650 : TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (rhs);
864 : 14650 : new_rhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
865 : 14650 : *rhsp = new_rhs;
866 : 14650 : TREE_THIS_VOLATILE (new_rhs) = TREE_THIS_VOLATILE (rhs);
867 : 14650 : TREE_SIDE_EFFECTS (new_rhs) = TREE_SIDE_EFFECTS (rhs);
868 : 14650 : *def_rhs_basep = saved;
869 : 14650 : fold_stmt_inplace (use_stmt_gsi);
870 : 14650 : tidy_after_forward_propagate_addr (use_stmt);
871 : 14650 : return res;
872 : : }
873 : : }
874 : :
875 : : /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there
876 : : is nothing to do. */
877 : 1555349 : if (gimple_assign_rhs_code (use_stmt) != POINTER_PLUS_EXPR
878 : 1555349 : || gimple_assign_rhs1 (use_stmt) != name)
879 : : return false;
880 : :
881 : : /* The remaining cases are all for turning pointer arithmetic into
882 : : array indexing. They only apply when we have the address of
883 : : element zero in an array. If that is not the case then there
884 : : is nothing to do. */
885 : 40230 : array_ref = TREE_OPERAND (def_rhs, 0);
886 : 40230 : if ((TREE_CODE (array_ref) != ARRAY_REF
887 : 4391 : || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
888 : 4391 : || TREE_CODE (TREE_OPERAND (array_ref, 1)) != INTEGER_CST)
889 : 41663 : && TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE)
890 : : return false;
891 : :
892 : 16588 : rhs2 = gimple_assign_rhs2 (use_stmt);
893 : : /* Optimize &x[C1] p+ C2 to &x p+ C3 with C3 = C1 * element_size + C2. */
894 : 16588 : if (TREE_CODE (rhs2) == INTEGER_CST)
895 : : {
896 : 0 : tree new_rhs = build1_loc (gimple_location (use_stmt),
897 : 0 : ADDR_EXPR, TREE_TYPE (def_rhs),
898 : 0 : fold_build2 (MEM_REF,
899 : : TREE_TYPE (TREE_TYPE (def_rhs)),
900 : : unshare_expr (def_rhs),
901 : : fold_convert (ptr_type_node,
902 : : rhs2)));
903 : 0 : gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs);
904 : 0 : use_stmt = gsi_stmt (*use_stmt_gsi);
905 : 0 : update_stmt (use_stmt);
906 : 0 : tidy_after_forward_propagate_addr (use_stmt);
907 : 0 : return true;
908 : : }
909 : :
910 : : return false;
911 : : }
912 : :
913 : : /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
914 : :
915 : : Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
916 : : Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
917 : : node or for recovery of array indexing from pointer arithmetic.
918 : :
919 : : PARENT_SINGLE_USE_P tells if, when in a recursive invocation, NAME was
920 : : the single use in the previous invocation. Pass true when calling
921 : : this as toplevel.
922 : :
923 : : Returns true, if all uses have been propagated into. */
924 : :
925 : : static bool
926 : 3361393 : forward_propagate_addr_expr (tree name, tree rhs, bool parent_single_use_p)
927 : : {
928 : 3361393 : imm_use_iterator iter;
929 : 3361393 : gimple *use_stmt;
930 : 3361393 : bool all = true;
931 : 3361393 : bool single_use_p = parent_single_use_p && has_single_use (name);
932 : :
933 : 11241251 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, name)
934 : : {
935 : 7879858 : bool result;
936 : 7879858 : tree use_rhs;
937 : :
938 : : /* If the use is not in a simple assignment statement, then
939 : : there is nothing we can do. */
940 : 7879858 : if (!is_gimple_assign (use_stmt))
941 : : {
942 : 4816491 : if (!is_gimple_debug (use_stmt))
943 : 1954068 : all = false;
944 : 4816491 : continue;
945 : : }
946 : :
947 : 3063367 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
948 : 3063367 : result = forward_propagate_addr_expr_1 (name, rhs, &gsi,
949 : : single_use_p);
950 : : /* If the use has moved to a different statement adjust
951 : : the update machinery for the old statement too. */
952 : 3063367 : if (use_stmt != gsi_stmt (gsi))
953 : : {
954 : 0 : update_stmt (use_stmt);
955 : 0 : use_stmt = gsi_stmt (gsi);
956 : : }
957 : 3063367 : update_stmt (use_stmt);
958 : 3063367 : all &= result;
959 : :
960 : : /* Remove intermediate now unused copy and conversion chains. */
961 : 3063367 : use_rhs = gimple_assign_rhs1 (use_stmt);
962 : 3063367 : if (result
963 : 1452512 : && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
964 : 1250581 : && TREE_CODE (use_rhs) == SSA_NAME
965 : 3141318 : && has_zero_uses (gimple_assign_lhs (use_stmt)))
966 : : {
967 : 77951 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
968 : 77951 : fwprop_invalidate_lattice (gimple_get_lhs (use_stmt));
969 : 77951 : release_defs (use_stmt);
970 : 77951 : gsi_remove (&gsi, true);
971 : : }
972 : 3361393 : }
973 : :
974 : 3361393 : return all && has_zero_uses (name);
975 : : }
976 : :
977 : :
978 : : /* Helper function for simplify_gimple_switch. Remove case labels that
979 : : have values outside the range of the new type. */
980 : :
981 : : static void
982 : 11773 : simplify_gimple_switch_label_vec (gswitch *stmt, tree index_type,
983 : : vec<std::pair<int, int> > &edges_to_remove)
984 : : {
985 : 11773 : unsigned int branch_num = gimple_switch_num_labels (stmt);
986 : 11773 : auto_vec<tree> labels (branch_num);
987 : 11773 : unsigned int i, len;
988 : :
989 : : /* Collect the existing case labels in a VEC, and preprocess it as if
990 : : we are gimplifying a GENERIC SWITCH_EXPR. */
991 : 76043 : for (i = 1; i < branch_num; i++)
992 : 52497 : labels.quick_push (gimple_switch_label (stmt, i));
993 : 11773 : preprocess_case_label_vec_for_gimple (labels, index_type, NULL);
994 : :
995 : : /* If any labels were removed, replace the existing case labels
996 : : in the GIMPLE_SWITCH statement with the correct ones.
997 : : Note that the type updates were done in-place on the case labels,
998 : : so we only have to replace the case labels in the GIMPLE_SWITCH
999 : : if the number of labels changed. */
1000 : 11773 : len = labels.length ();
1001 : 11773 : if (len < branch_num - 1)
1002 : : {
1003 : 0 : bitmap target_blocks;
1004 : 0 : edge_iterator ei;
1005 : 0 : edge e;
1006 : :
1007 : : /* Corner case: *all* case labels have been removed as being
1008 : : out-of-range for INDEX_TYPE. Push one label and let the
1009 : : CFG cleanups deal with this further. */
1010 : 0 : if (len == 0)
1011 : : {
1012 : 0 : tree label, elt;
1013 : :
1014 : 0 : label = CASE_LABEL (gimple_switch_default_label (stmt));
1015 : 0 : elt = build_case_label (build_int_cst (index_type, 0), NULL, label);
1016 : 0 : labels.quick_push (elt);
1017 : 0 : len = 1;
1018 : : }
1019 : :
1020 : 0 : for (i = 0; i < labels.length (); i++)
1021 : 0 : gimple_switch_set_label (stmt, i + 1, labels[i]);
1022 : 0 : for (i++ ; i < branch_num; i++)
1023 : 0 : gimple_switch_set_label (stmt, i, NULL_TREE);
1024 : 0 : gimple_switch_set_num_labels (stmt, len + 1);
1025 : :
1026 : : /* Cleanup any edges that are now dead. */
1027 : 0 : target_blocks = BITMAP_ALLOC (NULL);
1028 : 0 : for (i = 0; i < gimple_switch_num_labels (stmt); i++)
1029 : : {
1030 : 0 : tree elt = gimple_switch_label (stmt, i);
1031 : 0 : basic_block target = label_to_block (cfun, CASE_LABEL (elt));
1032 : 0 : bitmap_set_bit (target_blocks, target->index);
1033 : : }
1034 : 0 : for (ei = ei_start (gimple_bb (stmt)->succs); (e = ei_safe_edge (ei)); )
1035 : : {
1036 : 0 : if (! bitmap_bit_p (target_blocks, e->dest->index))
1037 : 0 : edges_to_remove.safe_push (std::make_pair (e->src->index,
1038 : 0 : e->dest->index));
1039 : : else
1040 : 0 : ei_next (&ei);
1041 : : }
1042 : 0 : BITMAP_FREE (target_blocks);
1043 : : }
1044 : 11773 : }
1045 : :
1046 : : /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
1047 : : the condition which we may be able to optimize better. */
1048 : :
1049 : : static bool
1050 : 117600 : simplify_gimple_switch (gswitch *stmt,
1051 : : vec<std::pair<int, int> > &edges_to_remove,
1052 : : bitmap simple_dce_worklist)
1053 : : {
1054 : : /* The optimization that we really care about is removing unnecessary
1055 : : casts. That will let us do much better in propagating the inferred
1056 : : constant at the switch target. */
1057 : 117600 : tree cond = gimple_switch_index (stmt);
1058 : 117600 : if (TREE_CODE (cond) == SSA_NAME)
1059 : : {
1060 : 117599 : gimple *def_stmt = SSA_NAME_DEF_STMT (cond);
1061 : 117599 : if (gimple_assign_cast_p (def_stmt))
1062 : : {
1063 : 12268 : tree def = gimple_assign_rhs1 (def_stmt);
1064 : 12268 : if (TREE_CODE (def) != SSA_NAME)
1065 : : return false;
1066 : :
1067 : : /* If we have an extension or sign-change that preserves the
1068 : : values we check against then we can copy the source value into
1069 : : the switch. */
1070 : 12268 : tree ti = TREE_TYPE (def);
1071 : 12268 : if (INTEGRAL_TYPE_P (ti)
1072 : 12268 : && TYPE_PRECISION (ti) <= TYPE_PRECISION (TREE_TYPE (cond)))
1073 : : {
1074 : 12024 : size_t n = gimple_switch_num_labels (stmt);
1075 : 12024 : tree min = NULL_TREE, max = NULL_TREE;
1076 : 12024 : if (n > 1)
1077 : : {
1078 : 12024 : min = CASE_LOW (gimple_switch_label (stmt, 1));
1079 : 12024 : if (CASE_HIGH (gimple_switch_label (stmt, n - 1)))
1080 : 149 : max = CASE_HIGH (gimple_switch_label (stmt, n - 1));
1081 : : else
1082 : 11875 : max = CASE_LOW (gimple_switch_label (stmt, n - 1));
1083 : : }
1084 : 12024 : if ((!min || int_fits_type_p (min, ti))
1085 : 12020 : && (!max || int_fits_type_p (max, ti)))
1086 : : {
1087 : 11773 : bitmap_set_bit (simple_dce_worklist,
1088 : 11773 : SSA_NAME_VERSION (cond));
1089 : 11773 : gimple_switch_set_index (stmt, def);
1090 : 11773 : simplify_gimple_switch_label_vec (stmt, ti,
1091 : : edges_to_remove);
1092 : 11773 : update_stmt (stmt);
1093 : 11773 : return true;
1094 : : }
1095 : : }
1096 : : }
1097 : : }
1098 : :
1099 : : return false;
1100 : : }
1101 : :
1102 : : /* For pointers p2 and p1 return p2 - p1 if the
1103 : : difference is known and constant, otherwise return NULL. */
1104 : :
1105 : : static tree
1106 : 5112 : constant_pointer_difference (tree p1, tree p2)
1107 : : {
1108 : 5112 : int i, j;
1109 : : #define CPD_ITERATIONS 5
1110 : 5112 : tree exps[2][CPD_ITERATIONS];
1111 : 5112 : tree offs[2][CPD_ITERATIONS];
1112 : 5112 : int cnt[2];
1113 : :
1114 : 15336 : for (i = 0; i < 2; i++)
1115 : : {
1116 : 10224 : tree p = i ? p1 : p2;
1117 : 10224 : tree off = size_zero_node;
1118 : 10224 : gimple *stmt;
1119 : 10224 : enum tree_code code;
1120 : :
1121 : : /* For each of p1 and p2 we need to iterate at least
1122 : : twice, to handle ADDR_EXPR directly in p1/p2,
1123 : : SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc.
1124 : : on definition's stmt RHS. Iterate a few extra times. */
1125 : 10224 : j = 0;
1126 : 12005 : do
1127 : : {
1128 : 12005 : if (!POINTER_TYPE_P (TREE_TYPE (p)))
1129 : : break;
1130 : 11999 : if (TREE_CODE (p) == ADDR_EXPR)
1131 : : {
1132 : 8784 : tree q = TREE_OPERAND (p, 0);
1133 : 8784 : poly_int64 offset;
1134 : 8784 : tree base = get_addr_base_and_unit_offset (q, &offset);
1135 : 8784 : if (base)
1136 : : {
1137 : 8054 : q = base;
1138 : 8054 : if (maybe_ne (offset, 0))
1139 : 3341 : off = size_binop (PLUS_EXPR, off, size_int (offset));
1140 : : }
1141 : 8784 : if (TREE_CODE (q) == MEM_REF
1142 : 8784 : && TREE_CODE (TREE_OPERAND (q, 0)) == SSA_NAME)
1143 : : {
1144 : 213 : p = TREE_OPERAND (q, 0);
1145 : 213 : off = size_binop (PLUS_EXPR, off,
1146 : : wide_int_to_tree (sizetype,
1147 : : mem_ref_offset (q)));
1148 : : }
1149 : : else
1150 : : {
1151 : 8571 : exps[i][j] = q;
1152 : 8571 : offs[i][j++] = off;
1153 : 8571 : break;
1154 : : }
1155 : : }
1156 : 3428 : if (TREE_CODE (p) != SSA_NAME)
1157 : : break;
1158 : 3428 : exps[i][j] = p;
1159 : 3428 : offs[i][j++] = off;
1160 : 3428 : if (j == CPD_ITERATIONS)
1161 : : break;
1162 : 3428 : stmt = SSA_NAME_DEF_STMT (p);
1163 : 3428 : if (!is_gimple_assign (stmt) || gimple_assign_lhs (stmt) != p)
1164 : : break;
1165 : 2621 : code = gimple_assign_rhs_code (stmt);
1166 : 2621 : if (code == POINTER_PLUS_EXPR)
1167 : : {
1168 : 1373 : if (TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
1169 : : break;
1170 : 882 : off = size_binop (PLUS_EXPR, off, gimple_assign_rhs2 (stmt));
1171 : 882 : p = gimple_assign_rhs1 (stmt);
1172 : : }
1173 : 1248 : else if (code == ADDR_EXPR || CONVERT_EXPR_CODE_P (code))
1174 : 899 : p = gimple_assign_rhs1 (stmt);
1175 : : else
1176 : : break;
1177 : : }
1178 : : while (1);
1179 : 10224 : cnt[i] = j;
1180 : : }
1181 : :
1182 : 7057 : for (i = 0; i < cnt[0]; i++)
1183 : 9243 : for (j = 0; j < cnt[1]; j++)
1184 : 7298 : if (exps[0][i] == exps[1][j])
1185 : 4241 : return size_binop (MINUS_EXPR, offs[0][i], offs[1][j]);
1186 : :
1187 : : return NULL_TREE;
1188 : : }
1189 : :
1190 : : /* Helper function for optimize_aggr_zeroprop.
1191 : : Props the zeroing (memset, VAL) that was done in DEST+OFFSET:LEN
1192 : : (DEFSTMT) into the STMT. Returns true if the STMT was updated. */
1193 : : static void
1194 : 21247968 : optimize_aggr_zeroprop_1 (gimple *defstmt, gimple *stmt,
1195 : : tree dest, poly_int64 offset, tree val,
1196 : : poly_offset_int len)
1197 : : {
1198 : 21247968 : tree src2;
1199 : 21247968 : tree len2 = NULL_TREE;
1200 : 21247968 : poly_int64 offset2;
1201 : :
1202 : 21247968 : if (gimple_call_builtin_p (stmt, BUILT_IN_MEMCPY)
1203 : 15763 : && TREE_CODE (gimple_call_arg (stmt, 1)) == ADDR_EXPR
1204 : 21259896 : && poly_int_tree_p (gimple_call_arg (stmt, 2)))
1205 : : {
1206 : 10900 : src2 = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
1207 : 10900 : len2 = gimple_call_arg (stmt, 2);
1208 : : }
1209 : 21237068 : else if (gimple_assign_load_p (stmt) && gimple_store_p (stmt))
1210 : : {
1211 : 1881506 : src2 = gimple_assign_rhs1 (stmt);
1212 : 1881506 : len2 = (TREE_CODE (src2) == COMPONENT_REF
1213 : 1881506 : ? DECL_SIZE_UNIT (TREE_OPERAND (src2, 1))
1214 : 1709536 : : TYPE_SIZE_UNIT (TREE_TYPE (src2)));
1215 : : /* Can only handle zero memsets. */
1216 : 1881506 : if (!integer_zerop (val))
1217 : 21225429 : return;
1218 : : }
1219 : : else
1220 : 19355562 : return;
1221 : :
1222 : 1891474 : if (len2 == NULL_TREE
1223 : 1891474 : || !poly_int_tree_p (len2))
1224 : : return;
1225 : :
1226 : 1891474 : src2 = get_addr_base_and_unit_offset (src2, &offset2);
1227 : 1891474 : if (src2 == NULL_TREE
1228 : 1891474 : || maybe_lt (offset2, offset))
1229 : : return;
1230 : :
1231 : 860204 : if (!operand_equal_p (dest, src2, 0))
1232 : : return;
1233 : :
1234 : : /* [ dest + offset, dest + offset + len - 1 ] is set to val.
1235 : : Make sure that
1236 : : [ dest + offset2, dest + offset2 + len2 - 1 ] is a subset of that. */
1237 : 132721 : if (maybe_gt (wi::to_poly_offset (len2) + (offset2 - offset),
1238 : : len))
1239 : : return;
1240 : :
1241 : 22539 : if (dump_file && (dump_flags & TDF_DETAILS))
1242 : : {
1243 : 32 : fprintf (dump_file, "Simplified\n ");
1244 : 32 : print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1245 : 32 : fprintf (dump_file, "after previous\n ");
1246 : 32 : print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
1247 : : }
1248 : 22539 : gimple *orig_stmt = stmt;
1249 : : /* For simplicity, don't change the kind of the stmt,
1250 : : turn dest = src; into dest = {}; and memcpy (&dest, &src, len);
1251 : : into memset (&dest, val, len);
1252 : : In theory we could change dest = src into memset if dest
1253 : : is addressable (maybe beneficial if val is not 0), or
1254 : : memcpy (&dest, &src, len) into dest = {} if len is the size
1255 : : of dest, dest isn't volatile. */
1256 : 22539 : if (is_gimple_assign (stmt))
1257 : : {
1258 : 22534 : tree ctor_type = TREE_TYPE (gimple_assign_lhs (stmt));
1259 : 22534 : tree ctor = build_constructor (ctor_type, NULL);
1260 : 22534 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1261 : 22534 : gimple_assign_set_rhs_from_tree (&gsi, ctor);
1262 : 22534 : update_stmt (stmt);
1263 : 22534 : statistics_counter_event (cfun, "copy zeroing propagation of aggregate", 1);
1264 : : }
1265 : : else /* If stmt is memcpy, transform it into memset. */
1266 : : {
1267 : 5 : gcall *call = as_a <gcall *> (stmt);
1268 : 5 : tree fndecl = builtin_decl_implicit (BUILT_IN_MEMSET);
1269 : 5 : gimple_call_set_fndecl (call, fndecl);
1270 : 5 : gimple_call_set_fntype (call, TREE_TYPE (fndecl));
1271 : 5 : gimple_call_set_arg (call, 1, val);
1272 : 5 : update_stmt (stmt);
1273 : 5 : statistics_counter_event (cfun, "memcpy to memset changed", 1);
1274 : : }
1275 : :
1276 : 22539 : if (dump_file && (dump_flags & TDF_DETAILS))
1277 : : {
1278 : 32 : fprintf (dump_file, "into\n ");
1279 : 32 : print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1280 : : }
1281 : :
1282 : : /* Mark the bb for eh cleanup if needed. */
1283 : 22539 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
1284 : 6 : bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
1285 : : }
1286 : :
1287 : : /* Optimize
1288 : : a = {}; // DEST = value ;; LEN(nullptr)
1289 : : b = a;
1290 : : into
1291 : : a = {};
1292 : : b = {};
1293 : : Similarly for memset (&a, ..., sizeof (a)); instead of a = {};
1294 : : and/or memcpy (&b, &a, sizeof (a)); instead of b = a; */
1295 : :
1296 : : static void
1297 : 39557604 : optimize_aggr_zeroprop (gimple *stmt, bool full_walk)
1298 : : {
1299 : 39557604 : ao_ref read;
1300 : 79115208 : if (gimple_has_volatile_ops (stmt))
1301 : 35482827 : return;
1302 : :
1303 : 30080017 : tree dest = NULL_TREE;
1304 : 30080017 : tree val = integer_zero_node;
1305 : 30080017 : tree len = NULL_TREE;
1306 : 30080017 : bool can_use_tbba = true;
1307 : :
1308 : 30080017 : if (gimple_call_builtin_p (stmt, BUILT_IN_MEMSET)
1309 : 109745 : && TREE_CODE (gimple_call_arg (stmt, 0)) == ADDR_EXPR
1310 : 53915 : && TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST
1311 : 30131333 : && poly_int_tree_p (gimple_call_arg (stmt, 2)))
1312 : : {
1313 : 48568 : dest = TREE_OPERAND (gimple_call_arg (stmt, 0), 0);
1314 : 48568 : len = gimple_call_arg (stmt, 2);
1315 : 48568 : val = gimple_call_arg (stmt, 1);
1316 : 48568 : ao_ref_init_from_ptr_and_size (&read, gimple_call_arg (stmt, 0), len);
1317 : 48568 : can_use_tbba = false;
1318 : : }
1319 : 30031449 : else if (gimple_store_p (stmt)
1320 : 29970112 : && gimple_assign_single_p (stmt)
1321 : 60001561 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == STRING_CST)
1322 : : {
1323 : 40171 : tree str = gimple_assign_rhs1 (stmt);
1324 : 40171 : dest = gimple_assign_lhs (stmt);
1325 : 40171 : ao_ref_init (&read, dest);
1326 : : /* The string must contain all null char's for now. */
1327 : 45794 : for (int i = 0; i < TREE_STRING_LENGTH (str); i++)
1328 : : {
1329 : 42923 : if (TREE_STRING_POINTER (str)[i] != 0)
1330 : : {
1331 : : dest = NULL_TREE;
1332 : : break;
1333 : : }
1334 : : }
1335 : : }
1336 : : /* A store of integer (scalar, vector or complex) zeros is
1337 : : a zero store. */
1338 : 29991278 : else if (gimple_store_p (stmt)
1339 : 29929941 : && gimple_assign_single_p (stmt)
1340 : 59921219 : && integer_zerop (gimple_assign_rhs1 (stmt)))
1341 : : {
1342 : 3645636 : tree rhs = gimple_assign_rhs1 (stmt);
1343 : 3645636 : tree type = TREE_TYPE (rhs);
1344 : 3645636 : dest = gimple_assign_lhs (stmt);
1345 : 3645636 : ao_ref_init (&read, dest);
1346 : : /* For integral types, the type precision needs to be a multiply of BITS_PER_UNIT. */
1347 : 3645636 : if (INTEGRAL_TYPE_P (type)
1348 : 3645636 : && (TYPE_PRECISION (type) % BITS_PER_UNIT) != 0)
1349 : : dest = NULL_TREE;
1350 : : }
1351 : 26345642 : else if (gimple_store_p (stmt)
1352 : 26284305 : && gimple_assign_single_p (stmt)
1353 : 26284305 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == CONSTRUCTOR
1354 : 27044510 : && !gimple_clobber_p (stmt))
1355 : : {
1356 : 698868 : dest = gimple_assign_lhs (stmt);
1357 : 698868 : ao_ref_init (&read, dest);
1358 : : }
1359 : :
1360 : 4195517 : if (dest == NULL_TREE)
1361 : 25921800 : return;
1362 : :
1363 : 4158217 : if (len == NULL_TREE)
1364 : 4109649 : len = (TREE_CODE (dest) == COMPONENT_REF
1365 : 4109649 : ? DECL_SIZE_UNIT (TREE_OPERAND (dest, 1))
1366 : 1745450 : : TYPE_SIZE_UNIT (TREE_TYPE (dest)));
1367 : 4109649 : if (len == NULL_TREE
1368 : 4158217 : || !poly_int_tree_p (len))
1369 : : return;
1370 : :
1371 : : /* This store needs to be on the byte boundary and pointing to an object. */
1372 : 4158217 : poly_int64 offset;
1373 : 4158217 : tree dest_base = get_addr_base_and_unit_offset (dest, &offset);
1374 : 4158217 : if (dest_base == NULL_TREE)
1375 : : return;
1376 : :
1377 : : /* Setup the worklist. */
1378 : 4074777 : auto_vec<std::pair<tree, unsigned>> worklist;
1379 : 4074777 : unsigned limit = full_walk ? param_sccvn_max_alias_queries_per_access : 0;
1380 : 8149554 : worklist.safe_push (std::make_pair (gimple_vdef (stmt), limit));
1381 : :
1382 : 26957267 : while (!worklist.is_empty ())
1383 : : {
1384 : 18807713 : std::pair<tree, unsigned> top = worklist.pop ();
1385 : 18807713 : tree vdef = top.first;
1386 : 18807713 : limit = top.second;
1387 : 18807713 : gimple *use_stmt;
1388 : 18807713 : imm_use_iterator iter;
1389 : 42045242 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
1390 : : {
1391 : : /* Handling PHI nodes might not be worth it so don't. */
1392 : 23237529 : if (is_a <gphi*> (use_stmt))
1393 : 1989561 : continue;
1394 : :
1395 : : /* If this statement does not clobber add the vdef stmt to the
1396 : : worklist.
1397 : : After hitting the limit, allow clobbers to able to pass through. */
1398 : 2144304 : if ((limit != 0 || gimple_clobber_p (use_stmt))
1399 : 19241713 : && gimple_vdef (use_stmt)
1400 : 37434292 : && !stmt_may_clobber_ref_p_1 (use_stmt, &read,
1401 : : /* tbaa_p = */ can_use_tbba))
1402 : : {
1403 : 14732936 : unsigned new_limit = limit == 0 ? 0 : limit - 1;
1404 : 29465872 : worklist.safe_push (std::make_pair (gimple_vdef (use_stmt),
1405 : : new_limit));
1406 : : }
1407 : :
1408 : 21247968 : optimize_aggr_zeroprop_1 (stmt, use_stmt, dest_base, offset,
1409 : 21247968 : val, wi::to_poly_offset (len));
1410 : 18807713 : }
1411 : : }
1412 : :
1413 : 4074777 : }
1414 : :
1415 : : /* Returns the pointer to the base of the object of the
1416 : : reference EXPR and extracts the information about
1417 : : the offset of the access, storing it to PBYTESIZE,
1418 : : PBYTEPOS and PREVERSEP.
1419 : : If the access is not a byte sized or position is not
1420 : : on the byte, return NULL. */
1421 : : static tree
1422 : 5275092 : split_core_and_offset_size (tree expr,
1423 : : poly_int64 *pbytesize, poly_int64 *pbytepos,
1424 : : tree *poffset, int *preversep)
1425 : : {
1426 : 5275092 : tree core;
1427 : 5275092 : machine_mode mode;
1428 : 5275092 : int unsignedp, volatilep;
1429 : 5275092 : poly_int64 bitsize;
1430 : 5275092 : poly_int64 bitpos;
1431 : 5275092 : location_t loc = EXPR_LOCATION (expr);
1432 : :
1433 : 5275092 : core = get_inner_reference (expr, &bitsize, &bitpos,
1434 : : poffset, &mode, &unsignedp, preversep,
1435 : : &volatilep);
1436 : 10550184 : if (!multiple_p (bitsize, BITS_PER_UNIT, pbytesize))
1437 : : return NULL_TREE;
1438 : 5275092 : if (!multiple_p (bitpos, BITS_PER_UNIT, pbytepos))
1439 : : return NULL_TREE;
1440 : : /* If we are left with MEM[a + CST] strip that and add it to the
1441 : : pbytepos and return a. */
1442 : 5275092 : if (TREE_CODE (core) == MEM_REF)
1443 : : {
1444 : 1251215 : poly_offset_int tem;
1445 : 1251215 : tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
1446 : 1251215 : tem += *pbytepos;
1447 : 1251215 : if (tem.to_shwi (pbytepos))
1448 : 1249561 : return TREE_OPERAND (core, 0);
1449 : : }
1450 : 4025531 : core = build_fold_addr_expr_loc (loc, core);
1451 : 4025531 : STRIP_NOPS (core);
1452 : 4025531 : return core;
1453 : : }
1454 : :
1455 : : /* Returns a new src based on the
1456 : : copy `DEST = SRC` and for the old SRC2.
1457 : : Returns null if SRC2 is not related to DEST. */
1458 : :
1459 : : static tree
1460 : 1206376 : new_src_based_on_copy (tree src2, tree dest, tree src)
1461 : : {
1462 : : /* If the second src is not exactly the same as dest,
1463 : : try to handle it seperately; see it is address/size equivalent.
1464 : : Handles `a` and `a.b` and `MEM<char[N]>(&a)` which all have
1465 : : the same size and offsets as address/size equivalent.
1466 : : This allows copying over a memcpy and also one for copying
1467 : : where one field is the same size as the whole struct. */
1468 : 1206376 : if (operand_equal_p (dest, src2))
1469 : : return src;
1470 : : /* if both dest and src2 are decls, then we know these 2
1471 : : accesses can't be the same. */
1472 : 715432 : if (DECL_P (dest) && DECL_P (src2))
1473 : : return NULL_TREE;
1474 : : /* A VCE can't be used with imag/real or BFR so reject them early. */
1475 : 369897 : if (TREE_CODE (src) == IMAGPART_EXPR
1476 : 369897 : || TREE_CODE (src) == REALPART_EXPR
1477 : 369897 : || TREE_CODE (src) == BIT_FIELD_REF)
1478 : : return NULL_TREE;
1479 : 369897 : tree core1, core2;
1480 : 369897 : poly_int64 bytepos1, bytepos2;
1481 : 369897 : poly_int64 bytesize1, bytesize2;
1482 : 369897 : tree toffset1, toffset2;
1483 : 369897 : int reversep1 = 0;
1484 : 369897 : int reversep2 = 0;
1485 : 369897 : poly_int64 diff = 0;
1486 : 369897 : core1 = split_core_and_offset_size (dest, &bytesize1, &bytepos1,
1487 : : &toffset1, &reversep1);
1488 : 369897 : core2 = split_core_and_offset_size (src2, &bytesize2, &bytepos2,
1489 : : &toffset2, &reversep2);
1490 : 369897 : if (!core1 || !core2)
1491 : : return NULL_TREE;
1492 : 369897 : if (reversep1 != reversep2)
1493 : : return NULL_TREE;
1494 : : /* The sizes of the 2 accesses need to be the same. */
1495 : 369897 : if (!known_eq (bytesize1, bytesize2))
1496 : : return NULL_TREE;
1497 : 160036 : if (!operand_equal_p (core1, core2, 0))
1498 : : return NULL_TREE;
1499 : :
1500 : 22714 : if (toffset1 && toffset2)
1501 : : {
1502 : 2 : tree type = TREE_TYPE (toffset1);
1503 : 2 : if (type != TREE_TYPE (toffset2))
1504 : 0 : toffset2 = fold_convert (type, toffset2);
1505 : :
1506 : 2 : tree tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
1507 : 2 : if (!cst_and_fits_in_hwi (tdiff))
1508 : : return NULL_TREE;
1509 : :
1510 : 0 : diff = int_cst_value (tdiff);
1511 : 0 : }
1512 : 22712 : else if (toffset1 || toffset2)
1513 : : {
1514 : : /* If only one of the offsets is non-constant, the difference cannot
1515 : : be a constant. */
1516 : : return NULL_TREE;
1517 : : }
1518 : 22680 : diff += bytepos1 - bytepos2;
1519 : : /* The offset between the 2 need to be 0. */
1520 : 22680 : if (!known_eq (diff, 0))
1521 : : return NULL_TREE;
1522 : 21955 : return fold_build1 (VIEW_CONVERT_EXPR,TREE_TYPE (src2), src);
1523 : : }
1524 : :
1525 : : /* Returns true if SRC and DEST are the same address such that
1526 : : `SRC == DEST;` is considered a nop. This is more than an
1527 : : operand_equal_p check as it needs to be similar to
1528 : : new_src_based_on_copy. */
1529 : :
1530 : : static bool
1531 : 4454853 : same_for_assignment (tree src, tree dest)
1532 : : {
1533 : 4454853 : if (operand_equal_p (dest, src, 0))
1534 : : return true;
1535 : : /* if both dest and src2 are decls, then we know these 2
1536 : : accesses can't be the same. */
1537 : 4451993 : if (DECL_P (dest) && DECL_P (src))
1538 : : return false;
1539 : :
1540 : 2267649 : tree core1, core2;
1541 : 2267649 : poly_int64 bytepos1, bytepos2;
1542 : 2267649 : poly_int64 bytesize1, bytesize2;
1543 : 2267649 : tree toffset1, toffset2;
1544 : 2267649 : int reversep1 = 0;
1545 : 2267649 : int reversep2 = 0;
1546 : 2267649 : poly_int64 diff = 0;
1547 : 2267649 : core1 = split_core_and_offset_size (dest, &bytesize1, &bytepos1,
1548 : : &toffset1, &reversep1);
1549 : 2267649 : core2 = split_core_and_offset_size (src, &bytesize2, &bytepos2,
1550 : : &toffset2, &reversep2);
1551 : 2267649 : if (!core1 || !core2)
1552 : : return false;
1553 : 2267649 : if (reversep1 != reversep2)
1554 : : return false;
1555 : : /* The sizes of the 2 accesses need to be the same. */
1556 : 2267649 : if (!known_eq (bytesize1, bytesize2))
1557 : : return false;
1558 : 2266563 : if (!operand_equal_p (core1, core2, 0))
1559 : : return false;
1560 : 6688 : if (toffset1 && toffset2)
1561 : : {
1562 : 343 : tree type = TREE_TYPE (toffset1);
1563 : 343 : if (type != TREE_TYPE (toffset2))
1564 : 0 : toffset2 = fold_convert (type, toffset2);
1565 : :
1566 : 343 : tree tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
1567 : 343 : if (!cst_and_fits_in_hwi (tdiff))
1568 : : return false;
1569 : :
1570 : 0 : diff = int_cst_value (tdiff);
1571 : 0 : }
1572 : 6345 : else if (toffset1 || toffset2)
1573 : : {
1574 : : /* If only one of the offsets is non-constant, the difference cannot
1575 : : be a constant. */
1576 : : return false;
1577 : : }
1578 : 6345 : diff += bytepos1 - bytepos2;
1579 : : /* The offset between the 2 need to be 0. */
1580 : 6345 : if (!known_eq (diff, 0))
1581 : : return false;
1582 : : return true;
1583 : : }
1584 : :
1585 : : /* Helper function for optimize_agr_copyprop.
1586 : : For aggregate copies in USE_STMT, see if DEST
1587 : : is on the lhs of USE_STMT and replace it with SRC. */
1588 : : static void
1589 : 1007370 : optimize_agr_copyprop_1 (gimple *stmt, gimple *use_stmt,
1590 : : tree dest, tree src)
1591 : : {
1592 : 1007370 : gcc_assert (gimple_assign_load_p (use_stmt)
1593 : : && gimple_store_p (use_stmt));
1594 : 2014740 : if (gimple_has_volatile_ops (use_stmt))
1595 : 599919 : return;
1596 : 1007369 : tree dest2 = gimple_assign_lhs (use_stmt);
1597 : 1007369 : tree src2 = gimple_assign_rhs1 (use_stmt);
1598 : : /* If the new store is `src2 = src2;` skip over it. */
1599 : 1007369 : if (same_for_assignment (src2, dest2))
1600 : : return;
1601 : 1006823 : src = new_src_based_on_copy (src2, dest, src);
1602 : 1006823 : if (!src)
1603 : : return;
1604 : : /* For 2 memory refences and using a temporary to do the copy,
1605 : : don't remove the temporary as the 2 memory references might overlap.
1606 : : Note t does not need to be decl as it could be field.
1607 : : See PR 22237 for full details.
1608 : : E.g.
1609 : : t = *a; #DEST = SRC;
1610 : : *b = t; #DEST2 = SRC2;
1611 : : Cannot be convert into
1612 : : t = *a;
1613 : : *b = *a;
1614 : : Though the following is allowed to be done:
1615 : : t = *a;
1616 : : *a = t;
1617 : : And convert it into:
1618 : : t = *a;
1619 : : *a = *a;
1620 : : */
1621 : 431400 : if (!operand_equal_p (dest2, src, 0)
1622 : 431400 : && !DECL_P (dest2) && !DECL_P (src))
1623 : : {
1624 : : /* If *a and *b have the same base see if
1625 : : the offset between the two is greater than
1626 : : or equal to the size of the type. */
1627 : 26838 : poly_int64 offset1, offset2;
1628 : 26838 : tree len = TYPE_SIZE_UNIT (TREE_TYPE (src));
1629 : 26838 : if (len == NULL_TREE
1630 : 26838 : || !tree_fits_poly_int64_p (len))
1631 : 23949 : return;
1632 : 26838 : tree base1 = get_addr_base_and_unit_offset (dest2, &offset1);
1633 : 26838 : tree base2 = get_addr_base_and_unit_offset (src, &offset2);
1634 : 26838 : poly_int64 size = tree_to_poly_int64 (len);
1635 : : /* If the bases are 2 different decls,
1636 : : then there can be no overlapping. */
1637 : 26838 : if (base1 && base2
1638 : 26788 : && DECL_P (base1) && DECL_P (base2)
1639 : 1520 : && base1 != base2)
1640 : : ;
1641 : : /* If we can't figure out the base or the bases are
1642 : : not equal then fall back to an alignment check. */
1643 : 25616 : else if (!base1
1644 : 25616 : || !base2
1645 : 25616 : || !operand_equal_p (base1, base2))
1646 : : {
1647 : 25177 : unsigned int align1 = get_object_alignment (src);
1648 : 25177 : unsigned int align2 = get_object_alignment (dest2);
1649 : 25177 : align1 /= BITS_PER_UNIT;
1650 : 25177 : align2 /= BITS_PER_UNIT;
1651 : : /* If the alignment of either object is less
1652 : : than the size then there is a possibility
1653 : : of overlapping. */
1654 : 25177 : if (maybe_lt (align1, size)
1655 : 25177 : || maybe_lt (align2, size))
1656 : 23949 : return;
1657 : : }
1658 : : /* Make sure [offset1, offset1 + len - 1] does
1659 : : not overlap with [offset2, offset2 + len - 1],
1660 : : it is ok if they are at the same location though. */
1661 : 439 : else if (ranges_maybe_overlap_p (offset1, size, offset2, size)
1662 : 439 : && !known_eq (offset2, offset1))
1663 : : return;
1664 : : }
1665 : :
1666 : 407451 : if (dump_file && (dump_flags & TDF_DETAILS))
1667 : : {
1668 : 11 : fprintf (dump_file, "Simplified\n ");
1669 : 11 : print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
1670 : 11 : fprintf (dump_file, "after previous\n ");
1671 : 11 : print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1672 : : }
1673 : 407451 : gimple *orig_stmt = use_stmt;
1674 : 407451 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1675 : 407451 : gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (src));
1676 : 407451 : update_stmt (use_stmt);
1677 : :
1678 : 407451 : if (dump_file && (dump_flags & TDF_DETAILS))
1679 : : {
1680 : 11 : fprintf (dump_file, "into\n ");
1681 : 11 : print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
1682 : : }
1683 : 407451 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, use_stmt))
1684 : 0 : bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
1685 : 407451 : statistics_counter_event (cfun, "copy prop for aggregate", 1);
1686 : : }
1687 : :
1688 : : /* Helper function for optimize_agr_copyprop_1, propagate aggregates
1689 : : into the arguments of USE_STMT if the argument matches with DEST;
1690 : : replacing it with SRC. */
1691 : : static void
1692 : 704866 : optimize_agr_copyprop_arg (gimple *defstmt, gcall *call,
1693 : : tree dest, tree src)
1694 : : {
1695 : 704866 : bool changed = false;
1696 : 2326018 : for (unsigned arg = 0; arg < gimple_call_num_args (call); arg++)
1697 : : {
1698 : 1621152 : tree *argptr = gimple_call_arg_ptr (call, arg);
1699 : 3042751 : if (TREE_CODE (*argptr) == SSA_NAME
1700 : 927787 : || is_gimple_min_invariant (*argptr)
1701 : 1820705 : || TYPE_VOLATILE (TREE_TYPE (*argptr)))
1702 : 1421599 : continue;
1703 : 199553 : tree newsrc = new_src_based_on_copy (*argptr, dest, src);
1704 : 199553 : if (!newsrc)
1705 : 118054 : continue;
1706 : :
1707 : 81499 : if (dump_file && (dump_flags & TDF_DETAILS))
1708 : : {
1709 : 2 : fprintf (dump_file, "Simplified\n ");
1710 : 2 : print_gimple_stmt (dump_file, call, 0, dump_flags);
1711 : 2 : fprintf (dump_file, "after previous\n ");
1712 : 2 : print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
1713 : : }
1714 : 81499 : *argptr = unshare_expr (newsrc);
1715 : 81499 : changed = true;
1716 : 81499 : if (dump_file && (dump_flags & TDF_DETAILS))
1717 : : {
1718 : 2 : fprintf (dump_file, "into\n ");
1719 : 2 : print_gimple_stmt (dump_file, call, 0, dump_flags);
1720 : : }
1721 : : }
1722 : 704866 : if (changed)
1723 : 81454 : update_stmt (call);
1724 : 704866 : }
1725 : :
1726 : : /* Optimizes
1727 : : DEST = SRC;
1728 : : DEST2 = DEST; # DEST2 = SRC2;
1729 : : into
1730 : : DEST = SRC;
1731 : : DEST2 = SRC;
1732 : : STMT is the first statement and SRC is the common
1733 : : between the statements.
1734 : :
1735 : : Also optimizes:
1736 : : DEST = SRC;
1737 : : call_func(..., DEST, ...);
1738 : : into:
1739 : : DEST = SRC;
1740 : : call_func(..., SRC, ...);
1741 : :
1742 : : */
1743 : : static void
1744 : 3860969 : optimize_agr_copyprop (gimple *stmt)
1745 : : {
1746 : 7721938 : if (gimple_has_volatile_ops (stmt))
1747 : 416042 : return;
1748 : :
1749 : : /* Can't prop if the statement could throw. */
1750 : 3859867 : if (stmt_could_throw_p (cfun, stmt))
1751 : : return;
1752 : :
1753 : 3447484 : tree dest = gimple_assign_lhs (stmt);
1754 : 3447484 : tree src = gimple_assign_rhs1 (stmt);
1755 : : /* If the statement is `src = src;` then ignore it. */
1756 : 3447484 : if (same_for_assignment (dest, src))
1757 : : return;
1758 : :
1759 : 3444927 : tree vdef = gimple_vdef (stmt);
1760 : 3444927 : imm_use_iterator iter;
1761 : 3444927 : gimple *use_stmt;
1762 : 9949626 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
1763 : : {
1764 : 6504699 : if (gimple_assign_load_p (use_stmt)
1765 : 6504699 : && gimple_store_p (use_stmt))
1766 : 1007370 : optimize_agr_copyprop_1 (stmt, use_stmt, dest, src);
1767 : 5497329 : else if (is_gimple_call (use_stmt))
1768 : 704866 : optimize_agr_copyprop_arg (stmt, as_a<gcall*>(use_stmt), dest, src);
1769 : 3444927 : }
1770 : : }
1771 : :
1772 : : /* Optimizes builtin memcmps for small constant sizes.
1773 : : GSI_P is the GSI for the call. STMT is the call itself.
1774 : : */
1775 : :
1776 : : static bool
1777 : 367984 : simplify_builtin_memcmp (gimple_stmt_iterator *gsi_p, gcall *stmt)
1778 : : {
1779 : : /* Make sure memcmp arguments are the correct type. */
1780 : 367984 : if (gimple_call_num_args (stmt) != 3)
1781 : : return false;
1782 : 367984 : tree arg1 = gimple_call_arg (stmt, 0);
1783 : 367984 : tree arg2 = gimple_call_arg (stmt, 1);
1784 : 367984 : tree len = gimple_call_arg (stmt, 2);
1785 : :
1786 : 367984 : if (!POINTER_TYPE_P (TREE_TYPE (arg1)))
1787 : : return false;
1788 : 367984 : if (!POINTER_TYPE_P (TREE_TYPE (arg2)))
1789 : : return false;
1790 : 367984 : if (!INTEGRAL_TYPE_P (TREE_TYPE (len)))
1791 : : return false;
1792 : :
1793 : : /* The return value of the memcmp has to be used
1794 : : equality comparison to zero. */
1795 : 367984 : tree res = gimple_call_lhs (stmt);
1796 : :
1797 : 367984 : if (!res || !use_in_zero_equality (res))
1798 : 14107 : return false;
1799 : :
1800 : 353877 : unsigned HOST_WIDE_INT leni;
1801 : :
1802 : 353877 : if (tree_fits_uhwi_p (len)
1803 : 474676 : && (leni = tree_to_uhwi (len)) <= GET_MODE_SIZE (word_mode)
1804 : 414917 : && pow2p_hwi (leni))
1805 : : {
1806 : 18661 : leni *= CHAR_TYPE_SIZE;
1807 : 18661 : unsigned align1 = get_pointer_alignment (arg1);
1808 : 18661 : unsigned align2 = get_pointer_alignment (arg2);
1809 : 18661 : unsigned align = MIN (align1, align2);
1810 : 18661 : scalar_int_mode mode;
1811 : 18661 : if (int_mode_for_size (leni, 1).exists (&mode)
1812 : 18661 : && (align >= leni || !targetm.slow_unaligned_access (mode, align)))
1813 : : {
1814 : 18661 : location_t loc = gimple_location (stmt);
1815 : 18661 : tree type, off;
1816 : 18661 : type = build_nonstandard_integer_type (leni, 1);
1817 : 37322 : gcc_assert (known_eq (GET_MODE_BITSIZE (TYPE_MODE (type)), leni));
1818 : 18661 : tree ptrtype = build_pointer_type_for_mode (char_type_node,
1819 : : ptr_mode, true);
1820 : 18661 : off = build_int_cst (ptrtype, 0);
1821 : :
1822 : : /* Create unaligned types if needed. */
1823 : 18661 : tree type1 = type, type2 = type;
1824 : 18661 : if (TYPE_ALIGN (type1) > align1)
1825 : 7351 : type1 = build_aligned_type (type1, align1);
1826 : 18661 : if (TYPE_ALIGN (type2) > align2)
1827 : 7851 : type2 = build_aligned_type (type2, align2);
1828 : :
1829 : 18661 : arg1 = build2_loc (loc, MEM_REF, type1, arg1, off);
1830 : 18661 : arg2 = build2_loc (loc, MEM_REF, type2, arg2, off);
1831 : 18661 : tree tem1 = fold_const_aggregate_ref (arg1);
1832 : 18661 : if (tem1)
1833 : 219 : arg1 = tem1;
1834 : 18661 : tree tem2 = fold_const_aggregate_ref (arg2);
1835 : 18661 : if (tem2)
1836 : 7034 : arg2 = tem2;
1837 : 18661 : res = fold_convert_loc (loc, TREE_TYPE (res),
1838 : : fold_build2_loc (loc, NE_EXPR,
1839 : : boolean_type_node,
1840 : : arg1, arg2));
1841 : 18661 : gimplify_and_update_call_from_tree (gsi_p, res);
1842 : 18661 : return true;
1843 : : }
1844 : : }
1845 : : return false;
1846 : : }
1847 : :
1848 : : /* Optimizes builtin memchrs for small constant sizes with a const string.
1849 : : GSI_P is the GSI for the call. STMT is the call itself.
1850 : : */
1851 : :
1852 : : static bool
1853 : 11176 : simplify_builtin_memchr (gimple_stmt_iterator *gsi_p, gcall *stmt)
1854 : : {
1855 : 11176 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
1856 : : return false;
1857 : :
1858 : 11176 : if (gimple_call_num_args (stmt) != 3)
1859 : : return false;
1860 : :
1861 : 11176 : tree res = gimple_call_lhs (stmt);
1862 : 11176 : if (!res || !use_in_zero_equality (res))
1863 : 10123 : return false;
1864 : :
1865 : 1053 : tree ptr = gimple_call_arg (stmt, 0);
1866 : 1053 : if (TREE_CODE (ptr) != ADDR_EXPR
1867 : 1053 : || TREE_CODE (TREE_OPERAND (ptr, 0)) != STRING_CST)
1868 : : return false;
1869 : :
1870 : 145 : unsigned HOST_WIDE_INT slen
1871 : 145 : = TREE_STRING_LENGTH (TREE_OPERAND (ptr, 0));
1872 : : /* It must be a non-empty string constant. */
1873 : 145 : if (slen < 2)
1874 : : return false;
1875 : :
1876 : : /* For -Os, only simplify strings with a single character. */
1877 : 141 : if (!optimize_bb_for_speed_p (gimple_bb (stmt))
1878 : 141 : && slen > 2)
1879 : : return false;
1880 : :
1881 : 125 : tree size = gimple_call_arg (stmt, 2);
1882 : : /* Size must be a constant which is <= UNITS_PER_WORD and
1883 : : <= the string length. */
1884 : 125 : if (!tree_fits_uhwi_p (size))
1885 : : return false;
1886 : :
1887 : 125 : unsigned HOST_WIDE_INT sz = tree_to_uhwi (size);
1888 : 126 : if (sz == 0 || sz > UNITS_PER_WORD || sz >= slen)
1889 : : return false;
1890 : :
1891 : 73 : tree ch = gimple_call_arg (stmt, 1);
1892 : 73 : location_t loc = gimple_location (stmt);
1893 : 73 : if (!useless_type_conversion_p (char_type_node,
1894 : 73 : TREE_TYPE (ch)))
1895 : 73 : ch = fold_convert_loc (loc, char_type_node, ch);
1896 : 73 : const char *p = TREE_STRING_POINTER (TREE_OPERAND (ptr, 0));
1897 : 73 : unsigned int isize = sz;
1898 : 73 : tree *op = XALLOCAVEC (tree, isize);
1899 : 315 : for (unsigned int i = 0; i < isize; i++)
1900 : : {
1901 : 242 : op[i] = build_int_cst (char_type_node, p[i]);
1902 : 242 : op[i] = fold_build2_loc (loc, EQ_EXPR, boolean_type_node,
1903 : : op[i], ch);
1904 : : }
1905 : 242 : for (unsigned int i = isize - 1; i >= 1; i--)
1906 : 169 : op[i - 1] = fold_convert_loc (loc, boolean_type_node,
1907 : : fold_build2_loc (loc,
1908 : : BIT_IOR_EXPR,
1909 : : boolean_type_node,
1910 : 169 : op[i - 1],
1911 : 169 : op[i]));
1912 : 73 : res = fold_convert_loc (loc, TREE_TYPE (res), op[0]);
1913 : 73 : gimplify_and_update_call_from_tree (gsi_p, res);
1914 : 73 : return true;
1915 : : }
1916 : :
1917 : : /* *GSI_P is a GIMPLE_CALL to a builtin function.
1918 : : Optimize
1919 : : memcpy (p, "abcd", 4); // STMT1
1920 : : memset (p + 4, ' ', 3); // STMT2
1921 : : into
1922 : : memcpy (p, "abcd ", 7);
1923 : : call if the latter can be stored by pieces during expansion.
1924 : : */
1925 : :
1926 : : static bool
1927 : 109905 : simplify_builtin_memcpy_memset (gimple_stmt_iterator *gsi_p, gcall *stmt2)
1928 : : {
1929 : 109905 : if (gimple_call_num_args (stmt2) != 3
1930 : 109905 : || gimple_call_lhs (stmt2)
1931 : : || CHAR_BIT != 8
1932 : 109905 : || BITS_PER_UNIT != 8)
1933 : : return false;
1934 : :
1935 : 210641 : tree vuse = gimple_vuse (stmt2);
1936 : 103069 : if (vuse == NULL)
1937 : : return false;
1938 : 103069 : gimple *stmt1 = SSA_NAME_DEF_STMT (vuse);
1939 : :
1940 : 103069 : tree callee1;
1941 : 103069 : tree ptr1, src1, str1, off1, len1, lhs1;
1942 : 103069 : tree ptr2 = gimple_call_arg (stmt2, 0);
1943 : 103069 : tree val2 = gimple_call_arg (stmt2, 1);
1944 : 103069 : tree len2 = gimple_call_arg (stmt2, 2);
1945 : 103069 : tree diff, vdef, new_str_cst;
1946 : 103069 : gimple *use_stmt;
1947 : 103069 : unsigned int ptr1_align;
1948 : 103069 : unsigned HOST_WIDE_INT src_len;
1949 : 103069 : char *src_buf;
1950 : 103069 : use_operand_p use_p;
1951 : :
1952 : 103069 : if (!tree_fits_shwi_p (val2)
1953 : 99039 : || !tree_fits_uhwi_p (len2)
1954 : 163404 : || compare_tree_int (len2, 1024) == 1)
1955 : 47649 : return false;
1956 : :
1957 : 55420 : if (is_gimple_call (stmt1))
1958 : : {
1959 : : /* If first stmt is a call, it needs to be memcpy
1960 : : or mempcpy, with string literal as second argument and
1961 : : constant length. */
1962 : 28473 : callee1 = gimple_call_fndecl (stmt1);
1963 : 28473 : if (callee1 == NULL_TREE
1964 : 28366 : || !fndecl_built_in_p (callee1, BUILT_IN_NORMAL)
1965 : 53498 : || gimple_call_num_args (stmt1) != 3)
1966 : : return false;
1967 : 23771 : if (DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMCPY
1968 : 23771 : && DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMPCPY)
1969 : : return false;
1970 : 10552 : ptr1 = gimple_call_arg (stmt1, 0);
1971 : 10552 : src1 = gimple_call_arg (stmt1, 1);
1972 : 10552 : len1 = gimple_call_arg (stmt1, 2);
1973 : 10552 : lhs1 = gimple_call_lhs (stmt1);
1974 : 10552 : if (!tree_fits_uhwi_p (len1))
1975 : : return false;
1976 : 10467 : str1 = string_constant (src1, &off1, NULL, NULL);
1977 : 10467 : if (str1 == NULL_TREE)
1978 : : return false;
1979 : 4732 : if (!tree_fits_uhwi_p (off1)
1980 : 4732 : || compare_tree_int (off1, TREE_STRING_LENGTH (str1) - 1) > 0
1981 : 4732 : || compare_tree_int (len1, TREE_STRING_LENGTH (str1)
1982 : 4732 : - tree_to_uhwi (off1)) > 0
1983 : 4732 : || TREE_CODE (TREE_TYPE (str1)) != ARRAY_TYPE
1984 : 14196 : || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1)))
1985 : 4732 : != TYPE_MODE (char_type_node))
1986 : 0 : return false;
1987 : : }
1988 : 26947 : else if (gimple_assign_single_p (stmt1))
1989 : : {
1990 : : /* Otherwise look for length 1 memcpy optimized into
1991 : : assignment. */
1992 : 16345 : ptr1 = gimple_assign_lhs (stmt1);
1993 : 16345 : src1 = gimple_assign_rhs1 (stmt1);
1994 : 16345 : if (TREE_CODE (ptr1) != MEM_REF
1995 : 3035 : || TYPE_MODE (TREE_TYPE (ptr1)) != TYPE_MODE (char_type_node)
1996 : 17284 : || !tree_fits_shwi_p (src1))
1997 : 15972 : return false;
1998 : 373 : ptr1 = build_fold_addr_expr (ptr1);
1999 : 373 : STRIP_USELESS_TYPE_CONVERSION (ptr1);
2000 : 373 : callee1 = NULL_TREE;
2001 : 373 : len1 = size_one_node;
2002 : 373 : lhs1 = NULL_TREE;
2003 : 373 : off1 = size_zero_node;
2004 : 373 : str1 = NULL_TREE;
2005 : : }
2006 : : else
2007 : : return false;
2008 : :
2009 : 5105 : diff = constant_pointer_difference (ptr1, ptr2);
2010 : 5105 : if (diff == NULL && lhs1 != NULL)
2011 : : {
2012 : 7 : diff = constant_pointer_difference (lhs1, ptr2);
2013 : 7 : if (DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
2014 : 7 : && diff != NULL)
2015 : 7 : diff = size_binop (PLUS_EXPR, diff,
2016 : : fold_convert (sizetype, len1));
2017 : : }
2018 : : /* If the difference between the second and first destination pointer
2019 : : is not constant, or is bigger than memcpy length, bail out. */
2020 : 5105 : if (diff == NULL
2021 : 4241 : || !tree_fits_uhwi_p (diff)
2022 : 4241 : || tree_int_cst_lt (len1, diff)
2023 : 9108 : || compare_tree_int (diff, 1024) == 1)
2024 : 1102 : return false;
2025 : :
2026 : : /* Use maximum of difference plus memset length and memcpy length
2027 : : as the new memcpy length, if it is too big, bail out. */
2028 : 4003 : src_len = tree_to_uhwi (diff);
2029 : 4003 : src_len += tree_to_uhwi (len2);
2030 : 4003 : if (src_len < tree_to_uhwi (len1))
2031 : : src_len = tree_to_uhwi (len1);
2032 : 4003 : if (src_len > 1024)
2033 : : return false;
2034 : :
2035 : : /* If mempcpy value is used elsewhere, bail out, as mempcpy
2036 : : with bigger length will return different result. */
2037 : 4003 : if (lhs1 != NULL_TREE
2038 : 187 : && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
2039 : 4010 : && (TREE_CODE (lhs1) != SSA_NAME
2040 : 7 : || !single_imm_use (lhs1, &use_p, &use_stmt)
2041 : 7 : || use_stmt != stmt2))
2042 : 0 : return false;
2043 : :
2044 : : /* If anything reads memory in between memcpy and memset
2045 : : call, the modified memcpy call might change it. */
2046 : 4003 : vdef = gimple_vdef (stmt1);
2047 : 4003 : if (vdef != NULL
2048 : 4003 : && (!single_imm_use (vdef, &use_p, &use_stmt)
2049 : 3274 : || use_stmt != stmt2))
2050 : : return false;
2051 : :
2052 : 3274 : ptr1_align = get_pointer_alignment (ptr1);
2053 : : /* Construct the new source string literal. */
2054 : 3274 : src_buf = XALLOCAVEC (char, src_len + 1);
2055 : 3274 : if (callee1)
2056 : 3149 : memcpy (src_buf,
2057 : 3149 : TREE_STRING_POINTER (str1) + tree_to_uhwi (off1),
2058 : : tree_to_uhwi (len1));
2059 : : else
2060 : 125 : src_buf[0] = tree_to_shwi (src1);
2061 : 3274 : memset (src_buf + tree_to_uhwi (diff),
2062 : 3274 : tree_to_shwi (val2), tree_to_uhwi (len2));
2063 : 3274 : src_buf[src_len] = '\0';
2064 : : /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
2065 : : handle embedded '\0's. */
2066 : 3274 : if (strlen (src_buf) != src_len)
2067 : : return false;
2068 : 3200 : rtl_profile_for_bb (gimple_bb (stmt2));
2069 : : /* If the new memcpy wouldn't be emitted by storing the literal
2070 : : by pieces, this optimization might enlarge .rodata too much,
2071 : : as commonly used string literals couldn't be shared any
2072 : : longer. */
2073 : 3200 : if (!can_store_by_pieces (src_len,
2074 : : builtin_strncpy_read_str,
2075 : : src_buf, ptr1_align, false))
2076 : : return false;
2077 : :
2078 : 2440 : new_str_cst = build_string_literal (src_len, src_buf);
2079 : 2440 : if (callee1)
2080 : : {
2081 : : /* If STMT1 is a mem{,p}cpy call, adjust it and remove
2082 : : memset call. */
2083 : 2333 : if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
2084 : 7 : gimple_call_set_lhs (stmt1, NULL_TREE);
2085 : 2333 : gimple_call_set_arg (stmt1, 1, new_str_cst);
2086 : 2333 : gimple_call_set_arg (stmt1, 2,
2087 : 2333 : build_int_cst (TREE_TYPE (len1), src_len));
2088 : 2333 : update_stmt (stmt1);
2089 : 2333 : unlink_stmt_vdef (stmt2);
2090 : 2333 : gsi_replace (gsi_p, gimple_build_nop (), false);
2091 : 2333 : fwprop_invalidate_lattice (gimple_get_lhs (stmt2));
2092 : 2333 : release_defs (stmt2);
2093 : 2333 : if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
2094 : : {
2095 : 7 : fwprop_invalidate_lattice (lhs1);
2096 : 7 : release_ssa_name (lhs1);
2097 : : }
2098 : 2333 : return true;
2099 : : }
2100 : : else
2101 : : {
2102 : : /* Otherwise, if STMT1 is length 1 memcpy optimized into
2103 : : assignment, remove STMT1 and change memset call into
2104 : : memcpy call. */
2105 : 107 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt1);
2106 : :
2107 : 107 : if (!is_gimple_val (ptr1))
2108 : 12 : ptr1 = force_gimple_operand_gsi (gsi_p, ptr1, true, NULL_TREE,
2109 : : true, GSI_SAME_STMT);
2110 : 107 : tree fndecl = builtin_decl_explicit (BUILT_IN_MEMCPY);
2111 : 107 : gimple_call_set_fndecl (stmt2, fndecl);
2112 : 107 : gimple_call_set_fntype (stmt2,
2113 : 107 : TREE_TYPE (fndecl));
2114 : 107 : gimple_call_set_arg (stmt2, 0, ptr1);
2115 : 107 : gimple_call_set_arg (stmt2, 1, new_str_cst);
2116 : 107 : gimple_call_set_arg (stmt2, 2,
2117 : 107 : build_int_cst (TREE_TYPE (len2), src_len));
2118 : 107 : unlink_stmt_vdef (stmt1);
2119 : 107 : gsi_remove (&gsi, true);
2120 : 107 : fwprop_invalidate_lattice (gimple_get_lhs (stmt1));
2121 : 107 : release_defs (stmt1);
2122 : 107 : update_stmt (stmt2);
2123 : 107 : return false;
2124 : : }
2125 : : }
2126 : :
2127 : : /* *GSI_P is a GIMPLE_CALL to a builtin function.
2128 : : Optimize
2129 : : memcpy (p, "abcd", 4);
2130 : : memset (p + 4, ' ', 3);
2131 : : into
2132 : : memcpy (p, "abcd ", 7);
2133 : : call if the latter can be stored by pieces during expansion.
2134 : :
2135 : : Optimize
2136 : : memchr ("abcd", a, 4) == 0;
2137 : : or
2138 : : memchr ("abcd", a, 4) != 0;
2139 : : to
2140 : : (a == 'a' || a == 'b' || a == 'c' || a == 'd') == 0
2141 : : or
2142 : : (a == 'a' || a == 'b' || a == 'c' || a == 'd') != 0
2143 : :
2144 : : Also canonicalize __atomic_fetch_op (p, x, y) op x
2145 : : to __atomic_op_fetch (p, x, y) or
2146 : : __atomic_op_fetch (p, x, y) iop x
2147 : : to __atomic_fetch_op (p, x, y) when possible (also __sync). */
2148 : :
2149 : : static bool
2150 : 6023155 : simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2, bool full_walk)
2151 : : {
2152 : 6023155 : gimple *stmt2 = gsi_stmt (*gsi_p);
2153 : 6023155 : enum built_in_function other_atomic = END_BUILTINS;
2154 : 6023155 : enum tree_code atomic_op = ERROR_MARK;
2155 : :
2156 : 6023155 : switch (DECL_FUNCTION_CODE (callee2))
2157 : : {
2158 : 367984 : case BUILT_IN_MEMCMP:
2159 : 367984 : case BUILT_IN_MEMCMP_EQ:
2160 : 367984 : return simplify_builtin_memcmp (gsi_p, as_a<gcall*>(stmt2));
2161 : 11176 : case BUILT_IN_MEMCHR:
2162 : 11176 : return simplify_builtin_memchr (gsi_p, as_a<gcall*>(stmt2));
2163 : :
2164 : 109905 : case BUILT_IN_MEMSET:
2165 : 109905 : if (gimple_call_num_args (stmt2) == 3)
2166 : : {
2167 : : /* Try to prop the zeroing/value of the memset to memcpy
2168 : : if the dest is an address and the value is a constant. */
2169 : 109905 : optimize_aggr_zeroprop (stmt2, full_walk);
2170 : : }
2171 : 109905 : return simplify_builtin_memcpy_memset (gsi_p, as_a<gcall*>(stmt2));
2172 : :
2173 : : #define CASE_ATOMIC(NAME, OTHER, OP) \
2174 : : case BUILT_IN_##NAME##_1: \
2175 : : case BUILT_IN_##NAME##_2: \
2176 : : case BUILT_IN_##NAME##_4: \
2177 : : case BUILT_IN_##NAME##_8: \
2178 : : case BUILT_IN_##NAME##_16: \
2179 : : atomic_op = OP; \
2180 : : other_atomic \
2181 : : = (enum built_in_function) (BUILT_IN_##OTHER##_1 \
2182 : : + (DECL_FUNCTION_CODE (callee2) \
2183 : : - BUILT_IN_##NAME##_1)); \
2184 : : goto handle_atomic_fetch_op;
2185 : :
2186 : 51301 : CASE_ATOMIC (ATOMIC_FETCH_ADD, ATOMIC_ADD_FETCH, PLUS_EXPR)
2187 : 8067 : CASE_ATOMIC (ATOMIC_FETCH_SUB, ATOMIC_SUB_FETCH, MINUS_EXPR)
2188 : 2856 : CASE_ATOMIC (ATOMIC_FETCH_AND, ATOMIC_AND_FETCH, BIT_AND_EXPR)
2189 : 2875 : CASE_ATOMIC (ATOMIC_FETCH_XOR, ATOMIC_XOR_FETCH, BIT_XOR_EXPR)
2190 : 3889 : CASE_ATOMIC (ATOMIC_FETCH_OR, ATOMIC_OR_FETCH, BIT_IOR_EXPR)
2191 : :
2192 : 2355 : CASE_ATOMIC (SYNC_FETCH_AND_ADD, SYNC_ADD_AND_FETCH, PLUS_EXPR)
2193 : 1996 : CASE_ATOMIC (SYNC_FETCH_AND_SUB, SYNC_SUB_AND_FETCH, MINUS_EXPR)
2194 : 1868 : CASE_ATOMIC (SYNC_FETCH_AND_AND, SYNC_AND_AND_FETCH, BIT_AND_EXPR)
2195 : 2136 : CASE_ATOMIC (SYNC_FETCH_AND_XOR, SYNC_XOR_AND_FETCH, BIT_XOR_EXPR)
2196 : 1979 : CASE_ATOMIC (SYNC_FETCH_AND_OR, SYNC_OR_AND_FETCH, BIT_IOR_EXPR)
2197 : :
2198 : 14359 : CASE_ATOMIC (ATOMIC_ADD_FETCH, ATOMIC_FETCH_ADD, MINUS_EXPR)
2199 : 9113 : CASE_ATOMIC (ATOMIC_SUB_FETCH, ATOMIC_FETCH_SUB, PLUS_EXPR)
2200 : 2366 : CASE_ATOMIC (ATOMIC_XOR_FETCH, ATOMIC_FETCH_XOR, BIT_XOR_EXPR)
2201 : :
2202 : 821 : CASE_ATOMIC (SYNC_ADD_AND_FETCH, SYNC_FETCH_AND_ADD, MINUS_EXPR)
2203 : 732 : CASE_ATOMIC (SYNC_SUB_AND_FETCH, SYNC_FETCH_AND_SUB, PLUS_EXPR)
2204 : 800 : CASE_ATOMIC (SYNC_XOR_AND_FETCH, SYNC_FETCH_AND_XOR, BIT_XOR_EXPR)
2205 : :
2206 : : #undef CASE_ATOMIC
2207 : :
2208 : 107513 : handle_atomic_fetch_op:
2209 : 107513 : if (gimple_call_num_args (stmt2) >= 2 && gimple_call_lhs (stmt2))
2210 : : {
2211 : 64515 : tree lhs2 = gimple_call_lhs (stmt2), lhsc = lhs2;
2212 : 64515 : tree arg = gimple_call_arg (stmt2, 1);
2213 : 64515 : gimple *use_stmt, *cast_stmt = NULL;
2214 : 64515 : use_operand_p use_p;
2215 : 64515 : tree ndecl = builtin_decl_explicit (other_atomic);
2216 : :
2217 : 64515 : if (ndecl == NULL_TREE || !single_imm_use (lhs2, &use_p, &use_stmt))
2218 : : break;
2219 : :
2220 : 61733 : if (gimple_assign_cast_p (use_stmt))
2221 : : {
2222 : 33396 : cast_stmt = use_stmt;
2223 : 33396 : lhsc = gimple_assign_lhs (cast_stmt);
2224 : 33396 : if (lhsc == NULL_TREE
2225 : 33396 : || !INTEGRAL_TYPE_P (TREE_TYPE (lhsc))
2226 : 32781 : || (TYPE_PRECISION (TREE_TYPE (lhsc))
2227 : 32781 : != TYPE_PRECISION (TREE_TYPE (lhs2)))
2228 : 64651 : || !single_imm_use (lhsc, &use_p, &use_stmt))
2229 : : {
2230 : 2725 : use_stmt = cast_stmt;
2231 : 2725 : cast_stmt = NULL;
2232 : 2725 : lhsc = lhs2;
2233 : : }
2234 : : }
2235 : :
2236 : 61733 : bool ok = false;
2237 : 61733 : tree oarg = NULL_TREE;
2238 : 61733 : enum tree_code ccode = ERROR_MARK;
2239 : 61733 : tree crhs1 = NULL_TREE, crhs2 = NULL_TREE;
2240 : 61733 : if (is_gimple_assign (use_stmt)
2241 : 61733 : && gimple_assign_rhs_code (use_stmt) == atomic_op)
2242 : : {
2243 : 1416 : if (gimple_assign_rhs1 (use_stmt) == lhsc)
2244 : 1025 : oarg = gimple_assign_rhs2 (use_stmt);
2245 : 391 : else if (atomic_op != MINUS_EXPR)
2246 : : oarg = gimple_assign_rhs1 (use_stmt);
2247 : : }
2248 : 60317 : else if (atomic_op == MINUS_EXPR
2249 : 13266 : && is_gimple_assign (use_stmt)
2250 : 3626 : && gimple_assign_rhs_code (use_stmt) == PLUS_EXPR
2251 : 195 : && TREE_CODE (arg) == INTEGER_CST
2252 : 60512 : && (TREE_CODE (gimple_assign_rhs2 (use_stmt))
2253 : : == INTEGER_CST))
2254 : : {
2255 : 179 : tree a = fold_convert (TREE_TYPE (lhs2), arg);
2256 : 179 : tree o = fold_convert (TREE_TYPE (lhs2),
2257 : : gimple_assign_rhs2 (use_stmt));
2258 : 179 : if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
2259 : : ok = true;
2260 : : }
2261 : 60138 : else if (atomic_op == BIT_AND_EXPR || atomic_op == BIT_IOR_EXPR)
2262 : : ;
2263 : 54952 : else if (gimple_code (use_stmt) == GIMPLE_COND)
2264 : : {
2265 : 20296 : ccode = gimple_cond_code (use_stmt);
2266 : 20296 : crhs1 = gimple_cond_lhs (use_stmt);
2267 : 20296 : crhs2 = gimple_cond_rhs (use_stmt);
2268 : : }
2269 : 34656 : else if (is_gimple_assign (use_stmt))
2270 : : {
2271 : 9568 : if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
2272 : : {
2273 : 4063 : ccode = gimple_assign_rhs_code (use_stmt);
2274 : 4063 : crhs1 = gimple_assign_rhs1 (use_stmt);
2275 : 4063 : crhs2 = gimple_assign_rhs2 (use_stmt);
2276 : : }
2277 : 5505 : else if (gimple_assign_rhs_code (use_stmt) == COND_EXPR)
2278 : : {
2279 : 0 : tree cond = gimple_assign_rhs1 (use_stmt);
2280 : 0 : if (COMPARISON_CLASS_P (cond))
2281 : : {
2282 : 0 : ccode = TREE_CODE (cond);
2283 : 0 : crhs1 = TREE_OPERAND (cond, 0);
2284 : 0 : crhs2 = TREE_OPERAND (cond, 1);
2285 : : }
2286 : : }
2287 : : }
2288 : 25384 : if (ccode == EQ_EXPR || ccode == NE_EXPR)
2289 : : {
2290 : : /* Deal with x - y == 0 or x ^ y == 0
2291 : : being optimized into x == y and x + cst == 0
2292 : : into x == -cst. */
2293 : 23147 : tree o = NULL_TREE;
2294 : 23147 : if (crhs1 == lhsc)
2295 : : o = crhs2;
2296 : 133 : else if (crhs2 == lhsc)
2297 : 133 : o = crhs1;
2298 : 23147 : if (o && atomic_op != PLUS_EXPR)
2299 : : oarg = o;
2300 : 10900 : else if (o
2301 : 10900 : && TREE_CODE (o) == INTEGER_CST
2302 : 10900 : && TREE_CODE (arg) == INTEGER_CST)
2303 : : {
2304 : 10168 : tree a = fold_convert (TREE_TYPE (lhs2), arg);
2305 : 10168 : o = fold_convert (TREE_TYPE (lhs2), o);
2306 : 10168 : if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
2307 : 61733 : ok = true;
2308 : : }
2309 : : }
2310 : 61733 : if (oarg && !ok)
2311 : : {
2312 : 13663 : if (operand_equal_p (arg, oarg, 0))
2313 : : ok = true;
2314 : 12317 : else if (TREE_CODE (arg) == SSA_NAME
2315 : 2179 : && TREE_CODE (oarg) == SSA_NAME)
2316 : : {
2317 : 745 : tree oarg2 = oarg;
2318 : 745 : if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (oarg)))
2319 : : {
2320 : 104 : gimple *g = SSA_NAME_DEF_STMT (oarg);
2321 : 104 : oarg2 = gimple_assign_rhs1 (g);
2322 : 104 : if (TREE_CODE (oarg2) != SSA_NAME
2323 : 104 : || !INTEGRAL_TYPE_P (TREE_TYPE (oarg2))
2324 : 208 : || (TYPE_PRECISION (TREE_TYPE (oarg2))
2325 : 104 : != TYPE_PRECISION (TREE_TYPE (oarg))))
2326 : : oarg2 = oarg;
2327 : : }
2328 : 745 : if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg)))
2329 : : {
2330 : 544 : gimple *g = SSA_NAME_DEF_STMT (arg);
2331 : 544 : tree rhs1 = gimple_assign_rhs1 (g);
2332 : : /* Handle e.g.
2333 : : x.0_1 = (long unsigned int) x_4(D);
2334 : : _2 = __atomic_fetch_add_8 (&vlong, x.0_1, 0);
2335 : : _3 = (long int) _2;
2336 : : _7 = x_4(D) + _3; */
2337 : 544 : if (rhs1 == oarg || rhs1 == oarg2)
2338 : : ok = true;
2339 : : /* Handle e.g.
2340 : : x.18_1 = (short unsigned int) x_5(D);
2341 : : _2 = (int) x.18_1;
2342 : : _3 = __atomic_fetch_xor_2 (&vshort, _2, 0);
2343 : : _4 = (short int) _3;
2344 : : _8 = x_5(D) ^ _4;
2345 : : This happens only for char/short. */
2346 : 160 : else if (TREE_CODE (rhs1) == SSA_NAME
2347 : 160 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
2348 : 320 : && (TYPE_PRECISION (TREE_TYPE (rhs1))
2349 : 160 : == TYPE_PRECISION (TREE_TYPE (lhs2))))
2350 : : {
2351 : 160 : g = SSA_NAME_DEF_STMT (rhs1);
2352 : 160 : if (gimple_assign_cast_p (g)
2353 : 160 : && (gimple_assign_rhs1 (g) == oarg
2354 : 0 : || gimple_assign_rhs1 (g) == oarg2))
2355 : : ok = true;
2356 : : }
2357 : : }
2358 : 745 : if (!ok && arg == oarg2)
2359 : : /* Handle e.g.
2360 : : _1 = __sync_fetch_and_add_4 (&v, x_5(D));
2361 : : _2 = (int) _1;
2362 : : x.0_3 = (int) x_5(D);
2363 : : _7 = _2 + x.0_3; */
2364 : : ok = true;
2365 : : }
2366 : : }
2367 : :
2368 : 60387 : if (ok)
2369 : : {
2370 : 2593 : tree new_lhs = make_ssa_name (TREE_TYPE (lhs2));
2371 : 2593 : gimple_call_set_lhs (stmt2, new_lhs);
2372 : 2593 : gimple_call_set_fndecl (stmt2, ndecl);
2373 : 2593 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
2374 : 2593 : if (ccode == ERROR_MARK)
2375 : 1992 : gimple_assign_set_rhs_with_ops (&gsi, cast_stmt
2376 : : ? NOP_EXPR : SSA_NAME,
2377 : : new_lhs);
2378 : : else
2379 : : {
2380 : 1374 : crhs1 = new_lhs;
2381 : 1374 : crhs2 = build_zero_cst (TREE_TYPE (lhs2));
2382 : 1374 : if (gimple_code (use_stmt) == GIMPLE_COND)
2383 : : {
2384 : 1021 : gcond *cond_stmt = as_a <gcond *> (use_stmt);
2385 : 1021 : gimple_cond_set_lhs (cond_stmt, crhs1);
2386 : 1021 : gimple_cond_set_rhs (cond_stmt, crhs2);
2387 : : }
2388 : 353 : else if (gimple_assign_rhs_class (use_stmt)
2389 : : == GIMPLE_BINARY_RHS)
2390 : : {
2391 : 353 : gimple_assign_set_rhs1 (use_stmt, crhs1);
2392 : 353 : gimple_assign_set_rhs2 (use_stmt, crhs2);
2393 : : }
2394 : : else
2395 : : {
2396 : 0 : gcc_checking_assert (gimple_assign_rhs_code (use_stmt)
2397 : : == COND_EXPR);
2398 : 0 : tree cond = build2 (ccode, boolean_type_node,
2399 : : crhs1, crhs2);
2400 : 0 : gimple_assign_set_rhs1 (use_stmt, cond);
2401 : : }
2402 : : }
2403 : 2593 : update_stmt (use_stmt);
2404 : 2593 : if (atomic_op != BIT_AND_EXPR
2405 : 2593 : && atomic_op != BIT_IOR_EXPR
2406 : 2593 : && !stmt_ends_bb_p (stmt2))
2407 : : {
2408 : : /* For the benefit of debug stmts, emit stmt(s) to set
2409 : : lhs2 to the value it had from the new builtin.
2410 : : E.g. if it was previously:
2411 : : lhs2 = __atomic_fetch_add_8 (ptr, arg, 0);
2412 : : emit:
2413 : : new_lhs = __atomic_add_fetch_8 (ptr, arg, 0);
2414 : : lhs2 = new_lhs - arg;
2415 : : We also keep cast_stmt if any in the IL for
2416 : : the same reasons.
2417 : : These stmts will be DCEd later and proper debug info
2418 : : will be emitted.
2419 : : This is only possible for reversible operations
2420 : : (+/-/^) and without -fnon-call-exceptions. */
2421 : 2252 : gsi = gsi_for_stmt (stmt2);
2422 : 2252 : tree type = TREE_TYPE (lhs2);
2423 : 2252 : if (TREE_CODE (arg) == INTEGER_CST)
2424 : 1670 : arg = fold_convert (type, arg);
2425 : 582 : else if (!useless_type_conversion_p (type, TREE_TYPE (arg)))
2426 : : {
2427 : 0 : tree narg = make_ssa_name (type);
2428 : 0 : gimple *g = gimple_build_assign (narg, NOP_EXPR, arg);
2429 : 0 : gsi_insert_after (&gsi, g, GSI_NEW_STMT);
2430 : 0 : arg = narg;
2431 : : }
2432 : 2252 : enum tree_code rcode;
2433 : 2252 : switch (atomic_op)
2434 : : {
2435 : : case PLUS_EXPR: rcode = MINUS_EXPR; break;
2436 : 740 : case MINUS_EXPR: rcode = PLUS_EXPR; break;
2437 : 492 : case BIT_XOR_EXPR: rcode = atomic_op; break;
2438 : 0 : default: gcc_unreachable ();
2439 : : }
2440 : 2252 : gimple *g = gimple_build_assign (lhs2, rcode, new_lhs, arg);
2441 : 2252 : gsi_insert_after (&gsi, g, GSI_NEW_STMT);
2442 : 2252 : update_stmt (stmt2);
2443 : : }
2444 : : else
2445 : : {
2446 : : /* For e.g.
2447 : : lhs2 = __atomic_fetch_or_8 (ptr, arg, 0);
2448 : : after we change it to
2449 : : new_lhs = __atomic_or_fetch_8 (ptr, arg, 0);
2450 : : there is no way to find out the lhs2 value (i.e.
2451 : : what the atomic memory contained before the operation),
2452 : : values of some bits are lost. We have checked earlier
2453 : : that we don't have any non-debug users except for what
2454 : : we are already changing, so we need to reset the
2455 : : debug stmts and remove the cast_stmt if any. */
2456 : 341 : imm_use_iterator iter;
2457 : 676 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs2)
2458 : 335 : if (use_stmt != cast_stmt)
2459 : : {
2460 : 168 : gcc_assert (is_gimple_debug (use_stmt));
2461 : 168 : gimple_debug_bind_reset_value (use_stmt);
2462 : 168 : update_stmt (use_stmt);
2463 : 341 : }
2464 : 341 : if (cast_stmt)
2465 : : {
2466 : 167 : gsi = gsi_for_stmt (cast_stmt);
2467 : 167 : gsi_remove (&gsi, true);
2468 : : }
2469 : 341 : update_stmt (stmt2);
2470 : 341 : release_ssa_name (lhs2);
2471 : : }
2472 : : }
2473 : : }
2474 : : break;
2475 : :
2476 : : default:
2477 : : break;
2478 : : }
2479 : : return false;
2480 : : }
2481 : :
2482 : : /* Given a ssa_name in NAME see if it was defined by an assignment and
2483 : : set CODE to be the code and ARG1 to the first operand on the rhs and ARG2
2484 : : to the second operand on the rhs. */
2485 : :
2486 : : static inline void
2487 : 17274952 : defcodefor_name (tree name, enum tree_code *code, tree *arg1, tree *arg2)
2488 : : {
2489 : 17274952 : gimple *def;
2490 : 17274952 : enum tree_code code1;
2491 : 17274952 : tree arg11;
2492 : 17274952 : tree arg21;
2493 : 17274952 : tree arg31;
2494 : 17274952 : enum gimple_rhs_class grhs_class;
2495 : :
2496 : 17274952 : code1 = TREE_CODE (name);
2497 : 17274952 : arg11 = name;
2498 : 17274952 : arg21 = NULL_TREE;
2499 : 17274952 : arg31 = NULL_TREE;
2500 : 17274952 : grhs_class = get_gimple_rhs_class (code1);
2501 : :
2502 : 17274952 : if (code1 == SSA_NAME)
2503 : : {
2504 : 11590393 : def = SSA_NAME_DEF_STMT (name);
2505 : :
2506 : 11590393 : if (def && is_gimple_assign (def)
2507 : 18818716 : && can_propagate_from (def))
2508 : : {
2509 : 5019503 : code1 = gimple_assign_rhs_code (def);
2510 : 5019503 : arg11 = gimple_assign_rhs1 (def);
2511 : 5019503 : arg21 = gimple_assign_rhs2 (def);
2512 : 5019503 : arg31 = gimple_assign_rhs3 (def);
2513 : : }
2514 : : }
2515 : 5684559 : else if (grhs_class != GIMPLE_SINGLE_RHS)
2516 : 0 : code1 = ERROR_MARK;
2517 : :
2518 : 17274952 : *code = code1;
2519 : 17274952 : *arg1 = arg11;
2520 : 17274952 : if (arg2)
2521 : 17255920 : *arg2 = arg21;
2522 : 17274952 : if (arg31)
2523 : 2584 : *code = ERROR_MARK;
2524 : 17274952 : }
2525 : :
2526 : :
2527 : : /* Recognize rotation patterns. Return true if a transformation
2528 : : applied, otherwise return false.
2529 : :
2530 : : We are looking for X with unsigned type T with bitsize B, OP being
2531 : : +, | or ^, some type T2 wider than T. For:
2532 : : (X << CNT1) OP (X >> CNT2) iff CNT1 + CNT2 == B
2533 : : ((T) ((T2) X << CNT1)) OP ((T) ((T2) X >> CNT2)) iff CNT1 + CNT2 == B
2534 : :
2535 : : transform these into:
2536 : : X r<< CNT1
2537 : :
2538 : : Or for:
2539 : : (X << Y) OP (X >> (B - Y))
2540 : : (X << (int) Y) OP (X >> (int) (B - Y))
2541 : : ((T) ((T2) X << Y)) OP ((T) ((T2) X >> (B - Y)))
2542 : : ((T) ((T2) X << (int) Y)) OP ((T) ((T2) X >> (int) (B - Y)))
2543 : : (X << Y) | (X >> ((-Y) & (B - 1)))
2544 : : (X << (int) Y) | (X >> (int) ((-Y) & (B - 1)))
2545 : : ((T) ((T2) X << Y)) | ((T) ((T2) X >> ((-Y) & (B - 1))))
2546 : : ((T) ((T2) X << (int) Y)) | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
2547 : :
2548 : : transform these into (last 2 only if ranger can prove Y < B
2549 : : or Y = N * B):
2550 : : X r<< Y
2551 : : or
2552 : : X r<< (& & (B - 1))
2553 : : The latter for the forms with T2 wider than T if ranger can't prove Y < B.
2554 : :
2555 : : Or for:
2556 : : (X << (Y & (B - 1))) | (X >> ((-Y) & (B - 1)))
2557 : : (X << (int) (Y & (B - 1))) | (X >> (int) ((-Y) & (B - 1)))
2558 : : ((T) ((T2) X << (Y & (B - 1)))) | ((T) ((T2) X >> ((-Y) & (B - 1))))
2559 : : ((T) ((T2) X << (int) (Y & (B - 1)))) \
2560 : : | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
2561 : :
2562 : : transform these into:
2563 : : X r<< (Y & (B - 1))
2564 : :
2565 : : Note, in the patterns with T2 type, the type of OP operands
2566 : : might be even a signed type, but should have precision B.
2567 : : Expressions with & (B - 1) should be recognized only if B is
2568 : : a power of 2. */
2569 : :
2570 : : static bool
2571 : 10207938 : simplify_rotate (gimple_stmt_iterator *gsi)
2572 : : {
2573 : 10207938 : gimple *stmt = gsi_stmt (*gsi);
2574 : 10207938 : tree arg[2], rtype, rotcnt = NULL_TREE;
2575 : 10207938 : tree def_arg1[2], def_arg2[2];
2576 : 10207938 : enum tree_code def_code[2];
2577 : 10207938 : tree lhs;
2578 : 10207938 : int i;
2579 : 10207938 : bool swapped_p = false;
2580 : 10207938 : gimple *g;
2581 : 10207938 : gimple *def_arg_stmt[2] = { NULL, NULL };
2582 : 10207938 : int wider_prec = 0;
2583 : 10207938 : bool add_masking = false;
2584 : :
2585 : 10207938 : arg[0] = gimple_assign_rhs1 (stmt);
2586 : 10207938 : arg[1] = gimple_assign_rhs2 (stmt);
2587 : 10207938 : rtype = TREE_TYPE (arg[0]);
2588 : :
2589 : : /* Only create rotates in complete modes. Other cases are not
2590 : : expanded properly. */
2591 : 10207938 : if (!INTEGRAL_TYPE_P (rtype)
2592 : 10207938 : || !type_has_mode_precision_p (rtype))
2593 : 1623528 : return false;
2594 : :
2595 : 25753230 : for (i = 0; i < 2; i++)
2596 : : {
2597 : 17168820 : defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
2598 : 17168820 : if (TREE_CODE (arg[i]) == SSA_NAME)
2599 : 11484261 : def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
2600 : : }
2601 : :
2602 : : /* Look through narrowing (or same precision) conversions. */
2603 : 7576672 : if (CONVERT_EXPR_CODE_P (def_code[0])
2604 : 1007738 : && CONVERT_EXPR_CODE_P (def_code[1])
2605 : 149046 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[0]))
2606 : 120468 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[1]))
2607 : 113238 : && TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
2608 : 113238 : == TYPE_PRECISION (TREE_TYPE (def_arg1[1]))
2609 : 63816 : && TYPE_PRECISION (TREE_TYPE (def_arg1[0])) >= TYPE_PRECISION (rtype)
2610 : 43301 : && has_single_use (arg[0])
2611 : 8618359 : && has_single_use (arg[1]))
2612 : : {
2613 : 29741 : wider_prec = TYPE_PRECISION (TREE_TYPE (def_arg1[0]));
2614 : 89223 : for (i = 0; i < 2; i++)
2615 : : {
2616 : 59482 : arg[i] = def_arg1[i];
2617 : 59482 : defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
2618 : 59482 : if (TREE_CODE (arg[i]) == SSA_NAME)
2619 : 59482 : def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
2620 : : }
2621 : : }
2622 : : else
2623 : : {
2624 : : /* Handle signed rotate; the RSHIFT_EXPR has to be done
2625 : : in unsigned type but LSHIFT_EXPR could be signed. */
2626 : 8554669 : i = (def_code[0] == LSHIFT_EXPR || def_code[0] == RSHIFT_EXPR);
2627 : 7558273 : if (CONVERT_EXPR_CODE_P (def_code[i])
2628 : 996396 : && (def_code[1 - i] == LSHIFT_EXPR || def_code[1 - i] == RSHIFT_EXPR)
2629 : 32729 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[i]))
2630 : 31537 : && TYPE_PRECISION (rtype) == TYPE_PRECISION (TREE_TYPE (def_arg1[i]))
2631 : 8558254 : && has_single_use (arg[i]))
2632 : : {
2633 : 2149 : arg[i] = def_arg1[i];
2634 : 2149 : defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
2635 : 2149 : if (TREE_CODE (arg[i]) == SSA_NAME)
2636 : 2149 : def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
2637 : : }
2638 : : }
2639 : :
2640 : : /* One operand has to be LSHIFT_EXPR and one RSHIFT_EXPR. */
2641 : 8793893 : for (i = 0; i < 2; i++)
2642 : 8765399 : if (def_code[i] != LSHIFT_EXPR && def_code[i] != RSHIFT_EXPR)
2643 : : return false;
2644 : 253729 : else if (!has_single_use (arg[i]))
2645 : : return false;
2646 : 28494 : if (def_code[0] == def_code[1])
2647 : : return false;
2648 : :
2649 : : /* If we've looked through narrowing conversions before, look through
2650 : : widening conversions from unsigned type with the same precision
2651 : : as rtype here. */
2652 : 23503 : if (TYPE_PRECISION (TREE_TYPE (def_arg1[0])) != TYPE_PRECISION (rtype))
2653 : 21603 : for (i = 0; i < 2; i++)
2654 : : {
2655 : 14404 : tree tem;
2656 : 14404 : enum tree_code code;
2657 : 14404 : defcodefor_name (def_arg1[i], &code, &tem, NULL);
2658 : 6 : if (!CONVERT_EXPR_CODE_P (code)
2659 : 14398 : || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
2660 : 28802 : || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
2661 : 6 : return false;
2662 : 14398 : def_arg1[i] = tem;
2663 : : }
2664 : : /* Both shifts have to use the same first operand. */
2665 : 23497 : if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
2666 : 37010 : || !types_compatible_p (TREE_TYPE (def_arg1[0]),
2667 : 13513 : TREE_TYPE (def_arg1[1])))
2668 : : {
2669 : 9984 : if ((TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
2670 : 9984 : != TYPE_PRECISION (TREE_TYPE (def_arg1[1])))
2671 : 9984 : || (TYPE_UNSIGNED (TREE_TYPE (def_arg1[0]))
2672 : 9984 : == TYPE_UNSIGNED (TREE_TYPE (def_arg1[1]))))
2673 : 9962 : return false;
2674 : :
2675 : : /* Handle signed rotate; the RSHIFT_EXPR has to be done
2676 : : in unsigned type but LSHIFT_EXPR could be signed. */
2677 : 619 : i = def_code[0] != RSHIFT_EXPR;
2678 : 619 : if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[i])))
2679 : : return false;
2680 : :
2681 : 568 : tree tem;
2682 : 568 : enum tree_code code;
2683 : 568 : defcodefor_name (def_arg1[i], &code, &tem, NULL);
2684 : 337 : if (!CONVERT_EXPR_CODE_P (code)
2685 : 231 : || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
2686 : 799 : || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
2687 : : return false;
2688 : 222 : def_arg1[i] = tem;
2689 : 222 : if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
2690 : 244 : || !types_compatible_p (TREE_TYPE (def_arg1[0]),
2691 : 22 : TREE_TYPE (def_arg1[1])))
2692 : 200 : return false;
2693 : : }
2694 : 13513 : else if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[0])))
2695 : : return false;
2696 : :
2697 : : /* CNT1 + CNT2 == B case above. */
2698 : 11800 : if (tree_fits_uhwi_p (def_arg2[0])
2699 : 1340 : && tree_fits_uhwi_p (def_arg2[1])
2700 : 11800 : && tree_to_uhwi (def_arg2[0])
2701 : 1340 : + tree_to_uhwi (def_arg2[1]) == TYPE_PRECISION (rtype))
2702 : : rotcnt = def_arg2[0];
2703 : 10872 : else if (TREE_CODE (def_arg2[0]) != SSA_NAME
2704 : 10460 : || TREE_CODE (def_arg2[1]) != SSA_NAME)
2705 : : return false;
2706 : : else
2707 : : {
2708 : 10460 : tree cdef_arg1[2], cdef_arg2[2], def_arg2_alt[2];
2709 : 10460 : enum tree_code cdef_code[2];
2710 : 10460 : gimple *def_arg_alt_stmt[2] = { NULL, NULL };
2711 : 10460 : int check_range = 0;
2712 : 10460 : gimple *check_range_stmt = NULL;
2713 : : /* Look through conversion of the shift count argument.
2714 : : The C/C++ FE cast any shift count argument to integer_type_node.
2715 : : The only problem might be if the shift count type maximum value
2716 : : is equal or smaller than number of bits in rtype. */
2717 : 31380 : for (i = 0; i < 2; i++)
2718 : : {
2719 : 20920 : def_arg2_alt[i] = def_arg2[i];
2720 : 20920 : defcodefor_name (def_arg2[i], &cdef_code[i],
2721 : : &cdef_arg1[i], &cdef_arg2[i]);
2722 : 16371 : if (CONVERT_EXPR_CODE_P (cdef_code[i])
2723 : 4549 : && INTEGRAL_TYPE_P (TREE_TYPE (cdef_arg1[i]))
2724 : 4549 : && TYPE_PRECISION (TREE_TYPE (cdef_arg1[i]))
2725 : 9098 : > floor_log2 (TYPE_PRECISION (rtype))
2726 : 25469 : && type_has_mode_precision_p (TREE_TYPE (cdef_arg1[i])))
2727 : : {
2728 : 4549 : def_arg2_alt[i] = cdef_arg1[i];
2729 : 4549 : if (TREE_CODE (def_arg2[i]) == SSA_NAME)
2730 : 4549 : def_arg_alt_stmt[i] = SSA_NAME_DEF_STMT (def_arg2[i]);
2731 : 4549 : defcodefor_name (def_arg2_alt[i], &cdef_code[i],
2732 : : &cdef_arg1[i], &cdef_arg2[i]);
2733 : : }
2734 : : else
2735 : 16371 : def_arg_alt_stmt[i] = def_arg_stmt[i];
2736 : : }
2737 : 28572 : for (i = 0; i < 2; i++)
2738 : : /* Check for one shift count being Y and the other B - Y,
2739 : : with optional casts. */
2740 : 20574 : if (cdef_code[i] == MINUS_EXPR
2741 : 880 : && tree_fits_shwi_p (cdef_arg1[i])
2742 : 880 : && tree_to_shwi (cdef_arg1[i]) == TYPE_PRECISION (rtype)
2743 : 21396 : && TREE_CODE (cdef_arg2[i]) == SSA_NAME)
2744 : : {
2745 : 822 : tree tem;
2746 : 822 : enum tree_code code;
2747 : :
2748 : 822 : if (cdef_arg2[i] == def_arg2[1 - i]
2749 : 472 : || cdef_arg2[i] == def_arg2_alt[1 - i])
2750 : : {
2751 : 350 : rotcnt = cdef_arg2[i];
2752 : 350 : check_range = -1;
2753 : 350 : if (cdef_arg2[i] == def_arg2[1 - i])
2754 : 350 : check_range_stmt = def_arg_stmt[1 - i];
2755 : : else
2756 : 0 : check_range_stmt = def_arg_alt_stmt[1 - i];
2757 : 806 : break;
2758 : : }
2759 : 472 : defcodefor_name (cdef_arg2[i], &code, &tem, NULL);
2760 : 16 : if (CONVERT_EXPR_CODE_P (code)
2761 : 456 : && INTEGRAL_TYPE_P (TREE_TYPE (tem))
2762 : 456 : && TYPE_PRECISION (TREE_TYPE (tem))
2763 : 912 : > floor_log2 (TYPE_PRECISION (rtype))
2764 : 456 : && type_has_mode_precision_p (TREE_TYPE (tem))
2765 : 928 : && (tem == def_arg2[1 - i]
2766 : 288 : || tem == def_arg2_alt[1 - i]))
2767 : : {
2768 : 456 : rotcnt = tem;
2769 : 456 : check_range = -1;
2770 : 456 : if (tem == def_arg2[1 - i])
2771 : 168 : check_range_stmt = def_arg_stmt[1 - i];
2772 : : else
2773 : 288 : check_range_stmt = def_arg_alt_stmt[1 - i];
2774 : : break;
2775 : : }
2776 : : }
2777 : : /* The above sequence isn't safe for Y being 0,
2778 : : because then one of the shifts triggers undefined behavior.
2779 : : This alternative is safe even for rotation count of 0.
2780 : : One shift count is Y and the other (-Y) & (B - 1).
2781 : : Or one shift count is Y & (B - 1) and the other (-Y) & (B - 1). */
2782 : 19752 : else if (cdef_code[i] == BIT_AND_EXPR
2783 : 31875 : && pow2p_hwi (TYPE_PRECISION (rtype))
2784 : 13763 : && tree_fits_shwi_p (cdef_arg2[i])
2785 : 27526 : && tree_to_shwi (cdef_arg2[i])
2786 : 13763 : == TYPE_PRECISION (rtype) - 1
2787 : 13682 : && TREE_CODE (cdef_arg1[i]) == SSA_NAME
2788 : 33434 : && gimple_assign_rhs_code (stmt) == BIT_IOR_EXPR)
2789 : : {
2790 : 2526 : tree tem;
2791 : 2526 : enum tree_code code;
2792 : :
2793 : 2526 : defcodefor_name (cdef_arg1[i], &code, &tem, NULL);
2794 : 2328 : if (CONVERT_EXPR_CODE_P (code)
2795 : 198 : && INTEGRAL_TYPE_P (TREE_TYPE (tem))
2796 : 198 : && TYPE_PRECISION (TREE_TYPE (tem))
2797 : 396 : > floor_log2 (TYPE_PRECISION (rtype))
2798 : 2724 : && type_has_mode_precision_p (TREE_TYPE (tem)))
2799 : 198 : defcodefor_name (tem, &code, &tem, NULL);
2800 : :
2801 : 2526 : if (code == NEGATE_EXPR)
2802 : : {
2803 : 1670 : if (tem == def_arg2[1 - i] || tem == def_arg2_alt[1 - i])
2804 : : {
2805 : 999 : rotcnt = tem;
2806 : 999 : check_range = 1;
2807 : 999 : if (tem == def_arg2[1 - i])
2808 : 991 : check_range_stmt = def_arg_stmt[1 - i];
2809 : : else
2810 : 8 : check_range_stmt = def_arg_alt_stmt[1 - i];
2811 : 1656 : break;
2812 : : }
2813 : 671 : tree tem2;
2814 : 671 : defcodefor_name (tem, &code, &tem2, NULL);
2815 : 237 : if (CONVERT_EXPR_CODE_P (code)
2816 : 434 : && INTEGRAL_TYPE_P (TREE_TYPE (tem2))
2817 : 434 : && TYPE_PRECISION (TREE_TYPE (tem2))
2818 : 868 : > floor_log2 (TYPE_PRECISION (rtype))
2819 : 1105 : && type_has_mode_precision_p (TREE_TYPE (tem2)))
2820 : : {
2821 : 434 : if (tem2 == def_arg2[1 - i]
2822 : 434 : || tem2 == def_arg2_alt[1 - i])
2823 : : {
2824 : 228 : rotcnt = tem2;
2825 : 228 : check_range = 1;
2826 : 228 : if (tem2 == def_arg2[1 - i])
2827 : 0 : check_range_stmt = def_arg_stmt[1 - i];
2828 : : else
2829 : 228 : check_range_stmt = def_arg_alt_stmt[1 - i];
2830 : : break;
2831 : : }
2832 : : }
2833 : : else
2834 : 237 : tem2 = NULL_TREE;
2835 : :
2836 : 443 : if (cdef_code[1 - i] == BIT_AND_EXPR
2837 : 430 : && tree_fits_shwi_p (cdef_arg2[1 - i])
2838 : 860 : && tree_to_shwi (cdef_arg2[1 - i])
2839 : 430 : == TYPE_PRECISION (rtype) - 1
2840 : 873 : && TREE_CODE (cdef_arg1[1 - i]) == SSA_NAME)
2841 : : {
2842 : 430 : if (tem == cdef_arg1[1 - i]
2843 : 205 : || tem2 == cdef_arg1[1 - i])
2844 : : {
2845 : : rotcnt = def_arg2[1 - i];
2846 : 429 : break;
2847 : : }
2848 : 193 : tree tem3;
2849 : 193 : defcodefor_name (cdef_arg1[1 - i], &code, &tem3, NULL);
2850 : 0 : if (CONVERT_EXPR_CODE_P (code)
2851 : 193 : && INTEGRAL_TYPE_P (TREE_TYPE (tem3))
2852 : 193 : && TYPE_PRECISION (TREE_TYPE (tem3))
2853 : 386 : > floor_log2 (TYPE_PRECISION (rtype))
2854 : 386 : && type_has_mode_precision_p (TREE_TYPE (tem3)))
2855 : : {
2856 : 193 : if (tem == tem3 || tem2 == tem3)
2857 : : {
2858 : : rotcnt = def_arg2[1 - i];
2859 : : break;
2860 : : }
2861 : : }
2862 : : }
2863 : : }
2864 : : }
2865 : 2462 : if (check_range && wider_prec > TYPE_PRECISION (rtype))
2866 : : {
2867 : 1678 : if (TREE_CODE (rotcnt) != SSA_NAME)
2868 : 718 : return false;
2869 : 1678 : int_range_max r;
2870 : 1678 : range_query *q = get_range_query (cfun);
2871 : 1678 : if (q == get_global_range_query ())
2872 : 1522 : q = enable_ranger (cfun);
2873 : 1678 : if (!q->range_of_expr (r, rotcnt, check_range_stmt))
2874 : : {
2875 : 0 : if (check_range > 0)
2876 : : return false;
2877 : 0 : r.set_varying (TREE_TYPE (rotcnt));
2878 : : }
2879 : 1678 : int prec = TYPE_PRECISION (TREE_TYPE (rotcnt));
2880 : 1678 : signop sign = TYPE_SIGN (TREE_TYPE (rotcnt));
2881 : 1678 : wide_int min = wide_int::from (TYPE_PRECISION (rtype), prec, sign);
2882 : 1678 : wide_int max = wide_int::from (wider_prec - 1, prec, sign);
2883 : 1678 : if (check_range < 0)
2884 : 616 : max = min;
2885 : 1678 : int_range<1> r2 (TREE_TYPE (rotcnt), min, max);
2886 : 1678 : r.intersect (r2);
2887 : 1678 : if (!r.undefined_p ())
2888 : : {
2889 : 1326 : if (check_range > 0)
2890 : : {
2891 : 734 : int_range_max r3;
2892 : 2296 : for (int i = TYPE_PRECISION (rtype) + 1; i < wider_prec;
2893 : 1562 : i += TYPE_PRECISION (rtype))
2894 : : {
2895 : 1562 : int j = i + TYPE_PRECISION (rtype) - 2;
2896 : 1562 : min = wide_int::from (i, prec, sign);
2897 : 1562 : max = wide_int::from (MIN (j, wider_prec - 1),
2898 : 1562 : prec, sign);
2899 : 1562 : int_range<1> r4 (TREE_TYPE (rotcnt), min, max);
2900 : 1562 : r3.union_ (r4);
2901 : 1562 : }
2902 : 734 : r.intersect (r3);
2903 : 734 : if (!r.undefined_p ())
2904 : 718 : return false;
2905 : 734 : }
2906 : : add_masking = true;
2907 : : }
2908 : 1678 : }
2909 : 9742 : if (rotcnt == NULL_TREE)
2910 : : return false;
2911 : 1744 : swapped_p = i != 1;
2912 : : }
2913 : :
2914 : 2672 : if (!useless_type_conversion_p (TREE_TYPE (def_arg2[0]),
2915 : 2672 : TREE_TYPE (rotcnt)))
2916 : : {
2917 : 496 : g = gimple_build_assign (make_ssa_name (TREE_TYPE (def_arg2[0])),
2918 : : NOP_EXPR, rotcnt);
2919 : 496 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
2920 : 496 : rotcnt = gimple_assign_lhs (g);
2921 : : }
2922 : 2672 : if (add_masking)
2923 : : {
2924 : 608 : g = gimple_build_assign (make_ssa_name (TREE_TYPE (rotcnt)),
2925 : : BIT_AND_EXPR, rotcnt,
2926 : 608 : build_int_cst (TREE_TYPE (rotcnt),
2927 : 608 : TYPE_PRECISION (rtype) - 1));
2928 : 608 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
2929 : 608 : rotcnt = gimple_assign_lhs (g);
2930 : : }
2931 : 2672 : lhs = gimple_assign_lhs (stmt);
2932 : 2672 : if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0])))
2933 : 1016 : lhs = make_ssa_name (TREE_TYPE (def_arg1[0]));
2934 : 2672 : g = gimple_build_assign (lhs,
2935 : 2672 : ((def_code[0] == LSHIFT_EXPR) ^ swapped_p)
2936 : : ? LROTATE_EXPR : RROTATE_EXPR, def_arg1[0], rotcnt);
2937 : 2672 : if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0])))
2938 : : {
2939 : 1016 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
2940 : 1016 : g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, lhs);
2941 : : }
2942 : 2672 : gsi_replace (gsi, g, false);
2943 : 2672 : return true;
2944 : : }
2945 : :
2946 : :
2947 : : /* Check whether an array contains a valid table according to VALIDATE_FN. */
2948 : : template<typename ValidateFn>
2949 : : static bool
2950 : 8 : check_table_array (tree ctor, HOST_WIDE_INT &zero_val, unsigned bits,
2951 : : ValidateFn validate_fn)
2952 : : {
2953 : : tree elt, idx;
2954 : 8 : unsigned HOST_WIDE_INT i, raw_idx = 0;
2955 : 8 : unsigned matched = 0;
2956 : :
2957 : 8 : zero_val = 0;
2958 : :
2959 : 350 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, elt)
2960 : : {
2961 : 350 : if (!tree_fits_shwi_p (idx))
2962 : : return false;
2963 : 350 : if (!tree_fits_shwi_p (elt) && TREE_CODE (elt) != RAW_DATA_CST)
2964 : : return false;
2965 : :
2966 : 350 : unsigned HOST_WIDE_INT index = tree_to_shwi (idx);
2967 : : HOST_WIDE_INT val;
2968 : :
2969 : 350 : if (TREE_CODE (elt) == INTEGER_CST)
2970 : 286 : val = tree_to_shwi (elt);
2971 : : else
2972 : : {
2973 : 64 : if (raw_idx == (unsigned) RAW_DATA_LENGTH (elt))
2974 : : {
2975 : 0 : raw_idx = 0;
2976 : 0 : continue;
2977 : : }
2978 : 64 : if (TYPE_UNSIGNED (TREE_TYPE (elt)))
2979 : 0 : val = RAW_DATA_UCHAR_ELT (elt, raw_idx);
2980 : : else
2981 : 64 : val = RAW_DATA_SCHAR_ELT (elt, raw_idx);
2982 : 64 : index += raw_idx;
2983 : 64 : raw_idx++;
2984 : 64 : i--;
2985 : : }
2986 : :
2987 : 350 : if (index > bits * 2)
2988 : : return false;
2989 : :
2990 : 350 : if (index == 0)
2991 : : {
2992 : 8 : zero_val = val;
2993 : 8 : matched++;
2994 : : }
2995 : :
2996 : 350 : if (val >= 0 && val < bits && validate_fn (val, index))
2997 : 288 : matched++;
2998 : :
2999 : 350 : if (matched > bits)
3000 : : return true;
3001 : : }
3002 : :
3003 : : return false;
3004 : : }
3005 : :
3006 : : /* Check whether a string contains a valid table according to VALIDATE_FN. */
3007 : : template<typename ValidateFn>
3008 : : static bool
3009 : 4 : check_table_string (tree string, HOST_WIDE_INT &zero_val,unsigned bits,
3010 : : ValidateFn validate_fn)
3011 : : {
3012 : 4 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (string);
3013 : 4 : unsigned matched = 0;
3014 : 4 : const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (string);
3015 : :
3016 : 4 : if (len < bits || len > bits * 2)
3017 : : return false;
3018 : :
3019 : 4 : zero_val = p[0];
3020 : :
3021 : 164 : for (unsigned i = 0; i < len; i++)
3022 : 160 : if (p[i] < bits && validate_fn (p[i], i))
3023 : 160 : matched++;
3024 : :
3025 : 4 : return matched == bits;
3026 : : }
3027 : :
3028 : : /* Check whether CTOR contains a valid table according to VALIDATE_FN. */
3029 : : template<typename ValidateFn>
3030 : : static bool
3031 : 20 : check_table (tree ctor, tree type, HOST_WIDE_INT &zero_val, unsigned bits,
3032 : : ValidateFn validate_fn)
3033 : : {
3034 : 20 : if (TREE_CODE (ctor) == CONSTRUCTOR)
3035 : 8 : return check_table_array (ctor, zero_val, bits, validate_fn);
3036 : : else if (TREE_CODE (ctor) == STRING_CST
3037 : 12 : && TYPE_PRECISION (type) == CHAR_TYPE_SIZE)
3038 : 4 : return check_table_string (ctor, zero_val, bits, validate_fn);
3039 : : return false;
3040 : : }
3041 : :
3042 : : /* Match.pd function to match the ctz expression. */
3043 : : extern bool gimple_ctz_table_index (tree, tree *, tree (*)(tree));
3044 : : extern bool gimple_clz_table_index (tree, tree *, tree (*)(tree));
3045 : :
3046 : : /* Recognize count leading and trailing zeroes idioms.
3047 : : The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
3048 : : constant which when multiplied by a power of 2 creates a unique value
3049 : : in the top 5 or 6 bits. This is then indexed into a table which maps it
3050 : : to the number of trailing zeroes. Array[0] is returned so the caller can
3051 : : emit an appropriate sequence depending on whether ctz (0) is defined on
3052 : : the target. */
3053 : :
3054 : : static bool
3055 : 1965253 : simplify_count_zeroes (gimple_stmt_iterator *gsi)
3056 : : {
3057 : 1965253 : gimple *stmt = gsi_stmt (*gsi);
3058 : 1965253 : tree array_ref = gimple_assign_rhs1 (stmt);
3059 : 1965253 : tree res_ops[3];
3060 : :
3061 : 1965253 : gcc_checking_assert (TREE_CODE (array_ref) == ARRAY_REF);
3062 : :
3063 : 1965253 : internal_fn fn = IFN_LAST;
3064 : : /* For CTZ we recognize ((x & -x) * C) >> SHIFT where the array data
3065 : : represents the number of trailing zeros. */
3066 : 1965253 : if (gimple_ctz_table_index (TREE_OPERAND (array_ref, 1), &res_ops[0], NULL))
3067 : : fn = IFN_CTZ;
3068 : : /* For CLZ we recognize
3069 : : x |= x >> 1;
3070 : : x |= x >> 2;
3071 : : x |= x >> 4;
3072 : : x |= x >> 8;
3073 : : x |= x >> 16;
3074 : : (x * C) >> SHIFT
3075 : : where 31 minus the array data represents the number of leading zeros. */
3076 : 1965231 : else if (gimple_clz_table_index (TREE_OPERAND (array_ref, 1), &res_ops[0],
3077 : : NULL))
3078 : : fn = IFN_CLZ;
3079 : : else
3080 : : return false;
3081 : :
3082 : 25 : HOST_WIDE_INT zero_val;
3083 : 25 : tree type = TREE_TYPE (array_ref);
3084 : 25 : tree array = TREE_OPERAND (array_ref, 0);
3085 : 25 : tree input_type = TREE_TYPE (res_ops[0]);
3086 : 25 : unsigned input_bits = tree_to_shwi (TYPE_SIZE (input_type));
3087 : :
3088 : : /* Check the array element type is not wider than 32 bits and the input is
3089 : : an unsigned 32-bit or 64-bit type. */
3090 : 25 : if (TYPE_PRECISION (type) > 32 || !TYPE_UNSIGNED (input_type))
3091 : : return false;
3092 : 21 : if (input_bits != 32 && input_bits != 64)
3093 : : return false;
3094 : :
3095 : 21 : if (!direct_internal_fn_supported_p (fn, input_type, OPTIMIZE_FOR_BOTH))
3096 : : return false;
3097 : :
3098 : : /* Check the lower bound of the array is zero. */
3099 : 21 : tree low = array_ref_low_bound (array_ref);
3100 : 21 : if (!low || !integer_zerop (low))
3101 : 0 : return false;
3102 : :
3103 : : /* Check the shift extracts the top 5..7 bits. */
3104 : 21 : unsigned shiftval = tree_to_shwi (res_ops[2]);
3105 : 21 : if (shiftval < input_bits - 7 || shiftval > input_bits - 5)
3106 : : return false;
3107 : :
3108 : 20 : tree ctor = ctor_for_folding (array);
3109 : 20 : if (!ctor)
3110 : : return false;
3111 : 20 : unsigned HOST_WIDE_INT mulval = tree_to_uhwi (res_ops[1]);
3112 : 20 : if (fn == IFN_CTZ)
3113 : : {
3114 : 429 : auto checkfn = [&](unsigned data, unsigned i) -> bool
3115 : : {
3116 : 412 : unsigned HOST_WIDE_INT mask
3117 : 412 : = ((HOST_WIDE_INT_1U << (input_bits - shiftval)) - 1) << shiftval;
3118 : 412 : return (((mulval << data) & mask) >> shiftval) == i;
3119 : 17 : };
3120 : 17 : if (!check_table (ctor, type, zero_val, input_bits, checkfn))
3121 : 8 : return false;
3122 : : }
3123 : 3 : else if (fn == IFN_CLZ)
3124 : : {
3125 : 99 : auto checkfn = [&](unsigned data, unsigned i) -> bool
3126 : : {
3127 : 96 : unsigned HOST_WIDE_INT mask
3128 : 96 : = ((HOST_WIDE_INT_1U << (input_bits - shiftval)) - 1) << shiftval;
3129 : 96 : return (((((HOST_WIDE_INT_1U << (data + 1)) - 1) * mulval) & mask)
3130 : 96 : >> shiftval) == i;
3131 : 3 : };
3132 : 3 : if (!check_table (ctor, type, zero_val, input_bits, checkfn))
3133 : 0 : return false;
3134 : : }
3135 : :
3136 : 12 : HOST_WIDE_INT ctz_val = -1;
3137 : 12 : bool zero_ok;
3138 : 12 : if (fn == IFN_CTZ)
3139 : : {
3140 : 9 : ctz_val = 0;
3141 : 18 : zero_ok = CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (input_type),
3142 : : ctz_val) == 2;
3143 : : }
3144 : 3 : else if (fn == IFN_CLZ)
3145 : : {
3146 : 3 : ctz_val = 32;
3147 : 3 : zero_ok = CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (input_type),
3148 : : ctz_val) == 2;
3149 : 3 : zero_val = input_bits - 1 - zero_val;
3150 : : }
3151 : 12 : int nargs = 2;
3152 : :
3153 : : /* If the input value can't be zero, don't special case ctz (0). */
3154 : 12 : range_query *q = get_range_query (cfun);
3155 : 12 : if (q == get_global_range_query ())
3156 : 12 : q = enable_ranger (cfun);
3157 : 12 : int_range_max vr;
3158 : 12 : if (q->range_of_expr (vr, res_ops[0], stmt)
3159 : 12 : && !range_includes_zero_p (vr))
3160 : : {
3161 : 4 : zero_ok = true;
3162 : 4 : zero_val = 0;
3163 : 4 : ctz_val = 0;
3164 : 4 : nargs = 1;
3165 : : }
3166 : :
3167 : 12 : gimple_seq seq = NULL;
3168 : 12 : gimple *g;
3169 : 12 : gcall *call = gimple_build_call_internal (fn, nargs, res_ops[0],
3170 : : nargs == 1 ? NULL_TREE
3171 : 20 : : build_int_cst (integer_type_node,
3172 : 8 : ctz_val));
3173 : 12 : gimple_set_location (call, gimple_location (stmt));
3174 : 12 : gimple_set_lhs (call, make_ssa_name (integer_type_node));
3175 : 12 : gimple_seq_add_stmt (&seq, call);
3176 : :
3177 : 12 : tree prev_lhs = gimple_call_lhs (call);
3178 : 12 : if (fn == IFN_CLZ)
3179 : : {
3180 : 3 : g = gimple_build_assign (make_ssa_name (integer_type_node),
3181 : : MINUS_EXPR,
3182 : : build_int_cst (integer_type_node,
3183 : 3 : input_bits - 1),
3184 : : prev_lhs);
3185 : 3 : gimple_set_location (g, gimple_location (stmt));
3186 : 3 : gimple_seq_add_stmt (&seq, g);
3187 : 3 : prev_lhs = gimple_assign_lhs (g);
3188 : : }
3189 : :
3190 : 12 : if (zero_ok && zero_val == ctz_val)
3191 : : ;
3192 : : /* Emit ctz (x) & 31 if ctz (0) is 32 but we need to return 0. */
3193 : 6 : else if (zero_ok && zero_val == 0 && ctz_val == input_bits)
3194 : : {
3195 : 5 : g = gimple_build_assign (make_ssa_name (integer_type_node),
3196 : : BIT_AND_EXPR, prev_lhs,
3197 : : build_int_cst (integer_type_node,
3198 : 5 : input_bits - 1));
3199 : 5 : gimple_set_location (g, gimple_location (stmt));
3200 : 5 : gimple_seq_add_stmt (&seq, g);
3201 : 5 : prev_lhs = gimple_assign_lhs (g);
3202 : : }
3203 : : /* As fallback emit a conditional move. */
3204 : : else
3205 : : {
3206 : 1 : g = gimple_build_assign (make_ssa_name (boolean_type_node), EQ_EXPR,
3207 : : res_ops[0], build_zero_cst (input_type));
3208 : 1 : gimple_set_location (g, gimple_location (stmt));
3209 : 1 : gimple_seq_add_stmt (&seq, g);
3210 : 1 : tree cond = gimple_assign_lhs (g);
3211 : 1 : g = gimple_build_assign (make_ssa_name (integer_type_node),
3212 : : COND_EXPR, cond,
3213 : 1 : build_int_cst (integer_type_node, zero_val),
3214 : : prev_lhs);
3215 : 1 : gimple_set_location (g, gimple_location (stmt));
3216 : 1 : gimple_seq_add_stmt (&seq, g);
3217 : 1 : prev_lhs = gimple_assign_lhs (g);
3218 : : }
3219 : :
3220 : 12 : g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, prev_lhs);
3221 : 12 : gimple_seq_add_stmt (&seq, g);
3222 : 12 : gsi_replace_with_seq (gsi, seq, true);
3223 : 12 : return true;
3224 : 12 : }
3225 : :
3226 : :
3227 : : /* Determine whether applying the 2 permutations (mask1 then mask2)
3228 : : gives back one of the input. */
3229 : :
3230 : : static int
3231 : 34 : is_combined_permutation_identity (tree mask1, tree mask2)
3232 : : {
3233 : 34 : tree mask;
3234 : 34 : unsigned HOST_WIDE_INT nelts, i, j;
3235 : 34 : bool maybe_identity1 = true;
3236 : 34 : bool maybe_identity2 = true;
3237 : :
3238 : 34 : gcc_checking_assert (TREE_CODE (mask1) == VECTOR_CST
3239 : : && TREE_CODE (mask2) == VECTOR_CST);
3240 : :
3241 : : /* For VLA masks, check for the following pattern:
3242 : : v1 = VEC_PERM_EXPR (v0, ..., mask1)
3243 : : v2 = VEC_PERM_EXPR (v1, ..., mask2)
3244 : : -->
3245 : : v2 = v0
3246 : : if mask1 == mask2 == {nelts - 1, nelts - 2, ...}. */
3247 : :
3248 : 34 : if (operand_equal_p (mask1, mask2, 0)
3249 : 34 : && !VECTOR_CST_NELTS (mask1).is_constant ())
3250 : : {
3251 : : vec_perm_builder builder;
3252 : : if (tree_to_vec_perm_builder (&builder, mask1))
3253 : : {
3254 : : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask1));
3255 : : vec_perm_indices sel (builder, 1, nelts);
3256 : : if (sel.series_p (0, 1, nelts - 1, -1))
3257 : : return 1;
3258 : : }
3259 : : }
3260 : :
3261 : 34 : mask = fold_ternary (VEC_PERM_EXPR, TREE_TYPE (mask1), mask1, mask1, mask2);
3262 : 34 : if (mask == NULL_TREE || TREE_CODE (mask) != VECTOR_CST)
3263 : : return 0;
3264 : :
3265 : 34 : if (!VECTOR_CST_NELTS (mask).is_constant (&nelts))
3266 : : return 0;
3267 : 60 : for (i = 0; i < nelts; i++)
3268 : : {
3269 : 60 : tree val = VECTOR_CST_ELT (mask, i);
3270 : 60 : gcc_assert (TREE_CODE (val) == INTEGER_CST);
3271 : 60 : j = TREE_INT_CST_LOW (val) & (2 * nelts - 1);
3272 : 60 : if (j == i)
3273 : : maybe_identity2 = false;
3274 : 47 : else if (j == i + nelts)
3275 : : maybe_identity1 = false;
3276 : : else
3277 : : return 0;
3278 : : }
3279 : 0 : return maybe_identity1 ? 1 : maybe_identity2 ? 2 : 0;
3280 : : }
3281 : :
3282 : : /* Combine a shuffle with its arguments. Returns true if there were any
3283 : : changes made. */
3284 : :
3285 : : static bool
3286 : 181234 : simplify_permutation (gimple_stmt_iterator *gsi)
3287 : : {
3288 : 181234 : gimple *stmt = gsi_stmt (*gsi);
3289 : 181234 : gimple *def_stmt = NULL;
3290 : 181234 : tree op0, op1, op2, op3, arg0, arg1;
3291 : 181234 : enum tree_code code, code2 = ERROR_MARK;
3292 : 181234 : bool single_use_op0 = false;
3293 : :
3294 : 181234 : gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
3295 : :
3296 : 181234 : op0 = gimple_assign_rhs1 (stmt);
3297 : 181234 : op1 = gimple_assign_rhs2 (stmt);
3298 : 181234 : op2 = gimple_assign_rhs3 (stmt);
3299 : :
3300 : 181234 : if (TREE_CODE (op2) != VECTOR_CST)
3301 : : return false;
3302 : :
3303 : 178516 : if (TREE_CODE (op0) == VECTOR_CST)
3304 : : {
3305 : : code = VECTOR_CST;
3306 : : arg0 = op0;
3307 : : }
3308 : 176675 : else if (TREE_CODE (op0) == SSA_NAME)
3309 : : {
3310 : 176675 : def_stmt = get_prop_source_stmt (op0, false, &single_use_op0);
3311 : 176675 : if (!def_stmt)
3312 : : return false;
3313 : 168726 : code = gimple_assign_rhs_code (def_stmt);
3314 : 168726 : if (code == VIEW_CONVERT_EXPR)
3315 : : {
3316 : 1477 : tree rhs = gimple_assign_rhs1 (def_stmt);
3317 : 1477 : tree name = TREE_OPERAND (rhs, 0);
3318 : 1477 : if (TREE_CODE (name) != SSA_NAME)
3319 : : return false;
3320 : 1477 : if (!has_single_use (name))
3321 : 204 : single_use_op0 = false;
3322 : : /* Here we update the def_stmt through this VIEW_CONVERT_EXPR,
3323 : : but still keep the code to indicate it comes from
3324 : : VIEW_CONVERT_EXPR. */
3325 : 1477 : def_stmt = SSA_NAME_DEF_STMT (name);
3326 : 1477 : if (!def_stmt || !is_gimple_assign (def_stmt))
3327 : : return false;
3328 : 693 : if (gimple_assign_rhs_code (def_stmt) != CONSTRUCTOR)
3329 : : return false;
3330 : : }
3331 : 167348 : if (!can_propagate_from (def_stmt))
3332 : : return false;
3333 : 18645 : arg0 = gimple_assign_rhs1 (def_stmt);
3334 : : }
3335 : : else
3336 : : return false;
3337 : :
3338 : : /* Two consecutive shuffles. */
3339 : 18645 : if (code == VEC_PERM_EXPR)
3340 : : {
3341 : 6069 : tree orig;
3342 : 6069 : int ident;
3343 : :
3344 : 6069 : if (op0 != op1)
3345 : : return false;
3346 : 34 : op3 = gimple_assign_rhs3 (def_stmt);
3347 : 34 : if (TREE_CODE (op3) != VECTOR_CST)
3348 : : return false;
3349 : 34 : ident = is_combined_permutation_identity (op3, op2);
3350 : 34 : if (!ident)
3351 : : return false;
3352 : 0 : orig = (ident == 1) ? gimple_assign_rhs1 (def_stmt)
3353 : 0 : : gimple_assign_rhs2 (def_stmt);
3354 : 0 : gimple_assign_set_rhs1 (stmt, unshare_expr (orig));
3355 : 0 : gimple_assign_set_rhs_code (stmt, TREE_CODE (orig));
3356 : 0 : gimple_set_num_ops (stmt, 2);
3357 : 0 : update_stmt (stmt);
3358 : 0 : remove_prop_source_from_use (op0);
3359 : 0 : return true;
3360 : : }
3361 : 14417 : else if (code == CONSTRUCTOR
3362 : 14417 : || code == VECTOR_CST
3363 : : || code == VIEW_CONVERT_EXPR)
3364 : : {
3365 : 2532 : if (op0 != op1)
3366 : : {
3367 : 2377 : if (TREE_CODE (op0) == SSA_NAME && !single_use_op0)
3368 : : return false;
3369 : :
3370 : 2047 : if (TREE_CODE (op1) == VECTOR_CST)
3371 : : arg1 = op1;
3372 : 1577 : else if (TREE_CODE (op1) == SSA_NAME)
3373 : : {
3374 : 1577 : gimple *def_stmt2 = get_prop_source_stmt (op1, true, NULL);
3375 : 1577 : if (!def_stmt2)
3376 : : return false;
3377 : 159 : code2 = gimple_assign_rhs_code (def_stmt2);
3378 : 159 : if (code2 == VIEW_CONVERT_EXPR)
3379 : : {
3380 : 0 : tree rhs = gimple_assign_rhs1 (def_stmt2);
3381 : 0 : tree name = TREE_OPERAND (rhs, 0);
3382 : 0 : if (TREE_CODE (name) != SSA_NAME)
3383 : : return false;
3384 : 0 : if (!has_single_use (name))
3385 : : return false;
3386 : 0 : def_stmt2 = SSA_NAME_DEF_STMT (name);
3387 : 0 : if (!def_stmt2 || !is_gimple_assign (def_stmt2))
3388 : : return false;
3389 : 0 : if (gimple_assign_rhs_code (def_stmt2) != CONSTRUCTOR)
3390 : : return false;
3391 : : }
3392 : 159 : else if (code2 != CONSTRUCTOR && code2 != VECTOR_CST)
3393 : : return false;
3394 : 49 : if (!can_propagate_from (def_stmt2))
3395 : : return false;
3396 : 49 : arg1 = gimple_assign_rhs1 (def_stmt2);
3397 : : }
3398 : : else
3399 : : return false;
3400 : : }
3401 : : else
3402 : : {
3403 : : /* Already used twice in this statement. */
3404 : 155 : if (TREE_CODE (op0) == SSA_NAME && num_imm_uses (op0) > 2)
3405 : : return false;
3406 : : arg1 = arg0;
3407 : : }
3408 : :
3409 : : /* If there are any VIEW_CONVERT_EXPRs found when finding permutation
3410 : : operands source, check whether it's valid to transform and prepare
3411 : : the required new operands. */
3412 : 587 : if (code == VIEW_CONVERT_EXPR || code2 == VIEW_CONVERT_EXPR)
3413 : : {
3414 : : /* Figure out the target vector type to which operands should be
3415 : : converted. If both are CONSTRUCTOR, the types should be the
3416 : : same, otherwise, use the one of CONSTRUCTOR. */
3417 : 18 : tree tgt_type = NULL_TREE;
3418 : 18 : if (code == VIEW_CONVERT_EXPR)
3419 : : {
3420 : 18 : gcc_assert (gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR);
3421 : 18 : code = CONSTRUCTOR;
3422 : 18 : tgt_type = TREE_TYPE (arg0);
3423 : : }
3424 : 18 : if (code2 == VIEW_CONVERT_EXPR)
3425 : : {
3426 : 0 : tree arg1_type = TREE_TYPE (arg1);
3427 : 0 : if (tgt_type == NULL_TREE)
3428 : : tgt_type = arg1_type;
3429 : 0 : else if (tgt_type != arg1_type)
3430 : 17 : return false;
3431 : : }
3432 : :
3433 : 18 : if (!VECTOR_TYPE_P (tgt_type))
3434 : : return false;
3435 : 18 : tree op2_type = TREE_TYPE (op2);
3436 : :
3437 : : /* Figure out the shrunk factor. */
3438 : 18 : poly_uint64 tgt_units = TYPE_VECTOR_SUBPARTS (tgt_type);
3439 : 18 : poly_uint64 op2_units = TYPE_VECTOR_SUBPARTS (op2_type);
3440 : 18 : if (maybe_gt (tgt_units, op2_units))
3441 : : return false;
3442 : 18 : unsigned int factor;
3443 : 35 : if (!constant_multiple_p (op2_units, tgt_units, &factor))
3444 : : return false;
3445 : :
3446 : : /* Build the new permutation control vector as target vector. */
3447 : 18 : vec_perm_builder builder;
3448 : 18 : if (!tree_to_vec_perm_builder (&builder, op2))
3449 : : return false;
3450 : 18 : vec_perm_indices indices (builder, 2, op2_units);
3451 : 18 : vec_perm_indices new_indices;
3452 : 18 : if (new_indices.new_shrunk_vector (indices, factor))
3453 : : {
3454 : 1 : tree mask_type = tgt_type;
3455 : 1 : if (!VECTOR_INTEGER_TYPE_P (mask_type))
3456 : : {
3457 : 0 : tree elem_type = TREE_TYPE (mask_type);
3458 : 0 : unsigned elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type));
3459 : 0 : tree int_type = build_nonstandard_integer_type (elem_size, 0);
3460 : 0 : mask_type = build_vector_type (int_type, tgt_units);
3461 : : }
3462 : 1 : op2 = vec_perm_indices_to_tree (mask_type, new_indices);
3463 : : }
3464 : : else
3465 : 17 : return false;
3466 : :
3467 : : /* Convert the VECTOR_CST to the appropriate vector type. */
3468 : 1 : if (tgt_type != TREE_TYPE (arg0))
3469 : 0 : arg0 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg0);
3470 : 1 : else if (tgt_type != TREE_TYPE (arg1))
3471 : 0 : arg1 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg1);
3472 : 35 : }
3473 : :
3474 : : /* VIEW_CONVERT_EXPR should be updated to CONSTRUCTOR before. */
3475 : 570 : gcc_assert (code == CONSTRUCTOR || code == VECTOR_CST);
3476 : :
3477 : : /* Shuffle of a constructor. */
3478 : 570 : tree res_type
3479 : 570 : = build_vector_type (TREE_TYPE (TREE_TYPE (arg0)),
3480 : 570 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (op2)));
3481 : 570 : tree opt = fold_ternary (VEC_PERM_EXPR, res_type, arg0, arg1, op2);
3482 : 570 : if (!opt
3483 : 280 : || (TREE_CODE (opt) != CONSTRUCTOR && TREE_CODE (opt) != VECTOR_CST))
3484 : : return false;
3485 : : /* Found VIEW_CONVERT_EXPR before, need one explicit conversion. */
3486 : 280 : if (res_type != TREE_TYPE (op0))
3487 : : {
3488 : 1 : tree name = make_ssa_name (TREE_TYPE (opt));
3489 : 1 : gimple *ass_stmt = gimple_build_assign (name, opt);
3490 : 1 : gsi_insert_before (gsi, ass_stmt, GSI_SAME_STMT);
3491 : 1 : opt = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op0), name);
3492 : : }
3493 : 280 : gimple_assign_set_rhs_from_tree (gsi, opt);
3494 : 280 : update_stmt (gsi_stmt (*gsi));
3495 : 280 : if (TREE_CODE (op0) == SSA_NAME)
3496 : 1 : remove_prop_source_from_use (op0);
3497 : 280 : if (op0 != op1 && TREE_CODE (op1) == SSA_NAME)
3498 : 0 : remove_prop_source_from_use (op1);
3499 : 280 : return true;
3500 : : }
3501 : :
3502 : : return false;
3503 : : }
3504 : :
3505 : : /* Get the BIT_FIELD_REF definition of VAL, if any, looking through
3506 : : conversions with code CONV_CODE or update it if still ERROR_MARK.
3507 : : Return NULL_TREE if no such matching def was found. */
3508 : :
3509 : : static tree
3510 : 394092 : get_bit_field_ref_def (tree val, enum tree_code &conv_code)
3511 : : {
3512 : 394092 : if (TREE_CODE (val) != SSA_NAME)
3513 : : return NULL_TREE ;
3514 : 368369 : gimple *def_stmt = get_prop_source_stmt (val, false, NULL);
3515 : 368369 : if (!def_stmt)
3516 : : return NULL_TREE;
3517 : 302098 : enum tree_code code = gimple_assign_rhs_code (def_stmt);
3518 : 302098 : if (code == FLOAT_EXPR
3519 : 302098 : || code == FIX_TRUNC_EXPR
3520 : : || CONVERT_EXPR_CODE_P (code))
3521 : : {
3522 : 182887 : tree op1 = gimple_assign_rhs1 (def_stmt);
3523 : 182887 : if (conv_code == ERROR_MARK)
3524 : 88894 : conv_code = code;
3525 : 93993 : else if (conv_code != code)
3526 : : return NULL_TREE;
3527 : 182861 : if (TREE_CODE (op1) != SSA_NAME)
3528 : : return NULL_TREE;
3529 : 72998 : def_stmt = SSA_NAME_DEF_STMT (op1);
3530 : 72998 : if (! is_gimple_assign (def_stmt))
3531 : : return NULL_TREE;
3532 : 58369 : code = gimple_assign_rhs_code (def_stmt);
3533 : : }
3534 : 177580 : if (code != BIT_FIELD_REF)
3535 : : return NULL_TREE;
3536 : 22312 : return gimple_assign_rhs1 (def_stmt);
3537 : : }
3538 : :
3539 : : /* Recognize a VEC_PERM_EXPR. Returns true if there were any changes. */
3540 : :
3541 : : static bool
3542 : 150467 : simplify_vector_constructor (gimple_stmt_iterator *gsi)
3543 : : {
3544 : 150467 : gimple *stmt = gsi_stmt (*gsi);
3545 : 150467 : tree op, orig[2], type, elem_type;
3546 : 150467 : unsigned elem_size, i;
3547 : 150467 : unsigned HOST_WIDE_INT nelts;
3548 : 150467 : unsigned HOST_WIDE_INT refnelts;
3549 : 150467 : enum tree_code conv_code;
3550 : 150467 : constructor_elt *elt;
3551 : :
3552 : 150467 : op = gimple_assign_rhs1 (stmt);
3553 : 150467 : type = TREE_TYPE (op);
3554 : 150467 : gcc_checking_assert (TREE_CODE (op) == CONSTRUCTOR
3555 : : && TREE_CODE (type) == VECTOR_TYPE);
3556 : :
3557 : 150467 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
3558 : : return false;
3559 : 150467 : elem_type = TREE_TYPE (type);
3560 : 150467 : elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type));
3561 : :
3562 : 150467 : orig[0] = NULL;
3563 : 150467 : orig[1] = NULL;
3564 : 150467 : conv_code = ERROR_MARK;
3565 : 150467 : bool maybe_ident = true;
3566 : 150467 : bool maybe_blend[2] = { true, true };
3567 : 150467 : tree one_constant = NULL_TREE;
3568 : 150467 : tree one_nonconstant = NULL_TREE;
3569 : 150467 : auto_vec<tree> constants;
3570 : 150467 : constants.safe_grow_cleared (nelts, true);
3571 : 150467 : auto_vec<std::pair<unsigned, unsigned>, 64> elts;
3572 : 424188 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (op), i, elt)
3573 : : {
3574 : 394092 : tree ref, op1;
3575 : 394092 : unsigned int elem;
3576 : :
3577 : 394092 : if (i >= nelts)
3578 : 150467 : return false;
3579 : :
3580 : : /* Look for elements extracted and possibly converted from
3581 : : another vector. */
3582 : 394092 : op1 = get_bit_field_ref_def (elt->value, conv_code);
3583 : 394092 : if (op1
3584 : 22312 : && TREE_CODE ((ref = TREE_OPERAND (op1, 0))) == SSA_NAME
3585 : 4419 : && VECTOR_TYPE_P (TREE_TYPE (ref))
3586 : 4416 : && useless_type_conversion_p (TREE_TYPE (op1),
3587 : 4416 : TREE_TYPE (TREE_TYPE (ref)))
3588 : 4031 : && constant_multiple_p (bit_field_offset (op1),
3589 : 398123 : bit_field_size (op1), &elem)
3590 : 398123 : && TYPE_VECTOR_SUBPARTS (TREE_TYPE (ref)).is_constant (&refnelts))
3591 : : {
3592 : : unsigned int j;
3593 : 4392 : for (j = 0; j < 2; ++j)
3594 : : {
3595 : 4381 : if (!orig[j])
3596 : : {
3597 : 2127 : if (j == 0
3598 : 2377 : || useless_type_conversion_p (TREE_TYPE (orig[0]),
3599 : 250 : TREE_TYPE (ref)))
3600 : : break;
3601 : : }
3602 : 2254 : else if (ref == orig[j])
3603 : : break;
3604 : : }
3605 : : /* Found a suitable vector element. */
3606 : 4031 : if (j < 2)
3607 : : {
3608 : 4020 : orig[j] = ref;
3609 : 4020 : if (elem != i || j != 0)
3610 : 1757 : maybe_ident = false;
3611 : 4020 : if (elem != i)
3612 : 1503 : maybe_blend[j] = false;
3613 : 4020 : elts.safe_push (std::make_pair (j, elem));
3614 : 4020 : continue;
3615 : : }
3616 : : /* Else fallthru. */
3617 : : }
3618 : : /* Handle elements not extracted from a vector.
3619 : : 1. constants by permuting with constant vector
3620 : : 2. a unique non-constant element by permuting with a splat vector */
3621 : 390072 : if (orig[1]
3622 : 240643 : && orig[1] != error_mark_node)
3623 : : return false;
3624 : 390060 : orig[1] = error_mark_node;
3625 : 390060 : if (CONSTANT_CLASS_P (elt->value))
3626 : : {
3627 : 25719 : if (one_nonconstant)
3628 : : return false;
3629 : 17787 : if (!one_constant)
3630 : 8730 : one_constant = elt->value;
3631 : 17787 : constants[i] = elt->value;
3632 : : }
3633 : : else
3634 : : {
3635 : 364341 : if (one_constant)
3636 : : return false;
3637 : 355846 : if (!one_nonconstant)
3638 : : one_nonconstant = elt->value;
3639 : 215147 : else if (!operand_equal_p (one_nonconstant, elt->value, 0))
3640 : : return false;
3641 : : }
3642 : 269701 : elts.safe_push (std::make_pair (1, i));
3643 : 269701 : maybe_ident = false;
3644 : : }
3645 : 30096 : if (i < nelts)
3646 : : return false;
3647 : :
3648 : 29331 : if (! orig[0]
3649 : 29331 : || ! VECTOR_TYPE_P (TREE_TYPE (orig[0])))
3650 : : return false;
3651 : 1449 : refnelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (orig[0])).to_constant ();
3652 : : /* We currently do not handle larger destination vectors. */
3653 : 1449 : if (refnelts < nelts)
3654 : : return false;
3655 : :
3656 : 1347 : if (maybe_ident)
3657 : : {
3658 : 452 : tree conv_src_type
3659 : : = (nelts != refnelts
3660 : 452 : ? (conv_code != ERROR_MARK
3661 : 12 : ? build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])), nelts)
3662 : : : type)
3663 : 440 : : TREE_TYPE (orig[0]));
3664 : 452 : if (conv_code != ERROR_MARK
3665 : 452 : && !supportable_convert_operation (conv_code, type, conv_src_type,
3666 : : &conv_code))
3667 : : {
3668 : : /* Only few targets implement direct conversion patterns so try
3669 : : some simple special cases via VEC_[UN]PACK[_FLOAT]_LO_EXPR. */
3670 : 4 : optab optab;
3671 : 4 : insn_code icode;
3672 : 4 : tree halfvectype, dblvectype;
3673 : 4 : enum tree_code unpack_op;
3674 : :
3675 : 4 : if (!BYTES_BIG_ENDIAN)
3676 : 4 : unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
3677 : 4 : ? VEC_UNPACK_FLOAT_LO_EXPR
3678 : : : VEC_UNPACK_LO_EXPR);
3679 : : else
3680 : : unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
3681 : : ? VEC_UNPACK_FLOAT_HI_EXPR
3682 : : : VEC_UNPACK_HI_EXPR);
3683 : :
3684 : : /* Conversions between DFP and FP have no special tree code
3685 : : but we cannot handle those since all relevant vector conversion
3686 : : optabs only have a single mode. */
3687 : 2 : if (CONVERT_EXPR_CODE_P (conv_code)
3688 : 2 : && FLOAT_TYPE_P (TREE_TYPE (type))
3689 : 8 : && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (type))
3690 : 2 : != DECIMAL_FLOAT_TYPE_P (TREE_TYPE (conv_src_type))))
3691 : : return false;
3692 : :
3693 : 2 : if (CONVERT_EXPR_CODE_P (conv_code)
3694 : 1 : && (2 * TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
3695 : 1 : == TYPE_PRECISION (TREE_TYPE (type)))
3696 : 0 : && mode_for_vector (as_a <scalar_mode>
3697 : 0 : (TYPE_MODE (TREE_TYPE (TREE_TYPE (orig[0])))),
3698 : 0 : nelts * 2).exists ()
3699 : 0 : && (dblvectype
3700 : 0 : = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
3701 : 0 : nelts * 2))
3702 : : /* Only use it for vector modes or for vector booleans
3703 : : represented as scalar bitmasks. See PR95528. */
3704 : 0 : && (VECTOR_MODE_P (TYPE_MODE (dblvectype))
3705 : 0 : || VECTOR_BOOLEAN_TYPE_P (dblvectype))
3706 : 0 : && (optab = optab_for_tree_code (unpack_op,
3707 : : dblvectype,
3708 : : optab_default))
3709 : 0 : && ((icode = optab_handler (optab, TYPE_MODE (dblvectype)))
3710 : : != CODE_FOR_nothing)
3711 : 3 : && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
3712 : : {
3713 : 0 : gimple_seq stmts = NULL;
3714 : 0 : tree dbl;
3715 : 0 : if (refnelts == nelts)
3716 : : {
3717 : : /* ??? Paradoxical subregs don't exist, so insert into
3718 : : the lower half of a wider zero vector. */
3719 : 0 : dbl = gimple_build (&stmts, BIT_INSERT_EXPR, dblvectype,
3720 : : build_zero_cst (dblvectype), orig[0],
3721 : 0 : bitsize_zero_node);
3722 : : }
3723 : 0 : else if (refnelts == 2 * nelts)
3724 : : dbl = orig[0];
3725 : : else
3726 : 0 : dbl = gimple_build (&stmts, BIT_FIELD_REF, dblvectype,
3727 : 0 : orig[0], TYPE_SIZE (dblvectype),
3728 : 0 : bitsize_zero_node);
3729 : 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3730 : 0 : gimple_assign_set_rhs_with_ops (gsi, unpack_op, dbl);
3731 : : }
3732 : 2 : else if (CONVERT_EXPR_CODE_P (conv_code)
3733 : 1 : && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
3734 : 1 : == 2 * TYPE_PRECISION (TREE_TYPE (type)))
3735 : 1 : && mode_for_vector (as_a <scalar_mode>
3736 : 1 : (TYPE_MODE
3737 : : (TREE_TYPE (TREE_TYPE (orig[0])))),
3738 : 2 : nelts / 2).exists ()
3739 : 1 : && (halfvectype
3740 : 1 : = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
3741 : 1 : nelts / 2))
3742 : : /* Only use it for vector modes or for vector booleans
3743 : : represented as scalar bitmasks. See PR95528. */
3744 : 1 : && (VECTOR_MODE_P (TYPE_MODE (halfvectype))
3745 : 0 : || VECTOR_BOOLEAN_TYPE_P (halfvectype))
3746 : 1 : && (optab = optab_for_tree_code (VEC_PACK_TRUNC_EXPR,
3747 : : halfvectype,
3748 : : optab_default))
3749 : 1 : && ((icode = optab_handler (optab, TYPE_MODE (halfvectype)))
3750 : : != CODE_FOR_nothing)
3751 : 4 : && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
3752 : : {
3753 : 0 : gimple_seq stmts = NULL;
3754 : 0 : tree low = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
3755 : 0 : orig[0], TYPE_SIZE (halfvectype),
3756 : 0 : bitsize_zero_node);
3757 : 0 : tree hig = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
3758 : 0 : orig[0], TYPE_SIZE (halfvectype),
3759 : 0 : TYPE_SIZE (halfvectype));
3760 : 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3761 : 0 : gimple_assign_set_rhs_with_ops (gsi, VEC_PACK_TRUNC_EXPR,
3762 : : low, hig);
3763 : : }
3764 : : else
3765 : 3 : return false;
3766 : 0 : update_stmt (gsi_stmt (*gsi));
3767 : 0 : return true;
3768 : : }
3769 : 448 : if (nelts != refnelts)
3770 : : {
3771 : 12 : gassign *lowpart
3772 : 12 : = gimple_build_assign (make_ssa_name (conv_src_type),
3773 : : build3 (BIT_FIELD_REF, conv_src_type,
3774 : 12 : orig[0], TYPE_SIZE (conv_src_type),
3775 : : bitsize_zero_node));
3776 : 12 : gsi_insert_before (gsi, lowpart, GSI_SAME_STMT);
3777 : 12 : orig[0] = gimple_assign_lhs (lowpart);
3778 : : }
3779 : 448 : if (conv_code == ERROR_MARK)
3780 : : {
3781 : 431 : tree src_type = TREE_TYPE (orig[0]);
3782 : 431 : if (!useless_type_conversion_p (type, src_type))
3783 : : {
3784 : 0 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
3785 : : TYPE_VECTOR_SUBPARTS (src_type))
3786 : : && useless_type_conversion_p (TREE_TYPE (type),
3787 : : TREE_TYPE (src_type)));
3788 : 0 : tree rhs = build1 (VIEW_CONVERT_EXPR, type, orig[0]);
3789 : 0 : orig[0] = make_ssa_name (type);
3790 : 0 : gassign *assign = gimple_build_assign (orig[0], rhs);
3791 : 0 : gsi_insert_before (gsi, assign, GSI_SAME_STMT);
3792 : : }
3793 : 431 : gimple_assign_set_rhs_from_tree (gsi, orig[0]);
3794 : : }
3795 : : else
3796 : 17 : gimple_assign_set_rhs_with_ops (gsi, conv_code, orig[0],
3797 : : NULL_TREE, NULL_TREE);
3798 : : }
3799 : : else
3800 : : {
3801 : : /* If we combine a vector with a non-vector avoid cases where
3802 : : we'll obviously end up with more GIMPLE stmts which is when
3803 : : we'll later not fold this to a single insert into the vector
3804 : : and we had a single extract originally. See PR92819. */
3805 : 895 : if (nelts == 2
3806 : 708 : && refnelts > 2
3807 : 50 : && orig[1] == error_mark_node
3808 : 25 : && !maybe_blend[0])
3809 : 359 : return false;
3810 : 872 : tree mask_type, perm_type, conv_src_type;
3811 : 872 : perm_type = TREE_TYPE (orig[0]);
3812 : 872 : conv_src_type = (nelts == refnelts
3813 : 872 : ? perm_type
3814 : 47 : : build_vector_type (TREE_TYPE (perm_type), nelts));
3815 : 872 : if (conv_code != ERROR_MARK
3816 : 872 : && !supportable_convert_operation (conv_code, type, conv_src_type,
3817 : : &conv_code))
3818 : : return false;
3819 : :
3820 : : /* Now that we know the number of elements of the source build the
3821 : : permute vector.
3822 : : ??? When the second vector has constant values we can shuffle
3823 : : it and its source indexes to make the permutation supported.
3824 : : For now it mimics a blend. */
3825 : 631 : vec_perm_builder sel (refnelts, refnelts, 1);
3826 : 631 : bool all_same_p = true;
3827 : 5398 : for (i = 0; i < elts.length (); ++i)
3828 : : {
3829 : 2068 : sel.quick_push (elts[i].second + elts[i].first * refnelts);
3830 : 2068 : all_same_p &= known_eq (sel[i], sel[0]);
3831 : : }
3832 : : /* And fill the tail with "something". It's really don't care,
3833 : : and ideally we'd allow VEC_PERM to have a smaller destination
3834 : : vector. As a heuristic:
3835 : :
3836 : : (a) if what we have so far duplicates a single element, make the
3837 : : tail do the same
3838 : :
3839 : : (b) otherwise preserve a uniform orig[0]. This facilitates
3840 : : later pattern-matching of VEC_PERM_EXPR to a BIT_INSERT_EXPR. */
3841 : 937 : for (; i < refnelts; ++i)
3842 : 612 : sel.quick_push (all_same_p
3843 : 918 : ? sel[0]
3844 : 24 : : (elts[0].second == 0 && elts[0].first == 0
3845 : 212 : ? 0 : refnelts) + i);
3846 : 744 : vec_perm_indices indices (sel, orig[1] ? 2 : 1, refnelts);
3847 : 631 : machine_mode vmode = TYPE_MODE (perm_type);
3848 : 631 : if (!can_vec_perm_const_p (vmode, vmode, indices))
3849 : : return false;
3850 : 536 : mask_type
3851 : 536 : = build_vector_type (build_nonstandard_integer_type (elem_size, 1),
3852 : 536 : refnelts);
3853 : 536 : if (GET_MODE_CLASS (TYPE_MODE (mask_type)) != MODE_VECTOR_INT
3854 : 1608 : || maybe_ne (GET_MODE_SIZE (TYPE_MODE (mask_type)),
3855 : 1072 : GET_MODE_SIZE (TYPE_MODE (perm_type))))
3856 : 0 : return false;
3857 : 536 : tree op2 = vec_perm_indices_to_tree (mask_type, indices);
3858 : 536 : bool converted_orig1 = false;
3859 : 536 : gimple_seq stmts = NULL;
3860 : 536 : if (!orig[1])
3861 : 74 : orig[1] = orig[0];
3862 : 462 : else if (orig[1] == error_mark_node
3863 : 229 : && one_nonconstant)
3864 : : {
3865 : : /* ??? We can see if we can safely convert to the original
3866 : : element type. */
3867 : 162 : converted_orig1 = conv_code != ERROR_MARK;
3868 : 323 : orig[1] = gimple_build_vector_from_val (&stmts, UNKNOWN_LOCATION,
3869 : : converted_orig1
3870 : : ? type : perm_type,
3871 : : one_nonconstant);
3872 : : }
3873 : 300 : else if (orig[1] == error_mark_node)
3874 : : {
3875 : : /* ??? See if we can convert the vector to the original type. */
3876 : 67 : converted_orig1 = conv_code != ERROR_MARK;
3877 : 67 : unsigned n = converted_orig1 ? nelts : refnelts;
3878 : 53 : tree_vector_builder vec (converted_orig1
3879 : 67 : ? type : perm_type, n, 1);
3880 : 409 : for (unsigned i = 0; i < n; ++i)
3881 : 680 : if (i < nelts && constants[i])
3882 : 171 : vec.quick_push (constants[i]);
3883 : : else
3884 : : /* ??? Push a don't-care value. */
3885 : 171 : vec.quick_push (one_constant);
3886 : 67 : orig[1] = vec.build ();
3887 : 67 : }
3888 : 303 : tree blend_op2 = NULL_TREE;
3889 : 303 : if (converted_orig1)
3890 : : {
3891 : : /* Make sure we can do a blend in the target type. */
3892 : 15 : vec_perm_builder sel (nelts, nelts, 1);
3893 : 75 : for (i = 0; i < elts.length (); ++i)
3894 : 60 : sel.quick_push (elts[i].first
3895 : 60 : ? elts[i].second + nelts : i);
3896 : 15 : vec_perm_indices indices (sel, 2, nelts);
3897 : 15 : machine_mode vmode = TYPE_MODE (type);
3898 : 15 : if (!can_vec_perm_const_p (vmode, vmode, indices))
3899 : : return false;
3900 : 15 : mask_type
3901 : 15 : = build_vector_type (build_nonstandard_integer_type (elem_size, 1),
3902 : 15 : nelts);
3903 : 15 : if (GET_MODE_CLASS (TYPE_MODE (mask_type)) != MODE_VECTOR_INT
3904 : 45 : || maybe_ne (GET_MODE_SIZE (TYPE_MODE (mask_type)),
3905 : 30 : GET_MODE_SIZE (TYPE_MODE (type))))
3906 : 0 : return false;
3907 : 15 : blend_op2 = vec_perm_indices_to_tree (mask_type, indices);
3908 : 15 : }
3909 : 536 : tree orig1_for_perm
3910 : 536 : = converted_orig1 ? build_zero_cst (perm_type) : orig[1];
3911 : 536 : tree res = gimple_build (&stmts, VEC_PERM_EXPR, perm_type,
3912 : : orig[0], orig1_for_perm, op2);
3913 : 536 : if (nelts != refnelts)
3914 : 32 : res = gimple_build (&stmts, BIT_FIELD_REF,
3915 : 32 : conv_code != ERROR_MARK ? conv_src_type : type,
3916 : 32 : res, TYPE_SIZE (type), bitsize_zero_node);
3917 : 536 : if (conv_code != ERROR_MARK)
3918 : 22 : res = gimple_build (&stmts, conv_code, type, res);
3919 : 514 : else if (!useless_type_conversion_p (type, TREE_TYPE (res)))
3920 : : {
3921 : 0 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
3922 : : TYPE_VECTOR_SUBPARTS (perm_type))
3923 : : && useless_type_conversion_p (TREE_TYPE (type),
3924 : : TREE_TYPE (perm_type)));
3925 : 0 : res = gimple_build (&stmts, VIEW_CONVERT_EXPR, type, res);
3926 : : }
3927 : : /* Blend in the actual constant. */
3928 : 536 : if (converted_orig1)
3929 : 15 : res = gimple_build (&stmts, VEC_PERM_EXPR, type,
3930 : 15 : res, orig[1], blend_op2);
3931 : 536 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3932 : 536 : gimple_assign_set_rhs_with_ops (gsi, SSA_NAME, res);
3933 : 631 : }
3934 : 984 : update_stmt (gsi_stmt (*gsi));
3935 : 984 : return true;
3936 : 150467 : }
3937 : :
3938 : : /* Prepare a TARGET_MEM_REF ref so that it can be subsetted as
3939 : : lvalue. This splits out an address computation stmt before *GSI
3940 : : and returns a MEM_REF wrapping the address. */
3941 : :
3942 : : static tree
3943 : 1202 : prepare_target_mem_ref_lvalue (tree ref, gimple_stmt_iterator *gsi)
3944 : : {
3945 : 1202 : if (TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR)
3946 : 345 : mark_addressable (TREE_OPERAND (TREE_OPERAND (ref, 0), 0));
3947 : 1202 : tree ptrtype = build_pointer_type (TREE_TYPE (ref));
3948 : 1202 : tree tem = make_ssa_name (ptrtype);
3949 : 1202 : gimple *new_stmt
3950 : 1202 : = gimple_build_assign (tem, build1 (ADDR_EXPR, TREE_TYPE (tem),
3951 : : unshare_expr (ref)));
3952 : 1202 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
3953 : 2404 : ref = build2_loc (EXPR_LOCATION (ref),
3954 : 1202 : MEM_REF, TREE_TYPE (ref), tem,
3955 : 1202 : build_int_cst (TREE_TYPE (TREE_OPERAND (ref, 1)), 0));
3956 : 1202 : return ref;
3957 : : }
3958 : :
3959 : : /* Rewrite the vector load at *GSI to component-wise loads if the load
3960 : : is only used in BIT_FIELD_REF extractions with eventual intermediate
3961 : : widening. */
3962 : :
3963 : : static void
3964 : 278872 : optimize_vector_load (gimple_stmt_iterator *gsi)
3965 : : {
3966 : 278872 : gimple *stmt = gsi_stmt (*gsi);
3967 : 278872 : tree lhs = gimple_assign_lhs (stmt);
3968 : 278872 : tree rhs = gimple_assign_rhs1 (stmt);
3969 : 278872 : tree vuse = gimple_vuse (stmt);
3970 : :
3971 : : /* Gather BIT_FIELD_REFs to rewrite, looking through
3972 : : VEC_UNPACK_{LO,HI}_EXPR. */
3973 : 278872 : use_operand_p use_p;
3974 : 278872 : imm_use_iterator iter;
3975 : 278872 : bool rewrite = true;
3976 : 278872 : auto_vec<gimple *, 8> bf_stmts;
3977 : 278872 : auto_vec<tree, 8> worklist;
3978 : 278872 : worklist.quick_push (lhs);
3979 : 280568 : do
3980 : : {
3981 : 280568 : tree def = worklist.pop ();
3982 : 280568 : unsigned HOST_WIDE_INT def_eltsize
3983 : 280568 : = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (def))));
3984 : 354152 : FOR_EACH_IMM_USE_FAST (use_p, iter, def)
3985 : : {
3986 : 334109 : gimple *use_stmt = USE_STMT (use_p);
3987 : 334109 : if (is_gimple_debug (use_stmt))
3988 : 73584 : continue;
3989 : 334076 : if (!is_gimple_assign (use_stmt))
3990 : : {
3991 : : rewrite = false;
3992 : 260525 : break;
3993 : : }
3994 : 300778 : enum tree_code use_code = gimple_assign_rhs_code (use_stmt);
3995 : 300778 : tree use_rhs = gimple_assign_rhs1 (use_stmt);
3996 : 370906 : if (use_code == BIT_FIELD_REF
3997 : 70129 : && TREE_OPERAND (use_rhs, 0) == def
3998 : : /* If its on the VEC_UNPACK_{HI,LO}_EXPR
3999 : : def need to verify it is element aligned. */
4000 : 370907 : && (def == lhs
4001 : 85 : || (known_eq (bit_field_size (use_rhs), def_eltsize)
4002 : 85 : && constant_multiple_p (bit_field_offset (use_rhs),
4003 : : def_eltsize)
4004 : : /* We can simulate the VEC_UNPACK_{HI,LO}_EXPR
4005 : : via a NOP_EXPR only for integral types.
4006 : : ??? Support VEC_UNPACK_FLOAT_{HI,LO}_EXPR. */
4007 : 85 : && INTEGRAL_TYPE_P (TREE_TYPE (use_rhs)))))
4008 : : {
4009 : 70128 : bf_stmts.safe_push (use_stmt);
4010 : 70128 : continue;
4011 : : }
4012 : : /* Walk through one level of VEC_UNPACK_{LO,HI}_EXPR. */
4013 : 230650 : if (def == lhs
4014 : 229001 : && (use_code == VEC_UNPACK_HI_EXPR
4015 : 229001 : || use_code == VEC_UNPACK_LO_EXPR)
4016 : 3423 : && use_rhs == lhs)
4017 : : {
4018 : 3423 : worklist.safe_push (gimple_assign_lhs (use_stmt));
4019 : 3423 : continue;
4020 : : }
4021 : : rewrite = false;
4022 : : break;
4023 : : }
4024 : 260525 : if (!rewrite)
4025 : : break;
4026 : : }
4027 : 40086 : while (!worklist.is_empty ());
4028 : :
4029 : 278872 : if (!rewrite)
4030 : : {
4031 : 260525 : gsi_next (gsi);
4032 : 260525 : return;
4033 : : }
4034 : : /* We now have all ultimate uses of the load to rewrite in bf_stmts. */
4035 : :
4036 : : /* Prepare the original ref to be wrapped in adjusted BIT_FIELD_REFs.
4037 : : For TARGET_MEM_REFs we have to separate the LEA from the reference. */
4038 : 18347 : tree load_rhs = rhs;
4039 : 18347 : if (TREE_CODE (load_rhs) == TARGET_MEM_REF)
4040 : 1201 : load_rhs = prepare_target_mem_ref_lvalue (load_rhs, gsi);
4041 : :
4042 : : /* Rewrite the BIT_FIELD_REFs to be actual loads, re-emitting them at
4043 : : the place of the original load. */
4044 : 120577 : for (gimple *use_stmt : bf_stmts)
4045 : : {
4046 : 65536 : tree bfr = gimple_assign_rhs1 (use_stmt);
4047 : 65536 : tree new_rhs = unshare_expr (load_rhs);
4048 : 65536 : if (TREE_OPERAND (bfr, 0) != lhs)
4049 : : {
4050 : : /* When the BIT_FIELD_REF is on the promoted vector we have to
4051 : : adjust it and emit a conversion afterwards. */
4052 : 84 : gimple *def_stmt
4053 : 84 : = SSA_NAME_DEF_STMT (TREE_OPERAND (bfr, 0));
4054 : 84 : enum tree_code def_code
4055 : 84 : = gimple_assign_rhs_code (def_stmt);
4056 : :
4057 : : /* The adjusted BIT_FIELD_REF is of the promotion source
4058 : : vector size and at half of the offset... */
4059 : 84 : new_rhs = fold_build3 (BIT_FIELD_REF,
4060 : : TREE_TYPE (TREE_TYPE (lhs)),
4061 : : new_rhs,
4062 : : TYPE_SIZE (TREE_TYPE (TREE_TYPE (lhs))),
4063 : : size_binop (EXACT_DIV_EXPR,
4064 : : TREE_OPERAND (bfr, 2),
4065 : : bitsize_int (2)));
4066 : : /* ... and offsetted by half of the vector if VEC_UNPACK_HI_EXPR. */
4067 : 84 : if (def_code == (!BYTES_BIG_ENDIAN
4068 : : ? VEC_UNPACK_HI_EXPR : VEC_UNPACK_LO_EXPR))
4069 : 42 : TREE_OPERAND (new_rhs, 2)
4070 : 84 : = size_binop (PLUS_EXPR, TREE_OPERAND (new_rhs, 2),
4071 : : size_binop (EXACT_DIV_EXPR,
4072 : : TYPE_SIZE (TREE_TYPE (lhs)),
4073 : : bitsize_int (2)));
4074 : 84 : tree tem = make_ssa_name (TREE_TYPE (TREE_TYPE (lhs)));
4075 : 84 : gimple *new_stmt = gimple_build_assign (tem, new_rhs);
4076 : 84 : location_t loc = gimple_location (use_stmt);
4077 : 84 : gimple_set_location (new_stmt, loc);
4078 : 84 : gimple_set_vuse (new_stmt, vuse);
4079 : 84 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4080 : : /* Perform scalar promotion. */
4081 : 84 : new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
4082 : : NOP_EXPR, tem);
4083 : 84 : gimple_set_location (new_stmt, loc);
4084 : 84 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4085 : : }
4086 : : else
4087 : : {
4088 : : /* When the BIT_FIELD_REF is on the original load result
4089 : : we can just wrap that. */
4090 : 65452 : tree new_rhs = fold_build3 (BIT_FIELD_REF, TREE_TYPE (bfr),
4091 : : unshare_expr (load_rhs),
4092 : : TREE_OPERAND (bfr, 1),
4093 : : TREE_OPERAND (bfr, 2));
4094 : 65452 : gimple *new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
4095 : : new_rhs);
4096 : 65452 : location_t loc = gimple_location (use_stmt);
4097 : 65452 : gimple_set_location (new_stmt, loc);
4098 : 65452 : gimple_set_vuse (new_stmt, vuse);
4099 : 65452 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4100 : : }
4101 : 65536 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
4102 : 65536 : unlink_stmt_vdef (use_stmt);
4103 : 65536 : gsi_remove (&gsi2, true);
4104 : : }
4105 : :
4106 : : /* Finally get rid of the intermediate stmts. */
4107 : 18347 : gimple *use_stmt;
4108 : 18449 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
4109 : : {
4110 : 102 : if (is_gimple_debug (use_stmt))
4111 : : {
4112 : 74 : if (gimple_debug_bind_p (use_stmt))
4113 : : {
4114 : 74 : gimple_debug_bind_reset_value (use_stmt);
4115 : 74 : update_stmt (use_stmt);
4116 : : }
4117 : 74 : continue;
4118 : : }
4119 : 28 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
4120 : 28 : unlink_stmt_vdef (use_stmt);
4121 : 28 : release_defs (use_stmt);
4122 : 28 : gsi_remove (&gsi2, true);
4123 : 18347 : }
4124 : : /* And the original load. */
4125 : 18347 : release_defs (stmt);
4126 : 18347 : gsi_remove (gsi, true);
4127 : 278872 : }
4128 : :
4129 : :
4130 : : /* Primitive "lattice" function for gimple_simplify. */
4131 : :
4132 : : static tree
4133 : 1497499191 : fwprop_ssa_val (tree name)
4134 : : {
4135 : : /* First valueize NAME. */
4136 : 1497499191 : if (TREE_CODE (name) == SSA_NAME
4137 : 1497499191 : && SSA_NAME_VERSION (name) < lattice.length ())
4138 : : {
4139 : 1496823370 : tree val = lattice[SSA_NAME_VERSION (name)];
4140 : 1496823370 : if (val)
4141 : 1497499191 : name = val;
4142 : : }
4143 : : /* We continue matching along SSA use-def edges for SSA names
4144 : : that are not single-use. Currently there are no patterns
4145 : : that would cause any issues with that. */
4146 : 1497499191 : return name;
4147 : : }
4148 : :
4149 : : /* Search for opportunities to free half of the lanes in the following pattern:
4150 : :
4151 : : v_in = {e0, e1, e2, e3}
4152 : : v_1 = VEC_PERM <v_in, v_in, {0, 2, 0, 2}>
4153 : : // v_1 = {e0, e2, e0, e2}
4154 : : v_2 = VEC_PERM <v_in, v_in, {1, 3, 1, 3}>
4155 : : // v_2 = {e1, e3, e1, e3}
4156 : :
4157 : : v_x = v_1 + v_2
4158 : : // v_x = {e0+e1, e2+e3, e0+e1, e2+e3}
4159 : : v_y = v_1 - v_2
4160 : : // v_y = {e0-e1, e2-e3, e0-e1, e2-e3}
4161 : :
4162 : : v_out = VEC_PERM <v_x, v_y, {0, 1, 6, 7}>
4163 : : // v_out = {e0+e1, e2+e3, e0-e1, e2-e3}
4164 : :
4165 : : The last statement could be simplified to:
4166 : : v_out' = VEC_PERM <v_x, v_y, {0, 1, 4, 5}>
4167 : : // v_out' = {e0+e1, e2+e3, e0-e1, e2-e3}
4168 : :
4169 : : Characteristic properties:
4170 : : - v_1 and v_2 are created from the same input vector v_in and introduce the
4171 : : lane duplication (in the selection operand) that we can eliminate.
4172 : : - v_x and v_y are results from lane-preserving operations that use v_1 and
4173 : : v_2 as inputs.
4174 : : - v_out is created by selecting from duplicated lanes. */
4175 : :
4176 : : static bool
4177 : 179170 : recognise_vec_perm_simplify_seq (gassign *stmt, vec_perm_simplify_seq *seq)
4178 : : {
4179 : 179170 : unsigned HOST_WIDE_INT nelts;
4180 : :
4181 : 179170 : gcc_checking_assert (stmt);
4182 : 179170 : gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
4183 : 179170 : basic_block bb = gimple_bb (stmt);
4184 : :
4185 : : /* Decompose the final vec permute statement. */
4186 : 179170 : tree v_x = gimple_assign_rhs1 (stmt);
4187 : 179170 : tree v_y = gimple_assign_rhs2 (stmt);
4188 : 179170 : tree sel = gimple_assign_rhs3 (stmt);
4189 : :
4190 : 179170 : if (TREE_CODE (sel) != VECTOR_CST
4191 : 176452 : || !VECTOR_CST_NELTS (sel).is_constant (&nelts)
4192 : 176452 : || TREE_CODE (v_x) != SSA_NAME
4193 : 174624 : || TREE_CODE (v_y) != SSA_NAME
4194 : 172316 : || !has_single_use (v_x)
4195 : 286281 : || !has_single_use (v_y))
4196 : 73694 : return false;
4197 : :
4198 : : /* Don't analyse sequences with many lanes. */
4199 : 105476 : if (nelts > 4)
4200 : : return false;
4201 : :
4202 : : /* Lookup the definition of v_x and v_y. */
4203 : 103520 : gassign *v_x_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x));
4204 : 103520 : gassign *v_y_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_y));
4205 : 103152 : if (!v_x_stmt || gimple_bb (v_x_stmt) != bb
4206 : 206672 : || !v_y_stmt || gimple_bb (v_y_stmt) != bb)
4207 : : return false;
4208 : :
4209 : : /* Check the operations that define v_x and v_y. */
4210 : 103151 : if (TREE_CODE_CLASS (gimple_assign_rhs_code (v_x_stmt)) != tcc_binary
4211 : 105065 : || TREE_CODE_CLASS (gimple_assign_rhs_code (v_y_stmt)) != tcc_binary)
4212 : : return false;
4213 : :
4214 : 1914 : tree v_x_1 = gimple_assign_rhs1 (v_x_stmt);
4215 : 1914 : tree v_x_2 = gimple_assign_rhs2 (v_x_stmt);
4216 : 1914 : tree v_y_1 = gimple_assign_rhs1 (v_y_stmt);
4217 : 1914 : tree v_y_2 = gimple_assign_rhs2 (v_y_stmt);
4218 : :
4219 : 1914 : if (v_x_stmt == v_y_stmt
4220 : 1914 : || TREE_CODE (v_x_1) != SSA_NAME
4221 : 1911 : || TREE_CODE (v_x_2) != SSA_NAME
4222 : 1909 : || num_imm_uses (v_x_1) != 2
4223 : 3749 : || num_imm_uses (v_x_2) != 2)
4224 : : return false;
4225 : :
4226 : 1740 : if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
4227 : : {
4228 : : /* Allow operands of commutative operators to swap. */
4229 : 627 : if (commutative_tree_code (gimple_assign_rhs_code (v_x_stmt)))
4230 : : {
4231 : : /* Keep v_x_1 the first operand for non-commutative operators. */
4232 : 241 : v_x_1 = gimple_assign_rhs2 (v_x_stmt);
4233 : 241 : v_x_2 = gimple_assign_rhs1 (v_x_stmt);
4234 : 241 : if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
4235 : : return false;
4236 : : }
4237 : 386 : else if (commutative_tree_code (gimple_assign_rhs_code (v_y_stmt)))
4238 : : {
4239 : 386 : if (v_x_1 != v_y_2 || v_x_2 != v_y_1)
4240 : : return false;
4241 : : }
4242 : : else
4243 : : return false;
4244 : : }
4245 : 1740 : gassign *v_1_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_1));
4246 : 1740 : gassign *v_2_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_2));
4247 : 1676 : if (!v_1_stmt || gimple_bb (v_1_stmt) != bb
4248 : 3416 : || !v_2_stmt || gimple_bb (v_2_stmt) != bb)
4249 : : return false;
4250 : :
4251 : 1672 : if (gimple_assign_rhs_code (v_1_stmt) != VEC_PERM_EXPR
4252 : 1782 : || gimple_assign_rhs_code (v_2_stmt) != VEC_PERM_EXPR)
4253 : : return false;
4254 : :
4255 : : /* Decompose initial VEC_PERM_EXPRs. */
4256 : 106 : tree v_in = gimple_assign_rhs1 (v_1_stmt);
4257 : 106 : tree v_1_sel = gimple_assign_rhs3 (v_1_stmt);
4258 : 106 : tree v_2_sel = gimple_assign_rhs3 (v_2_stmt);
4259 : 106 : if (v_in != gimple_assign_rhs2 (v_1_stmt)
4260 : 101 : || v_in != gimple_assign_rhs1 (v_2_stmt)
4261 : 205 : || v_in != gimple_assign_rhs2 (v_2_stmt))
4262 : : return false;
4263 : :
4264 : 99 : unsigned HOST_WIDE_INT v_1_nelts, v_2_nelts;
4265 : 99 : if (TREE_CODE (v_1_sel) != VECTOR_CST
4266 : 99 : || !VECTOR_CST_NELTS (v_1_sel).is_constant (&v_1_nelts)
4267 : 99 : || TREE_CODE (v_2_sel) != VECTOR_CST
4268 : 198 : || !VECTOR_CST_NELTS (v_2_sel).is_constant (&v_2_nelts))
4269 : 0 : return false;
4270 : :
4271 : 99 : if (nelts != v_1_nelts || nelts != v_2_nelts)
4272 : : return false;
4273 : :
4274 : : /* Create the new selector. */
4275 : 99 : vec_perm_builder new_sel_perm (nelts, nelts, 1);
4276 : 99 : auto_vec<unsigned int> lanes (nelts);
4277 : 99 : lanes.quick_grow_cleared (nelts);
4278 : 495 : for (unsigned int i = 0; i < nelts; i++)
4279 : : {
4280 : : /* Extract the i-th value from the selector. */
4281 : 396 : unsigned int sel_cst = TREE_INT_CST_LOW (VECTOR_CST_ELT (sel, i));
4282 : 396 : unsigned int lane = sel_cst % nelts;
4283 : 396 : unsigned int offs = sel_cst / nelts;
4284 : :
4285 : : /* Check what's in the lane. */
4286 : 396 : unsigned int e_1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, lane));
4287 : 396 : unsigned int e_2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, lane));
4288 : :
4289 : : /* Reuse previous lane (if any). */
4290 : 396 : unsigned int l = 0;
4291 : 675 : for (; l < lane; l++)
4292 : : {
4293 : 477 : if ((TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, l)) == e_1)
4294 : 477 : && (TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, l)) == e_2))
4295 : : break;
4296 : : }
4297 : :
4298 : : /* Add to narrowed selector. */
4299 : 396 : new_sel_perm.quick_push (l + offs * nelts);
4300 : :
4301 : : /* Mark lane as used. */
4302 : 396 : lanes[l] = 1;
4303 : : }
4304 : :
4305 : : /* Count how many lanes are need. */
4306 : : unsigned int cnt = 0;
4307 : 495 : for (unsigned int i = 0; i < nelts; i++)
4308 : 396 : cnt += lanes[i];
4309 : :
4310 : : /* If more than (nelts/2) lanes are needed, skip the sequence. */
4311 : 99 : if (cnt > nelts / 2)
4312 : : return false;
4313 : :
4314 : : /* Check if the resulting permuation is cheap. */
4315 : 99 : vec_perm_indices new_indices (new_sel_perm, 2, nelts);
4316 : 99 : tree vectype = TREE_TYPE (gimple_assign_lhs (stmt));
4317 : 99 : machine_mode vmode = TYPE_MODE (vectype);
4318 : 99 : if (!can_vec_perm_const_p (vmode, vmode, new_indices, false))
4319 : : return false;
4320 : :
4321 : 99 : *seq = XNEW (struct _vec_perm_simplify_seq);
4322 : 99 : (*seq)->stmt = stmt;
4323 : 99 : (*seq)->v_1_stmt = v_1_stmt;
4324 : 99 : (*seq)->v_2_stmt = v_2_stmt;
4325 : 99 : (*seq)->v_x_stmt = v_x_stmt;
4326 : 99 : (*seq)->v_y_stmt = v_y_stmt;
4327 : 99 : (*seq)->nelts = nelts;
4328 : 99 : (*seq)->new_sel = vect_gen_perm_mask_checked (vectype, new_indices);
4329 : :
4330 : 99 : if (dump_file)
4331 : : {
4332 : 26 : fprintf (dump_file, "Found vec perm simplify sequence ending with:\n\t");
4333 : 26 : print_gimple_stmt (dump_file, stmt, 0);
4334 : :
4335 : 26 : if (dump_flags & TDF_DETAILS)
4336 : : {
4337 : 26 : fprintf (dump_file, "\tNarrowed vec_perm selector: ");
4338 : 26 : print_generic_expr (dump_file, (*seq)->new_sel);
4339 : 26 : fprintf (dump_file, "\n");
4340 : : }
4341 : : }
4342 : :
4343 : : return true;
4344 : 198 : }
4345 : :
4346 : : /* Reduce the lane consumption of a simplifiable vec perm sequence. */
4347 : :
4348 : : static void
4349 : 72 : narrow_vec_perm_simplify_seq (const vec_perm_simplify_seq &seq)
4350 : : {
4351 : 72 : gassign *stmt = seq->stmt;
4352 : 72 : if (dump_file && (dump_flags & TDF_DETAILS))
4353 : : {
4354 : 20 : fprintf (dump_file, "Updating VEC_PERM statment:\n");
4355 : 20 : fprintf (dump_file, "Old stmt: ");
4356 : 20 : print_gimple_stmt (dump_file, stmt, 0);
4357 : : }
4358 : :
4359 : : /* Update the last VEC_PERM statement. */
4360 : 72 : gimple_assign_set_rhs3 (stmt, seq->new_sel);
4361 : 72 : update_stmt (stmt);
4362 : :
4363 : 72 : if (dump_file && (dump_flags & TDF_DETAILS))
4364 : : {
4365 : 20 : fprintf (dump_file, "New stmt: ");
4366 : 20 : print_gimple_stmt (dump_file, stmt, 0);
4367 : : }
4368 : 72 : }
4369 : :
4370 : : /* Test if we can blend two simplifiable vec permute sequences.
4371 : : NEED_SWAP will be set, if sequences must be swapped for blending. */
4372 : :
4373 : : static bool
4374 : 46 : can_blend_vec_perm_simplify_seqs_p (vec_perm_simplify_seq seq1,
4375 : : vec_perm_simplify_seq seq2,
4376 : : bool *need_swap)
4377 : : {
4378 : 46 : unsigned int nelts = seq1->nelts;
4379 : 46 : basic_block bb = gimple_bb (seq1->stmt);
4380 : :
4381 : 46 : gcc_assert (gimple_bb (seq2->stmt) == bb);
4382 : :
4383 : : /* BBs and number of elements must be equal. */
4384 : 46 : if (gimple_bb (seq2->stmt) != bb || seq2->nelts != nelts)
4385 : : return false;
4386 : :
4387 : : /* We need vectors of the same type. */
4388 : 46 : if (TREE_TYPE (gimple_assign_lhs (seq1->stmt))
4389 : 46 : != TREE_TYPE (gimple_assign_lhs (seq2->stmt)))
4390 : : return false;
4391 : :
4392 : : /* We require isomorphic operators. */
4393 : 40 : if (((gimple_assign_rhs_code (seq1->v_x_stmt)
4394 : 40 : != gimple_assign_rhs_code (seq2->v_x_stmt))
4395 : 40 : || (gimple_assign_rhs_code (seq1->v_y_stmt)
4396 : 40 : != gimple_assign_rhs_code (seq2->v_y_stmt))))
4397 : : return false;
4398 : :
4399 : : /* We cannot have any dependencies between the sequences.
4400 : :
4401 : : For merging, we will reuse seq1->v_1_stmt and seq1->v_2_stmt.
4402 : : seq1's v_in is defined before these statements, but we need
4403 : : to check if seq2's v_in is defined before them as well.
4404 : :
4405 : : Further, we will reuse seq2->stmt. We need to ensure that
4406 : : seq1->v_x_stmt and seq1->v_y_stmt are before it.
4407 : :
4408 : : Note, that we don't need to check the BBs here, because all
4409 : : statements of both sequences have to be in the same BB.
4410 : : */
4411 : :
4412 : 40 : tree seq2_v_in = gimple_assign_rhs1 (seq2->v_1_stmt);
4413 : 40 : if (TREE_CODE (seq2_v_in) != SSA_NAME)
4414 : : return false;
4415 : :
4416 : 40 : gassign *seq2_v_in_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq2_v_in));
4417 : 40 : if (!seq2_v_in_stmt || gimple_bb (seq2_v_in_stmt) != bb
4418 : 40 : || (gimple_uid (seq2_v_in_stmt) > gimple_uid (seq1->v_1_stmt))
4419 : 36 : || (gimple_uid (seq1->v_x_stmt) > gimple_uid (seq2->stmt))
4420 : 36 : || (gimple_uid (seq1->v_y_stmt) > gimple_uid (seq2->stmt)))
4421 : : {
4422 : 4 : tree seq1_v_in = gimple_assign_rhs1 (seq1->v_1_stmt);
4423 : 4 : if (TREE_CODE (seq1_v_in) != SSA_NAME)
4424 : : return false;
4425 : :
4426 : 4 : gassign *seq1_v_in_stmt
4427 : 4 : = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq1_v_in));
4428 : : /* Let's try to see if we succeed when swapping the sequences. */
4429 : 4 : if (!seq1_v_in_stmt || gimple_bb (seq1_v_in_stmt)
4430 : 0 : || (gimple_uid (seq1_v_in_stmt) > gimple_uid (seq2->v_1_stmt))
4431 : 0 : || (gimple_uid (seq2->v_x_stmt) > gimple_uid (seq1->stmt))
4432 : 0 : || (gimple_uid (seq2->v_y_stmt) > gimple_uid (seq1->stmt)))
4433 : : return false;
4434 : 0 : *need_swap = true;
4435 : : }
4436 : : else
4437 : 36 : *need_swap = false;
4438 : :
4439 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4440 : 10 : fprintf (dump_file, "Found vec perm simplify sequence pair.\n");
4441 : :
4442 : : return true;
4443 : : }
4444 : :
4445 : : /* Calculate the permutations for blending the two given vec permute
4446 : : sequences. This may fail if the resulting permutation is not
4447 : : supported. */
4448 : :
4449 : : static bool
4450 : 36 : calc_perm_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
4451 : : vec_perm_simplify_seq seq2,
4452 : : vec_perm_indices *seq2_stmt_indices,
4453 : : vec_perm_indices *seq1_v_1_stmt_indices,
4454 : : vec_perm_indices *seq1_v_2_stmt_indices)
4455 : : {
4456 : 36 : unsigned int i;
4457 : 36 : unsigned int nelts = seq1->nelts;
4458 : 36 : auto_vec<int> lane_assignment;
4459 : 36 : lane_assignment.create (nelts);
4460 : :
4461 : : /* Mark all lanes as free. */
4462 : 36 : lane_assignment.quick_grow_cleared (nelts);
4463 : :
4464 : : /* Allocate lanes for seq1. */
4465 : 180 : for (i = 0; i < nelts; i++)
4466 : : {
4467 : 144 : unsigned int l = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq1->new_sel, i));
4468 : 144 : l %= nelts;
4469 : 144 : lane_assignment[l] = 1;
4470 : : }
4471 : :
4472 : : /* Allocate lanes for seq2 and calculate selector for seq2->stmt. */
4473 : 36 : vec_perm_builder seq2_stmt_sel_perm (nelts, nelts, 1);
4474 : 180 : for (i = 0; i < nelts; i++)
4475 : : {
4476 : 144 : unsigned int sel = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, i));
4477 : 144 : unsigned int lane = sel % nelts;
4478 : 144 : unsigned int offs = sel / nelts;
4479 : 144 : unsigned int new_sel;
4480 : :
4481 : : /* Check if we already allocated the lane for seq2. */
4482 : 144 : unsigned int j = 0;
4483 : 255 : for (; j < i; j++)
4484 : : {
4485 : 183 : unsigned int sel_old;
4486 : 183 : sel_old = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, j));
4487 : 183 : unsigned int lane_old = sel_old % nelts;
4488 : 183 : if (lane == lane_old)
4489 : : {
4490 : 72 : new_sel = seq2_stmt_sel_perm[j].to_constant ();
4491 : 72 : new_sel = (new_sel % nelts) + offs * nelts;
4492 : 72 : break;
4493 : : }
4494 : : }
4495 : :
4496 : : /* If the lane is not allocated, we need to do that now. */
4497 : 144 : if (j == i)
4498 : : {
4499 : : unsigned int l_orig = lane;
4500 : 176 : while (lane_assignment[lane] != 0)
4501 : : {
4502 : 104 : lane = (lane + 1) % nelts;
4503 : :
4504 : : /* This should not happen if both sequences utilize no more than
4505 : : half of the lanes. Test anyway to guarantee termination. */
4506 : 104 : if (lane == l_orig)
4507 : 0 : return false;
4508 : : }
4509 : :
4510 : : /* Allocate lane. */
4511 : 72 : lane_assignment[lane] = 2;
4512 : 72 : new_sel = lane + offs * nelts;
4513 : : }
4514 : :
4515 : 144 : seq2_stmt_sel_perm.quick_push (new_sel);
4516 : : }
4517 : :
4518 : : /* Check if the resulting permuation is cheap. */
4519 : 36 : seq2_stmt_indices->new_vector (seq2_stmt_sel_perm, 2, nelts);
4520 : 36 : tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
4521 : 36 : machine_mode vmode = TYPE_MODE (vectype);
4522 : 36 : if (!can_vec_perm_const_p (vmode, vmode, *seq2_stmt_indices, false))
4523 : : return false;
4524 : :
4525 : : /* Calculate selectors for seq1->v_1_stmt and seq1->v_2_stmt. */
4526 : 36 : vec_perm_builder seq1_v_1_stmt_sel_perm (nelts, nelts, 1);
4527 : 36 : vec_perm_builder seq1_v_2_stmt_sel_perm (nelts, nelts, 1);
4528 : 180 : for (i = 0; i < nelts; i++)
4529 : : {
4530 : 144 : bool use_seq1 = lane_assignment[i] != 2;
4531 : 144 : unsigned int l1, l2;
4532 : :
4533 : 144 : if (use_seq1)
4534 : : {
4535 : : /* Just reuse the selector indices. */
4536 : 72 : tree s1 = gimple_assign_rhs3 (seq1->v_1_stmt);
4537 : 72 : tree s2 = gimple_assign_rhs3 (seq1->v_2_stmt);
4538 : 72 : l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, i));
4539 : 72 : l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, i));
4540 : : }
4541 : : else
4542 : : {
4543 : : /* We moved the lanes for seq2, so we need to adjust for that. */
4544 : 72 : tree s1 = gimple_assign_rhs3 (seq2->v_1_stmt);
4545 : 72 : tree s2 = gimple_assign_rhs3 (seq2->v_2_stmt);
4546 : :
4547 : 72 : unsigned int j = 0;
4548 : 128 : for (; j < i; j++)
4549 : : {
4550 : 128 : unsigned int sel_new;
4551 : 128 : sel_new = seq2_stmt_sel_perm[j].to_constant ();
4552 : 128 : sel_new %= nelts;
4553 : 128 : if (sel_new == i)
4554 : : break;
4555 : : }
4556 : :
4557 : : /* This should not happen. Test anyway to guarantee correctness. */
4558 : 72 : if (j == i)
4559 : : return false;
4560 : :
4561 : 72 : l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, j));
4562 : 72 : l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, j));
4563 : : }
4564 : :
4565 : 216 : seq1_v_1_stmt_sel_perm.quick_push (l1 + (use_seq1 ? 0 : nelts));
4566 : 216 : seq1_v_2_stmt_sel_perm.quick_push (l2 + (use_seq1 ? 0 : nelts));
4567 : : }
4568 : :
4569 : 36 : seq1_v_1_stmt_indices->new_vector (seq1_v_1_stmt_sel_perm, 2, nelts);
4570 : 36 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
4571 : 36 : vmode = TYPE_MODE (vectype);
4572 : 36 : if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_1_stmt_indices, false))
4573 : : return false;
4574 : :
4575 : 36 : seq1_v_2_stmt_indices->new_vector (seq1_v_2_stmt_sel_perm, 2, nelts);
4576 : 36 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
4577 : 36 : vmode = TYPE_MODE (vectype);
4578 : 36 : if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_2_stmt_indices, false))
4579 : : return false;
4580 : :
4581 : : return true;
4582 : 72 : }
4583 : :
4584 : : /* Blend the two given simplifiable vec permute sequences using the
4585 : : given permutations. */
4586 : :
4587 : : static void
4588 : 36 : blend_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
4589 : : vec_perm_simplify_seq seq2,
4590 : : const vec_perm_indices &seq2_stmt_indices,
4591 : : const vec_perm_indices &seq1_v_1_stmt_indices,
4592 : : const vec_perm_indices &seq1_v_2_stmt_indices)
4593 : : {
4594 : : /* We don't need to adjust seq1->stmt because its lanes consumption
4595 : : was already narrowed before entering this function. */
4596 : :
4597 : : /* Adjust seq2->stmt: copy RHS1/RHS2 from seq1->stmt and set new sel. */
4598 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4599 : : {
4600 : 10 : fprintf (dump_file, "Updating VEC_PERM statment:\n");
4601 : 10 : fprintf (dump_file, "Old stmt: ");
4602 : 10 : print_gimple_stmt (dump_file, seq2->stmt, 0);
4603 : : }
4604 : :
4605 : 36 : gimple_assign_set_rhs1 (seq2->stmt, gimple_assign_rhs1 (seq1->stmt));
4606 : 72 : gimple_assign_set_rhs2 (seq2->stmt, gimple_assign_rhs2 (seq1->stmt));
4607 : 36 : tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
4608 : 36 : tree sel = vect_gen_perm_mask_checked (vectype, seq2_stmt_indices);
4609 : 36 : gimple_assign_set_rhs3 (seq2->stmt, sel);
4610 : 36 : update_stmt (seq2->stmt);
4611 : :
4612 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4613 : : {
4614 : 10 : fprintf (dump_file, "New stmt: ");
4615 : 10 : print_gimple_stmt (dump_file, seq2->stmt, 0);
4616 : : }
4617 : :
4618 : : /* Adjust seq1->v_1_stmt: copy RHS2 from seq2->v_1_stmt and set new sel. */
4619 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4620 : : {
4621 : 10 : fprintf (dump_file, "Updating VEC_PERM statment:\n");
4622 : 10 : fprintf (dump_file, "Old stmt: ");
4623 : 10 : print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
4624 : : }
4625 : :
4626 : 36 : gimple_assign_set_rhs2 (seq1->v_1_stmt, gimple_assign_rhs1 (seq2->v_1_stmt));
4627 : 36 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
4628 : 36 : sel = vect_gen_perm_mask_checked (vectype, seq1_v_1_stmt_indices);
4629 : 36 : gimple_assign_set_rhs3 (seq1->v_1_stmt, sel);
4630 : 36 : update_stmt (seq1->v_1_stmt);
4631 : :
4632 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4633 : : {
4634 : 10 : fprintf (dump_file, "New stmt: ");
4635 : 10 : print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
4636 : : }
4637 : :
4638 : : /* Adjust seq1->v_2_stmt: copy RHS2 from seq2->v_2_stmt and set new sel. */
4639 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4640 : : {
4641 : 10 : fprintf (dump_file, "Updating VEC_PERM statment:\n");
4642 : 10 : fprintf (dump_file, "Old stmt: ");
4643 : 10 : print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
4644 : : }
4645 : :
4646 : 36 : gimple_assign_set_rhs2 (seq1->v_2_stmt, gimple_assign_rhs1 (seq2->v_2_stmt));
4647 : 36 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
4648 : 36 : sel = vect_gen_perm_mask_checked (vectype, seq1_v_2_stmt_indices);
4649 : 36 : gimple_assign_set_rhs3 (seq1->v_2_stmt, sel);
4650 : 36 : update_stmt (seq1->v_2_stmt);
4651 : :
4652 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4653 : : {
4654 : 10 : fprintf (dump_file, "New stmt: ");
4655 : 10 : print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
4656 : : }
4657 : :
4658 : : /* At this point, we have four unmodified seq2 stmts, which will be
4659 : : eliminated by DCE. */
4660 : :
4661 : 36 : if (dump_file)
4662 : 10 : fprintf (dump_file, "Vec perm simplify sequences have been blended.\n\n");
4663 : 36 : }
4664 : :
4665 : : /* Try to blend narrowed vec_perm_simplify_seqs pairwise.
4666 : : The provided list will be empty after this call. */
4667 : :
4668 : : static void
4669 : 321149902 : process_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l)
4670 : : {
4671 : 321149902 : unsigned int i, j;
4672 : 321149902 : vec_perm_simplify_seq seq1, seq2;
4673 : :
4674 : 321149902 : if (l->is_empty ())
4675 : 321149858 : return;
4676 : :
4677 : 44 : if (dump_file && (dump_flags & TDF_DETAILS))
4678 : 12 : fprintf (dump_file, "\nProcessing %u vec perm simplify sequences.\n",
4679 : : l->length ());
4680 : :
4681 : 107 : FOR_EACH_VEC_ELT (*l, i, seq1)
4682 : : {
4683 : 63 : if (i + 1 < l->length ())
4684 : : {
4685 : 50 : FOR_EACH_VEC_ELT_FROM (*l, j, seq2, i + 1)
4686 : : {
4687 : 46 : bool swap = false;
4688 : 46 : if (can_blend_vec_perm_simplify_seqs_p (seq1, seq2, &swap))
4689 : : {
4690 : 36 : vec_perm_indices seq2_stmt_indices;
4691 : 36 : vec_perm_indices seq1_v_1_stmt_indices;
4692 : 36 : vec_perm_indices seq1_v_2_stmt_indices;
4693 : 108 : if (calc_perm_vec_perm_simplify_seqs (swap ? seq2 : seq1,
4694 : : swap ? seq1 : seq2,
4695 : : &seq2_stmt_indices,
4696 : : &seq1_v_1_stmt_indices,
4697 : : &seq1_v_2_stmt_indices))
4698 : : {
4699 : : /* Narrow lane usage. */
4700 : 36 : narrow_vec_perm_simplify_seq (seq1);
4701 : 36 : narrow_vec_perm_simplify_seq (seq2);
4702 : :
4703 : : /* Blend sequences. */
4704 : 36 : blend_vec_perm_simplify_seqs (swap ? seq2 : seq1,
4705 : : swap ? seq1 : seq2,
4706 : : seq2_stmt_indices,
4707 : : seq1_v_1_stmt_indices,
4708 : : seq1_v_2_stmt_indices);
4709 : :
4710 : : /* We can use unordered_remove as we break the loop. */
4711 : 36 : l->unordered_remove (j);
4712 : 36 : XDELETE (seq2);
4713 : 36 : break;
4714 : : }
4715 : 36 : }
4716 : : }
4717 : : }
4718 : :
4719 : : /* We don't need to call l->remove for seq1. */
4720 : 63 : XDELETE (seq1);
4721 : : }
4722 : :
4723 : 44 : l->truncate (0);
4724 : : }
4725 : :
4726 : : static void
4727 : 99 : append_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l,
4728 : : const vec_perm_simplify_seq &seq)
4729 : : {
4730 : : /* If no space on list left, then process the list. */
4731 : 99 : if (!l->space (1))
4732 : 0 : process_vec_perm_simplify_seq_list (l);
4733 : :
4734 : 99 : l->quick_push (seq);
4735 : 99 : }
4736 : :
4737 : : /* Main entry point for the forward propagation and statement combine
4738 : : optimizer. */
4739 : :
4740 : : namespace {
4741 : :
4742 : : const pass_data pass_data_forwprop =
4743 : : {
4744 : : GIMPLE_PASS, /* type */
4745 : : "forwprop", /* name */
4746 : : OPTGROUP_NONE, /* optinfo_flags */
4747 : : TV_TREE_FORWPROP, /* tv_id */
4748 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
4749 : : 0, /* properties_provided */
4750 : : 0, /* properties_destroyed */
4751 : : 0, /* todo_flags_start */
4752 : : 0, /* todo_flags_finish */
4753 : : };
4754 : :
4755 : : class pass_forwprop : public gimple_opt_pass
4756 : : {
4757 : : public:
4758 : 1142756 : pass_forwprop (gcc::context *ctxt)
4759 : 2285512 : : gimple_opt_pass (pass_data_forwprop, ctxt), last_p (false)
4760 : : {}
4761 : :
4762 : : /* opt_pass methods: */
4763 : 857067 : opt_pass * clone () final override { return new pass_forwprop (m_ctxt); }
4764 : 1142756 : void set_pass_param (unsigned int n, bool param) final override
4765 : : {
4766 : 1142756 : switch (n)
4767 : : {
4768 : 857067 : case 0:
4769 : 857067 : m_full_walk = param;
4770 : 857067 : break;
4771 : 285689 : case 1:
4772 : 285689 : last_p = param;
4773 : 285689 : break;
4774 : 0 : default:
4775 : 0 : gcc_unreachable();
4776 : : }
4777 : 1142756 : }
4778 : 5640109 : bool gate (function *) final override { return flag_tree_forwprop; }
4779 : : unsigned int execute (function *) final override;
4780 : :
4781 : : private:
4782 : : /* Determines whether the pass instance should set PROP_last_full_fold. */
4783 : : bool last_p;
4784 : :
4785 : : /* True if the aggregate props are doing a full walk or not. */
4786 : : bool m_full_walk = false;
4787 : : }; // class pass_forwprop
4788 : :
4789 : : unsigned int
4790 : 5637581 : pass_forwprop::execute (function *fun)
4791 : : {
4792 : 5637581 : unsigned int todoflags = 0;
4793 : : /* Handle a full walk only when expensive optimizations are on. */
4794 : 5637581 : bool full_walk = m_full_walk && flag_expensive_optimizations;
4795 : :
4796 : 5637581 : cfg_changed = false;
4797 : 5637581 : if (last_p)
4798 : 1042276 : fun->curr_properties |= PROP_last_full_fold;
4799 : :
4800 : 5637581 : calculate_dominance_info (CDI_DOMINATORS);
4801 : :
4802 : : /* Combine stmts with the stmts defining their operands. Do that
4803 : : in an order that guarantees visiting SSA defs before SSA uses. */
4804 : 11275162 : lattice.create (num_ssa_names);
4805 : 11275162 : lattice.quick_grow_cleared (num_ssa_names);
4806 : 5637581 : int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (fun));
4807 : 5637581 : int postorder_num = pre_and_rev_post_order_compute_fn (fun, NULL,
4808 : : postorder, false);
4809 : 5637581 : int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
4810 : 51324828 : for (int i = 0; i < postorder_num; ++i)
4811 : : {
4812 : 45687247 : bb_to_rpo[postorder[i]] = i;
4813 : 45687247 : edge_iterator ei;
4814 : 45687247 : edge e;
4815 : 109956967 : FOR_EACH_EDGE (e, ei, BASIC_BLOCK_FOR_FN (fun, postorder[i])->succs)
4816 : 64269720 : e->flags &= ~EDGE_EXECUTABLE;
4817 : : }
4818 : 5637581 : single_succ_edge (BASIC_BLOCK_FOR_FN (fun, ENTRY_BLOCK))->flags
4819 : 5637581 : |= EDGE_EXECUTABLE;
4820 : 5637581 : auto_vec<gimple *, 4> to_fixup;
4821 : 5637581 : auto_vec<gimple *, 32> to_remove;
4822 : 5637581 : auto_vec<unsigned, 32> to_remove_defs;
4823 : 5637581 : auto_vec<std::pair<int, int>, 10> edges_to_remove;
4824 : 5637581 : auto_bitmap simple_dce_worklist;
4825 : 5637581 : auto_bitmap need_ab_cleanup;
4826 : 5637581 : to_purge = BITMAP_ALLOC (NULL);
4827 : 5637581 : auto_vec<vec_perm_simplify_seq, 8> vec_perm_simplify_seq_list;
4828 : 51324828 : for (int i = 0; i < postorder_num; ++i)
4829 : : {
4830 : 45687247 : gimple_stmt_iterator gsi;
4831 : 45687247 : basic_block bb = BASIC_BLOCK_FOR_FN (fun, postorder[i]);
4832 : 45687247 : edge_iterator ei;
4833 : 45687247 : edge e;
4834 : :
4835 : : /* Skip processing not executable blocks. We could improve
4836 : : single_use tracking by at least unlinking uses from unreachable
4837 : : blocks but since blocks with uses are not processed in a
4838 : : meaningful order this is probably not worth it. */
4839 : 45687247 : bool any = false;
4840 : 46818033 : FOR_EACH_EDGE (e, ei, bb->preds)
4841 : : {
4842 : 46805563 : if ((e->flags & EDGE_EXECUTABLE)
4843 : : /* We can handle backedges in natural loops correctly but
4844 : : for irreducible regions we have to take all backedges
4845 : : conservatively when we did not visit the source yet. */
4846 : 46805563 : || (bb_to_rpo[e->src->index] > i
4847 : 665900 : && !dominated_by_p (CDI_DOMINATORS, e->src, e->dest)))
4848 : : {
4849 : : any = true;
4850 : : break;
4851 : : }
4852 : : }
4853 : 45687247 : if (!any)
4854 : 12470 : continue;
4855 : :
4856 : : /* Record degenerate PHIs in the lattice. */
4857 : 62333936 : for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
4858 : 16659159 : gsi_next (&si))
4859 : : {
4860 : 16659159 : gphi *phi = si.phi ();
4861 : 16659159 : tree res = gimple_phi_result (phi);
4862 : 33318318 : if (virtual_operand_p (res))
4863 : 7745268 : continue;
4864 : :
4865 : 8913891 : tree first = NULL_TREE;
4866 : 8913891 : bool all_same = true;
4867 : 8913891 : edge_iterator ei;
4868 : 8913891 : edge e;
4869 : 18332089 : FOR_EACH_EDGE (e, ei, bb->preds)
4870 : : {
4871 : : /* Ignore not executable forward edges. */
4872 : 18071556 : if (!(e->flags & EDGE_EXECUTABLE))
4873 : : {
4874 : 4058288 : if (bb_to_rpo[e->src->index] < i)
4875 : 5190 : continue;
4876 : : /* Avoid equivalences from backedges - while we might
4877 : : be able to make irreducible regions reducible and
4878 : : thus turning a back into a forward edge we do not
4879 : : want to deal with the intermediate SSA issues that
4880 : : exposes. */
4881 : : all_same = false;
4882 : : }
4883 : 18066366 : tree use = PHI_ARG_DEF_FROM_EDGE (phi, e);
4884 : 18066366 : if (use == res)
4885 : : /* The PHI result can also appear on a backedge, if so
4886 : : we can ignore this case for the purpose of determining
4887 : : the singular value. */
4888 : : ;
4889 : 18052850 : else if (! first)
4890 : : first = use;
4891 : 9138959 : else if (! operand_equal_p (first, use, 0))
4892 : : {
4893 : : all_same = false;
4894 : : break;
4895 : : }
4896 : : }
4897 : 8913891 : if (all_same)
4898 : : {
4899 : 255308 : if (may_propagate_copy (res, first))
4900 : 254731 : to_remove_defs.safe_push (SSA_NAME_VERSION (res));
4901 : 255308 : fwprop_set_lattice_val (res, first);
4902 : : }
4903 : : }
4904 : :
4905 : : /* Apply forward propagation to all stmts in the basic-block.
4906 : : Note we update GSI within the loop as necessary. */
4907 : 45674777 : unsigned int uid = 1;
4908 : 433433514 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
4909 : : {
4910 : 342083960 : gimple *stmt = gsi_stmt (gsi);
4911 : 342083960 : tree lhs, rhs;
4912 : 342083960 : enum tree_code code;
4913 : :
4914 : 342083960 : gimple_set_uid (stmt, uid++);
4915 : :
4916 : 342083960 : if (!is_gimple_assign (stmt))
4917 : : {
4918 : 235437546 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
4919 : 235437546 : gsi_next (&gsi);
4920 : 235437546 : continue;
4921 : : }
4922 : :
4923 : 106646414 : lhs = gimple_assign_lhs (stmt);
4924 : 106646414 : rhs = gimple_assign_rhs1 (stmt);
4925 : 106646414 : code = gimple_assign_rhs_code (stmt);
4926 : :
4927 : 146683993 : if (TREE_CODE (lhs) != SSA_NAME
4928 : 106646414 : || has_zero_uses (lhs))
4929 : : {
4930 : 40037579 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
4931 : 40037579 : gsi_next (&gsi);
4932 : 40037579 : continue;
4933 : : }
4934 : :
4935 : : /* If this statement sets an SSA_NAME to an address,
4936 : : try to propagate the address into the uses of the SSA_NAME. */
4937 : 66608835 : if ((code == ADDR_EXPR
4938 : : /* Handle pointer conversions on invariant addresses
4939 : : as well, as this is valid gimple. */
4940 : 64236582 : || (CONVERT_EXPR_CODE_P (code)
4941 : 8861534 : && TREE_CODE (rhs) == ADDR_EXPR
4942 : 356932 : && POINTER_TYPE_P (TREE_TYPE (lhs))))
4943 : 66609059 : && TREE_CODE (TREE_OPERAND (rhs, 0)) != TARGET_MEM_REF)
4944 : : {
4945 : 2371975 : tree base = get_base_address (TREE_OPERAND (rhs, 0));
4946 : 2371975 : if ((!base
4947 : 2371975 : || !DECL_P (base)
4948 : 143962 : || decl_address_invariant_p (base))
4949 : 2371975 : && !stmt_references_abnormal_ssa_name (stmt)
4950 : 4743934 : && forward_propagate_addr_expr (lhs, rhs, true))
4951 : : {
4952 : 451204 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
4953 : 451204 : release_defs (stmt);
4954 : 451204 : gsi_remove (&gsi, true);
4955 : : }
4956 : : else
4957 : 1920771 : gsi_next (&gsi);
4958 : : }
4959 : 64236860 : else if (code == POINTER_PLUS_EXPR)
4960 : : {
4961 : 3638101 : tree off = gimple_assign_rhs2 (stmt);
4962 : 3638101 : if (TREE_CODE (off) == INTEGER_CST
4963 : 1140179 : && can_propagate_from (stmt)
4964 : 1139826 : && !simple_iv_increment_p (stmt)
4965 : : /* ??? Better adjust the interface to that function
4966 : : instead of building new trees here. */
4967 : 4482582 : && forward_propagate_addr_expr
4968 : 2533443 : (lhs,
4969 : : build1_loc (gimple_location (stmt),
4970 : 844481 : ADDR_EXPR, TREE_TYPE (rhs),
4971 : 844481 : fold_build2 (MEM_REF,
4972 : : TREE_TYPE (TREE_TYPE (rhs)),
4973 : : rhs,
4974 : : fold_convert (ptr_type_node,
4975 : : off))), true))
4976 : : {
4977 : 296817 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
4978 : 296817 : release_defs (stmt);
4979 : 296817 : gsi_remove (&gsi, true);
4980 : : }
4981 : 3341284 : else if (is_gimple_min_invariant (rhs))
4982 : : {
4983 : : /* Make sure to fold &a[0] + off_1 here. */
4984 : 439863 : fold_stmt_inplace (&gsi);
4985 : 439863 : update_stmt (stmt);
4986 : 439863 : if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
4987 : 439845 : gsi_next (&gsi);
4988 : : }
4989 : : else
4990 : 2901421 : gsi_next (&gsi);
4991 : : }
4992 : 60598759 : else if (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE
4993 : 211255 : && gimple_assign_load_p (stmt)
4994 : 134208 : && !gimple_has_volatile_ops (stmt)
4995 : 40139 : && TREE_CODE (rhs) != TARGET_MEM_REF
4996 : 40114 : && TREE_CODE (rhs) != BIT_FIELD_REF
4997 : 60638869 : && !stmt_can_throw_internal (fun, stmt))
4998 : : {
4999 : : /* Rewrite loads used only in real/imagpart extractions to
5000 : : component-wise loads. */
5001 : 39985 : use_operand_p use_p;
5002 : 39985 : imm_use_iterator iter;
5003 : 39985 : tree vuse = gimple_vuse (stmt);
5004 : 39985 : bool rewrite = true;
5005 : 44707 : FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
5006 : : {
5007 : 42635 : gimple *use_stmt = USE_STMT (use_p);
5008 : 42635 : if (is_gimple_debug (use_stmt))
5009 : 692 : continue;
5010 : 41943 : if (!is_gimple_assign (use_stmt)
5011 : 27493 : || (gimple_assign_rhs_code (use_stmt) != REALPART_EXPR
5012 : 25463 : && gimple_assign_rhs_code (use_stmt) != IMAGPART_EXPR)
5013 : 45973 : || TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) != lhs)
5014 : : {
5015 : : rewrite = false;
5016 : : break;
5017 : : }
5018 : : }
5019 : 39985 : if (rewrite)
5020 : : {
5021 : 2072 : gimple *use_stmt;
5022 : 6536 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
5023 : : {
5024 : 4464 : if (is_gimple_debug (use_stmt))
5025 : : {
5026 : 453 : if (gimple_debug_bind_p (use_stmt))
5027 : : {
5028 : 453 : gimple_debug_bind_reset_value (use_stmt);
5029 : 453 : update_stmt (use_stmt);
5030 : : }
5031 : 453 : continue;
5032 : : }
5033 : :
5034 : 8022 : tree new_rhs = build1 (gimple_assign_rhs_code (use_stmt),
5035 : 4011 : TREE_TYPE (TREE_TYPE (rhs)),
5036 : : unshare_expr (rhs));
5037 : 4011 : gimple *new_stmt
5038 : 4011 : = gimple_build_assign (gimple_assign_lhs (use_stmt),
5039 : : new_rhs);
5040 : :
5041 : 4011 : location_t loc = gimple_location (use_stmt);
5042 : 4011 : gimple_set_location (new_stmt, loc);
5043 : 4011 : gimple_set_vuse (new_stmt, vuse);
5044 : 4011 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5045 : 4011 : unlink_stmt_vdef (use_stmt);
5046 : 4011 : gsi_remove (&gsi2, true);
5047 : :
5048 : 4011 : gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
5049 : 2072 : }
5050 : :
5051 : 2072 : release_defs (stmt);
5052 : 2072 : gsi_remove (&gsi, true);
5053 : : }
5054 : : else
5055 : 37913 : gsi_next (&gsi);
5056 : : }
5057 : 60558774 : else if (TREE_CODE (TREE_TYPE (lhs)) == VECTOR_TYPE
5058 : 1585122 : && (TYPE_MODE (TREE_TYPE (lhs)) == BLKmode
5059 : : /* After vector lowering rewrite all loads, but
5060 : : initially do not since this conflicts with
5061 : : vector CONSTRUCTOR to shuffle optimization. */
5062 : 1564322 : || (fun->curr_properties & PROP_gimple_lvec))
5063 : 871929 : && gimple_assign_load_p (stmt)
5064 : 293219 : && !gimple_has_volatile_ops (stmt)
5065 : 279374 : && !stmt_can_throw_internal (fun, stmt)
5066 : 60838148 : && (!VAR_P (rhs) || !DECL_HARD_REGISTER (rhs)))
5067 : 278872 : optimize_vector_load (&gsi);
5068 : :
5069 : 60279902 : else if (code == COMPLEX_EXPR)
5070 : : {
5071 : : /* Rewrite stores of a single-use complex build expression
5072 : : to component-wise stores. */
5073 : 36405 : use_operand_p use_p;
5074 : 36405 : gimple *use_stmt, *def1, *def2;
5075 : 36405 : tree rhs2;
5076 : 36405 : if (single_imm_use (lhs, &use_p, &use_stmt)
5077 : 34261 : && gimple_store_p (use_stmt)
5078 : 41018 : && !gimple_has_volatile_ops (use_stmt)
5079 : 2593 : && is_gimple_assign (use_stmt)
5080 : 2589 : && (TREE_CODE (TREE_TYPE (gimple_assign_lhs (use_stmt)))
5081 : : == COMPLEX_TYPE)
5082 : 38989 : && (TREE_CODE (gimple_assign_lhs (use_stmt))
5083 : : != TARGET_MEM_REF))
5084 : : {
5085 : 2580 : tree use_lhs = gimple_assign_lhs (use_stmt);
5086 : 2580 : if (auto_var_p (use_lhs))
5087 : 600 : DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
5088 : 5160 : tree new_lhs = build1 (REALPART_EXPR,
5089 : 2580 : TREE_TYPE (TREE_TYPE (use_lhs)),
5090 : : unshare_expr (use_lhs));
5091 : 2580 : gimple *new_stmt = gimple_build_assign (new_lhs, rhs);
5092 : 2580 : location_t loc = gimple_location (use_stmt);
5093 : 2580 : gimple_set_location (new_stmt, loc);
5094 : 5160 : gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
5095 : 2580 : gimple_set_vdef (new_stmt, make_ssa_name (gimple_vop (fun)));
5096 : 5160 : SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
5097 : 5160 : gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
5098 : 2580 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5099 : 2580 : gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
5100 : :
5101 : 5160 : new_lhs = build1 (IMAGPART_EXPR,
5102 : 2580 : TREE_TYPE (TREE_TYPE (use_lhs)),
5103 : : unshare_expr (use_lhs));
5104 : 2580 : gimple_assign_set_lhs (use_stmt, new_lhs);
5105 : 2580 : gimple_assign_set_rhs1 (use_stmt, gimple_assign_rhs2 (stmt));
5106 : 2580 : update_stmt (use_stmt);
5107 : :
5108 : 2580 : release_defs (stmt);
5109 : 2580 : gsi_remove (&gsi, true);
5110 : : }
5111 : : /* Rewrite a component-wise load of a complex to a complex
5112 : : load if the components are not used separately. */
5113 : 33825 : else if (TREE_CODE (rhs) == SSA_NAME
5114 : 33384 : && has_single_use (rhs)
5115 : 29936 : && ((rhs2 = gimple_assign_rhs2 (stmt)), true)
5116 : 29936 : && TREE_CODE (rhs2) == SSA_NAME
5117 : 28224 : && has_single_use (rhs2)
5118 : 27803 : && (def1 = SSA_NAME_DEF_STMT (rhs),
5119 : 27803 : gimple_assign_load_p (def1))
5120 : 1091 : && (def2 = SSA_NAME_DEF_STMT (rhs2),
5121 : 1091 : gimple_assign_load_p (def2))
5122 : 1598 : && (gimple_vuse (def1) == gimple_vuse (def2))
5123 : 796 : && !gimple_has_volatile_ops (def1)
5124 : 796 : && !gimple_has_volatile_ops (def2)
5125 : 796 : && !stmt_can_throw_internal (fun, def1)
5126 : 796 : && !stmt_can_throw_internal (fun, def2)
5127 : 796 : && gimple_assign_rhs_code (def1) == REALPART_EXPR
5128 : 541 : && gimple_assign_rhs_code (def2) == IMAGPART_EXPR
5129 : 34366 : && operand_equal_p (TREE_OPERAND (gimple_assign_rhs1
5130 : : (def1), 0),
5131 : 541 : TREE_OPERAND (gimple_assign_rhs1
5132 : : (def2), 0)))
5133 : : {
5134 : 541 : tree cl = TREE_OPERAND (gimple_assign_rhs1 (def1), 0);
5135 : 541 : gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (cl));
5136 : 541 : gcc_assert (gsi_stmt (gsi) == stmt);
5137 : 1082 : gimple_set_vuse (stmt, gimple_vuse (def1));
5138 : 541 : gimple_set_modified (stmt, true);
5139 : 541 : gimple_stmt_iterator gsi2 = gsi_for_stmt (def1);
5140 : 541 : gsi_remove (&gsi, false);
5141 : 541 : gsi_insert_after (&gsi2, stmt, GSI_SAME_STMT);
5142 : : }
5143 : : else
5144 : 33284 : gsi_next (&gsi);
5145 : : }
5146 : 60243497 : else if (code == CONSTRUCTOR
5147 : 152562 : && VECTOR_TYPE_P (TREE_TYPE (rhs))
5148 : 152562 : && TYPE_MODE (TREE_TYPE (rhs)) == BLKmode
5149 : 2819 : && CONSTRUCTOR_NELTS (rhs) > 0
5150 : 60246316 : && (!VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
5151 : 503 : || (TYPE_MODE (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
5152 : : != BLKmode)))
5153 : : {
5154 : : /* Rewrite stores of a single-use vector constructors
5155 : : to component-wise stores if the mode isn't supported. */
5156 : 2818 : use_operand_p use_p;
5157 : 2818 : gimple *use_stmt;
5158 : 2818 : if (single_imm_use (lhs, &use_p, &use_stmt)
5159 : 2407 : && gimple_store_p (use_stmt)
5160 : 2906 : && !gimple_has_volatile_ops (use_stmt)
5161 : 1447 : && !stmt_can_throw_internal (fun, use_stmt)
5162 : 4255 : && is_gimple_assign (use_stmt))
5163 : : {
5164 : 1437 : tree elt_t = TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value);
5165 : 1437 : unsigned HOST_WIDE_INT elt_w
5166 : 1437 : = tree_to_uhwi (TYPE_SIZE (elt_t));
5167 : 1437 : unsigned HOST_WIDE_INT n
5168 : 1437 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs)));
5169 : 1437 : tree use_lhs = gimple_assign_lhs (use_stmt);
5170 : 1437 : if (auto_var_p (use_lhs))
5171 : 535 : DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
5172 : 902 : else if (TREE_CODE (use_lhs) == TARGET_MEM_REF)
5173 : : {
5174 : 1 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5175 : 1 : use_lhs = prepare_target_mem_ref_lvalue (use_lhs, &gsi2);
5176 : : }
5177 : 32668 : for (unsigned HOST_WIDE_INT bi = 0; bi < n; bi += elt_w)
5178 : : {
5179 : 31231 : unsigned HOST_WIDE_INT ci = bi / elt_w;
5180 : 31231 : tree new_rhs;
5181 : 31231 : if (ci < CONSTRUCTOR_NELTS (rhs))
5182 : 30613 : new_rhs = CONSTRUCTOR_ELT (rhs, ci)->value;
5183 : : else
5184 : 618 : new_rhs = build_zero_cst (elt_t);
5185 : 31231 : tree new_lhs = build3 (BIT_FIELD_REF,
5186 : : elt_t,
5187 : : unshare_expr (use_lhs),
5188 : 31231 : bitsize_int (elt_w),
5189 : 31231 : bitsize_int (bi));
5190 : 31231 : gimple *new_stmt = gimple_build_assign (new_lhs, new_rhs);
5191 : 31231 : location_t loc = gimple_location (use_stmt);
5192 : 31231 : gimple_set_location (new_stmt, loc);
5193 : 62462 : gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
5194 : 31231 : gimple_set_vdef (new_stmt,
5195 : : make_ssa_name (gimple_vop (fun)));
5196 : 62462 : SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
5197 : 62462 : gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
5198 : 31231 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5199 : 31231 : gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
5200 : : }
5201 : 1437 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5202 : 1437 : unlink_stmt_vdef (use_stmt);
5203 : 1437 : release_defs (use_stmt);
5204 : 1437 : gsi_remove (&gsi2, true);
5205 : 1437 : release_defs (stmt);
5206 : 1437 : gsi_remove (&gsi, true);
5207 : : }
5208 : : else
5209 : 1381 : gsi_next (&gsi);
5210 : : }
5211 : 60240679 : else if (code == VEC_PERM_EXPR)
5212 : : {
5213 : : /* Find vectorized sequences, where we can reduce the lane
5214 : : utilization. The narrowing will be donw later and only
5215 : : if we find a pair of sequences that can be blended. */
5216 : 179170 : gassign *assign = dyn_cast <gassign *> (stmt);
5217 : 179170 : vec_perm_simplify_seq seq;
5218 : 179170 : if (recognise_vec_perm_simplify_seq (assign, &seq))
5219 : 99 : append_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list,
5220 : : seq);
5221 : :
5222 : 179170 : gsi_next (&gsi);
5223 : : }
5224 : : else
5225 : 60061509 : gsi_next (&gsi);
5226 : : }
5227 : :
5228 : 45674777 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
5229 : :
5230 : : /* Combine stmts with the stmts defining their operands.
5231 : : Note we update GSI within the loop as necessary. */
5232 : 433091724 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5233 : : {
5234 : 341742170 : gimple *stmt = gsi_stmt (gsi);
5235 : :
5236 : : /* Mark stmt as potentially needing revisiting. */
5237 : 341742170 : gimple_set_plf (stmt, GF_PLF_1, false);
5238 : :
5239 : 341742170 : bool can_make_abnormal_goto = (is_gimple_call (stmt)
5240 : 341742170 : && stmt_can_make_abnormal_goto (stmt));
5241 : :
5242 : : /* Substitute from our lattice. We need to do so only once. */
5243 : 341742170 : bool substituted_p = false;
5244 : 341742170 : use_operand_p usep;
5245 : 341742170 : ssa_op_iter iter;
5246 : 502635620 : FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
5247 : : {
5248 : 160893450 : tree use = USE_FROM_PTR (usep);
5249 : 160893450 : tree val = fwprop_ssa_val (use);
5250 : 160893450 : if (val && val != use)
5251 : : {
5252 : 1916863 : if (!is_gimple_debug (stmt))
5253 : 1561408 : bitmap_set_bit (simple_dce_worklist, SSA_NAME_VERSION (use));
5254 : 1916863 : if (may_propagate_copy (use, val))
5255 : : {
5256 : 1913495 : propagate_value (usep, val);
5257 : 1913495 : substituted_p = true;
5258 : : }
5259 : : }
5260 : : }
5261 : 341742170 : if (substituted_p)
5262 : 1861941 : update_stmt (stmt);
5263 : 1861941 : if (substituted_p
5264 : 1861941 : && is_gimple_assign (stmt)
5265 : 1111903 : && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
5266 : 25247 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
5267 : 341742170 : if (substituted_p
5268 : 341742170 : && can_make_abnormal_goto
5269 : 341742170 : && !stmt_can_make_abnormal_goto (stmt))
5270 : 3 : bitmap_set_bit (need_ab_cleanup, bb->index);
5271 : :
5272 : 344725590 : bool changed;
5273 : 689451180 : do
5274 : : {
5275 : 344725590 : gimple *orig_stmt = stmt = gsi_stmt (gsi);
5276 : 344725590 : bool was_noreturn = (is_gimple_call (stmt)
5277 : 344725590 : && gimple_call_noreturn_p (stmt));
5278 : 344725590 : changed = false;
5279 : :
5280 : 344725590 : auto_vec<tree, 8> uses;
5281 : 509027188 : FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
5282 : 164301598 : if (uses.space (1))
5283 : 163918532 : uses.quick_push (USE_FROM_PTR (usep));
5284 : :
5285 : 344725590 : if (fold_stmt (&gsi, fwprop_ssa_val, simple_dce_worklist))
5286 : : {
5287 : 2776058 : changed = true;
5288 : 2776058 : stmt = gsi_stmt (gsi);
5289 : : /* Cleanup the CFG if we simplified a condition to
5290 : : true or false. */
5291 : 2776058 : if (gcond *cond = dyn_cast <gcond *> (stmt))
5292 : 966880 : if (gimple_cond_true_p (cond)
5293 : 966880 : || gimple_cond_false_p (cond))
5294 : 14146 : cfg_changed = true;
5295 : : /* Queue old uses for simple DCE if not debug statement. */
5296 : 2776058 : if (!is_gimple_debug (stmt))
5297 : 11987087 : for (tree use : uses)
5298 : 3699155 : if (TREE_CODE (use) == SSA_NAME
5299 : 3699155 : && !SSA_NAME_IS_DEFAULT_DEF (use))
5300 : 3498721 : bitmap_set_bit (simple_dce_worklist,
5301 : 3498721 : SSA_NAME_VERSION (use));
5302 : 2776058 : update_stmt (stmt);
5303 : : }
5304 : :
5305 : 344725590 : switch (gimple_code (stmt))
5306 : : {
5307 : 107964650 : case GIMPLE_ASSIGN:
5308 : 107964650 : {
5309 : 107964650 : tree rhs1 = gimple_assign_rhs1 (stmt);
5310 : 107964650 : enum tree_code code = gimple_assign_rhs_code (stmt);
5311 : 107964650 : if (gimple_store_p (stmt))
5312 : : {
5313 : 39447699 : optimize_aggr_zeroprop (stmt, full_walk);
5314 : 39447699 : if (gimple_assign_load_p (stmt))
5315 : 3860969 : optimize_agr_copyprop (stmt);
5316 : : }
5317 : 68516951 : else if (TREE_CODE_CLASS (code) == tcc_comparison)
5318 : 2542166 : changed |= forward_propagate_into_comparison (&gsi);
5319 : 65974785 : else if ((code == PLUS_EXPR
5320 : 65974785 : || code == BIT_IOR_EXPR
5321 : 55906698 : || code == BIT_XOR_EXPR)
5322 : 66114636 : && simplify_rotate (&gsi))
5323 : : changed = true;
5324 : 65972113 : else if (code == VEC_PERM_EXPR)
5325 : 181234 : changed |= simplify_permutation (&gsi);
5326 : 65790879 : else if (code == CONSTRUCTOR
5327 : 65790879 : && TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
5328 : 150467 : changed |= simplify_vector_constructor (&gsi);
5329 : 65640412 : else if (code == ARRAY_REF)
5330 : 1965253 : changed |= simplify_count_zeroes (&gsi);
5331 : : break;
5332 : : }
5333 : :
5334 : 117600 : case GIMPLE_SWITCH:
5335 : 117600 : changed |= simplify_gimple_switch (as_a <gswitch *> (stmt),
5336 : : edges_to_remove,
5337 : : simple_dce_worklist);
5338 : 117600 : break;
5339 : :
5340 : 19221850 : case GIMPLE_COND:
5341 : 19221850 : {
5342 : 19221850 : int did_something = forward_propagate_into_gimple_cond
5343 : 19221850 : (as_a <gcond *> (stmt));
5344 : 19221850 : if (did_something == 2)
5345 : 1773 : cfg_changed = true;
5346 : 19221850 : changed |= did_something != 0;
5347 : 19221850 : break;
5348 : : }
5349 : :
5350 : 23027416 : case GIMPLE_CALL:
5351 : 23027416 : {
5352 : 23027416 : tree callee = gimple_call_fndecl (stmt);
5353 : 23027416 : if (callee != NULL_TREE
5354 : 23027416 : && fndecl_built_in_p (callee, BUILT_IN_NORMAL))
5355 : 6023155 : changed |= simplify_builtin_call (&gsi, callee, full_walk);
5356 : : break;
5357 : : }
5358 : :
5359 : 344722918 : default:;
5360 : : }
5361 : :
5362 : 344722918 : if (changed || substituted_p)
5363 : : {
5364 : 4342920 : substituted_p = false;
5365 : 4342920 : stmt = gsi_stmt (gsi);
5366 : 4342920 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
5367 : 69 : bitmap_set_bit (to_purge, bb->index);
5368 : 4342920 : if (!was_noreturn
5369 : 4342920 : && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
5370 : 12 : to_fixup.safe_push (stmt);
5371 : : }
5372 : 4342920 : if (changed)
5373 : : {
5374 : : /* If the stmt changed then re-visit it and the statements
5375 : : inserted before it. */
5376 : 9305510 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
5377 : 5935962 : if (gimple_plf (gsi_stmt (gsi), GF_PLF_1))
5378 : : break;
5379 : 2983420 : if (gsi_end_p (gsi))
5380 : 417006 : gsi = gsi_start_bb (bb);
5381 : : else
5382 : 2774917 : gsi_next (&gsi);
5383 : : }
5384 : 344725590 : }
5385 : : while (changed);
5386 : :
5387 : : /* Stmt no longer needs to be revisited. */
5388 : 341742170 : stmt = gsi_stmt (gsi);
5389 : 341742170 : gcc_checking_assert (!gimple_plf (stmt, GF_PLF_1));
5390 : 341742170 : gimple_set_plf (stmt, GF_PLF_1, true);
5391 : :
5392 : : /* Fill up the lattice. */
5393 : 341742170 : if (gimple_assign_single_p (stmt))
5394 : : {
5395 : 72067846 : tree lhs = gimple_assign_lhs (stmt);
5396 : 72067846 : tree rhs = gimple_assign_rhs1 (stmt);
5397 : 72067846 : if (TREE_CODE (lhs) == SSA_NAME)
5398 : : {
5399 : 32639709 : tree val = lhs;
5400 : 32639709 : if (TREE_CODE (rhs) == SSA_NAME)
5401 : 784134 : val = fwprop_ssa_val (rhs);
5402 : 31855575 : else if (is_gimple_min_invariant (rhs))
5403 : 443933 : val = rhs;
5404 : : /* If we can propagate the lattice-value mark the
5405 : : stmt for removal. */
5406 : 32639709 : if (val != lhs
5407 : 32639709 : && may_propagate_copy (lhs, val))
5408 : 1224397 : to_remove_defs.safe_push (SSA_NAME_VERSION (lhs));
5409 : 32639709 : fwprop_set_lattice_val (lhs, val);
5410 : : }
5411 : : }
5412 : 269674324 : else if (gimple_nop_p (stmt))
5413 : 83731 : to_remove.safe_push (stmt);
5414 : : }
5415 : :
5416 : : /* Substitute in destination PHI arguments. */
5417 : 109934570 : FOR_EACH_EDGE (e, ei, bb->succs)
5418 : 64259793 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
5419 : 106717944 : !gsi_end_p (gsi); gsi_next (&gsi))
5420 : : {
5421 : 42458151 : gphi *phi = gsi.phi ();
5422 : 42458151 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
5423 : 42458151 : tree arg = USE_FROM_PTR (use_p);
5424 : 70148094 : if (TREE_CODE (arg) != SSA_NAME
5425 : 42458151 : || virtual_operand_p (arg))
5426 : 27689943 : continue;
5427 : 14768208 : tree val = fwprop_ssa_val (arg);
5428 : 14768208 : if (val != arg
5429 : 14768208 : && may_propagate_copy (arg, val, !(e->flags & EDGE_ABNORMAL)))
5430 : 273726 : propagate_value (use_p, val);
5431 : : }
5432 : :
5433 : : /* Mark outgoing exectuable edges. */
5434 : 45674777 : if (edge e = find_taken_edge (bb, NULL))
5435 : : {
5436 : 19700997 : e->flags |= EDGE_EXECUTABLE;
5437 : 45694906 : if (EDGE_COUNT (bb->succs) > 1)
5438 : 20129 : cfg_changed = true;
5439 : : }
5440 : : else
5441 : : {
5442 : 70512446 : FOR_EACH_EDGE (e, ei, bb->succs)
5443 : 44538666 : e->flags |= EDGE_EXECUTABLE;
5444 : : }
5445 : : }
5446 : 5637581 : free (postorder);
5447 : 5637581 : free (bb_to_rpo);
5448 : 5637581 : lattice.release ();
5449 : :
5450 : : /* First remove chains of stmts where we check no uses remain. */
5451 : 5637581 : simple_dce_from_worklist (simple_dce_worklist, to_purge);
5452 : :
5453 : 6000104 : auto remove = [](gimple *stmt)
5454 : : {
5455 : 362523 : if (dump_file && (dump_flags & TDF_DETAILS))
5456 : : {
5457 : 1 : fprintf (dump_file, "Removing dead stmt ");
5458 : 1 : print_gimple_stmt (dump_file, stmt, 0);
5459 : 1 : fprintf (dump_file, "\n");
5460 : : }
5461 : 362523 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
5462 : 362523 : if (gimple_code (stmt) == GIMPLE_PHI)
5463 : 120915 : remove_phi_node (&gsi, true);
5464 : : else
5465 : : {
5466 : 241608 : unlink_stmt_vdef (stmt);
5467 : 241608 : gsi_remove (&gsi, true);
5468 : 241608 : release_defs (stmt);
5469 : : }
5470 : 362523 : };
5471 : :
5472 : : /* Then remove stmts we know we can remove even though we did not
5473 : : substitute in dead code regions, so uses can remain. Do so in reverse
5474 : : order to make debug stmt creation possible. */
5475 : 12754290 : while (!to_remove_defs.is_empty())
5476 : : {
5477 : 1479128 : tree def = ssa_name (to_remove_defs.pop ());
5478 : : /* For example remove_prop_source_from_use can remove stmts queued
5479 : : for removal. Deal with this gracefully. */
5480 : 1479128 : if (!def)
5481 : 1200336 : continue;
5482 : 278792 : gimple *stmt = SSA_NAME_DEF_STMT (def);
5483 : 278792 : remove (stmt);
5484 : : }
5485 : :
5486 : : /* Wipe other queued stmts that do not have SSA defs. */
5487 : 5721312 : while (!to_remove.is_empty())
5488 : : {
5489 : 83731 : gimple *stmt = to_remove.pop ();
5490 : 83731 : remove (stmt);
5491 : : }
5492 : :
5493 : : /* Fixup stmts that became noreturn calls. This may require splitting
5494 : : blocks and thus isn't possible during the walk. Do this
5495 : : in reverse order so we don't inadvertedly remove a stmt we want to
5496 : : fixup by visiting a dominating now noreturn call first. */
5497 : 5637593 : while (!to_fixup.is_empty ())
5498 : : {
5499 : 12 : gimple *stmt = to_fixup.pop ();
5500 : 12 : if (dump_file && dump_flags & TDF_DETAILS)
5501 : : {
5502 : 0 : fprintf (dump_file, "Fixing up noreturn call ");
5503 : 0 : print_gimple_stmt (dump_file, stmt, 0);
5504 : 0 : fprintf (dump_file, "\n");
5505 : : }
5506 : 12 : cfg_changed |= fixup_noreturn_call (stmt);
5507 : : }
5508 : :
5509 : 5637581 : cfg_changed |= gimple_purge_all_dead_eh_edges (to_purge);
5510 : 5637581 : cfg_changed |= gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
5511 : 5637581 : BITMAP_FREE (to_purge);
5512 : :
5513 : : /* Remove edges queued from switch stmt simplification. */
5514 : 16912743 : for (auto ep : edges_to_remove)
5515 : : {
5516 : 0 : basic_block src = BASIC_BLOCK_FOR_FN (fun, ep.first);
5517 : 0 : basic_block dest = BASIC_BLOCK_FOR_FN (fun, ep.second);
5518 : 0 : edge e;
5519 : 0 : if (src && dest && (e = find_edge (src, dest)))
5520 : : {
5521 : 0 : free_dominance_info (CDI_DOMINATORS);
5522 : 0 : remove_edge (e);
5523 : 0 : cfg_changed = true;
5524 : : }
5525 : : }
5526 : :
5527 : 11273628 : if (get_range_query (fun) != get_global_range_query ())
5528 : 1534 : disable_ranger (fun);
5529 : :
5530 : 5637581 : if (cfg_changed)
5531 : 9447 : todoflags |= TODO_cleanup_cfg;
5532 : :
5533 : 5637581 : return todoflags;
5534 : 5637581 : }
5535 : :
5536 : : } // anon namespace
5537 : :
5538 : : gimple_opt_pass *
5539 : 285689 : make_pass_forwprop (gcc::context *ctxt)
5540 : : {
5541 : 285689 : return new pass_forwprop (ctxt);
5542 : : }
|