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