Branch data Line data Source code
1 : : /* Forward propagation of expressions for single use variables.
2 : : Copyright (C) 2004-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify
7 : : it under the terms of the GNU General Public License as published by
8 : : the Free Software Foundation; either version 3, or (at your option)
9 : : any later version.
10 : :
11 : : GCC is distributed in the hope that it will be useful,
12 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : GNU General Public License for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along with GCC; see the file COPYING3. If not see
18 : : <http://www.gnu.org/licenses/>. */
19 : :
20 : : #include "config.h"
21 : : #include "system.h"
22 : : #include "coretypes.h"
23 : : #include "backend.h"
24 : : #include "rtl.h"
25 : : #include "tree.h"
26 : : #include "gimple.h"
27 : : #include "cfghooks.h"
28 : : #include "tree-pass.h"
29 : : #include "ssa.h"
30 : : #include "expmed.h"
31 : : #include "optabs-query.h"
32 : : #include "gimple-pretty-print.h"
33 : : #include "fold-const.h"
34 : : #include "stor-layout.h"
35 : : #include "gimple-iterator.h"
36 : : #include "gimple-fold.h"
37 : : #include "tree-eh.h"
38 : : #include "gimplify.h"
39 : : #include "gimplify-me.h"
40 : : #include "tree-cfg.h"
41 : : #include "expr.h"
42 : : #include "tree-dfa.h"
43 : : #include "tree-ssa-propagate.h"
44 : : #include "tree-ssa-dom.h"
45 : : #include "tree-ssa-strlen.h"
46 : : #include "builtins.h"
47 : : #include "tree-cfgcleanup.h"
48 : : #include "cfganal.h"
49 : : #include "optabs-tree.h"
50 : : #include "insn-config.h"
51 : : #include "recog.h"
52 : : #include "cfgloop.h"
53 : : #include "tree-vectorizer.h"
54 : : #include "tree-vector-builder.h"
55 : : #include "vec-perm-indices.h"
56 : : #include "internal-fn.h"
57 : : #include "cgraph.h"
58 : : #include "tree-ssa.h"
59 : : #include "gimple-range.h"
60 : : #include "tree-ssa-dce.h"
61 : :
62 : : /* This pass propagates the RHS of assignment statements into use
63 : : sites of the LHS of the assignment. It's basically a specialized
64 : : form of tree combination. It is hoped all of this can disappear
65 : : when we have a generalized tree combiner.
66 : :
67 : : One class of common cases we handle is forward propagating a single use
68 : : variable into a COND_EXPR.
69 : :
70 : : bb0:
71 : : x = a COND b;
72 : : if (x) goto ... else goto ...
73 : :
74 : : Will be transformed into:
75 : :
76 : : bb0:
77 : : if (a COND b) goto ... else goto ...
78 : :
79 : : Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
80 : :
81 : : Or (assuming c1 and c2 are constants):
82 : :
83 : : bb0:
84 : : x = a + c1;
85 : : if (x EQ/NEQ c2) goto ... else goto ...
86 : :
87 : : Will be transformed into:
88 : :
89 : : bb0:
90 : : if (a EQ/NEQ (c2 - c1)) goto ... else goto ...
91 : :
92 : : Similarly for x = a - c1.
93 : :
94 : : Or
95 : :
96 : : bb0:
97 : : x = !a
98 : : if (x) goto ... else goto ...
99 : :
100 : : Will be transformed into:
101 : :
102 : : bb0:
103 : : if (a == 0) goto ... else goto ...
104 : :
105 : : Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
106 : : For these cases, we propagate A into all, possibly more than one,
107 : : COND_EXPRs that use X.
108 : :
109 : : Or
110 : :
111 : : bb0:
112 : : x = (typecast) a
113 : : if (x) goto ... else goto ...
114 : :
115 : : Will be transformed into:
116 : :
117 : : bb0:
118 : : if (a != 0) goto ... else goto ...
119 : :
120 : : (Assuming a is an integral type and x is a boolean or x is an
121 : : integral and a is a boolean.)
122 : :
123 : : Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
124 : : For these cases, we propagate A into all, possibly more than one,
125 : : COND_EXPRs that use X.
126 : :
127 : : In addition to eliminating the variable and the statement which assigns
128 : : a value to the variable, we may be able to later thread the jump without
129 : : adding insane complexity in the dominator optimizer.
130 : :
131 : : Also note these transformations can cascade. We handle this by having
132 : : a worklist of COND_EXPR statements to examine. As we make a change to
133 : : a statement, we put it back on the worklist to examine on the next
134 : : iteration of the main loop.
135 : :
136 : : A second class of propagation opportunities arises for ADDR_EXPR
137 : : nodes.
138 : :
139 : : ptr = &x->y->z;
140 : : res = *ptr;
141 : :
142 : : Will get turned into
143 : :
144 : : res = x->y->z;
145 : :
146 : : Or
147 : : ptr = (type1*)&type2var;
148 : : res = *ptr
149 : :
150 : : Will get turned into (if type1 and type2 are the same size
151 : : and neither have volatile on them):
152 : : res = VIEW_CONVERT_EXPR<type1>(type2var)
153 : :
154 : : Or
155 : :
156 : : ptr = &x[0];
157 : : ptr2 = ptr + <constant>;
158 : :
159 : : Will get turned into
160 : :
161 : : ptr2 = &x[constant/elementsize];
162 : :
163 : : Or
164 : :
165 : : ptr = &x[0];
166 : : offset = index * element_size;
167 : : offset_p = (pointer) offset;
168 : : ptr2 = ptr + offset_p
169 : :
170 : : Will get turned into:
171 : :
172 : : ptr2 = &x[index];
173 : :
174 : : Or
175 : : ssa = (int) decl
176 : : res = ssa & 1
177 : :
178 : : Provided that decl has known alignment >= 2, will get turned into
179 : :
180 : : res = 0
181 : :
182 : : We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to
183 : : allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent
184 : : {NOT_EXPR,NEG_EXPR}.
185 : :
186 : : This will (of course) be extended as other needs arise. */
187 : :
188 : : /* Data structure that contains simplifiable vectorized permute sequences.
189 : : See recognise_vec_perm_simplify_seq () for a description of the sequence. */
190 : :
191 : : struct _vec_perm_simplify_seq
192 : : {
193 : : /* Defining stmts of vectors in the sequence. */
194 : : gassign *v_1_stmt;
195 : : gassign *v_2_stmt;
196 : : gassign *v_x_stmt;
197 : : gassign *v_y_stmt;
198 : : /* Final permute statment. */
199 : : gassign *stmt;
200 : : /* New selector indices for stmt. */
201 : : tree new_sel;
202 : : /* Elements of each vector and selector. */
203 : : unsigned int nelts;
204 : : };
205 : : typedef struct _vec_perm_simplify_seq *vec_perm_simplify_seq;
206 : :
207 : : static bool forward_propagate_addr_expr (tree, tree, bool);
208 : :
209 : : /* Set to true if we delete dead edges during the optimization. */
210 : : static bool cfg_changed;
211 : :
212 : : static tree rhs_to_tree (tree type, gimple *stmt);
213 : :
214 : : static bitmap to_purge;
215 : :
216 : : /* Const-and-copy lattice. */
217 : : static vec<tree> lattice;
218 : :
219 : : /* Set the lattice entry for NAME to VAL. */
220 : : static void
221 : 32828014 : fwprop_set_lattice_val (tree name, tree val)
222 : : {
223 : 32828014 : if (TREE_CODE (name) == SSA_NAME)
224 : : {
225 : 32828014 : if (SSA_NAME_VERSION (name) >= lattice.length ())
226 : : {
227 : 32154 : lattice.reserve (num_ssa_names - lattice.length ());
228 : 21436 : lattice.quick_grow_cleared (num_ssa_names);
229 : : }
230 : 32828014 : lattice[SSA_NAME_VERSION (name)] = val;
231 : : /* As this now constitutes a copy duplicate points-to
232 : : and range info appropriately. */
233 : 32828014 : if (TREE_CODE (val) == SSA_NAME)
234 : 32383870 : maybe_duplicate_ssa_info_at_copy (name, val);
235 : : }
236 : 32828014 : }
237 : :
238 : : /* Invalidate the lattice entry for NAME, done when releasing SSA names. */
239 : : static void
240 : 923413 : fwprop_invalidate_lattice (tree name)
241 : : {
242 : 923413 : if (name
243 : 921075 : && TREE_CODE (name) == SSA_NAME
244 : 1844361 : && SSA_NAME_VERSION (name) < lattice.length ())
245 : 920919 : lattice[SSA_NAME_VERSION (name)] = NULL_TREE;
246 : 923413 : }
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 : 27958035 : get_prop_source_stmt (tree name, bool single_use_only, bool *single_use_p)
258 : : {
259 : 27958035 : bool single_use = true;
260 : :
261 : 27959007 : do {
262 : 27958521 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
263 : :
264 : 27958521 : if (!has_single_use (name))
265 : : {
266 : 15167609 : single_use = false;
267 : 15167609 : 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 : 27957106 : if (!is_gimple_assign (def_stmt))
273 : : return NULL;
274 : :
275 : : /* If def_stmt is a simple copy, continue looking. */
276 : 19669129 : if (gimple_assign_rhs_code (def_stmt) == SSA_NAME)
277 : 486 : name = gimple_assign_rhs1 (def_stmt);
278 : : else
279 : : {
280 : 19668643 : if (!single_use_only && single_use_p)
281 : 19367412 : *single_use_p = single_use;
282 : :
283 : 19668643 : 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 : 27569582 : can_propagate_from (gimple *def_stmt)
293 : : {
294 : 27569582 : gcc_assert (is_gimple_assign (def_stmt));
295 : :
296 : : /* If the rhs has side-effects we cannot propagate from it. */
297 : 27569582 : if (gimple_has_volatile_ops (def_stmt))
298 : : return false;
299 : :
300 : : /* If the rhs is a load we cannot propagate from it. */
301 : 26980020 : if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_reference
302 : 26980020 : || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_declaration)
303 : : return false;
304 : :
305 : : /* Constants can be always propagated. */
306 : 13397798 : if (gimple_assign_single_p (def_stmt)
307 : 13397798 : && 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 : 13397798 : 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 : 13397163 : if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
319 : : {
320 : 3220566 : tree rhs = gimple_assign_rhs1 (def_stmt);
321 : 3220566 : 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 : 225463 : remove_prop_source_from_use (tree name)
336 : : {
337 : 284666 : gimple_stmt_iterator gsi;
338 : 284666 : gimple *stmt;
339 : :
340 : 284666 : do {
341 : 284666 : basic_block bb;
342 : :
343 : 284666 : if (SSA_NAME_IN_FREE_LIST (name)
344 : 284623 : || SSA_NAME_IS_DEFAULT_DEF (name)
345 : 565851 : || !has_zero_uses (name))
346 : : break;
347 : :
348 : 59651 : stmt = SSA_NAME_DEF_STMT (name);
349 : 59651 : if (gimple_code (stmt) == GIMPLE_PHI
350 : 59651 : || gimple_has_side_effects (stmt))
351 : : break;
352 : :
353 : 59651 : bb = gimple_bb (stmt);
354 : 59651 : gsi = gsi_for_stmt (stmt);
355 : 59651 : unlink_stmt_vdef (stmt);
356 : 59651 : if (gsi_remove (&gsi, true))
357 : 6 : bitmap_set_bit (to_purge, bb->index);
358 : 59651 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
359 : 59651 : release_defs (stmt);
360 : :
361 : 59651 : name = is_gimple_assign (stmt) ? gimple_assign_rhs1 (stmt) : NULL_TREE;
362 : 59651 : } while (name && TREE_CODE (name) == SSA_NAME);
363 : :
364 : 225463 : }
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 : 7331124 : rhs_to_tree (tree type, gimple *stmt)
375 : : {
376 : 7331124 : location_t loc = gimple_location (stmt);
377 : 7331124 : enum tree_code code = gimple_assign_rhs_code (stmt);
378 : 7331124 : switch (get_gimple_rhs_class (code))
379 : : {
380 : 11922 : case GIMPLE_TERNARY_RHS:
381 : 11922 : return fold_build3_loc (loc, code, type, gimple_assign_rhs1 (stmt),
382 : : gimple_assign_rhs2 (stmt),
383 : 11922 : gimple_assign_rhs3 (stmt));
384 : 5065344 : case GIMPLE_BINARY_RHS:
385 : 5065344 : return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (stmt),
386 : 5065344 : gimple_assign_rhs2 (stmt));
387 : 2011182 : case GIMPLE_UNARY_RHS:
388 : 2011182 : return build1 (code, type, gimple_assign_rhs1 (stmt));
389 : 242676 : case GIMPLE_SINGLE_RHS:
390 : 242676 : 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 : 8247022 : combine_cond_expr_cond (gimple *stmt, enum tree_code code, tree type,
404 : : tree op0, tree op1, bool invariant_only)
405 : : {
406 : 8247022 : tree t;
407 : :
408 : 8247022 : gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
409 : :
410 : 8247022 : fold_defer_overflow_warnings ();
411 : 8247022 : t = fold_binary_loc (gimple_location (stmt), code, type, op0, op1);
412 : 8247022 : if (!t)
413 : : {
414 : 4691501 : fold_undefer_overflow_warnings (false, NULL, 0);
415 : 4691501 : return NULL_TREE;
416 : : }
417 : :
418 : : /* Require that we got a boolean type out if we put one in. */
419 : 3555521 : gcc_assert (TREE_CODE (TREE_TYPE (t)) == TREE_CODE (type));
420 : :
421 : : /* Canonicalize the combined condition for use in a COND_EXPR. */
422 : 3555521 : t = canonicalize_cond_expr_cond (t);
423 : :
424 : : /* Bail out if we required an invariant but didn't get one. */
425 : 3555521 : if (!t || (invariant_only && !is_gimple_min_invariant (t)))
426 : : {
427 : 3331936 : fold_undefer_overflow_warnings (false, NULL, 0);
428 : 3331936 : return NULL_TREE;
429 : : }
430 : :
431 : 223585 : bool nowarn = warning_suppressed_p (stmt, OPT_Wstrict_overflow);
432 : 223585 : fold_undefer_overflow_warnings (!nowarn, stmt, 0);
433 : :
434 : 223585 : 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 : 21979893 : forward_propagate_into_comparison_1 (gimple *stmt,
443 : : enum tree_code code, tree type,
444 : : tree op0, tree op1)
445 : : {
446 : 21979893 : tree tmp = NULL_TREE;
447 : 21979893 : tree rhs0 = NULL_TREE, rhs1 = NULL_TREE;
448 : 21979893 : 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 : 21979893 : if (TREE_CODE (op0) == SSA_NAME)
453 : : {
454 : 21943340 : gimple *def_stmt = get_prop_source_stmt (op0, false, &single_use0_p);
455 : 21943340 : if (def_stmt && can_propagate_from (def_stmt))
456 : : {
457 : 5587747 : enum tree_code def_code = gimple_assign_rhs_code (def_stmt);
458 : 5587747 : bool invariant_only_p = !single_use0_p;
459 : :
460 : 5587747 : rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt);
461 : :
462 : : /* Always combine comparisons or conversions from booleans. */
463 : 5587747 : if (TREE_CODE (op1) == INTEGER_CST
464 : 5587747 : && ((CONVERT_EXPR_CODE_P (def_code)
465 : 862259 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs0, 0)))
466 : : == BOOLEAN_TYPE)
467 : 3615488 : || TREE_CODE_CLASS (def_code) == tcc_comparison))
468 : : invariant_only_p = false;
469 : :
470 : 5587747 : tmp = combine_cond_expr_cond (stmt, code, type,
471 : : rhs0, op1, invariant_only_p);
472 : 5587747 : if (tmp)
473 : : return tmp;
474 : : }
475 : : }
476 : :
477 : : /* If that wasn't successful, try the second operand. */
478 : 21764283 : if (TREE_CODE (op1) == SSA_NAME)
479 : : {
480 : 5466228 : gimple *def_stmt = get_prop_source_stmt (op1, false, &single_use1_p);
481 : 5466228 : if (def_stmt && can_propagate_from (def_stmt))
482 : : {
483 : 1743377 : rhs1 = rhs_to_tree (TREE_TYPE (op0), def_stmt);
484 : 3486754 : tmp = combine_cond_expr_cond (stmt, code, type,
485 : 1743377 : op0, rhs1, !single_use1_p);
486 : 1743377 : if (tmp)
487 : : return tmp;
488 : : }
489 : : }
490 : :
491 : : /* If that wasn't successful either, try both operands. */
492 : 21758244 : if (rhs0 != NULL_TREE
493 : 21758244 : && rhs1 != NULL_TREE)
494 : 915898 : tmp = combine_cond_expr_cond (stmt, code, type,
495 : : rhs0, rhs1,
496 : 915898 : !(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 : 2568359 : forward_propagate_into_comparison (gimple_stmt_iterator *gsi)
507 : : {
508 : 2568359 : gimple *stmt = gsi_stmt (*gsi);
509 : 2568359 : tree tmp;
510 : 2568359 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
511 : 2568359 : tree rhs1 = gimple_assign_rhs1 (stmt);
512 : 2568359 : tree rhs2 = gimple_assign_rhs2 (stmt);
513 : :
514 : : /* Combine the comparison with defining statements. */
515 : 2568359 : tmp = forward_propagate_into_comparison_1 (stmt,
516 : : gimple_assign_rhs_code (stmt),
517 : : type, rhs1, rhs2);
518 : 2568359 : if (tmp && useless_type_conversion_p (type, TREE_TYPE (tmp)))
519 : : {
520 : 6945 : 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 : 6945 : gimple_assign_set_rhs_from_tree (gsi, tmp);
529 : 6945 : fold_stmt (gsi);
530 : 6945 : update_stmt (gsi_stmt (*gsi));
531 : :
532 : 6945 : if (TREE_CODE (rhs1) == SSA_NAME)
533 : 6945 : remove_prop_source_from_use (rhs1);
534 : 6945 : if (TREE_CODE (rhs2) == SSA_NAME)
535 : 2766 : remove_prop_source_from_use (rhs2);
536 : 6945 : 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 : 19411534 : forward_propagate_into_gimple_cond (gcond *stmt)
549 : : {
550 : 19411534 : tree tmp;
551 : 19411534 : enum tree_code code = gimple_cond_code (stmt);
552 : 19411534 : tree rhs1 = gimple_cond_lhs (stmt);
553 : 19411534 : tree rhs2 = gimple_cond_rhs (stmt);
554 : :
555 : : /* GIMPLE_COND will always be a comparison. */
556 : 19411534 : gcc_assert (TREE_CODE_CLASS (gimple_cond_code (stmt)) == tcc_comparison);
557 : :
558 : 19411534 : tmp = forward_propagate_into_comparison_1 (stmt, code,
559 : : boolean_type_node,
560 : : rhs1, rhs2);
561 : 19411534 : if (tmp
562 : 19411534 : && is_gimple_condexpr_for_cond (tmp))
563 : : {
564 : 210308 : 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 : 210308 : gimple_cond_set_condition_from_tree (stmt, unshare_expr (tmp));
574 : 210308 : update_stmt (stmt);
575 : :
576 : 210308 : if (TREE_CODE (rhs1) == SSA_NAME)
577 : 210308 : remove_prop_source_from_use (rhs1);
578 : 210308 : if (TREE_CODE (rhs2) == SSA_NAME)
579 : 5443 : remove_prop_source_from_use (rhs2);
580 : 210308 : return is_gimple_min_invariant (tmp) ? 2 : 1;
581 : : }
582 : :
583 : 19201226 : 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 : 1974694 : tidy_after_forward_propagate_addr (gimple *stmt)
594 : : {
595 : : /* We may have turned a trapping insn into a non-trapping insn. */
596 : 1974694 : if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
597 : 131 : bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
598 : :
599 : 1974694 : if (TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
600 : 248310 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
601 : 1974694 : }
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 : 2820979 : forward_propagate_addr_expr_1 (tree name, tree def_rhs,
615 : : gimple_stmt_iterator *use_stmt_gsi,
616 : : bool single_use_p)
617 : : {
618 : 2820979 : tree lhs, rhs, rhs2, array_ref;
619 : 2820979 : gimple *use_stmt = gsi_stmt (*use_stmt_gsi);
620 : 2820979 : enum tree_code rhs_code;
621 : 2820979 : bool res = true;
622 : :
623 : 2820979 : gcc_assert (TREE_CODE (def_rhs) == ADDR_EXPR);
624 : :
625 : 2820979 : lhs = gimple_assign_lhs (use_stmt);
626 : 2820979 : rhs_code = gimple_assign_rhs_code (use_stmt);
627 : 2820979 : rhs = gimple_assign_rhs1 (use_stmt);
628 : :
629 : : /* Do not perform copy-propagation but recurse through copy chains. */
630 : 2820979 : if (TREE_CODE (lhs) == SSA_NAME
631 : 1393289 : && rhs_code == SSA_NAME)
632 : 6298 : 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 : 2814681 : if (TREE_CODE (lhs) == SSA_NAME
640 : 1386991 : && 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 : 25671 : if (single_use_p
646 : 25671 : && 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 : 51340 : if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
655 : 2 : || POINTER_TYPE_P (TREE_TYPE (lhs)))
656 : 51340 : && (TYPE_PRECISION (TREE_TYPE (lhs))
657 : 25670 : >= TYPE_PRECISION (TREE_TYPE (def_rhs))))
658 : 25603 : 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 : 2789010 : if (!types_compatible_p (TREE_TYPE (name), TREE_TYPE (def_rhs)))
666 : : return false;
667 : :
668 : : /* Propagate through constant pointer adjustments. */
669 : 2766583 : if (TREE_CODE (lhs) == SSA_NAME
670 : 1340080 : && rhs_code == POINTER_PLUS_EXPR
671 : 1340080 : && rhs == name
672 : 2925233 : && TREE_CODE (gimple_assign_rhs2 (use_stmt)) == INTEGER_CST)
673 : : {
674 : 117497 : 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 : 117497 : 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 : 117497 : if (TREE_CODE (new_def_rhs) == MEM_REF
684 : 117497 : && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs, 0)))
685 : : return false;
686 : 113786 : 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 : 113786 : if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p))
691 : : return true;
692 : :
693 : 37882 : if (useless_type_conversion_p (TREE_TYPE (lhs),
694 : 37882 : TREE_TYPE (new_def_rhs)))
695 : 37882 : 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 : 37882 : gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt);
702 : 37882 : update_stmt (use_stmt);
703 : 37882 : 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 : 2649086 : tree *lhsp = gimple_assign_lhs_ptr (use_stmt);
709 : 4015429 : while (handled_component_p (*lhsp))
710 : 1366343 : lhsp = &TREE_OPERAND (*lhsp, 0);
711 : 2649086 : 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 : 2649086 : if (TREE_CODE (lhs) == MEM_REF
716 : 2649086 : && TREE_OPERAND (lhs, 0) == name)
717 : : {
718 : 883284 : tree def_rhs_base;
719 : 883284 : poly_int64 def_rhs_offset;
720 : : /* If the address is invariant we can always fold it. */
721 : 883284 : if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
722 : : &def_rhs_offset)))
723 : : {
724 : 841618 : poly_offset_int off = mem_ref_offset (lhs);
725 : 841618 : tree new_ptr;
726 : 841618 : off += def_rhs_offset;
727 : 841618 : if (TREE_CODE (def_rhs_base) == MEM_REF)
728 : : {
729 : 821696 : off += mem_ref_offset (def_rhs_base);
730 : 821696 : new_ptr = TREE_OPERAND (def_rhs_base, 0);
731 : : }
732 : : else
733 : 19922 : new_ptr = build_fold_addr_expr (def_rhs_base);
734 : 841618 : TREE_OPERAND (lhs, 0) = new_ptr;
735 : 841618 : TREE_OPERAND (lhs, 1)
736 : 841618 : = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs, 1)), off);
737 : 841618 : tidy_after_forward_propagate_addr (use_stmt);
738 : : /* Continue propagating into the RHS if this was not the only use. */
739 : 841618 : if (single_use_p)
740 : 221482 : 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 : 41666 : else if (integer_zerop (TREE_OPERAND (lhs, 1))
746 : 16711 : && ((gimple_assign_lhs (use_stmt) == lhs
747 : 13338 : && useless_type_conversion_p
748 : 13338 : (TREE_TYPE (TREE_OPERAND (def_rhs, 0)),
749 : 13338 : TREE_TYPE (gimple_assign_rhs1 (use_stmt))))
750 : 12957 : || types_compatible_p (TREE_TYPE (lhs),
751 : 12957 : 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 : 48446 : && (!gimple_clobber_p (use_stmt)
755 : 161 : || TREE_CODE (TREE_OPERAND (def_rhs, 0)) == MEM_REF))
756 : : {
757 : 6619 : tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
758 : 6619 : tree new_offset, new_base, saved, new_lhs;
759 : 23508 : while (handled_component_p (*def_rhs_basep))
760 : 10270 : def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
761 : 6619 : saved = *def_rhs_basep;
762 : 6619 : if (TREE_CODE (*def_rhs_basep) == MEM_REF)
763 : : {
764 : 3699 : new_base = TREE_OPERAND (*def_rhs_basep, 0);
765 : 3699 : new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (lhs, 1)),
766 : : TREE_OPERAND (*def_rhs_basep, 1));
767 : : }
768 : : else
769 : : {
770 : 2920 : new_base = build_fold_addr_expr (*def_rhs_basep);
771 : 2920 : new_offset = TREE_OPERAND (lhs, 1);
772 : : }
773 : 6619 : *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep),
774 : : new_base, new_offset);
775 : 6619 : TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (lhs);
776 : 6619 : TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (lhs);
777 : 6619 : TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (lhs);
778 : 6619 : new_lhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
779 : 6619 : *lhsp = new_lhs;
780 : 6619 : TREE_THIS_VOLATILE (new_lhs) = TREE_THIS_VOLATILE (lhs);
781 : 6619 : TREE_SIDE_EFFECTS (new_lhs) = TREE_SIDE_EFFECTS (lhs);
782 : 6619 : *def_rhs_basep = saved;
783 : 6619 : tidy_after_forward_propagate_addr (use_stmt);
784 : : /* Continue propagating into the RHS if this was not the
785 : : only use. */
786 : 6619 : 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 : 2424777 : tree *rhsp = gimple_assign_rhs1_ptr (use_stmt);
799 : 2424777 : if (TREE_CODE (*rhsp) == ADDR_EXPR)
800 : 236176 : rhsp = &TREE_OPERAND (*rhsp, 0);
801 : 3434437 : while (handled_component_p (*rhsp))
802 : 1009660 : rhsp = &TREE_OPERAND (*rhsp, 0);
803 : 2424777 : 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 : 2424777 : if (TREE_CODE (rhs) == MEM_REF
808 : 2424777 : && TREE_OPERAND (rhs, 0) == name)
809 : : {
810 : 1145146 : tree def_rhs_base;
811 : 1145146 : poly_int64 def_rhs_offset;
812 : 1145146 : if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
813 : : &def_rhs_offset)))
814 : : {
815 : 1111870 : poly_offset_int off = mem_ref_offset (rhs);
816 : 1111870 : tree new_ptr;
817 : 1111870 : off += def_rhs_offset;
818 : 1111870 : if (TREE_CODE (def_rhs_base) == MEM_REF)
819 : : {
820 : 1081631 : off += mem_ref_offset (def_rhs_base);
821 : 1081631 : new_ptr = TREE_OPERAND (def_rhs_base, 0);
822 : : }
823 : : else
824 : 30239 : new_ptr = build_fold_addr_expr (def_rhs_base);
825 : 1111870 : TREE_OPERAND (rhs, 0) = new_ptr;
826 : 1111870 : TREE_OPERAND (rhs, 1)
827 : 1111870 : = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs, 1)), off);
828 : 1111870 : fold_stmt_inplace (use_stmt_gsi);
829 : 1111870 : tidy_after_forward_propagate_addr (use_stmt);
830 : 1111870 : 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 : 33276 : else if (integer_zerop (TREE_OPERAND (rhs, 1))
836 : 33276 : && ((gimple_assign_rhs1 (use_stmt) == rhs
837 : 19423 : && useless_type_conversion_p
838 : 19423 : (TREE_TYPE (gimple_assign_lhs (use_stmt)),
839 : 19423 : TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
840 : 22045 : || types_compatible_p (TREE_TYPE (rhs),
841 : 22045 : TREE_TYPE (TREE_OPERAND (def_rhs, 0)))))
842 : : {
843 : 14587 : tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
844 : 14587 : tree new_offset, new_base, saved, new_rhs;
845 : 51369 : while (handled_component_p (*def_rhs_basep))
846 : 22195 : def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
847 : 14587 : saved = *def_rhs_basep;
848 : 14587 : if (TREE_CODE (*def_rhs_basep) == MEM_REF)
849 : : {
850 : 7249 : new_base = TREE_OPERAND (*def_rhs_basep, 0);
851 : 7249 : new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (rhs, 1)),
852 : : TREE_OPERAND (*def_rhs_basep, 1));
853 : : }
854 : : else
855 : : {
856 : 7338 : new_base = build_fold_addr_expr (*def_rhs_basep);
857 : 7338 : new_offset = TREE_OPERAND (rhs, 1);
858 : : }
859 : 14587 : *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep),
860 : : new_base, new_offset);
861 : 14587 : TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (rhs);
862 : 14587 : TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (rhs);
863 : 14587 : TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (rhs);
864 : 14587 : new_rhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
865 : 14587 : *rhsp = new_rhs;
866 : 14587 : TREE_THIS_VOLATILE (new_rhs) = TREE_THIS_VOLATILE (rhs);
867 : 14587 : TREE_SIDE_EFFECTS (new_rhs) = TREE_SIDE_EFFECTS (rhs);
868 : 14587 : *def_rhs_basep = saved;
869 : 14587 : fold_stmt_inplace (use_stmt_gsi);
870 : 14587 : tidy_after_forward_propagate_addr (use_stmt);
871 : 14587 : 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 : 1298320 : if (gimple_assign_rhs_code (use_stmt) != POINTER_PLUS_EXPR
878 : 1298320 : || 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 : 41153 : array_ref = TREE_OPERAND (def_rhs, 0);
886 : 41153 : if ((TREE_CODE (array_ref) != ARRAY_REF
887 : 4398 : || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
888 : 4398 : || TREE_CODE (TREE_OPERAND (array_ref, 1)) != INTEGER_CST)
889 : 42593 : && TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE)
890 : : return false;
891 : :
892 : 17953 : rhs2 = gimple_assign_rhs2 (use_stmt);
893 : : /* Optimize &x[C1] p+ C2 to &x p+ C3 with C3 = C1 * element_size + C2. */
894 : 17953 : 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 : 3292474 : forward_propagate_addr_expr (tree name, tree rhs, bool parent_single_use_p)
927 : : {
928 : 3292474 : bool all = true;
929 : 3292474 : bool single_use_p = parent_single_use_p && has_single_use (name);
930 : :
931 : 17300368 : for (gimple *use_stmt : gather_imm_use_stmts (name))
932 : : {
933 : 7422946 : bool result;
934 : 7422946 : tree use_rhs;
935 : :
936 : : /* If the use is not in a simple assignment statement, then
937 : : there is nothing we can do. */
938 : 7422946 : if (!is_gimple_assign (use_stmt))
939 : : {
940 : 4601967 : if (!is_gimple_debug (use_stmt))
941 : 1907276 : all = false;
942 : 4601967 : continue;
943 : : }
944 : :
945 : 2820979 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
946 : 2820979 : 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 : 2820979 : if (use_stmt != gsi_stmt (gsi))
951 : : {
952 : 0 : update_stmt (use_stmt);
953 : 0 : use_stmt = gsi_stmt (gsi);
954 : : }
955 : 2820979 : update_stmt (use_stmt);
956 : 2820979 : all &= result;
957 : :
958 : : /* Remove intermediate now unused copy and conversion chains. */
959 : 2820979 : use_rhs = gimple_assign_rhs1 (use_stmt);
960 : 2820979 : if (result
961 : 1466765 : && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
962 : 1229223 : && TREE_CODE (use_rhs) == SSA_NAME
963 : 2899095 : && has_zero_uses (gimple_assign_lhs (use_stmt)))
964 : : {
965 : 78116 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
966 : 78116 : fwprop_invalidate_lattice (gimple_get_lhs (use_stmt));
967 : 78116 : release_defs (use_stmt);
968 : 78116 : gsi_remove (&gsi, true);
969 : : }
970 : 3292474 : }
971 : :
972 : 3292474 : 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 : 11829 : simplify_gimple_switch_label_vec (gswitch *stmt, tree index_type,
981 : : vec<std::pair<int, int> > &edges_to_remove)
982 : : {
983 : 11829 : unsigned int branch_num = gimple_switch_num_labels (stmt);
984 : 11829 : auto_vec<tree> labels (branch_num);
985 : 11829 : 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 : 77727 : for (i = 1; i < branch_num; i++)
990 : 54069 : labels.quick_push (gimple_switch_label (stmt, i));
991 : 11829 : 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 : 11829 : len = labels.length ();
999 : 11829 : 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 : 11829 : }
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 : 118956 : 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 : 118956 : tree cond = gimple_switch_index (stmt);
1056 : 118956 : if (TREE_CODE (cond) == SSA_NAME)
1057 : : {
1058 : 118955 : gimple *def_stmt = SSA_NAME_DEF_STMT (cond);
1059 : 118955 : if (gimple_assign_cast_p (def_stmt))
1060 : : {
1061 : 12335 : tree def = gimple_assign_rhs1 (def_stmt);
1062 : 12335 : 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 : 12335 : tree ti = TREE_TYPE (def);
1069 : 12335 : if (INTEGRAL_TYPE_P (ti)
1070 : 12335 : && TYPE_PRECISION (ti) <= TYPE_PRECISION (TREE_TYPE (cond)))
1071 : : {
1072 : 12080 : size_t n = gimple_switch_num_labels (stmt);
1073 : 12080 : tree min = NULL_TREE, max = NULL_TREE;
1074 : 12080 : if (n > 1)
1075 : : {
1076 : 12080 : min = CASE_LOW (gimple_switch_label (stmt, 1));
1077 : 12080 : if (CASE_HIGH (gimple_switch_label (stmt, n - 1)))
1078 : 154 : max = CASE_HIGH (gimple_switch_label (stmt, n - 1));
1079 : : else
1080 : 11926 : max = CASE_LOW (gimple_switch_label (stmt, n - 1));
1081 : : }
1082 : 12080 : if ((!min || int_fits_type_p (min, ti))
1083 : 12076 : && (!max || int_fits_type_p (max, ti)))
1084 : : {
1085 : 11829 : bitmap_set_bit (simple_dce_worklist,
1086 : 11829 : SSA_NAME_VERSION (cond));
1087 : 11829 : gimple_switch_set_index (stmt, def);
1088 : 11829 : simplify_gimple_switch_label_vec (stmt, ti,
1089 : : edges_to_remove);
1090 : 11829 : update_stmt (stmt);
1091 : 11829 : 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 : 5238 : constant_pointer_difference (tree p1, tree p2)
1105 : : {
1106 : 5238 : int i, j;
1107 : : #define CPD_ITERATIONS 5
1108 : 5238 : tree exps[2][CPD_ITERATIONS];
1109 : 5238 : tree offs[2][CPD_ITERATIONS];
1110 : 5238 : int cnt[2];
1111 : :
1112 : 15714 : for (i = 0; i < 2; i++)
1113 : : {
1114 : 10476 : tree p = i ? p1 : p2;
1115 : 10476 : tree off = size_zero_node;
1116 : 10476 : gimple *stmt;
1117 : 10476 : 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 : 10476 : j = 0;
1124 : 12392 : do
1125 : : {
1126 : 12392 : if (!POINTER_TYPE_P (TREE_TYPE (p)))
1127 : : break;
1128 : 12386 : if (TREE_CODE (p) == ADDR_EXPR)
1129 : : {
1130 : 8996 : tree q = TREE_OPERAND (p, 0);
1131 : 8996 : poly_int64 offset;
1132 : 8996 : tree base = get_addr_base_and_unit_offset (q, &offset);
1133 : 8996 : if (base)
1134 : : {
1135 : 8176 : q = base;
1136 : 8176 : if (maybe_ne (offset, 0))
1137 : 3381 : off = size_binop (PLUS_EXPR, off, size_int (offset));
1138 : : }
1139 : 8996 : if (TREE_CODE (q) == MEM_REF
1140 : 8996 : && 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 : 8783 : exps[i][j] = q;
1150 : 8783 : offs[i][j++] = off;
1151 : 8783 : break;
1152 : : }
1153 : : }
1154 : 3603 : if (TREE_CODE (p) != SSA_NAME)
1155 : : break;
1156 : 3603 : exps[i][j] = p;
1157 : 3603 : offs[i][j++] = off;
1158 : 3603 : if (j == CPD_ITERATIONS)
1159 : : break;
1160 : 3603 : stmt = SSA_NAME_DEF_STMT (p);
1161 : 3603 : if (!is_gimple_assign (stmt) || gimple_assign_lhs (stmt) != p)
1162 : : break;
1163 : 2796 : code = gimple_assign_rhs_code (stmt);
1164 : 2796 : if (code == POINTER_PLUS_EXPR)
1165 : : {
1166 : 1458 : if (TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
1167 : : break;
1168 : 927 : off = size_binop (PLUS_EXPR, off, gimple_assign_rhs2 (stmt));
1169 : 927 : 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 : 10476 : cnt[i] = j;
1178 : : }
1179 : :
1180 : 7342 : for (i = 0; i < cnt[0]; i++)
1181 : 9753 : for (j = 0; j < cnt[1]; j++)
1182 : 7649 : if (exps[0][i] == exps[1][j])
1183 : 4298 : 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 : 20667106 : optimize_aggr_zeroprop_1 (gimple *defstmt, gimple *stmt,
1193 : : tree dest, poly_int64 offset, tree val,
1194 : : poly_offset_int len)
1195 : : {
1196 : 20667106 : tree src2;
1197 : 20667106 : tree len2 = NULL_TREE;
1198 : 20667106 : poly_int64 offset2;
1199 : :
1200 : 20667106 : if (gimple_call_builtin_p (stmt, BUILT_IN_MEMCPY)
1201 : 15871 : && TREE_CODE (gimple_call_arg (stmt, 1)) == ADDR_EXPR
1202 : 20679096 : && poly_int_tree_p (gimple_call_arg (stmt, 2)))
1203 : : {
1204 : 11009 : src2 = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
1205 : 11009 : len2 = gimple_call_arg (stmt, 2);
1206 : : }
1207 : 20656097 : else if (gimple_assign_load_p (stmt) && gimple_store_p (stmt))
1208 : : {
1209 : 1882299 : src2 = gimple_assign_rhs1 (stmt);
1210 : 1882299 : len2 = (TREE_CODE (src2) == COMPONENT_REF
1211 : 1882299 : ? DECL_SIZE_UNIT (TREE_OPERAND (src2, 1))
1212 : 1708053 : : TYPE_SIZE_UNIT (TREE_TYPE (src2)));
1213 : : /* Can only handle zero memsets. */
1214 : 1882299 : if (!integer_zerop (val))
1215 : 20644878 : return;
1216 : : }
1217 : : else
1218 : 18773798 : return;
1219 : :
1220 : 1892360 : if (len2 == NULL_TREE
1221 : 1892360 : || !poly_int_tree_p (len2))
1222 : : return;
1223 : :
1224 : 1892360 : src2 = get_addr_base_and_unit_offset (src2, &offset2);
1225 : 1892360 : if (src2 == NULL_TREE
1226 : 1892360 : || maybe_lt (offset2, offset))
1227 : : return;
1228 : :
1229 : 864882 : 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 : 134557 : if (maybe_gt (wi::to_poly_offset (len2) + (offset2 - offset),
1236 : : len))
1237 : : return;
1238 : :
1239 : 22228 : 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 : 22228 : 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 : 22228 : if (is_gimple_assign (stmt))
1255 : : {
1256 : 22223 : tree ctor_type = TREE_TYPE (gimple_assign_lhs (stmt));
1257 : 22223 : tree ctor = build_constructor (ctor_type, NULL);
1258 : 22223 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1259 : 22223 : gimple_assign_set_rhs_from_tree (&gsi, ctor);
1260 : 22223 : update_stmt (stmt);
1261 : 22223 : 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 : 22228 : 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 : 22228 : 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 : 31000033 : optimize_aggr_zeroprop (gimple *stmt, bool full_walk)
1296 : : {
1297 : 31000033 : ao_ref read;
1298 : 62000066 : if (gimple_has_volatile_ops (stmt))
1299 : 26975548 : return;
1300 : :
1301 : 30075573 : tree dest = NULL_TREE;
1302 : 30075573 : tree val = integer_zero_node;
1303 : 30075573 : tree len = NULL_TREE;
1304 : 30075573 : bool can_use_tbba = true;
1305 : :
1306 : 30075573 : if (gimple_call_builtin_p (stmt, BUILT_IN_MEMSET)
1307 : 112127 : && TREE_CODE (gimple_call_arg (stmt, 0)) == ADDR_EXPR
1308 : 55369 : && TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST
1309 : 30128302 : && poly_int_tree_p (gimple_call_arg (stmt, 2)))
1310 : : {
1311 : 49985 : dest = TREE_OPERAND (gimple_call_arg (stmt, 0), 0);
1312 : 49985 : len = gimple_call_arg (stmt, 2);
1313 : 49985 : val = gimple_call_arg (stmt, 1);
1314 : 49985 : ao_ref_init_from_ptr_and_size (&read, gimple_call_arg (stmt, 0), len);
1315 : 49985 : can_use_tbba = false;
1316 : : }
1317 : 30025588 : else if (gimple_store_p (stmt)
1318 : 29963286 : && gimple_assign_single_p (stmt)
1319 : 59988874 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == STRING_CST)
1320 : : {
1321 : 40551 : tree str = gimple_assign_rhs1 (stmt);
1322 : 40551 : dest = gimple_assign_lhs (stmt);
1323 : 40551 : ao_ref_init (&read, dest);
1324 : : /* The string must contain all null char's for now. */
1325 : 46317 : for (int i = 0; i < TREE_STRING_LENGTH (str); i++)
1326 : : {
1327 : 43322 : 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 : 29985037 : else if (gimple_store_p (stmt)
1337 : 29922735 : && gimple_assign_single_p (stmt)
1338 : 59907772 : && integer_zerop (gimple_assign_rhs1 (stmt)))
1339 : : {
1340 : 3580053 : tree rhs = gimple_assign_rhs1 (stmt);
1341 : 3580053 : tree type = TREE_TYPE (rhs);
1342 : 3580053 : dest = gimple_assign_lhs (stmt);
1343 : 3580053 : ao_ref_init (&read, dest);
1344 : : /* For integral types, the type precision needs to be a multiply of BITS_PER_UNIT. */
1345 : 3580053 : if (INTEGRAL_TYPE_P (type)
1346 : 3580053 : && (TYPE_PRECISION (type) % BITS_PER_UNIT) != 0)
1347 : : dest = NULL_TREE;
1348 : : }
1349 : 26404984 : else if (gimple_store_p (stmt)
1350 : 26342682 : && gimple_assign_single_p (stmt)
1351 : 26342682 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == CONSTRUCTOR
1352 : 27113385 : && !gimple_clobber_p (stmt))
1353 : : {
1354 : 708401 : dest = gimple_assign_lhs (stmt);
1355 : 708401 : ao_ref_init (&read, dest);
1356 : : }
1357 : :
1358 : 4146523 : if (dest == NULL_TREE)
1359 : 25966606 : return;
1360 : :
1361 : 4108967 : if (len == NULL_TREE)
1362 : 4058982 : len = (TREE_CODE (dest) == COMPONENT_REF
1363 : 4058982 : ? DECL_SIZE_UNIT (TREE_OPERAND (dest, 1))
1364 : 1741405 : : TYPE_SIZE_UNIT (TREE_TYPE (dest)));
1365 : 4058982 : if (len == NULL_TREE
1366 : 4108967 : || !poly_int_tree_p (len))
1367 : : return;
1368 : :
1369 : : /* This store needs to be on the byte boundary and pointing to an object. */
1370 : 4108967 : poly_int64 offset;
1371 : 4108967 : tree dest_base = get_addr_base_and_unit_offset (dest, &offset);
1372 : 4108967 : if (dest_base == NULL_TREE)
1373 : : return;
1374 : :
1375 : : /* Setup the worklist. */
1376 : 4024485 : auto_vec<std::pair<tree, unsigned>> worklist;
1377 : 4024485 : unsigned limit = full_walk ? param_sccvn_max_alias_queries_per_access : 0;
1378 : 8048970 : worklist.safe_push (std::make_pair (gimple_vdef (stmt), limit));
1379 : :
1380 : 26319817 : while (!worklist.is_empty ())
1381 : : {
1382 : 18270847 : std::pair<tree, unsigned> top = worklist.pop ();
1383 : 18270847 : tree vdef = top.first;
1384 : 18270847 : limit = top.second;
1385 : 18270847 : gimple *use_stmt;
1386 : 18270847 : imm_use_iterator iter;
1387 : 59154705 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
1388 : : {
1389 : : /* Handling PHI nodes might not be worth it so don't. */
1390 : 22613011 : if (is_a <gphi*> (use_stmt))
1391 : 1945905 : 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 : 2019448 : if ((limit != 0 || gimple_clobber_p (use_stmt))
1397 : 18684928 : && gimple_vdef (use_stmt)
1398 : 36356014 : && !stmt_may_clobber_ref_p_1 (use_stmt, &read,
1399 : : /* tbaa_p = */ can_use_tbba))
1400 : : {
1401 : 14246362 : unsigned new_limit = limit == 0 ? 0 : limit - 1;
1402 : 28492724 : worklist.safe_push (std::make_pair (gimple_vdef (use_stmt),
1403 : : new_limit));
1404 : : }
1405 : :
1406 : 20667106 : optimize_aggr_zeroprop_1 (stmt, use_stmt, dest_base, offset,
1407 : 20667106 : val, wi::to_poly_offset (len));
1408 : 18270847 : }
1409 : : }
1410 : :
1411 : 4024485 : }
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 : 5367924 : split_core_and_offset_size (tree expr,
1421 : : poly_int64 *pbytesize, poly_int64 *pbytepos,
1422 : : tree *poffset, int *preversep)
1423 : : {
1424 : 5367924 : tree core;
1425 : 5367924 : machine_mode mode;
1426 : 5367924 : int unsignedp, volatilep;
1427 : 5367924 : poly_int64 bitsize;
1428 : 5367924 : poly_int64 bitpos;
1429 : 5367924 : location_t loc = EXPR_LOCATION (expr);
1430 : :
1431 : 5367924 : core = get_inner_reference (expr, &bitsize, &bitpos,
1432 : : poffset, &mode, &unsignedp, preversep,
1433 : : &volatilep);
1434 : 10735848 : if (!multiple_p (bitsize, BITS_PER_UNIT, pbytesize))
1435 : : return NULL_TREE;
1436 : 5367924 : 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 : 5367924 : if (TREE_CODE (core) == MEM_REF)
1441 : : {
1442 : 1271926 : poly_offset_int tem;
1443 : 1271926 : tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
1444 : 1271926 : tem += *pbytepos;
1445 : 1271926 : if (tem.to_shwi (pbytepos))
1446 : 1270216 : return TREE_OPERAND (core, 0);
1447 : : }
1448 : 4097708 : core = build_fold_addr_expr_loc (loc, core);
1449 : 4097708 : STRIP_NOPS (core);
1450 : 4097708 : 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 : 1226507 : 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 : 1226507 : 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 : 724851 : 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 : 375251 : if (TREE_CODE (src) == IMAGPART_EXPR
1474 : 375251 : || TREE_CODE (src) == REALPART_EXPR
1475 : 375251 : || TREE_CODE (src) == BIT_FIELD_REF)
1476 : : return NULL_TREE;
1477 : 375251 : tree core1, core2;
1478 : 375251 : poly_int64 bytepos1, bytepos2;
1479 : 375251 : poly_int64 bytesize1, bytesize2;
1480 : 375251 : tree toffset1, toffset2;
1481 : 375251 : int reversep1 = 0;
1482 : 375251 : int reversep2 = 0;
1483 : 375251 : poly_int64 diff = 0;
1484 : 375251 : core1 = split_core_and_offset_size (dest, &bytesize1, &bytepos1,
1485 : : &toffset1, &reversep1);
1486 : 375251 : core2 = split_core_and_offset_size (src2, &bytesize2, &bytepos2,
1487 : : &toffset2, &reversep2);
1488 : 375251 : if (!core1 || !core2)
1489 : : return NULL_TREE;
1490 : 375251 : if (reversep1 != reversep2)
1491 : : return NULL_TREE;
1492 : : /* The sizes of the 2 accesses need to be the same. */
1493 : 375251 : if (!known_eq (bytesize1, bytesize2))
1494 : : return NULL_TREE;
1495 : 164437 : if (!operand_equal_p (core1, core2, 0))
1496 : : return NULL_TREE;
1497 : :
1498 : 23849 : 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 : 23847 : 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 : 23815 : diff += bytepos1 - bytepos2;
1517 : : /* The offset between the 2 need to be 0. */
1518 : 23815 : if (!known_eq (diff, 0))
1519 : : return NULL_TREE;
1520 : 23051 : 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 : 4510505 : same_for_assignment (tree src, tree dest)
1530 : : {
1531 : 4510505 : 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 : 4507605 : if (DECL_P (dest) && DECL_P (src))
1536 : : return false;
1537 : :
1538 : 2308711 : tree core1, core2;
1539 : 2308711 : poly_int64 bytepos1, bytepos2;
1540 : 2308711 : poly_int64 bytesize1, bytesize2;
1541 : 2308711 : tree toffset1, toffset2;
1542 : 2308711 : int reversep1 = 0;
1543 : 2308711 : int reversep2 = 0;
1544 : 2308711 : poly_int64 diff = 0;
1545 : 2308711 : core1 = split_core_and_offset_size (dest, &bytesize1, &bytepos1,
1546 : : &toffset1, &reversep1);
1547 : 2308711 : core2 = split_core_and_offset_size (src, &bytesize2, &bytepos2,
1548 : : &toffset2, &reversep2);
1549 : 2308711 : if (!core1 || !core2)
1550 : : return false;
1551 : 2308711 : if (reversep1 != reversep2)
1552 : : return false;
1553 : : /* The sizes of the 2 accesses need to be the same. */
1554 : 2308711 : if (!known_eq (bytesize1, bytesize2))
1555 : : return false;
1556 : 2307604 : if (!operand_equal_p (core1, core2, 0))
1557 : : return false;
1558 : 6735 : 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 : 6392 : 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 : 6392 : diff += bytepos1 - bytepos2;
1577 : : /* The offset between the 2 need to be 0. */
1578 : 6392 : 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 : 1022872 : optimize_agr_copyprop_1 (gimple *stmt, gimple *use_stmt,
1588 : : tree dest, tree src)
1589 : : {
1590 : 1022872 : gcc_assert (gimple_assign_load_p (use_stmt)
1591 : : && gimple_store_p (use_stmt));
1592 : 2045744 : if (gimple_has_volatile_ops (use_stmt))
1593 : 607130 : return;
1594 : 1022871 : tree dest2 = gimple_assign_lhs (use_stmt);
1595 : 1022871 : tree src2 = gimple_assign_rhs1 (use_stmt);
1596 : : /* If the new store is `src2 = src2;` skip over it. */
1597 : 1022871 : if (same_for_assignment (src2, dest2))
1598 : : return;
1599 : 1022310 : src = new_src_based_on_copy (src2, dest, src);
1600 : 1022310 : 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 : 440960 : if (!operand_equal_p (dest2, src, 0)
1620 : 440960 : && !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 : 28711 : poly_int64 offset1, offset2;
1626 : 28711 : tree len = TYPE_SIZE_UNIT (TREE_TYPE (src));
1627 : 28711 : if (len == NULL_TREE
1628 : 28711 : || !tree_fits_poly_int64_p (len))
1629 : 25218 : return;
1630 : 28711 : tree base1 = get_addr_base_and_unit_offset (dest2, &offset1);
1631 : 28711 : tree base2 = get_addr_base_and_unit_offset (src, &offset2);
1632 : 28711 : poly_int64 size = tree_to_poly_int64 (len);
1633 : : /* If the bases are 2 different decls,
1634 : : then there can be no overlapping. */
1635 : 28711 : if (base1 && base2
1636 : 28664 : && DECL_P (base1) && DECL_P (base2)
1637 : 1899 : && 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 : 27122 : else if (!base1
1642 : 27122 : || !base2
1643 : 27122 : || !operand_equal_p (base1, base2))
1644 : : {
1645 : 26669 : unsigned int align1 = get_object_alignment (src);
1646 : 26669 : unsigned int align2 = get_object_alignment (dest2);
1647 : 26669 : align1 /= BITS_PER_UNIT;
1648 : 26669 : 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 : 26669 : if (maybe_lt (align1, size)
1653 : 26669 : || maybe_lt (align2, size))
1654 : 25218 : 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 : 453 : else if (ranges_maybe_overlap_p (offset1, size, offset2, size)
1660 : 453 : && !known_eq (offset2, offset1))
1661 : : return;
1662 : : }
1663 : :
1664 : 415742 : 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 : 415742 : gimple *orig_stmt = use_stmt;
1672 : 415742 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1673 : 415742 : gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (src));
1674 : 415742 : update_stmt (use_stmt);
1675 : :
1676 : 415742 : 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 : 415742 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, use_stmt))
1682 : 0 : bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
1683 : 415742 : 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 : 714324 : optimize_agr_copyprop_arg (gimple *defstmt, gcall *call,
1691 : : tree dest, tree src)
1692 : : {
1693 : 714324 : bool changed = false;
1694 : 2370849 : for (unsigned arg = 0; arg < gimple_call_num_args (call); arg++)
1695 : : {
1696 : 1656525 : tree *argptr = gimple_call_arg_ptr (call, arg);
1697 : 3108853 : if (TREE_CODE (*argptr) == SSA_NAME
1698 : 962452 : || is_gimple_min_invariant (*argptr)
1699 : 1860722 : || TYPE_VOLATILE (TREE_TYPE (*argptr)))
1700 : 1452328 : continue;
1701 : 204197 : tree newsrc = new_src_based_on_copy (*argptr, dest, src);
1702 : 204197 : if (!newsrc)
1703 : 120450 : continue;
1704 : :
1705 : 83747 : 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 : 83747 : *argptr = unshare_expr (newsrc);
1713 : 83747 : changed = true;
1714 : 83747 : 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 : 714324 : if (changed)
1721 : 83499 : update_stmt (call);
1722 : 714324 : }
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 : 3901150 : optimize_agr_copyprop (gimple *stmt)
1743 : : {
1744 : 7802300 : if (gimple_has_volatile_ops (stmt))
1745 : 416098 : return;
1746 : :
1747 : : /* Can't prop if the statement could throw. */
1748 : 3900043 : if (stmt_could_throw_p (cfun, stmt))
1749 : : return;
1750 : :
1751 : 3487634 : tree dest = gimple_assign_lhs (stmt);
1752 : 3487634 : tree src = gimple_assign_rhs1 (stmt);
1753 : : /* If the statement is `src = src;` then ignore it. */
1754 : 3487634 : if (same_for_assignment (dest, src))
1755 : : return;
1756 : :
1757 : 3485052 : tree vdef = gimple_vdef (stmt);
1758 : 3485052 : imm_use_iterator iter;
1759 : 3485052 : gimple *use_stmt;
1760 : 13513581 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
1761 : : {
1762 : 6543477 : if (gimple_assign_load_p (use_stmt)
1763 : 6543477 : && gimple_store_p (use_stmt))
1764 : 1022872 : optimize_agr_copyprop_1 (stmt, use_stmt, dest, src);
1765 : 5520605 : else if (is_gimple_call (use_stmt))
1766 : 714324 : optimize_agr_copyprop_arg (stmt, as_a<gcall*>(use_stmt), dest, src);
1767 : 3485052 : }
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 : 7120367 : 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 : 7120367 : if (optimize_debug)
1787 : 4933067 : return;
1788 : 7116946 : ao_ref read;
1789 : 7116946 : basic_block bb = gimple_bb (stmt);
1790 : 7116946 : tree lhs = gimple_assign_lhs (stmt);
1791 : : /* Only handle clobbers of a full decl. */
1792 : 7116946 : if (!DECL_P (lhs))
1793 : : return;
1794 : 6406473 : ao_ref_init (&read, lhs);
1795 : 6406473 : tree vuse = gimple_vuse (stmt);
1796 : 6406473 : unsigned limit = full_walk ? param_sccvn_max_alias_queries_per_access : 4;
1797 : 16283424 : while (limit)
1798 : : {
1799 : 16272137 : gimple *ostmt = SSA_NAME_DEF_STMT (vuse);
1800 : : /* Don't handle phis, just declare to be done. */
1801 : 16272137 : if (is_a<gphi*>(ostmt) || gimple_nop_p (ostmt))
1802 : : break;
1803 : 14096124 : 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 : 14096124 : if (obb != bb && !dominated_by_p (CDI_DOMINATORS, bb, obb))
1807 : 4219173 : return;
1808 : 14096124 : gimple *use_stmt;
1809 : 14096124 : imm_use_iterator iter;
1810 : 56968861 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, gimple_vdef (ostmt))
1811 : : {
1812 : 16560557 : basic_block ubb = gimple_bb (use_stmt);
1813 : 16560557 : if (stmt == use_stmt)
1814 : 4877547 : 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 : 11683010 : if (gimple_clobber_p (use_stmt)
1819 : 11683010 : && lhs == gimple_assign_lhs (use_stmt))
1820 : 169753 : 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 : 11513257 : if (gphi *use_phi = dyn_cast<gphi*>(use_stmt))
1824 : : {
1825 : 345242 : use_operand_p ou;
1826 : 345242 : gimple *ostmt;
1827 : 345242 : if (single_imm_use (gimple_phi_result (use_phi), &ou, &ostmt)
1828 : 294361 : && gimple_clobber_p (ostmt)
1829 : 576040 : && lhs == gimple_assign_lhs (ostmt))
1830 : 67356 : continue;
1831 : : /* A phi node will never be dominating the clobber. */
1832 : 277886 : return;
1833 : : }
1834 : : /* The use needs to be dominating the clobber. */
1835 : 1402302 : if ((ubb != bb && !dominated_by_p (CDI_DOMINATORS, bb, ubb))
1836 : 11876673 : || ref_maybe_used_by_stmt_p (use_stmt, &read, false))
1837 : 1140535 : return;
1838 : : /* Count the above alias lookup towards the limit. */
1839 : 10027480 : limit--;
1840 : 10027480 : if (limit == 0)
1841 : : return;
1842 : 1880068 : }
1843 : 12216056 : 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 : 12216056 : if (is_a <gcall*>(ostmt)
1847 : 12216056 : && lhs == gimple_call_lhs (ostmt))
1848 : : {
1849 : : /* Don't remove stores/statements that are needed for non-call
1850 : : eh to work. */
1851 : 3530 : 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 : 3524 : if (stmt_could_throw_p (cfun, ostmt))
1856 : 959 : bitmap_set_bit (to_purge, obb->index);
1857 : 3524 : int flags = gimple_call_flags (ostmt);
1858 : 3524 : if ((flags & (ECF_PURE|ECF_CONST|ECF_NOVOPS))
1859 : 252 : && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
1860 : : {
1861 : 171 : gimple_stmt_iterator gsi = gsi_for_stmt (ostmt);
1862 : 171 : 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 : 171 : unlink_stmt_vdef (ostmt);
1869 : 171 : release_defs (ostmt);
1870 : 171 : gsi_remove (&gsi, true);
1871 : 171 : statistics_counter_event (cfun, "delete call dead store", 1);
1872 : : /* Only remove the first store previous statement. */
1873 : 171 : return;
1874 : : }
1875 : : /* Make sure we do not remove a return slot we cannot reconstruct
1876 : : later. */
1877 : 3353 : if (gimple_call_return_slot_opt_p (as_a <gcall *>(ostmt))
1878 : 3353 : && (TREE_ADDRESSABLE (TREE_TYPE (gimple_call_fntype (ostmt)))
1879 : 566 : || !poly_int_tree_p
1880 : 566 : (TYPE_SIZE (TREE_TYPE (gimple_call_fntype (ostmt))))))
1881 : : return;
1882 : 664 : 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 : 664 : gimple_call_set_lhs (ostmt, NULL_TREE);
1889 : 664 : update_stmt (ostmt);
1890 : 664 : statistics_counter_event (cfun, "removed lhs call", 1);
1891 : 664 : return;
1892 : : }
1893 : : /* This an assignment store to the clobbered decl,
1894 : : then maybe remove it. */
1895 : 12212526 : if (is_a <gassign*>(ostmt)
1896 : 10349469 : && gimple_store_p (ostmt)
1897 : 10349469 : && !gimple_clobber_p (ostmt)
1898 : 15283102 : && lhs == gimple_assign_lhs (ostmt))
1899 : : {
1900 : : /* Don't remove stores/statements that are needed for non-call
1901 : : eh to work. */
1902 : 168089 : 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 : 163099 : if (stmt_could_throw_p (cfun, ostmt))
1907 : 0 : bitmap_set_bit (to_purge, obb->index);
1908 : 163099 : gimple_stmt_iterator gsi = gsi_for_stmt (ostmt);
1909 : 163099 : 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 : 163099 : unlink_stmt_vdef (ostmt);
1916 : 163099 : release_defs (ostmt);
1917 : 163099 : gsi_remove (&gsi, true);
1918 : 163099 : statistics_counter_event (cfun, "delete dead store", 1);
1919 : : /* Only remove the first store previous statement. */
1920 : 163099 : 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 : 12044437 : if (stmt_may_clobber_ref_p_1 (ostmt, &read, false)
1932 : 12044437 : || ref_maybe_used_by_stmt_p (ostmt, &read, false))
1933 : 2167486 : return;
1934 : 9876951 : 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 : 460900 : simplify_builtin_memcmp (gimple_stmt_iterator *gsi_p, gcall *stmt)
1944 : : {
1945 : : /* Make sure memcmp arguments are the correct type. */
1946 : 460900 : if (gimple_call_num_args (stmt) != 3)
1947 : : return false;
1948 : 460900 : tree arg1 = gimple_call_arg (stmt, 0);
1949 : 460900 : tree arg2 = gimple_call_arg (stmt, 1);
1950 : 460900 : tree len = gimple_call_arg (stmt, 2);
1951 : :
1952 : 460900 : if (!POINTER_TYPE_P (TREE_TYPE (arg1)))
1953 : : return false;
1954 : 460900 : if (!POINTER_TYPE_P (TREE_TYPE (arg2)))
1955 : : return false;
1956 : 460900 : 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 : 460900 : tree res = gimple_call_lhs (stmt);
1962 : :
1963 : 460900 : if (!res || !use_in_zero_equality (res))
1964 : 13791 : return false;
1965 : :
1966 : 447109 : unsigned HOST_WIDE_INT leni;
1967 : :
1968 : 447109 : if (tree_fits_uhwi_p (len)
1969 : 620772 : && (leni = tree_to_uhwi (len)) <= GET_MODE_SIZE (word_mode)
1970 : 526116 : && pow2p_hwi (leni))
1971 : : {
1972 : 18977 : leni *= CHAR_TYPE_SIZE;
1973 : 18977 : unsigned align1 = get_pointer_alignment (arg1);
1974 : 18977 : unsigned align2 = get_pointer_alignment (arg2);
1975 : 18977 : unsigned align = MIN (align1, align2);
1976 : 18977 : scalar_int_mode mode;
1977 : 18977 : if (int_mode_for_size (leni, 1).exists (&mode)
1978 : 18977 : && (align >= leni || !targetm.slow_unaligned_access (mode, align)))
1979 : : {
1980 : 18977 : location_t loc = gimple_location (stmt);
1981 : 18977 : tree type, off;
1982 : 18977 : type = build_nonstandard_integer_type (leni, 1);
1983 : 37954 : gcc_assert (known_eq (GET_MODE_BITSIZE (TYPE_MODE (type)), leni));
1984 : 18977 : tree ptrtype = build_pointer_type_for_mode (char_type_node,
1985 : : ptr_mode, true);
1986 : 18977 : off = build_int_cst (ptrtype, 0);
1987 : :
1988 : : /* Create unaligned types if needed. */
1989 : 18977 : tree type1 = type, type2 = type;
1990 : 18977 : if (TYPE_ALIGN (type1) > align1)
1991 : 7661 : type1 = build_aligned_type (type1, align1);
1992 : 18977 : if (TYPE_ALIGN (type2) > align2)
1993 : 8161 : type2 = build_aligned_type (type2, align2);
1994 : :
1995 : 18977 : arg1 = build2_loc (loc, MEM_REF, type1, arg1, off);
1996 : 18977 : arg2 = build2_loc (loc, MEM_REF, type2, arg2, off);
1997 : 18977 : tree tem1 = fold_const_aggregate_ref (arg1);
1998 : 18977 : if (tem1)
1999 : 219 : arg1 = tem1;
2000 : 18977 : tree tem2 = fold_const_aggregate_ref (arg2);
2001 : 18977 : if (tem2)
2002 : 7333 : arg2 = tem2;
2003 : 18977 : res = fold_convert_loc (loc, TREE_TYPE (res),
2004 : : fold_build2_loc (loc, NE_EXPR,
2005 : : boolean_type_node,
2006 : : arg1, arg2));
2007 : 18977 : gimplify_and_update_call_from_tree (gsi_p, res);
2008 : 18977 : return true;
2009 : : }
2010 : : }
2011 : :
2012 : : /* Replace memcmp with memcmp_eq if the above fails. */
2013 : 428132 : if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)) == BUILT_IN_MEMCMP_EQ)
2014 : : return false;
2015 : 339245 : if (!fold_before_rtl_expansion_p ())
2016 : : return false;
2017 : 88887 : gimple_call_set_fndecl (stmt, builtin_decl_explicit (BUILT_IN_MEMCMP_EQ));
2018 : 88887 : update_stmt (stmt);
2019 : 88887 : 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 : 16179 : simplify_builtin_memchr (gimple_stmt_iterator *gsi_p, gcall *stmt)
2028 : : {
2029 : 16179 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
2030 : : return false;
2031 : :
2032 : 16179 : if (gimple_call_num_args (stmt) != 3)
2033 : : return false;
2034 : :
2035 : 16179 : tree res = gimple_call_lhs (stmt);
2036 : 16179 : if (!res || !use_in_zero_equality (res))
2037 : 15018 : return false;
2038 : :
2039 : 1161 : tree ptr = gimple_call_arg (stmt, 0);
2040 : 1161 : if (TREE_CODE (ptr) != ADDR_EXPR
2041 : 1161 : || TREE_CODE (TREE_OPERAND (ptr, 0)) != STRING_CST)
2042 : : return false;
2043 : :
2044 : 229 : unsigned HOST_WIDE_INT slen
2045 : 229 : = TREE_STRING_LENGTH (TREE_OPERAND (ptr, 0));
2046 : : /* It must be a non-empty string constant. */
2047 : 229 : if (slen < 2)
2048 : : return false;
2049 : :
2050 : : /* For -Os, only simplify strings with a single character. */
2051 : 225 : if (!optimize_bb_for_speed_p (gimple_bb (stmt))
2052 : 225 : && slen > 2)
2053 : : return false;
2054 : :
2055 : 209 : tree size = gimple_call_arg (stmt, 2);
2056 : : /* Size must be a constant which is <= UNITS_PER_WORD and
2057 : : <= the string length. */
2058 : 209 : if (!tree_fits_uhwi_p (size))
2059 : : return false;
2060 : :
2061 : 209 : unsigned HOST_WIDE_INT sz = tree_to_uhwi (size);
2062 : 210 : if (sz == 0 || sz > UNITS_PER_WORD || sz >= slen)
2063 : : return false;
2064 : :
2065 : 157 : tree ch = gimple_call_arg (stmt, 1);
2066 : 157 : location_t loc = gimple_location (stmt);
2067 : 157 : if (!useless_type_conversion_p (char_type_node,
2068 : 157 : TREE_TYPE (ch)))
2069 : 157 : ch = fold_convert_loc (loc, char_type_node, ch);
2070 : 157 : const char *p = TREE_STRING_POINTER (TREE_OPERAND (ptr, 0));
2071 : 157 : unsigned int isize = sz;
2072 : 157 : tree *op = XALLOCAVEC (tree, isize);
2073 : 651 : for (unsigned int i = 0; i < isize; i++)
2074 : : {
2075 : 494 : op[i] = build_int_cst (char_type_node, p[i]);
2076 : 494 : op[i] = fold_build2_loc (loc, EQ_EXPR, boolean_type_node,
2077 : : op[i], ch);
2078 : : }
2079 : 494 : for (unsigned int i = isize - 1; i >= 1; i--)
2080 : 337 : op[i - 1] = fold_convert_loc (loc, boolean_type_node,
2081 : : fold_build2_loc (loc,
2082 : : BIT_IOR_EXPR,
2083 : : boolean_type_node,
2084 : 337 : op[i - 1],
2085 : 337 : op[i]));
2086 : 157 : res = fold_convert_loc (loc, TREE_TYPE (res), op[0]);
2087 : 157 : gimplify_and_update_call_from_tree (gsi_p, res);
2088 : 157 : 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 : 112287 : simplify_builtin_memcpy_memset (gimple_stmt_iterator *gsi_p, gcall *stmt2)
2102 : : {
2103 : 112287 : if (gimple_call_num_args (stmt2) != 3
2104 : 112287 : || gimple_call_lhs (stmt2)
2105 : : || CHAR_BIT != 8
2106 : 112287 : || BITS_PER_UNIT != 8)
2107 : : return false;
2108 : :
2109 : 215352 : tree vuse = gimple_vuse (stmt2);
2110 : 105403 : if (vuse == NULL)
2111 : : return false;
2112 : 105403 : gimple *stmt1 = SSA_NAME_DEF_STMT (vuse);
2113 : :
2114 : 105403 : tree callee1;
2115 : 105403 : tree ptr1, src1, str1, off1, len1, lhs1;
2116 : 105403 : tree ptr2 = gimple_call_arg (stmt2, 0);
2117 : 105403 : tree val2 = gimple_call_arg (stmt2, 1);
2118 : 105403 : tree len2 = gimple_call_arg (stmt2, 2);
2119 : 105403 : tree diff, vdef, new_str_cst;
2120 : 105403 : gimple *use_stmt;
2121 : 105403 : unsigned int ptr1_align;
2122 : 105403 : unsigned HOST_WIDE_INT src_len;
2123 : 105403 : char *src_buf;
2124 : 105403 : use_operand_p use_p;
2125 : :
2126 : 105403 : if (!tree_fits_shwi_p (val2)
2127 : 101330 : || !tree_fits_uhwi_p (len2)
2128 : 167361 : || compare_tree_int (len2, 1024) == 1)
2129 : 48531 : return false;
2130 : :
2131 : 56872 : 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 : 29376 : callee1 = gimple_call_fndecl (stmt1);
2137 : 29376 : if (callee1 == NULL_TREE
2138 : 29201 : || !fndecl_built_in_p (callee1, BUILT_IN_NORMAL)
2139 : 55179 : || gimple_call_num_args (stmt1) != 3)
2140 : : return false;
2141 : 24543 : if (DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMCPY
2142 : 24543 : && DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMPCPY)
2143 : : return false;
2144 : 10830 : ptr1 = gimple_call_arg (stmt1, 0);
2145 : 10830 : src1 = gimple_call_arg (stmt1, 1);
2146 : 10830 : len1 = gimple_call_arg (stmt1, 2);
2147 : 10830 : lhs1 = gimple_call_lhs (stmt1);
2148 : 10830 : if (!tree_fits_uhwi_p (len1))
2149 : : return false;
2150 : 10743 : str1 = string_constant (src1, &off1, NULL, NULL);
2151 : 10743 : 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 : 27496 : else if (gimple_assign_single_p (stmt1))
2163 : : {
2164 : : /* Otherwise look for length 1 memcpy optimized into
2165 : : assignment. */
2166 : 16748 : ptr1 = gimple_assign_lhs (stmt1);
2167 : 16748 : src1 = gimple_assign_rhs1 (stmt1);
2168 : 16748 : if (TREE_CODE (ptr1) != MEM_REF
2169 : 3175 : || TYPE_MODE (TREE_TYPE (ptr1)) != TYPE_MODE (char_type_node)
2170 : 17728 : || !tree_fits_shwi_p (src1))
2171 : 16356 : return false;
2172 : 392 : ptr1 = build_fold_addr_expr (ptr1);
2173 : 392 : STRIP_USELESS_TYPE_CONVERSION (ptr1);
2174 : 392 : callee1 = NULL_TREE;
2175 : 392 : len1 = size_one_node;
2176 : 392 : lhs1 = NULL_TREE;
2177 : 392 : off1 = size_zero_node;
2178 : 392 : str1 = NULL_TREE;
2179 : : }
2180 : : else
2181 : : return false;
2182 : :
2183 : 5231 : diff = constant_pointer_difference (ptr1, ptr2);
2184 : 5231 : 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 : 5231 : if (diff == NULL
2195 : 4298 : || !tree_fits_uhwi_p (diff)
2196 : 4298 : || tree_int_cst_lt (len1, diff)
2197 : 9285 : || compare_tree_int (diff, 1024) == 1)
2198 : 1177 : 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 : 4054 : src_len = tree_to_uhwi (diff);
2203 : 4054 : src_len += tree_to_uhwi (len2);
2204 : 4054 : if (src_len < tree_to_uhwi (len1))
2205 : : src_len = tree_to_uhwi (len1);
2206 : 4054 : 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 : 4054 : if (lhs1 != NULL_TREE
2212 : 193 : && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
2213 : 4061 : && (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 : 4054 : vdef = gimple_vdef (stmt1);
2221 : 4054 : if (vdef != NULL
2222 : 4054 : && (!single_imm_use (vdef, &use_p, &use_stmt)
2223 : 3299 : || use_stmt != stmt2))
2224 : : return false;
2225 : :
2226 : 3299 : ptr1_align = get_pointer_alignment (ptr1);
2227 : : /* Construct the new source string literal. */
2228 : 3299 : src_buf = XALLOCAVEC (char, src_len + 1);
2229 : 3299 : 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 : 145 : src_buf[0] = tree_to_shwi (src1);
2235 : 3299 : memset (src_buf + tree_to_uhwi (diff),
2236 : 3299 : tree_to_shwi (val2), tree_to_uhwi (len2));
2237 : 3299 : src_buf[src_len] = '\0';
2238 : : /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
2239 : : handle embedded '\0's. */
2240 : 3299 : 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 : 6123926 : simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2, bool full_walk)
2435 : : {
2436 : 6123926 : gimple *stmt2 = gsi_stmt (*gsi_p);
2437 : 6123926 : enum built_in_function other_atomic = END_BUILTINS;
2438 : 6123926 : enum tree_code atomic_op = ERROR_MARK;
2439 : :
2440 : 6123926 : 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 : 460900 : case BUILT_IN_MEMCMP:
2445 : 460900 : case BUILT_IN_MEMCMP_EQ:
2446 : 460900 : return simplify_builtin_memcmp (gsi_p, as_a<gcall*>(stmt2));
2447 : 16179 : case BUILT_IN_MEMCHR:
2448 : 16179 : return simplify_builtin_memchr (gsi_p, as_a<gcall*>(stmt2));
2449 : :
2450 : 112287 : case BUILT_IN_MEMSET:
2451 : 112287 : 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 : 112287 : optimize_aggr_zeroprop (stmt2, full_walk);
2456 : : }
2457 : 112287 : 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 : 51140 : CASE_ATOMIC (ATOMIC_FETCH_ADD, ATOMIC_ADD_FETCH, PLUS_EXPR)
2473 : 8074 : CASE_ATOMIC (ATOMIC_FETCH_SUB, ATOMIC_SUB_FETCH, MINUS_EXPR)
2474 : 2880 : CASE_ATOMIC (ATOMIC_FETCH_AND, ATOMIC_AND_FETCH, BIT_AND_EXPR)
2475 : 2899 : CASE_ATOMIC (ATOMIC_FETCH_XOR, ATOMIC_XOR_FETCH, BIT_XOR_EXPR)
2476 : 3862 : 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 : 14417 : CASE_ATOMIC (ATOMIC_ADD_FETCH, ATOMIC_FETCH_ADD, MINUS_EXPR)
2485 : 9228 : CASE_ATOMIC (ATOMIC_SUB_FETCH, ATOMIC_FETCH_SUB, PLUS_EXPR)
2486 : 2390 : 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 : 107617 : handle_atomic_fetch_op:
2495 : 107617 : if (gimple_call_num_args (stmt2) >= 2 && gimple_call_lhs (stmt2))
2496 : : {
2497 : 63025 : tree lhs2 = gimple_call_lhs (stmt2), lhsc = lhs2;
2498 : 63025 : tree arg = gimple_call_arg (stmt2, 1);
2499 : 63025 : gimple *use_stmt, *cast_stmt = NULL;
2500 : 63025 : use_operand_p use_p;
2501 : 63025 : tree ndecl = builtin_decl_explicit (other_atomic);
2502 : :
2503 : 63025 : if (ndecl == NULL_TREE || !single_imm_use (lhs2, &use_p, &use_stmt))
2504 : : break;
2505 : :
2506 : 61863 : if (gimple_assign_cast_p (use_stmt))
2507 : : {
2508 : 33397 : cast_stmt = use_stmt;
2509 : 33397 : lhsc = gimple_assign_lhs (cast_stmt);
2510 : 33397 : if (lhsc == NULL_TREE
2511 : 33397 : || !INTEGRAL_TYPE_P (TREE_TYPE (lhsc))
2512 : 32782 : || (TYPE_PRECISION (TREE_TYPE (lhsc))
2513 : 32782 : != TYPE_PRECISION (TREE_TYPE (lhs2)))
2514 : 64653 : || !single_imm_use (lhsc, &use_p, &use_stmt))
2515 : : {
2516 : 2725 : use_stmt = cast_stmt;
2517 : 2725 : cast_stmt = NULL;
2518 : 2725 : lhsc = lhs2;
2519 : : }
2520 : : }
2521 : :
2522 : 61863 : bool ok = false;
2523 : 61863 : tree oarg = NULL_TREE;
2524 : 61863 : enum tree_code ccode = ERROR_MARK;
2525 : 61863 : tree crhs1 = NULL_TREE, crhs2 = NULL_TREE;
2526 : 61863 : if (is_gimple_assign (use_stmt)
2527 : 61863 : && 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 : 60447 : else if (atomic_op == MINUS_EXPR
2535 : 13337 : && is_gimple_assign (use_stmt)
2536 : 3678 : && gimple_assign_rhs_code (use_stmt) == PLUS_EXPR
2537 : 203 : && TREE_CODE (arg) == INTEGER_CST
2538 : 60650 : && (TREE_CODE (gimple_assign_rhs2 (use_stmt))
2539 : : == INTEGER_CST))
2540 : : {
2541 : 187 : tree a = fold_convert (TREE_TYPE (lhs2), arg);
2542 : 187 : tree o = fold_convert (TREE_TYPE (lhs2),
2543 : : gimple_assign_rhs2 (use_stmt));
2544 : 187 : if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
2545 : : ok = true;
2546 : : }
2547 : 60260 : else if (atomic_op == BIT_AND_EXPR || atomic_op == BIT_IOR_EXPR)
2548 : : ;
2549 : 55009 : else if (gimple_code (use_stmt) == GIMPLE_COND)
2550 : : {
2551 : 20353 : ccode = gimple_cond_code (use_stmt);
2552 : 20353 : crhs1 = gimple_cond_lhs (use_stmt);
2553 : 20353 : crhs2 = gimple_cond_rhs (use_stmt);
2554 : : }
2555 : 34656 : else if (is_gimple_assign (use_stmt))
2556 : : {
2557 : 9766 : if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
2558 : : {
2559 : 4033 : ccode = gimple_assign_rhs_code (use_stmt);
2560 : 4033 : crhs1 = gimple_assign_rhs1 (use_stmt);
2561 : 4033 : crhs2 = gimple_assign_rhs2 (use_stmt);
2562 : : }
2563 : 5733 : 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 : 25402 : 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 : 23202 : tree o = NULL_TREE;
2580 : 23202 : if (crhs1 == lhsc)
2581 : : o = crhs2;
2582 : 133 : else if (crhs2 == lhsc)
2583 : 133 : o = crhs1;
2584 : 23202 : if (o && atomic_op != PLUS_EXPR)
2585 : : oarg = o;
2586 : 10958 : else if (o
2587 : 10958 : && TREE_CODE (o) == INTEGER_CST
2588 : 10958 : && TREE_CODE (arg) == INTEGER_CST)
2589 : : {
2590 : 10226 : tree a = fold_convert (TREE_TYPE (lhs2), arg);
2591 : 10226 : o = fold_convert (TREE_TYPE (lhs2), o);
2592 : 10226 : if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
2593 : 61863 : ok = true;
2594 : : }
2595 : : }
2596 : 61863 : if (oarg && !ok)
2597 : : {
2598 : 13660 : if (operand_equal_p (arg, oarg, 0))
2599 : : ok = true;
2600 : 12314 : 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 : 60517 : if (ok)
2655 : : {
2656 : 2604 : tree new_lhs = make_ssa_name (TREE_TYPE (lhs2));
2657 : 2604 : gimple_call_set_lhs (stmt2, new_lhs);
2658 : 2604 : gimple_call_set_fndecl (stmt2, ndecl);
2659 : 2604 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
2660 : 2604 : if (ccode == ERROR_MARK)
2661 : 2008 : gimple_assign_set_rhs_with_ops (&gsi, cast_stmt
2662 : : ? NOP_EXPR : SSA_NAME,
2663 : : new_lhs);
2664 : : else
2665 : : {
2666 : 1377 : crhs1 = new_lhs;
2667 : 1377 : crhs2 = build_zero_cst (TREE_TYPE (lhs2));
2668 : 1377 : if (gimple_code (use_stmt) == GIMPLE_COND)
2669 : : {
2670 : 1024 : gcond *cond_stmt = as_a <gcond *> (use_stmt);
2671 : 1024 : gimple_cond_set_lhs (cond_stmt, crhs1);
2672 : 1024 : gimple_cond_set_rhs (cond_stmt, crhs2);
2673 : : }
2674 : 353 : else if (gimple_assign_rhs_class (use_stmt)
2675 : : == GIMPLE_BINARY_RHS)
2676 : : {
2677 : 353 : gimple_assign_set_rhs1 (use_stmt, crhs1);
2678 : 353 : 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 : 2604 : update_stmt (use_stmt);
2690 : 2604 : if (atomic_op != BIT_AND_EXPR
2691 : 2604 : && atomic_op != BIT_IOR_EXPR
2692 : 2604 : && !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 : 2263 : gsi = gsi_for_stmt (stmt2);
2708 : 2263 : tree type = TREE_TYPE (lhs2);
2709 : 2263 : if (TREE_CODE (arg) == INTEGER_CST)
2710 : 1681 : 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 : 2263 : enum tree_code rcode;
2719 : 2263 : switch (atomic_op)
2720 : : {
2721 : : case PLUS_EXPR: rcode = MINUS_EXPR; break;
2722 : 748 : case MINUS_EXPR: rcode = PLUS_EXPR; break;
2723 : 492 : case BIT_XOR_EXPR: rcode = atomic_op; break;
2724 : 0 : default: gcc_unreachable ();
2725 : : }
2726 : 2263 : gimple *g = gimple_build_assign (lhs2, rcode, new_lhs, arg);
2727 : 2263 : gsi_insert_after (&gsi, g, GSI_NEW_STMT);
2728 : 2263 : 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 : 17167270 : defcodefor_name (tree name, enum tree_code *code, tree *arg1, tree *arg2)
2774 : : {
2775 : 17167270 : gimple *def;
2776 : 17167270 : enum tree_code code1;
2777 : 17167270 : tree arg11;
2778 : 17167270 : tree arg21;
2779 : 17167270 : tree arg31;
2780 : 17167270 : enum gimple_rhs_class grhs_class;
2781 : :
2782 : 17167270 : code1 = TREE_CODE (name);
2783 : 17167270 : arg11 = name;
2784 : 17167270 : arg21 = NULL_TREE;
2785 : 17167270 : arg31 = NULL_TREE;
2786 : 17167270 : grhs_class = get_gimple_rhs_class (code1);
2787 : :
2788 : 17167270 : if (code1 == SSA_NAME)
2789 : : {
2790 : 11431422 : def = SSA_NAME_DEF_STMT (name);
2791 : :
2792 : 11431422 : if (def && is_gimple_assign (def)
2793 : 18487927 : && can_propagate_from (def))
2794 : : {
2795 : 4899626 : code1 = gimple_assign_rhs_code (def);
2796 : 4899626 : arg11 = gimple_assign_rhs1 (def);
2797 : 4899626 : arg21 = gimple_assign_rhs2 (def);
2798 : 4899626 : arg31 = gimple_assign_rhs3 (def);
2799 : : }
2800 : : }
2801 : 5735848 : else if (grhs_class != GIMPLE_SINGLE_RHS)
2802 : 0 : code1 = ERROR_MARK;
2803 : :
2804 : 17167270 : *code = code1;
2805 : 17167270 : *arg1 = arg11;
2806 : 17167270 : if (arg2)
2807 : 17150034 : *arg2 = arg21;
2808 : 17167270 : if (arg31)
2809 : 2540 : *code = ERROR_MARK;
2810 : 17167270 : }
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 : 10100974 : simplify_rotate (gimple_stmt_iterator *gsi)
2858 : : {
2859 : 10100974 : gimple *stmt = gsi_stmt (*gsi);
2860 : 10100974 : tree arg[2], rtype, rotcnt = NULL_TREE;
2861 : 10100974 : tree def_arg1[2], def_arg2[2];
2862 : 10100974 : enum tree_code def_code[2];
2863 : 10100974 : tree lhs;
2864 : 10100974 : int i;
2865 : 10100974 : bool swapped_p = false;
2866 : 10100974 : gimple *g;
2867 : 10100974 : gimple *def_arg_stmt[2] = { NULL, NULL };
2868 : 10100974 : int wider_prec = 0;
2869 : 10100974 : bool add_masking = false;
2870 : :
2871 : 10100974 : arg[0] = gimple_assign_rhs1 (stmt);
2872 : 10100974 : arg[1] = gimple_assign_rhs2 (stmt);
2873 : 10100974 : rtype = TREE_TYPE (arg[0]);
2874 : :
2875 : : /* Only create rotates in complete modes. Other cases are not
2876 : : expanded properly. */
2877 : 10100974 : if (!INTEGRAL_TYPE_P (rtype)
2878 : 10100974 : || !type_has_mode_precision_p (rtype))
2879 : 1566495 : return false;
2880 : :
2881 : 25603437 : for (i = 0; i < 2; i++)
2882 : : {
2883 : 17068958 : defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
2884 : 17068958 : if (TREE_CODE (arg[i]) == SSA_NAME)
2885 : 11333110 : def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
2886 : : }
2887 : :
2888 : : /* Look through narrowing (or same precision) conversions. */
2889 : 7553477 : if (CONVERT_EXPR_CODE_P (def_code[0])
2890 : 981002 : && CONVERT_EXPR_CODE_P (def_code[1])
2891 : 138372 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[0]))
2892 : 114843 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[1]))
2893 : 107671 : && TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
2894 : 107671 : == TYPE_PRECISION (TREE_TYPE (def_arg1[1]))
2895 : 61047 : && TYPE_PRECISION (TREE_TYPE (def_arg1[0])) >= TYPE_PRECISION (rtype)
2896 : 41264 : && has_single_use (arg[0])
2897 : 8566077 : && has_single_use (arg[1]))
2898 : : {
2899 : 27956 : wider_prec = TYPE_PRECISION (TREE_TYPE (def_arg1[0]));
2900 : 83868 : for (i = 0; i < 2; i++)
2901 : : {
2902 : 55912 : arg[i] = def_arg1[i];
2903 : 55912 : defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
2904 : 55912 : if (TREE_CODE (arg[i]) == SSA_NAME)
2905 : 55912 : 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 : 8506523 : i = (def_code[0] == LSHIFT_EXPR || def_code[0] == RSHIFT_EXPR);
2913 : 7535359 : if (CONVERT_EXPR_CODE_P (def_code[i])
2914 : 971164 : && (def_code[1 - i] == LSHIFT_EXPR || def_code[1 - i] == RSHIFT_EXPR)
2915 : 29694 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[i]))
2916 : 28610 : && TYPE_PRECISION (rtype) == TYPE_PRECISION (TREE_TYPE (def_arg1[i]))
2917 : 8509812 : && 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 : 8735013 : for (i = 0; i < 2; i++)
2928 : 8710386 : if (def_code[i] != LSHIFT_EXPR && def_code[i] != RSHIFT_EXPR)
2929 : : return false;
2930 : 245565 : else if (!has_single_use (arg[i]))
2931 : : return false;
2932 : 24627 : 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 : 20214 : 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 : 20210 : if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
2952 : 32139 : || !types_compatible_p (TREE_TYPE (def_arg1[0]),
2953 : 11929 : TREE_TYPE (def_arg1[1])))
2954 : : {
2955 : 8281 : if ((TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
2956 : 8281 : != TYPE_PRECISION (TREE_TYPE (def_arg1[1])))
2957 : 8281 : || (TYPE_UNSIGNED (TREE_TYPE (def_arg1[0]))
2958 : 8281 : == TYPE_UNSIGNED (TREE_TYPE (def_arg1[1]))))
2959 : 8257 : 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 : 1967452 : simplify_count_zeroes (gimple_stmt_iterator *gsi)
3342 : : {
3343 : 1967452 : gimple *stmt = gsi_stmt (*gsi);
3344 : 1967452 : tree array_ref = gimple_assign_rhs1 (stmt);
3345 : 1967452 : tree res_ops[3];
3346 : :
3347 : 1967452 : gcc_checking_assert (TREE_CODE (array_ref) == ARRAY_REF);
3348 : :
3349 : 1967452 : 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 : 1967452 : 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 : 1967430 : 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 : 178620 : simplify_permutation (gimple_stmt_iterator *gsi)
3574 : : {
3575 : 178620 : gimple *stmt = gsi_stmt (*gsi);
3576 : 178620 : gimple *def_stmt = NULL;
3577 : 178620 : tree op0, op1, op2, op3, arg0, arg1;
3578 : 178620 : enum tree_code code, code2 = ERROR_MARK;
3579 : 178620 : bool single_use_op0 = false;
3580 : :
3581 : 178620 : gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
3582 : :
3583 : 178620 : op0 = gimple_assign_rhs1 (stmt);
3584 : 178620 : op1 = gimple_assign_rhs2 (stmt);
3585 : 178620 : op2 = gimple_assign_rhs3 (stmt);
3586 : :
3587 : 178620 : if (TREE_CODE (op2) != VECTOR_CST)
3588 : : return false;
3589 : :
3590 : 175902 : if (TREE_CODE (op0) == VECTOR_CST)
3591 : : {
3592 : : code = VECTOR_CST;
3593 : : arg0 = op0;
3594 : : }
3595 : 174052 : else if (TREE_CODE (op0) == SSA_NAME)
3596 : : {
3597 : 174052 : def_stmt = get_prop_source_stmt (op0, false, &single_use_op0);
3598 : 174052 : if (!def_stmt)
3599 : : return false;
3600 : 166048 : code = gimple_assign_rhs_code (def_stmt);
3601 : 166048 : 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 : 164883 : if (!can_propagate_from (def_stmt))
3619 : : return false;
3620 : 19117 : arg0 = gimple_assign_rhs1 (def_stmt);
3621 : : }
3622 : : else
3623 : : return false;
3624 : :
3625 : : /* Two consecutive shuffles. */
3626 : 19117 : if (code == VEC_PERM_EXPR)
3627 : : {
3628 : 6091 : tree orig;
3629 : 6091 : int ident;
3630 : :
3631 : 6091 : 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 : 14876 : else if (code == CONSTRUCTOR
3649 : 14876 : || code == VECTOR_CST
3650 : : || code == VIEW_CONVERT_EXPR)
3651 : : {
3652 : 2564 : if (op0 != op1)
3653 : : {
3654 : 2410 : if (TREE_CODE (op0) == SSA_NAME && !single_use_op0)
3655 : : return false;
3656 : :
3657 : 2056 : if (TREE_CODE (op1) == VECTOR_CST)
3658 : : arg1 = op1;
3659 : 1590 : else if (TREE_CODE (op1) == SSA_NAME)
3660 : : {
3661 : 1590 : gimple *def_stmt2 = get_prop_source_stmt (op1, true, NULL);
3662 : 1590 : 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 : 398963 : get_bit_field_ref_def (tree val, enum tree_code &conv_code)
3798 : : {
3799 : 398963 : if (TREE_CODE (val) != SSA_NAME)
3800 : : return NULL_TREE ;
3801 : 372825 : gimple *def_stmt = get_prop_source_stmt (val, false, NULL);
3802 : 372825 : if (!def_stmt)
3803 : : return NULL_TREE;
3804 : 301076 : enum tree_code code = gimple_assign_rhs_code (def_stmt);
3805 : 301076 : if (code == FLOAT_EXPR
3806 : 301076 : || code == FIX_TRUNC_EXPR
3807 : : || CONVERT_EXPR_CODE_P (code))
3808 : : {
3809 : 183597 : tree op1 = gimple_assign_rhs1 (def_stmt);
3810 : 183597 : if (conv_code == ERROR_MARK)
3811 : 88831 : conv_code = code;
3812 : 94766 : else if (conv_code != code)
3813 : : return NULL_TREE;
3814 : 183571 : if (TREE_CODE (op1) != SSA_NAME)
3815 : : return NULL_TREE;
3816 : 73347 : def_stmt = SSA_NAME_DEF_STMT (op1);
3817 : 73347 : if (! is_gimple_assign (def_stmt))
3818 : : return NULL_TREE;
3819 : 58450 : code = gimple_assign_rhs_code (def_stmt);
3820 : : }
3821 : 175929 : if (code != BIT_FIELD_REF)
3822 : : return NULL_TREE;
3823 : 22677 : 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 : 151917 : simplify_vector_constructor (gimple_stmt_iterator *gsi)
3830 : : {
3831 : 151917 : gimple *stmt = gsi_stmt (*gsi);
3832 : 151917 : tree op, orig[2], type, elem_type;
3833 : 151917 : unsigned elem_size, i;
3834 : 151917 : unsigned HOST_WIDE_INT nelts;
3835 : 151917 : unsigned HOST_WIDE_INT refnelts;
3836 : 151917 : enum tree_code conv_code;
3837 : 151917 : constructor_elt *elt;
3838 : :
3839 : 151917 : op = gimple_assign_rhs1 (stmt);
3840 : 151917 : type = TREE_TYPE (op);
3841 : 151917 : gcc_checking_assert (TREE_CODE (op) == CONSTRUCTOR
3842 : : && TREE_CODE (type) == VECTOR_TYPE);
3843 : :
3844 : 151917 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
3845 : : return false;
3846 : 151917 : elem_type = TREE_TYPE (type);
3847 : 151917 : elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type));
3848 : :
3849 : 151917 : orig[0] = NULL;
3850 : 151917 : orig[1] = NULL;
3851 : 151917 : conv_code = ERROR_MARK;
3852 : 151917 : bool maybe_ident = true;
3853 : 151917 : bool maybe_blend[2] = { true, true };
3854 : 151917 : tree one_constant = NULL_TREE;
3855 : 151917 : tree one_nonconstant = NULL_TREE;
3856 : 151917 : tree subelt;
3857 : 151917 : auto_vec<tree> constants;
3858 : 151917 : constants.safe_grow_cleared (nelts, true);
3859 : 151917 : auto_vec<std::pair<unsigned, unsigned>, 64> elts;
3860 : 151917 : unsigned int tsubelts = 0;
3861 : 429574 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (op), i, elt)
3862 : : {
3863 : 398963 : tree ref, op1;
3864 : 398963 : unsigned int elem, src_elem_size;
3865 : 398963 : unsigned HOST_WIDE_INT nsubelts = 1;
3866 : :
3867 : 398963 : if (i >= nelts)
3868 : 151917 : return false;
3869 : :
3870 : : /* Look for elements extracted and possibly converted from
3871 : : another vector. */
3872 : 398963 : op1 = get_bit_field_ref_def (elt->value, conv_code);
3873 : 398963 : if (op1
3874 : 22677 : && TREE_CODE ((ref = TREE_OPERAND (op1, 0))) == SSA_NAME
3875 : 4791 : && VECTOR_TYPE_P (TREE_TYPE (ref))
3876 : 4788 : && (tree_nop_conversion_p (TREE_TYPE (op1),
3877 : 4788 : TREE_TYPE (TREE_TYPE (ref)))
3878 : 667 : || (VECTOR_TYPE_P (TREE_TYPE (op1))
3879 : 29 : && tree_nop_conversion_p (TREE_TYPE (TREE_TYPE (op1)),
3880 : 29 : TREE_TYPE (TREE_TYPE (ref)))
3881 : 29 : && TYPE_VECTOR_SUBPARTS (TREE_TYPE (op1))
3882 : 29 : .is_constant (&nsubelts)))
3883 : 4150 : && constant_multiple_p (bit_field_size (op1), nsubelts,
3884 : : &src_elem_size)
3885 : 403113 : && constant_multiple_p (bit_field_offset (op1), src_elem_size, &elem)
3886 : 403113 : && TYPE_VECTOR_SUBPARTS (TREE_TYPE (ref)).is_constant (&refnelts))
3887 : : {
3888 : : unsigned int j;
3889 : 4380 : for (j = 0; j < 2; ++j)
3890 : : {
3891 : 4365 : if (!orig[j])
3892 : : {
3893 : 2098 : if (j == 0
3894 : 2260 : || useless_type_conversion_p (TREE_TYPE (orig[0]),
3895 : 162 : TREE_TYPE (ref)))
3896 : : break;
3897 : : }
3898 : 2267 : else if (ref == orig[j])
3899 : : break;
3900 : : }
3901 : : /* Found a suitable vector element. */
3902 : 4150 : if (j < 2)
3903 : : {
3904 : 4135 : orig[j] = ref;
3905 : 4135 : if (elem != i || j != 0)
3906 : 1837 : maybe_ident = false;
3907 : 4135 : if (elem != i)
3908 : 1775 : maybe_blend[j] = false;
3909 : 8291 : for (unsigned int k = 0; k < nsubelts; ++k)
3910 : 4156 : elts.safe_push (std::make_pair (j, elem + k));
3911 : 4135 : tsubelts += nsubelts;
3912 : 4135 : continue;
3913 : 4135 : }
3914 : : /* Else fallthru. */
3915 : : }
3916 : : /* Handle elements not extracted from a vector.
3917 : : 1. constants by permuting with constant vector
3918 : : 2. a unique non-constant element by permuting with a splat vector */
3919 : 394828 : if (orig[1]
3920 : 243967 : && orig[1] != error_mark_node)
3921 : : return false;
3922 : 394812 : orig[1] = error_mark_node;
3923 : 394812 : if (VECTOR_TYPE_P (TREE_TYPE (elt->value))
3924 : 394812 : && !TYPE_VECTOR_SUBPARTS (TREE_TYPE (elt->value))
3925 : 1504 : .is_constant (&nsubelts))
3926 : : return false;
3927 : 394812 : if (CONSTANT_CLASS_P (elt->value))
3928 : : {
3929 : 26134 : if (one_nonconstant)
3930 : : return false;
3931 : 17691 : if (!one_constant)
3932 : 8598 : one_constant = TREE_CODE (elt->value) == VECTOR_CST
3933 : 8598 : ? VECTOR_CST_ELT (elt->value, 0)
3934 : : : elt->value;
3935 : 17691 : if (TREE_CODE (elt->value) == VECTOR_CST)
3936 : : {
3937 : 37 : for (unsigned int k = 0; k < nsubelts; k++)
3938 : 23 : constants[tsubelts + k] = VECTOR_CST_ELT (elt->value, k);
3939 : : }
3940 : : else
3941 : 17677 : constants[tsubelts] = elt->value;
3942 : : }
3943 : : else
3944 : : {
3945 : 368678 : if (one_constant)
3946 : : return false;
3947 : 360355 : subelt = VECTOR_TYPE_P (TREE_TYPE (elt->value))
3948 : 360355 : ? ssa_uniform_vector_p (elt->value)
3949 : : : elt->value;
3950 : 360355 : if (!subelt)
3951 : : return false;
3952 : 358875 : if (!one_nonconstant)
3953 : : one_nonconstant = subelt;
3954 : 218092 : else if (!operand_equal_p (one_nonconstant, subelt, 0))
3955 : : return false;
3956 : : }
3957 : 547055 : for (unsigned int k = 0; k < nsubelts; ++k)
3958 : 273533 : elts.safe_push (std::make_pair (1, tsubelts + k));
3959 : 273522 : tsubelts += nsubelts;
3960 : 273522 : maybe_ident = false;
3961 : : }
3962 : :
3963 : 61222 : if (elts.length () < nelts)
3964 : : return false;
3965 : :
3966 : 29859 : if (! orig[0]
3967 : 29859 : || ! VECTOR_TYPE_P (TREE_TYPE (orig[0])))
3968 : : return false;
3969 : 1494 : refnelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (orig[0])).to_constant ();
3970 : : /* We currently do not handle larger destination vectors. */
3971 : 1494 : if (refnelts < nelts)
3972 : : return false;
3973 : :
3974 : 1368 : if (maybe_ident)
3975 : : {
3976 : 490 : tree conv_src_type
3977 : : = (nelts != refnelts
3978 : 490 : ? (conv_code != ERROR_MARK
3979 : 18 : ? build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])), nelts)
3980 : : : type)
3981 : 472 : : TREE_TYPE (orig[0]));
3982 : 490 : if (conv_code != ERROR_MARK
3983 : 490 : && !supportable_convert_operation (conv_code, type, conv_src_type,
3984 : : &conv_code))
3985 : : {
3986 : : /* Only few targets implement direct conversion patterns so try
3987 : : some simple special cases via VEC_[UN]PACK[_FLOAT]_LO_EXPR. */
3988 : 5 : optab optab;
3989 : 5 : insn_code icode;
3990 : 5 : tree halfvectype, dblvectype;
3991 : 5 : enum tree_code unpack_op;
3992 : :
3993 : 5 : if (!BYTES_BIG_ENDIAN)
3994 : 5 : unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
3995 : 5 : ? VEC_UNPACK_FLOAT_LO_EXPR
3996 : : : VEC_UNPACK_LO_EXPR);
3997 : : else
3998 : : unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
3999 : : ? VEC_UNPACK_FLOAT_HI_EXPR
4000 : : : VEC_UNPACK_HI_EXPR);
4001 : :
4002 : : /* Conversions between DFP and FP have no special tree code
4003 : : but we cannot handle those since all relevant vector conversion
4004 : : optabs only have a single mode. */
4005 : 3 : if (CONVERT_EXPR_CODE_P (conv_code)
4006 : 2 : && FLOAT_TYPE_P (TREE_TYPE (type))
4007 : 9 : && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (type))
4008 : 2 : != DECIMAL_FLOAT_TYPE_P (TREE_TYPE (conv_src_type))))
4009 : : return false;
4010 : :
4011 : 3 : if (CONVERT_EXPR_CODE_P (conv_code)
4012 : 1 : && (2 * TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
4013 : 1 : == TYPE_PRECISION (TREE_TYPE (type)))
4014 : 0 : && mode_for_vector (as_a <scalar_mode>
4015 : 0 : (TYPE_MODE (TREE_TYPE (TREE_TYPE (orig[0])))),
4016 : 0 : nelts * 2).exists ()
4017 : 0 : && (dblvectype
4018 : 0 : = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
4019 : 0 : nelts * 2))
4020 : : /* Only use it for vector modes or for vector booleans
4021 : : represented as scalar bitmasks. See PR95528. */
4022 : 0 : && (VECTOR_MODE_P (TYPE_MODE (dblvectype))
4023 : 0 : || VECTOR_BOOLEAN_TYPE_P (dblvectype))
4024 : 0 : && (optab = optab_for_tree_code (unpack_op,
4025 : : dblvectype,
4026 : : optab_default))
4027 : 0 : && ((icode = optab_handler (optab, TYPE_MODE (dblvectype)))
4028 : : != CODE_FOR_nothing)
4029 : 4 : && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
4030 : : {
4031 : 0 : gimple_seq stmts = NULL;
4032 : 0 : tree dbl;
4033 : 0 : if (refnelts == nelts)
4034 : : {
4035 : : /* ??? Paradoxical subregs don't exist, so insert into
4036 : : the lower half of a wider zero vector. */
4037 : 0 : dbl = gimple_build (&stmts, BIT_INSERT_EXPR, dblvectype,
4038 : : build_zero_cst (dblvectype), orig[0],
4039 : 0 : bitsize_zero_node);
4040 : : }
4041 : 0 : else if (refnelts == 2 * nelts)
4042 : : dbl = orig[0];
4043 : : else
4044 : 0 : dbl = gimple_build (&stmts, BIT_FIELD_REF, dblvectype,
4045 : 0 : orig[0], TYPE_SIZE (dblvectype),
4046 : 0 : bitsize_zero_node);
4047 : 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
4048 : 0 : gimple_assign_set_rhs_with_ops (gsi, unpack_op, dbl);
4049 : : }
4050 : 3 : else if (CONVERT_EXPR_CODE_P (conv_code)
4051 : 1 : && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
4052 : 1 : == 2 * TYPE_PRECISION (TREE_TYPE (type)))
4053 : 1 : && mode_for_vector (as_a <scalar_mode>
4054 : 1 : (TYPE_MODE
4055 : : (TREE_TYPE (TREE_TYPE (orig[0])))),
4056 : 2 : nelts / 2).exists ()
4057 : 1 : && (halfvectype
4058 : 1 : = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
4059 : 1 : nelts / 2))
4060 : : /* Only use it for vector modes or for vector booleans
4061 : : represented as scalar bitmasks. See PR95528. */
4062 : 1 : && (VECTOR_MODE_P (TYPE_MODE (halfvectype))
4063 : 0 : || VECTOR_BOOLEAN_TYPE_P (halfvectype))
4064 : 1 : && (optab = optab_for_tree_code (VEC_PACK_TRUNC_EXPR,
4065 : : halfvectype,
4066 : : optab_default))
4067 : 1 : && ((icode = optab_handler (optab, TYPE_MODE (halfvectype)))
4068 : : != CODE_FOR_nothing)
4069 : 5 : && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
4070 : : {
4071 : 0 : gimple_seq stmts = NULL;
4072 : 0 : tree low = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
4073 : 0 : orig[0], TYPE_SIZE (halfvectype),
4074 : 0 : bitsize_zero_node);
4075 : 0 : tree hig = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
4076 : 0 : orig[0], TYPE_SIZE (halfvectype),
4077 : 0 : TYPE_SIZE (halfvectype));
4078 : 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
4079 : 0 : gimple_assign_set_rhs_with_ops (gsi, VEC_PACK_TRUNC_EXPR,
4080 : : low, hig);
4081 : : }
4082 : : else
4083 : 4 : return false;
4084 : 0 : update_stmt (gsi_stmt (*gsi));
4085 : 0 : return true;
4086 : : }
4087 : 485 : if (nelts != refnelts)
4088 : : {
4089 : 18 : gassign *lowpart
4090 : 18 : = gimple_build_assign (make_ssa_name (conv_src_type),
4091 : : build3 (BIT_FIELD_REF, conv_src_type,
4092 : 18 : orig[0], TYPE_SIZE (conv_src_type),
4093 : : bitsize_zero_node));
4094 : 18 : gsi_insert_before (gsi, lowpart, GSI_SAME_STMT);
4095 : 18 : orig[0] = gimple_assign_lhs (lowpart);
4096 : : }
4097 : 485 : if (conv_code == ERROR_MARK)
4098 : : {
4099 : 468 : tree src_type = TREE_TYPE (orig[0]);
4100 : 468 : if (!useless_type_conversion_p (type, src_type))
4101 : : {
4102 : 0 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
4103 : : TYPE_VECTOR_SUBPARTS (src_type))
4104 : : && tree_nop_conversion_p (TREE_TYPE (type),
4105 : : TREE_TYPE (src_type)));
4106 : 0 : tree rhs = build1 (VIEW_CONVERT_EXPR, type, orig[0]);
4107 : 0 : orig[0] = make_ssa_name (type);
4108 : 0 : gassign *assign = gimple_build_assign (orig[0], rhs);
4109 : 0 : gsi_insert_before (gsi, assign, GSI_SAME_STMT);
4110 : : }
4111 : 468 : gimple_assign_set_rhs_from_tree (gsi, orig[0]);
4112 : : }
4113 : : else
4114 : 17 : gimple_assign_set_rhs_with_ops (gsi, conv_code, orig[0],
4115 : : NULL_TREE, NULL_TREE);
4116 : : }
4117 : : else
4118 : : {
4119 : : /* If we combine a vector with a non-vector avoid cases where
4120 : : we'll obviously end up with more GIMPLE stmts which is when
4121 : : we'll later not fold this to a single insert into the vector
4122 : : and we had a single extract originally. See PR92819. */
4123 : 878 : if (nelts == 2
4124 : 701 : && refnelts > 2
4125 : 122 : && orig[1] == error_mark_node
4126 : 31 : && !maybe_blend[0])
4127 : 385 : return false;
4128 : 849 : tree mask_type, perm_type, conv_src_type;
4129 : 849 : perm_type = TREE_TYPE (orig[0]);
4130 : 849 : conv_src_type = (nelts == refnelts
4131 : 849 : ? perm_type
4132 : 116 : : build_vector_type (TREE_TYPE (perm_type), nelts));
4133 : 849 : if (conv_code != ERROR_MARK
4134 : 849 : && !supportable_convert_operation (conv_code, type, conv_src_type,
4135 : : &conv_code))
4136 : : return false;
4137 : :
4138 : : /* Now that we know the number of elements of the source build the
4139 : : permute vector.
4140 : : ??? When the second vector has constant values we can shuffle
4141 : : it and its source indexes to make the permutation supported.
4142 : : For now it mimics a blend. */
4143 : 609 : vec_perm_builder sel (refnelts, refnelts, 1);
4144 : 609 : bool all_same_p = true;
4145 : 5282 : for (i = 0; i < elts.length (); ++i)
4146 : : {
4147 : 2032 : sel.quick_push (elts[i].second + elts[i].first * refnelts);
4148 : 2032 : all_same_p &= known_eq (sel[i], sel[0]);
4149 : : }
4150 : : /* And fill the tail with "something". It's really don't care,
4151 : : and ideally we'd allow VEC_PERM to have a smaller destination
4152 : : vector. As a heuristic:
4153 : :
4154 : : (a) if what we have so far duplicates a single element, make the
4155 : : tail do the same
4156 : :
4157 : : (b) otherwise preserve a uniform orig[0]. This facilitates
4158 : : later pattern-matching of VEC_PERM_EXPR to a BIT_INSERT_EXPR. */
4159 : 1263 : for (; i < refnelts; ++i)
4160 : 1308 : sel.quick_push (all_same_p
4161 : 1962 : ? sel[0]
4162 : 36 : : (elts[0].second == 0 && elts[0].first == 0
4163 : 908 : ? 0 : refnelts) + i);
4164 : 772 : vec_perm_indices indices (sel, orig[1] ? 2 : 1, refnelts);
4165 : 609 : machine_mode vmode = TYPE_MODE (perm_type);
4166 : 609 : if (!can_vec_perm_const_p (vmode, vmode, indices))
4167 : : return false;
4168 : 493 : mask_type
4169 : 493 : = build_vector_type (build_nonstandard_integer_type (elem_size, 1),
4170 : 493 : refnelts);
4171 : 493 : if (GET_MODE_CLASS (TYPE_MODE (mask_type)) != MODE_VECTOR_INT
4172 : 1479 : || maybe_ne (GET_MODE_SIZE (TYPE_MODE (mask_type)),
4173 : 986 : GET_MODE_SIZE (TYPE_MODE (perm_type))))
4174 : 0 : return false;
4175 : 493 : tree op2 = vec_perm_indices_to_tree (mask_type, indices);
4176 : 493 : bool converted_orig1 = false;
4177 : 493 : gimple_seq stmts = NULL;
4178 : 493 : if (!orig[1])
4179 : 120 : orig[1] = orig[0];
4180 : 373 : else if (orig[1] == error_mark_node
4181 : 240 : && one_nonconstant)
4182 : : {
4183 : : /* ??? We can see if we can safely convert to the original
4184 : : element type. */
4185 : 153 : converted_orig1 = conv_code != ERROR_MARK;
4186 : 153 : tree target_type = converted_orig1 ? type : perm_type;
4187 : 153 : tree nonconstant_for_splat = one_nonconstant;
4188 : : /* If there's a nop conversion between the target element type and
4189 : : the nonconstant's type, convert it. */
4190 : 153 : if (!useless_type_conversion_p (TREE_TYPE (target_type),
4191 : 153 : TREE_TYPE (one_nonconstant)))
4192 : 0 : nonconstant_for_splat
4193 : 0 : = gimple_build (&stmts, NOP_EXPR, TREE_TYPE (target_type),
4194 : : one_nonconstant);
4195 : 153 : orig[1] = gimple_build_vector_from_val (&stmts, UNKNOWN_LOCATION,
4196 : : target_type,
4197 : : nonconstant_for_splat);
4198 : 153 : }
4199 : 220 : else if (orig[1] == error_mark_node)
4200 : : {
4201 : : /* ??? See if we can convert the vector to the original type. */
4202 : 87 : converted_orig1 = conv_code != ERROR_MARK;
4203 : 87 : unsigned n = converted_orig1 ? nelts : refnelts;
4204 : 73 : tree target_type = converted_orig1 ? type : perm_type;
4205 : 87 : tree_vector_builder vec (target_type, n, 1);
4206 : 533 : for (unsigned i = 0; i < n; ++i)
4207 : 864 : if (i < nelts && constants[i])
4208 : : {
4209 : 225 : tree constant = constants[i];
4210 : : /* If there's a nop conversion, convert the constant. */
4211 : 225 : if (!useless_type_conversion_p (TREE_TYPE (target_type),
4212 : 225 : TREE_TYPE (constant)))
4213 : 0 : constant = fold_convert (TREE_TYPE (target_type), constant);
4214 : 225 : vec.quick_push (constant);
4215 : : }
4216 : : else
4217 : : {
4218 : : /* ??? Push a don't-care value. */
4219 : 221 : tree constant = one_constant;
4220 : 221 : if (!useless_type_conversion_p (TREE_TYPE (target_type),
4221 : 221 : TREE_TYPE (constant)))
4222 : 0 : constant = fold_convert (TREE_TYPE (target_type), constant);
4223 : 221 : vec.quick_push (constant);
4224 : : }
4225 : 87 : orig[1] = vec.build ();
4226 : 87 : }
4227 : 360 : tree blend_op2 = NULL_TREE;
4228 : 360 : if (converted_orig1)
4229 : : {
4230 : : /* Make sure we can do a blend in the target type. */
4231 : 15 : vec_perm_builder sel (nelts, nelts, 1);
4232 : 75 : for (i = 0; i < elts.length (); ++i)
4233 : 60 : sel.quick_push (elts[i].first
4234 : 60 : ? elts[i].second + nelts : i);
4235 : 15 : vec_perm_indices indices (sel, 2, nelts);
4236 : 15 : machine_mode vmode = TYPE_MODE (type);
4237 : 15 : if (!can_vec_perm_const_p (vmode, vmode, indices))
4238 : : return false;
4239 : 15 : mask_type
4240 : 15 : = build_vector_type (build_nonstandard_integer_type (elem_size, 1),
4241 : 15 : nelts);
4242 : 15 : if (GET_MODE_CLASS (TYPE_MODE (mask_type)) != MODE_VECTOR_INT
4243 : 45 : || maybe_ne (GET_MODE_SIZE (TYPE_MODE (mask_type)),
4244 : 30 : GET_MODE_SIZE (TYPE_MODE (type))))
4245 : 0 : return false;
4246 : 15 : blend_op2 = vec_perm_indices_to_tree (mask_type, indices);
4247 : 15 : }
4248 : :
4249 : : /* For a real orig[1] (no splat, constant etc.) we might need to
4250 : : nop-convert it. Do so here. */
4251 : 493 : if (orig[1] && orig[1] != error_mark_node
4252 : 493 : && !useless_type_conversion_p (perm_type, TREE_TYPE (orig[1]))
4253 : 508 : && tree_nop_conversion_p (TREE_TYPE (perm_type),
4254 : 15 : TREE_TYPE (TREE_TYPE (orig[1]))))
4255 : 0 : orig[1] = gimple_build (&stmts, VIEW_CONVERT_EXPR, perm_type,
4256 : : orig[1]);
4257 : :
4258 : 493 : tree orig1_for_perm
4259 : 493 : = converted_orig1 ? build_zero_cst (perm_type) : orig[1];
4260 : 493 : tree res = gimple_build (&stmts, VEC_PERM_EXPR, perm_type,
4261 : : orig[0], orig1_for_perm, op2);
4262 : 493 : if (nelts != refnelts)
4263 : 97 : res = gimple_build (&stmts, BIT_FIELD_REF,
4264 : 97 : conv_code != ERROR_MARK ? conv_src_type : type,
4265 : 97 : res, TYPE_SIZE (type), bitsize_zero_node);
4266 : 493 : if (conv_code != ERROR_MARK)
4267 : 22 : res = gimple_build (&stmts, conv_code, type, res);
4268 : 471 : else if (!useless_type_conversion_p (type, TREE_TYPE (res)))
4269 : : {
4270 : 1 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
4271 : : TYPE_VECTOR_SUBPARTS (perm_type))
4272 : : && tree_nop_conversion_p (TREE_TYPE (type),
4273 : : TREE_TYPE (perm_type)));
4274 : 1 : res = gimple_build (&stmts, VIEW_CONVERT_EXPR, type, res);
4275 : : }
4276 : : /* Blend in the actual constant. */
4277 : 493 : if (converted_orig1)
4278 : 15 : res = gimple_build (&stmts, VEC_PERM_EXPR, type,
4279 : 15 : res, orig[1], blend_op2);
4280 : 493 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
4281 : 493 : gimple_assign_set_rhs_with_ops (gsi, SSA_NAME, res);
4282 : 609 : }
4283 : 978 : update_stmt (gsi_stmt (*gsi));
4284 : 978 : return true;
4285 : 151917 : }
4286 : :
4287 : : /* Prepare a TARGET_MEM_REF ref so that it can be subsetted as
4288 : : lvalue. This splits out an address computation stmt before *GSI
4289 : : and returns a MEM_REF wrapping the address. */
4290 : :
4291 : : static tree
4292 : 1094 : prepare_target_mem_ref_lvalue (tree ref, gimple_stmt_iterator *gsi)
4293 : : {
4294 : 1094 : if (TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR)
4295 : 223 : mark_addressable (TREE_OPERAND (TREE_OPERAND (ref, 0), 0));
4296 : 1094 : tree ptrtype = build_pointer_type (TREE_TYPE (ref));
4297 : 1094 : tree tem = make_ssa_name (ptrtype);
4298 : 1094 : gimple *new_stmt
4299 : 1094 : = gimple_build_assign (tem, build1 (ADDR_EXPR, TREE_TYPE (tem),
4300 : : unshare_expr (ref)));
4301 : 1094 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4302 : 2188 : ref = build2_loc (EXPR_LOCATION (ref),
4303 : 1094 : MEM_REF, TREE_TYPE (ref), tem,
4304 : 1094 : build_int_cst (TREE_TYPE (TREE_OPERAND (ref, 1)), 0));
4305 : 1094 : return ref;
4306 : : }
4307 : :
4308 : : /* Rewrite the vector load at *GSI to component-wise loads if the load
4309 : : is only used in BIT_FIELD_REF extractions with eventual intermediate
4310 : : widening. */
4311 : :
4312 : : static void
4313 : 279667 : optimize_vector_load (gimple_stmt_iterator *gsi)
4314 : : {
4315 : 279667 : gimple *stmt = gsi_stmt (*gsi);
4316 : 279667 : tree lhs = gimple_assign_lhs (stmt);
4317 : 279667 : tree rhs = gimple_assign_rhs1 (stmt);
4318 : 279667 : tree vuse = gimple_vuse (stmt);
4319 : :
4320 : : /* Gather BIT_FIELD_REFs to rewrite, looking through
4321 : : VEC_UNPACK_{LO,HI}_EXPR. */
4322 : 279667 : use_operand_p use_p;
4323 : 279667 : imm_use_iterator iter;
4324 : 279667 : bool rewrite = true;
4325 : 279667 : bool scalar_use = false;
4326 : 279667 : bool unpack_use = false;
4327 : 279667 : auto_vec<gimple *, 8> bf_stmts;
4328 : 279667 : auto_vec<tree, 8> worklist;
4329 : 279667 : worklist.quick_push (lhs);
4330 : 281521 : do
4331 : : {
4332 : 281521 : tree def = worklist.pop ();
4333 : 281521 : unsigned HOST_WIDE_INT def_eltsize
4334 : 281521 : = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (def))));
4335 : 637123 : FOR_EACH_IMM_USE_FAST (use_p, iter, def)
4336 : : {
4337 : 335377 : gimple *use_stmt = USE_STMT (use_p);
4338 : 335377 : if (is_gimple_debug (use_stmt))
4339 : 74081 : continue;
4340 : 335325 : if (!is_gimple_assign (use_stmt))
4341 : : {
4342 : : rewrite = false;
4343 : 261296 : break;
4344 : : }
4345 : 301918 : enum tree_code use_code = gimple_assign_rhs_code (use_stmt);
4346 : 301918 : tree use_rhs = gimple_assign_rhs1 (use_stmt);
4347 : 372228 : if (use_code == BIT_FIELD_REF
4348 : 70311 : && TREE_OPERAND (use_rhs, 0) == def
4349 : : /* If its on the VEC_UNPACK_{HI,LO}_EXPR
4350 : : def need to verify it is element aligned. */
4351 : 372229 : && (def == lhs
4352 : 85 : || (known_eq (bit_field_size (use_rhs), def_eltsize)
4353 : 85 : && constant_multiple_p (bit_field_offset (use_rhs),
4354 : : def_eltsize)
4355 : : /* We can simulate the VEC_UNPACK_{HI,LO}_EXPR
4356 : : via a NOP_EXPR only for integral types.
4357 : : ??? Support VEC_UNPACK_FLOAT_{HI,LO}_EXPR. */
4358 : 85 : && INTEGRAL_TYPE_P (TREE_TYPE (use_rhs)))))
4359 : : {
4360 : 70310 : if (!VECTOR_TYPE_P (TREE_TYPE (gimple_assign_lhs (use_stmt))))
4361 : 68246 : scalar_use = true;
4362 : 70310 : bf_stmts.safe_push (use_stmt);
4363 : 70310 : continue;
4364 : : }
4365 : : /* Walk through one level of VEC_UNPACK_{LO,HI}_EXPR. */
4366 : 231608 : if (def == lhs
4367 : 229801 : && (use_code == VEC_UNPACK_HI_EXPR
4368 : 229801 : || use_code == VEC_UNPACK_LO_EXPR)
4369 : 3719 : && use_rhs == lhs)
4370 : : {
4371 : 3719 : unpack_use = true;
4372 : 3719 : worklist.safe_push (gimple_assign_lhs (use_stmt));
4373 : 3719 : continue;
4374 : : }
4375 : : rewrite = false;
4376 : : break;
4377 : 281521 : }
4378 : 281521 : if (!rewrite)
4379 : : break;
4380 : : }
4381 : 40450 : while (!worklist.is_empty ());
4382 : :
4383 : 279667 : rewrite = rewrite && (scalar_use
4384 : 18371 : || unpack_use
4385 : 575 : || !can_implement_p (mov_optab,
4386 : 575 : TYPE_MODE (TREE_TYPE (lhs))));
4387 : 279667 : if (!rewrite)
4388 : : {
4389 : 261471 : gsi_next (gsi);
4390 : 261471 : return;
4391 : : }
4392 : : /* We now have all ultimate uses of the load to rewrite in bf_stmts. */
4393 : :
4394 : : /* Prepare the original ref to be wrapped in adjusted BIT_FIELD_REFs.
4395 : : For TARGET_MEM_REFs we have to separate the LEA from the reference. */
4396 : 18196 : tree load_rhs = rhs;
4397 : 18196 : if (TREE_CODE (load_rhs) == TARGET_MEM_REF)
4398 : 1093 : load_rhs = prepare_target_mem_ref_lvalue (load_rhs, gsi);
4399 : :
4400 : : /* Rewrite the BIT_FIELD_REFs to be actual loads, re-emitting them at
4401 : : the place of the original load. */
4402 : 119834 : for (gimple *use_stmt : bf_stmts)
4403 : : {
4404 : 65246 : tree bfr = gimple_assign_rhs1 (use_stmt);
4405 : 65246 : tree new_rhs = unshare_expr (load_rhs);
4406 : 65246 : if (TREE_OPERAND (bfr, 0) != lhs)
4407 : : {
4408 : : /* When the BIT_FIELD_REF is on the promoted vector we have to
4409 : : adjust it and emit a conversion afterwards. */
4410 : 84 : gimple *def_stmt
4411 : 84 : = SSA_NAME_DEF_STMT (TREE_OPERAND (bfr, 0));
4412 : 84 : enum tree_code def_code
4413 : 84 : = gimple_assign_rhs_code (def_stmt);
4414 : :
4415 : : /* The adjusted BIT_FIELD_REF is of the promotion source
4416 : : vector size and at half of the offset... */
4417 : 84 : new_rhs = fold_build3 (BIT_FIELD_REF,
4418 : : TREE_TYPE (TREE_TYPE (lhs)),
4419 : : new_rhs,
4420 : : TYPE_SIZE (TREE_TYPE (TREE_TYPE (lhs))),
4421 : : size_binop (EXACT_DIV_EXPR,
4422 : : TREE_OPERAND (bfr, 2),
4423 : : bitsize_int (2)));
4424 : : /* ... and offsetted by half of the vector if VEC_UNPACK_HI_EXPR. */
4425 : 84 : if (def_code == (!BYTES_BIG_ENDIAN
4426 : : ? VEC_UNPACK_HI_EXPR : VEC_UNPACK_LO_EXPR))
4427 : 42 : TREE_OPERAND (new_rhs, 2)
4428 : 84 : = size_binop (PLUS_EXPR, TREE_OPERAND (new_rhs, 2),
4429 : : size_binop (EXACT_DIV_EXPR,
4430 : : TYPE_SIZE (TREE_TYPE (lhs)),
4431 : : bitsize_int (2)));
4432 : 84 : tree tem = make_ssa_name (TREE_TYPE (TREE_TYPE (lhs)));
4433 : 84 : gimple *new_stmt = gimple_build_assign (tem, new_rhs);
4434 : 84 : location_t loc = gimple_location (use_stmt);
4435 : 84 : gimple_set_location (new_stmt, loc);
4436 : 84 : gimple_set_vuse (new_stmt, vuse);
4437 : 84 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4438 : : /* Perform scalar promotion. */
4439 : 84 : new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
4440 : : NOP_EXPR, tem);
4441 : 84 : gimple_set_location (new_stmt, loc);
4442 : 84 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4443 : : }
4444 : : else
4445 : : {
4446 : : /* When the BIT_FIELD_REF is on the original load result
4447 : : we can just wrap that. */
4448 : 65162 : tree new_rhs = fold_build3 (BIT_FIELD_REF, TREE_TYPE (bfr),
4449 : : unshare_expr (load_rhs),
4450 : : TREE_OPERAND (bfr, 1),
4451 : : TREE_OPERAND (bfr, 2));
4452 : 65162 : gimple *new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
4453 : : new_rhs);
4454 : 65162 : location_t loc = gimple_location (use_stmt);
4455 : 65162 : gimple_set_location (new_stmt, loc);
4456 : 65162 : gimple_set_vuse (new_stmt, vuse);
4457 : 65162 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4458 : : }
4459 : 65246 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
4460 : 65246 : unlink_stmt_vdef (use_stmt);
4461 : 65246 : gsi_remove (&gsi2, true);
4462 : : }
4463 : :
4464 : : /* Finally get rid of the intermediate stmts. */
4465 : 18196 : gimple *use_stmt;
4466 : 36505 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
4467 : : {
4468 : 113 : if (is_gimple_debug (use_stmt))
4469 : : {
4470 : 85 : if (gimple_debug_bind_p (use_stmt))
4471 : : {
4472 : 85 : gimple_debug_bind_reset_value (use_stmt);
4473 : 85 : update_stmt (use_stmt);
4474 : : }
4475 : 85 : continue;
4476 : : }
4477 : 28 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
4478 : 28 : unlink_stmt_vdef (use_stmt);
4479 : 28 : release_defs (use_stmt);
4480 : 28 : gsi_remove (&gsi2, true);
4481 : 18196 : }
4482 : : /* And the original load. */
4483 : 18196 : release_defs (stmt);
4484 : 18196 : gsi_remove (gsi, true);
4485 : 279667 : }
4486 : :
4487 : :
4488 : : /* Primitive "lattice" function for gimple_simplify. */
4489 : :
4490 : : static tree
4491 : 1572852981 : fwprop_ssa_val (tree name)
4492 : : {
4493 : : /* First valueize NAME. */
4494 : 1572852981 : if (TREE_CODE (name) == SSA_NAME
4495 : 1572852981 : && SSA_NAME_VERSION (name) < lattice.length ())
4496 : : {
4497 : 1572036761 : tree val = lattice[SSA_NAME_VERSION (name)];
4498 : 1572036761 : if (val)
4499 : 1572852981 : name = val;
4500 : : }
4501 : : /* We continue matching along SSA use-def edges for SSA names
4502 : : that are not single-use. Currently there are no patterns
4503 : : that would cause any issues with that. */
4504 : 1572852981 : return name;
4505 : : }
4506 : :
4507 : : /* Search for opportunities to free half of the lanes in the following pattern:
4508 : :
4509 : : v_in = {e0, e1, e2, e3}
4510 : : v_1 = VEC_PERM <v_in, v_in, {0, 2, 0, 2}>
4511 : : // v_1 = {e0, e2, e0, e2}
4512 : : v_2 = VEC_PERM <v_in, v_in, {1, 3, 1, 3}>
4513 : : // v_2 = {e1, e3, e1, e3}
4514 : :
4515 : : v_x = v_1 + v_2
4516 : : // v_x = {e0+e1, e2+e3, e0+e1, e2+e3}
4517 : : v_y = v_1 - v_2
4518 : : // v_y = {e0-e1, e2-e3, e0-e1, e2-e3}
4519 : :
4520 : : v_out = VEC_PERM <v_x, v_y, {0, 1, 6, 7}>
4521 : : // v_out = {e0+e1, e2+e3, e0-e1, e2-e3}
4522 : :
4523 : : The last statement could be simplified to:
4524 : : v_out' = VEC_PERM <v_x, v_y, {0, 1, 4, 5}>
4525 : : // v_out' = {e0+e1, e2+e3, e0-e1, e2-e3}
4526 : :
4527 : : Characteristic properties:
4528 : : - v_1 and v_2 are created from the same input vector v_in and introduce the
4529 : : lane duplication (in the selection operand) that we can eliminate.
4530 : : - v_x and v_y are results from lane-preserving operations that use v_1 and
4531 : : v_2 as inputs.
4532 : : - v_out is created by selecting from duplicated lanes. */
4533 : :
4534 : : static bool
4535 : 176591 : recognise_vec_perm_simplify_seq (gassign *stmt, vec_perm_simplify_seq *seq)
4536 : : {
4537 : 176591 : unsigned HOST_WIDE_INT nelts;
4538 : :
4539 : 176591 : gcc_checking_assert (stmt);
4540 : 176591 : gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
4541 : 176591 : basic_block bb = gimple_bb (stmt);
4542 : :
4543 : : /* Decompose the final vec permute statement. */
4544 : 176591 : tree v_x = gimple_assign_rhs1 (stmt);
4545 : 176591 : tree v_y = gimple_assign_rhs2 (stmt);
4546 : 176591 : tree sel = gimple_assign_rhs3 (stmt);
4547 : :
4548 : 176591 : if (TREE_CODE (sel) != VECTOR_CST
4549 : 173873 : || !VECTOR_CST_NELTS (sel).is_constant (&nelts)
4550 : 173873 : || TREE_CODE (v_x) != SSA_NAME
4551 : 172038 : || TREE_CODE (v_y) != SSA_NAME
4552 : 169553 : || !has_single_use (v_x)
4553 : 283987 : || !has_single_use (v_y))
4554 : 70831 : return false;
4555 : :
4556 : : /* Don't analyse sequences with many lanes. */
4557 : 105760 : if (nelts > 4)
4558 : : return false;
4559 : :
4560 : : /* Lookup the definition of v_x and v_y. */
4561 : 103782 : gassign *v_x_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x));
4562 : 103782 : gassign *v_y_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_y));
4563 : 103405 : if (!v_x_stmt || gimple_bb (v_x_stmt) != bb
4564 : 207187 : || !v_y_stmt || gimple_bb (v_y_stmt) != bb)
4565 : : return false;
4566 : :
4567 : : /* Check the operations that define v_x and v_y. */
4568 : 103398 : if (TREE_CODE_CLASS (gimple_assign_rhs_code (v_x_stmt)) != tcc_binary
4569 : 105308 : || TREE_CODE_CLASS (gimple_assign_rhs_code (v_y_stmt)) != tcc_binary)
4570 : : return false;
4571 : :
4572 : 1910 : tree v_x_1 = gimple_assign_rhs1 (v_x_stmt);
4573 : 1910 : tree v_x_2 = gimple_assign_rhs2 (v_x_stmt);
4574 : 1910 : tree v_y_1 = gimple_assign_rhs1 (v_y_stmt);
4575 : 1910 : tree v_y_2 = gimple_assign_rhs2 (v_y_stmt);
4576 : :
4577 : 1910 : if (v_x_stmt == v_y_stmt
4578 : 1910 : || TREE_CODE (v_x_1) != SSA_NAME
4579 : 1907 : || TREE_CODE (v_x_2) != SSA_NAME
4580 : 1905 : || num_imm_uses (v_x_1) != 2
4581 : 3673 : || num_imm_uses (v_x_2) != 2)
4582 : : return false;
4583 : :
4584 : 1727 : if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
4585 : : {
4586 : : /* Allow operands of commutative operators to swap. */
4587 : 583 : if (commutative_tree_code (gimple_assign_rhs_code (v_x_stmt)))
4588 : : {
4589 : : /* Keep v_x_1 the first operand for non-commutative operators. */
4590 : 239 : v_x_1 = gimple_assign_rhs2 (v_x_stmt);
4591 : 239 : v_x_2 = gimple_assign_rhs1 (v_x_stmt);
4592 : 239 : if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
4593 : : return false;
4594 : : }
4595 : 344 : else if (commutative_tree_code (gimple_assign_rhs_code (v_y_stmt)))
4596 : : {
4597 : 344 : if (v_x_1 != v_y_2 || v_x_2 != v_y_1)
4598 : : return false;
4599 : : }
4600 : : else
4601 : : return false;
4602 : : }
4603 : 1727 : gassign *v_1_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_1));
4604 : 1727 : gassign *v_2_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_2));
4605 : 1663 : if (!v_1_stmt || gimple_bb (v_1_stmt) != bb
4606 : 3390 : || !v_2_stmt || gimple_bb (v_2_stmt) != bb)
4607 : : return false;
4608 : :
4609 : 1659 : if (gimple_assign_rhs_code (v_1_stmt) != VEC_PERM_EXPR
4610 : 1769 : || gimple_assign_rhs_code (v_2_stmt) != VEC_PERM_EXPR)
4611 : : return false;
4612 : :
4613 : : /* Decompose initial VEC_PERM_EXPRs. */
4614 : 106 : tree v_in = gimple_assign_rhs1 (v_1_stmt);
4615 : 106 : tree v_1_sel = gimple_assign_rhs3 (v_1_stmt);
4616 : 106 : tree v_2_sel = gimple_assign_rhs3 (v_2_stmt);
4617 : 106 : if (v_in != gimple_assign_rhs2 (v_1_stmt)
4618 : 101 : || v_in != gimple_assign_rhs1 (v_2_stmt)
4619 : 205 : || v_in != gimple_assign_rhs2 (v_2_stmt))
4620 : : return false;
4621 : :
4622 : 99 : unsigned HOST_WIDE_INT v_1_nelts, v_2_nelts;
4623 : 99 : if (TREE_CODE (v_1_sel) != VECTOR_CST
4624 : 99 : || !VECTOR_CST_NELTS (v_1_sel).is_constant (&v_1_nelts)
4625 : 99 : || TREE_CODE (v_2_sel) != VECTOR_CST
4626 : 198 : || !VECTOR_CST_NELTS (v_2_sel).is_constant (&v_2_nelts))
4627 : 0 : return false;
4628 : :
4629 : 99 : if (nelts != v_1_nelts || nelts != v_2_nelts)
4630 : : return false;
4631 : :
4632 : : /* Create the new selector. */
4633 : 99 : vec_perm_builder new_sel_perm (nelts, nelts, 1);
4634 : 99 : auto_vec<unsigned int> lanes (nelts);
4635 : 99 : lanes.quick_grow_cleared (nelts);
4636 : 495 : for (unsigned int i = 0; i < nelts; i++)
4637 : : {
4638 : : /* Extract the i-th value from the selector. */
4639 : 396 : unsigned int sel_cst = TREE_INT_CST_LOW (VECTOR_CST_ELT (sel, i));
4640 : 396 : unsigned int lane = sel_cst % nelts;
4641 : 396 : unsigned int offs = sel_cst / nelts;
4642 : :
4643 : : /* Check what's in the lane. */
4644 : 396 : unsigned int e_1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, lane));
4645 : 396 : unsigned int e_2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, lane));
4646 : :
4647 : : /* Reuse previous lane (if any). */
4648 : 396 : unsigned int l = 0;
4649 : 675 : for (; l < lane; l++)
4650 : : {
4651 : 477 : if ((TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, l)) == e_1)
4652 : 477 : && (TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, l)) == e_2))
4653 : : break;
4654 : : }
4655 : :
4656 : : /* Add to narrowed selector. */
4657 : 396 : new_sel_perm.quick_push (l + offs * nelts);
4658 : :
4659 : : /* Mark lane as used. */
4660 : 396 : lanes[l] = 1;
4661 : : }
4662 : :
4663 : : /* Count how many lanes are need. */
4664 : : unsigned int cnt = 0;
4665 : 495 : for (unsigned int i = 0; i < nelts; i++)
4666 : 396 : cnt += lanes[i];
4667 : :
4668 : : /* If more than (nelts/2) lanes are needed, skip the sequence. */
4669 : 99 : if (cnt > nelts / 2)
4670 : : return false;
4671 : :
4672 : : /* Check if the resulting permuation is cheap. */
4673 : 99 : vec_perm_indices new_indices (new_sel_perm, 2, nelts);
4674 : 99 : tree vectype = TREE_TYPE (gimple_assign_lhs (stmt));
4675 : 99 : machine_mode vmode = TYPE_MODE (vectype);
4676 : 99 : if (!can_vec_perm_const_p (vmode, vmode, new_indices, false))
4677 : : return false;
4678 : :
4679 : 99 : *seq = XNEW (struct _vec_perm_simplify_seq);
4680 : 99 : (*seq)->stmt = stmt;
4681 : 99 : (*seq)->v_1_stmt = v_1_stmt;
4682 : 99 : (*seq)->v_2_stmt = v_2_stmt;
4683 : 99 : (*seq)->v_x_stmt = v_x_stmt;
4684 : 99 : (*seq)->v_y_stmt = v_y_stmt;
4685 : 99 : (*seq)->nelts = nelts;
4686 : 99 : (*seq)->new_sel = vect_gen_perm_mask_checked (vectype, new_indices);
4687 : :
4688 : 99 : if (dump_file)
4689 : : {
4690 : 26 : fprintf (dump_file, "Found vec perm simplify sequence ending with:\n\t");
4691 : 26 : print_gimple_stmt (dump_file, stmt, 0);
4692 : :
4693 : 26 : if (dump_flags & TDF_DETAILS)
4694 : : {
4695 : 26 : fprintf (dump_file, "\tNarrowed vec_perm selector: ");
4696 : 26 : print_generic_expr (dump_file, (*seq)->new_sel);
4697 : 26 : fprintf (dump_file, "\n");
4698 : : }
4699 : : }
4700 : :
4701 : : return true;
4702 : 198 : }
4703 : :
4704 : : /* Reduce the lane consumption of a simplifiable vec perm sequence. */
4705 : :
4706 : : static void
4707 : 72 : narrow_vec_perm_simplify_seq (const vec_perm_simplify_seq &seq)
4708 : : {
4709 : 72 : gassign *stmt = seq->stmt;
4710 : 72 : if (dump_file && (dump_flags & TDF_DETAILS))
4711 : : {
4712 : 20 : fprintf (dump_file, "Updating VEC_PERM statment:\n");
4713 : 20 : fprintf (dump_file, "Old stmt: ");
4714 : 20 : print_gimple_stmt (dump_file, stmt, 0);
4715 : : }
4716 : :
4717 : : /* Update the last VEC_PERM statement. */
4718 : 72 : gimple_assign_set_rhs3 (stmt, seq->new_sel);
4719 : 72 : update_stmt (stmt);
4720 : :
4721 : 72 : if (dump_file && (dump_flags & TDF_DETAILS))
4722 : : {
4723 : 20 : fprintf (dump_file, "New stmt: ");
4724 : 20 : print_gimple_stmt (dump_file, stmt, 0);
4725 : : }
4726 : 72 : }
4727 : :
4728 : : /* Test if we can blend two simplifiable vec permute sequences.
4729 : : NEED_SWAP will be set, if sequences must be swapped for blending. */
4730 : :
4731 : : static bool
4732 : 46 : can_blend_vec_perm_simplify_seqs_p (vec_perm_simplify_seq seq1,
4733 : : vec_perm_simplify_seq seq2,
4734 : : bool *need_swap)
4735 : : {
4736 : 46 : unsigned int nelts = seq1->nelts;
4737 : 46 : basic_block bb = gimple_bb (seq1->stmt);
4738 : :
4739 : 46 : gcc_assert (gimple_bb (seq2->stmt) == bb);
4740 : :
4741 : : /* BBs and number of elements must be equal. */
4742 : 46 : if (gimple_bb (seq2->stmt) != bb || seq2->nelts != nelts)
4743 : : return false;
4744 : :
4745 : : /* We need vectors of the same type. */
4746 : 46 : if (TREE_TYPE (gimple_assign_lhs (seq1->stmt))
4747 : 46 : != TREE_TYPE (gimple_assign_lhs (seq2->stmt)))
4748 : : return false;
4749 : :
4750 : : /* We require isomorphic operators. */
4751 : 40 : if (((gimple_assign_rhs_code (seq1->v_x_stmt)
4752 : 40 : != gimple_assign_rhs_code (seq2->v_x_stmt))
4753 : 40 : || (gimple_assign_rhs_code (seq1->v_y_stmt)
4754 : 40 : != gimple_assign_rhs_code (seq2->v_y_stmt))))
4755 : : return false;
4756 : :
4757 : : /* We cannot have any dependencies between the sequences.
4758 : :
4759 : : For merging, we will reuse seq1->v_1_stmt and seq1->v_2_stmt.
4760 : : seq1's v_in is defined before these statements, but we need
4761 : : to check if seq2's v_in is defined before them as well.
4762 : :
4763 : : Further, we will reuse seq2->stmt. We need to ensure that
4764 : : seq1->v_x_stmt and seq1->v_y_stmt are before it.
4765 : :
4766 : : Note, that we don't need to check the BBs here, because all
4767 : : statements of both sequences have to be in the same BB.
4768 : : */
4769 : :
4770 : 40 : tree seq2_v_in = gimple_assign_rhs1 (seq2->v_1_stmt);
4771 : 40 : if (TREE_CODE (seq2_v_in) != SSA_NAME)
4772 : : return false;
4773 : :
4774 : 40 : gassign *seq2_v_in_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq2_v_in));
4775 : 40 : if (!seq2_v_in_stmt || gimple_bb (seq2_v_in_stmt) != bb
4776 : 40 : || (gimple_uid (seq2_v_in_stmt) > gimple_uid (seq1->v_1_stmt))
4777 : 36 : || (gimple_uid (seq1->v_x_stmt) > gimple_uid (seq2->stmt))
4778 : 36 : || (gimple_uid (seq1->v_y_stmt) > gimple_uid (seq2->stmt)))
4779 : : {
4780 : 4 : tree seq1_v_in = gimple_assign_rhs1 (seq1->v_1_stmt);
4781 : 4 : if (TREE_CODE (seq1_v_in) != SSA_NAME)
4782 : : return false;
4783 : :
4784 : 4 : gassign *seq1_v_in_stmt
4785 : 4 : = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq1_v_in));
4786 : : /* Let's try to see if we succeed when swapping the sequences. */
4787 : 4 : if (!seq1_v_in_stmt || gimple_bb (seq1_v_in_stmt)
4788 : 0 : || (gimple_uid (seq1_v_in_stmt) > gimple_uid (seq2->v_1_stmt))
4789 : 0 : || (gimple_uid (seq2->v_x_stmt) > gimple_uid (seq1->stmt))
4790 : 0 : || (gimple_uid (seq2->v_y_stmt) > gimple_uid (seq1->stmt)))
4791 : : return false;
4792 : 0 : *need_swap = true;
4793 : : }
4794 : : else
4795 : 36 : *need_swap = false;
4796 : :
4797 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4798 : 10 : fprintf (dump_file, "Found vec perm simplify sequence pair.\n");
4799 : :
4800 : : return true;
4801 : : }
4802 : :
4803 : : /* Calculate the permutations for blending the two given vec permute
4804 : : sequences. This may fail if the resulting permutation is not
4805 : : supported. */
4806 : :
4807 : : static bool
4808 : 36 : calc_perm_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
4809 : : vec_perm_simplify_seq seq2,
4810 : : vec_perm_indices *seq2_stmt_indices,
4811 : : vec_perm_indices *seq1_v_1_stmt_indices,
4812 : : vec_perm_indices *seq1_v_2_stmt_indices)
4813 : : {
4814 : 36 : unsigned int i;
4815 : 36 : unsigned int nelts = seq1->nelts;
4816 : 36 : auto_vec<int> lane_assignment;
4817 : 36 : lane_assignment.create (nelts);
4818 : :
4819 : : /* Mark all lanes as free. */
4820 : 36 : lane_assignment.quick_grow_cleared (nelts);
4821 : :
4822 : : /* Allocate lanes for seq1. */
4823 : 180 : for (i = 0; i < nelts; i++)
4824 : : {
4825 : 144 : unsigned int l = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq1->new_sel, i));
4826 : 144 : l %= nelts;
4827 : 144 : lane_assignment[l] = 1;
4828 : : }
4829 : :
4830 : : /* Allocate lanes for seq2 and calculate selector for seq2->stmt. */
4831 : 36 : vec_perm_builder seq2_stmt_sel_perm (nelts, nelts, 1);
4832 : 180 : for (i = 0; i < nelts; i++)
4833 : : {
4834 : 144 : unsigned int sel = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, i));
4835 : 144 : unsigned int lane = sel % nelts;
4836 : 144 : unsigned int offs = sel / nelts;
4837 : 144 : unsigned int new_sel;
4838 : :
4839 : : /* Check if we already allocated the lane for seq2. */
4840 : 144 : unsigned int j = 0;
4841 : 255 : for (; j < i; j++)
4842 : : {
4843 : 183 : unsigned int sel_old;
4844 : 183 : sel_old = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, j));
4845 : 183 : unsigned int lane_old = sel_old % nelts;
4846 : 183 : if (lane == lane_old)
4847 : : {
4848 : 72 : new_sel = seq2_stmt_sel_perm[j].to_constant ();
4849 : 72 : new_sel = (new_sel % nelts) + offs * nelts;
4850 : 72 : break;
4851 : : }
4852 : : }
4853 : :
4854 : : /* If the lane is not allocated, we need to do that now. */
4855 : 144 : if (j == i)
4856 : : {
4857 : : unsigned int l_orig = lane;
4858 : 176 : while (lane_assignment[lane] != 0)
4859 : : {
4860 : 104 : lane = (lane + 1) % nelts;
4861 : :
4862 : : /* This should not happen if both sequences utilize no more than
4863 : : half of the lanes. Test anyway to guarantee termination. */
4864 : 104 : if (lane == l_orig)
4865 : 0 : return false;
4866 : : }
4867 : :
4868 : : /* Allocate lane. */
4869 : 72 : lane_assignment[lane] = 2;
4870 : 72 : new_sel = lane + offs * nelts;
4871 : : }
4872 : :
4873 : 144 : seq2_stmt_sel_perm.quick_push (new_sel);
4874 : : }
4875 : :
4876 : : /* Check if the resulting permuation is cheap. */
4877 : 36 : seq2_stmt_indices->new_vector (seq2_stmt_sel_perm, 2, nelts);
4878 : 36 : tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
4879 : 36 : machine_mode vmode = TYPE_MODE (vectype);
4880 : 36 : if (!can_vec_perm_const_p (vmode, vmode, *seq2_stmt_indices, false))
4881 : : return false;
4882 : :
4883 : : /* Calculate selectors for seq1->v_1_stmt and seq1->v_2_stmt. */
4884 : 36 : vec_perm_builder seq1_v_1_stmt_sel_perm (nelts, nelts, 1);
4885 : 36 : vec_perm_builder seq1_v_2_stmt_sel_perm (nelts, nelts, 1);
4886 : 180 : for (i = 0; i < nelts; i++)
4887 : : {
4888 : 144 : bool use_seq1 = lane_assignment[i] != 2;
4889 : 144 : unsigned int l1, l2;
4890 : :
4891 : 144 : if (use_seq1)
4892 : : {
4893 : : /* Just reuse the selector indices. */
4894 : 72 : tree s1 = gimple_assign_rhs3 (seq1->v_1_stmt);
4895 : 72 : tree s2 = gimple_assign_rhs3 (seq1->v_2_stmt);
4896 : 72 : l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, i));
4897 : 72 : l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, i));
4898 : : }
4899 : : else
4900 : : {
4901 : : /* We moved the lanes for seq2, so we need to adjust for that. */
4902 : 72 : tree s1 = gimple_assign_rhs3 (seq2->v_1_stmt);
4903 : 72 : tree s2 = gimple_assign_rhs3 (seq2->v_2_stmt);
4904 : :
4905 : 72 : unsigned int j = 0;
4906 : 128 : for (; j < i; j++)
4907 : : {
4908 : 128 : unsigned int sel_new;
4909 : 128 : sel_new = seq2_stmt_sel_perm[j].to_constant ();
4910 : 128 : sel_new %= nelts;
4911 : 128 : if (sel_new == i)
4912 : : break;
4913 : : }
4914 : :
4915 : : /* This should not happen. Test anyway to guarantee correctness. */
4916 : 72 : if (j == i)
4917 : : return false;
4918 : :
4919 : 72 : l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, j));
4920 : 72 : l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, j));
4921 : : }
4922 : :
4923 : 216 : seq1_v_1_stmt_sel_perm.quick_push (l1 + (use_seq1 ? 0 : nelts));
4924 : 216 : seq1_v_2_stmt_sel_perm.quick_push (l2 + (use_seq1 ? 0 : nelts));
4925 : : }
4926 : :
4927 : 36 : seq1_v_1_stmt_indices->new_vector (seq1_v_1_stmt_sel_perm, 2, nelts);
4928 : 36 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
4929 : 36 : vmode = TYPE_MODE (vectype);
4930 : 36 : if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_1_stmt_indices, false))
4931 : : return false;
4932 : :
4933 : 36 : seq1_v_2_stmt_indices->new_vector (seq1_v_2_stmt_sel_perm, 2, nelts);
4934 : 36 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
4935 : 36 : vmode = TYPE_MODE (vectype);
4936 : 36 : if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_2_stmt_indices, false))
4937 : : return false;
4938 : :
4939 : : return true;
4940 : 72 : }
4941 : :
4942 : : /* Blend the two given simplifiable vec permute sequences using the
4943 : : given permutations. */
4944 : :
4945 : : static void
4946 : 36 : blend_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
4947 : : vec_perm_simplify_seq seq2,
4948 : : const vec_perm_indices &seq2_stmt_indices,
4949 : : const vec_perm_indices &seq1_v_1_stmt_indices,
4950 : : const vec_perm_indices &seq1_v_2_stmt_indices)
4951 : : {
4952 : : /* We don't need to adjust seq1->stmt because its lanes consumption
4953 : : was already narrowed before entering this function. */
4954 : :
4955 : : /* Adjust seq2->stmt: copy RHS1/RHS2 from seq1->stmt and set new sel. */
4956 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4957 : : {
4958 : 10 : fprintf (dump_file, "Updating VEC_PERM statment:\n");
4959 : 10 : fprintf (dump_file, "Old stmt: ");
4960 : 10 : print_gimple_stmt (dump_file, seq2->stmt, 0);
4961 : : }
4962 : :
4963 : 36 : gimple_assign_set_rhs1 (seq2->stmt, gimple_assign_rhs1 (seq1->stmt));
4964 : 72 : gimple_assign_set_rhs2 (seq2->stmt, gimple_assign_rhs2 (seq1->stmt));
4965 : 36 : tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
4966 : 36 : tree sel = vect_gen_perm_mask_checked (vectype, seq2_stmt_indices);
4967 : 36 : gimple_assign_set_rhs3 (seq2->stmt, sel);
4968 : 36 : update_stmt (seq2->stmt);
4969 : :
4970 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4971 : : {
4972 : 10 : fprintf (dump_file, "New stmt: ");
4973 : 10 : print_gimple_stmt (dump_file, seq2->stmt, 0);
4974 : : }
4975 : :
4976 : : /* Adjust seq1->v_1_stmt: copy RHS2 from seq2->v_1_stmt and set new sel. */
4977 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4978 : : {
4979 : 10 : fprintf (dump_file, "Updating VEC_PERM statment:\n");
4980 : 10 : fprintf (dump_file, "Old stmt: ");
4981 : 10 : print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
4982 : : }
4983 : :
4984 : 36 : gimple_assign_set_rhs2 (seq1->v_1_stmt, gimple_assign_rhs1 (seq2->v_1_stmt));
4985 : 36 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
4986 : 36 : sel = vect_gen_perm_mask_checked (vectype, seq1_v_1_stmt_indices);
4987 : 36 : gimple_assign_set_rhs3 (seq1->v_1_stmt, sel);
4988 : 36 : update_stmt (seq1->v_1_stmt);
4989 : :
4990 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4991 : : {
4992 : 10 : fprintf (dump_file, "New stmt: ");
4993 : 10 : print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
4994 : : }
4995 : :
4996 : : /* Adjust seq1->v_2_stmt: copy RHS2 from seq2->v_2_stmt and set new sel. */
4997 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
4998 : : {
4999 : 10 : fprintf (dump_file, "Updating VEC_PERM statment:\n");
5000 : 10 : fprintf (dump_file, "Old stmt: ");
5001 : 10 : print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
5002 : : }
5003 : :
5004 : 36 : gimple_assign_set_rhs2 (seq1->v_2_stmt, gimple_assign_rhs1 (seq2->v_2_stmt));
5005 : 36 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
5006 : 36 : sel = vect_gen_perm_mask_checked (vectype, seq1_v_2_stmt_indices);
5007 : 36 : gimple_assign_set_rhs3 (seq1->v_2_stmt, sel);
5008 : 36 : update_stmt (seq1->v_2_stmt);
5009 : :
5010 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
5011 : : {
5012 : 10 : fprintf (dump_file, "New stmt: ");
5013 : 10 : print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
5014 : : }
5015 : :
5016 : : /* At this point, we have four unmodified seq2 stmts, which will be
5017 : : eliminated by DCE. */
5018 : :
5019 : 36 : if (dump_file)
5020 : 10 : fprintf (dump_file, "Vec perm simplify sequences have been blended.\n\n");
5021 : 36 : }
5022 : :
5023 : : /* Try to blend narrowed vec_perm_simplify_seqs pairwise.
5024 : : The provided list will be empty after this call. */
5025 : :
5026 : : static void
5027 : 316067806 : process_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l)
5028 : : {
5029 : 316067806 : unsigned int i, j;
5030 : 316067806 : vec_perm_simplify_seq seq1, seq2;
5031 : :
5032 : 316067806 : if (l->is_empty ())
5033 : 316067762 : return;
5034 : :
5035 : 44 : if (dump_file && (dump_flags & TDF_DETAILS))
5036 : 12 : fprintf (dump_file, "\nProcessing %u vec perm simplify sequences.\n",
5037 : : l->length ());
5038 : :
5039 : 107 : FOR_EACH_VEC_ELT (*l, i, seq1)
5040 : : {
5041 : 63 : if (i + 1 < l->length ())
5042 : : {
5043 : 50 : FOR_EACH_VEC_ELT_FROM (*l, j, seq2, i + 1)
5044 : : {
5045 : 46 : bool swap = false;
5046 : 46 : if (can_blend_vec_perm_simplify_seqs_p (seq1, seq2, &swap))
5047 : : {
5048 : 36 : vec_perm_indices seq2_stmt_indices;
5049 : 36 : vec_perm_indices seq1_v_1_stmt_indices;
5050 : 36 : vec_perm_indices seq1_v_2_stmt_indices;
5051 : 108 : if (calc_perm_vec_perm_simplify_seqs (swap ? seq2 : seq1,
5052 : : swap ? seq1 : seq2,
5053 : : &seq2_stmt_indices,
5054 : : &seq1_v_1_stmt_indices,
5055 : : &seq1_v_2_stmt_indices))
5056 : : {
5057 : : /* Narrow lane usage. */
5058 : 36 : narrow_vec_perm_simplify_seq (seq1);
5059 : 36 : narrow_vec_perm_simplify_seq (seq2);
5060 : :
5061 : : /* Blend sequences. */
5062 : 36 : blend_vec_perm_simplify_seqs (swap ? seq2 : seq1,
5063 : : swap ? seq1 : seq2,
5064 : : seq2_stmt_indices,
5065 : : seq1_v_1_stmt_indices,
5066 : : seq1_v_2_stmt_indices);
5067 : :
5068 : : /* We can use unordered_remove as we break the loop. */
5069 : 36 : l->unordered_remove (j);
5070 : 36 : XDELETE (seq2);
5071 : 36 : break;
5072 : : }
5073 : 36 : }
5074 : : }
5075 : : }
5076 : :
5077 : : /* We don't need to call l->remove for seq1. */
5078 : 63 : XDELETE (seq1);
5079 : : }
5080 : :
5081 : 44 : l->truncate (0);
5082 : : }
5083 : :
5084 : : static void
5085 : 99 : append_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l,
5086 : : const vec_perm_simplify_seq &seq)
5087 : : {
5088 : : /* If no space on list left, then process the list. */
5089 : 99 : if (!l->space (1))
5090 : 0 : process_vec_perm_simplify_seq_list (l);
5091 : :
5092 : 99 : l->quick_push (seq);
5093 : 99 : }
5094 : :
5095 : : /* Main entry point for the forward propagation and statement combine
5096 : : optimizer. */
5097 : :
5098 : : namespace {
5099 : :
5100 : : const pass_data pass_data_forwprop =
5101 : : {
5102 : : GIMPLE_PASS, /* type */
5103 : : "forwprop", /* name */
5104 : : OPTGROUP_NONE, /* optinfo_flags */
5105 : : TV_TREE_FORWPROP, /* tv_id */
5106 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
5107 : : 0, /* properties_provided */
5108 : : 0, /* properties_destroyed */
5109 : : 0, /* todo_flags_start */
5110 : : 0, /* todo_flags_finish */
5111 : : };
5112 : :
5113 : : class pass_forwprop : public gimple_opt_pass
5114 : : {
5115 : : public:
5116 : 1450165 : pass_forwprop (gcc::context *ctxt)
5117 : 2900330 : : gimple_opt_pass (pass_data_forwprop, ctxt), last_p (false)
5118 : : {}
5119 : :
5120 : : /* opt_pass methods: */
5121 : 1160132 : opt_pass * clone () final override { return new pass_forwprop (m_ctxt); }
5122 : 1740198 : void set_pass_param (unsigned int n, bool param) final override
5123 : : {
5124 : 1740198 : switch (n)
5125 : : {
5126 : 1160132 : case 0:
5127 : 1160132 : m_full_walk = param;
5128 : 1160132 : break;
5129 : 580066 : case 1:
5130 : 580066 : last_p = param;
5131 : 580066 : break;
5132 : 0 : default:
5133 : 0 : gcc_unreachable();
5134 : : }
5135 : 1740198 : }
5136 : 5632793 : bool gate (function *) final override { return flag_tree_forwprop; }
5137 : : unsigned int execute (function *) final override;
5138 : :
5139 : : private:
5140 : : /* Determines whether the pass instance should set PROP_last_full_fold. */
5141 : : bool last_p;
5142 : :
5143 : : /* True if the aggregate props are doing a full walk or not. */
5144 : : bool m_full_walk = false;
5145 : : }; // class pass_forwprop
5146 : :
5147 : : /* Attemp to make the BB block of __builtin_unreachable unreachable by changing
5148 : : the incoming jumps. Return true if at least one jump was changed. */
5149 : :
5150 : : static bool
5151 : 3984 : optimize_unreachable (basic_block bb)
5152 : : {
5153 : 3984 : gimple_stmt_iterator gsi;
5154 : 3984 : gimple *stmt;
5155 : 3984 : edge_iterator ei;
5156 : 3984 : edge e;
5157 : 3984 : bool ret;
5158 : :
5159 : 3984 : ret = false;
5160 : 9837 : FOR_EACH_EDGE (e, ei, bb->preds)
5161 : : {
5162 : 5853 : gsi = gsi_last_bb (e->src);
5163 : 5853 : if (gsi_end_p (gsi))
5164 : 327 : continue;
5165 : :
5166 : 5526 : stmt = gsi_stmt (gsi);
5167 : 5526 : if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
5168 : : {
5169 : : /* If the condition is already true/false
5170 : : ignore it. This can happen during copy prop of forwprop. */
5171 : 583 : if (gimple_cond_true_p (cond_stmt)
5172 : 575 : || gimple_cond_false_p (cond_stmt))
5173 : 8 : continue;
5174 : 567 : else if (e->flags & EDGE_TRUE_VALUE)
5175 : 493 : gimple_cond_make_false (cond_stmt);
5176 : 74 : else if (e->flags & EDGE_FALSE_VALUE)
5177 : 74 : gimple_cond_make_true (cond_stmt);
5178 : : else
5179 : 0 : gcc_unreachable ();
5180 : 567 : update_stmt (cond_stmt);
5181 : : }
5182 : : else
5183 : : {
5184 : : /* Todo: handle other cases. Note that unreachable switch case
5185 : : statements have already been removed. */
5186 : 4951 : continue;
5187 : : }
5188 : :
5189 : 567 : ret = true;
5190 : : }
5191 : :
5192 : 3984 : return ret;
5193 : : }
5194 : :
5195 : : unsigned int
5196 : 5630245 : pass_forwprop::execute (function *fun)
5197 : : {
5198 : 5630245 : unsigned int todoflags = 0;
5199 : : /* Handle a full walk only when expensive optimizations are on. */
5200 : 5630245 : bool full_walk = m_full_walk && flag_expensive_optimizations;
5201 : :
5202 : 5630245 : cfg_changed = false;
5203 : 5630245 : if (last_p)
5204 : 1046011 : fun->curr_properties |= PROP_last_full_fold;
5205 : :
5206 : 5630245 : calculate_dominance_info (CDI_DOMINATORS);
5207 : :
5208 : : /* Combine stmts with the stmts defining their operands. Do that
5209 : : in an order that guarantees visiting SSA defs before SSA uses. */
5210 : 11260490 : lattice.create (num_ssa_names);
5211 : 11260490 : lattice.quick_grow_cleared (num_ssa_names);
5212 : 5630245 : int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (fun));
5213 : 5630245 : int postorder_num = pre_and_rev_post_order_compute_fn (fun, NULL,
5214 : : postorder, false);
5215 : 5630245 : int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
5216 : 51031865 : for (int i = 0; i < postorder_num; ++i)
5217 : : {
5218 : 45401620 : bb_to_rpo[postorder[i]] = i;
5219 : 45401620 : edge_iterator ei;
5220 : 45401620 : edge e;
5221 : 109521717 : FOR_EACH_EDGE (e, ei, BASIC_BLOCK_FOR_FN (fun, postorder[i])->succs)
5222 : 64120097 : e->flags &= ~EDGE_EXECUTABLE;
5223 : : }
5224 : 5630245 : single_succ_edge (BASIC_BLOCK_FOR_FN (fun, ENTRY_BLOCK))->flags
5225 : 5630245 : |= EDGE_EXECUTABLE;
5226 : 5630245 : auto_vec<gimple *, 4> to_fixup;
5227 : 5630245 : auto_vec<gimple *, 32> to_remove;
5228 : 5630245 : auto_vec<unsigned, 32> to_remove_defs;
5229 : 5630245 : auto_vec<std::pair<int, int>, 10> edges_to_remove;
5230 : 5630245 : auto_bitmap simple_dce_worklist;
5231 : 5630245 : auto_bitmap need_ab_cleanup;
5232 : 5630245 : to_purge = BITMAP_ALLOC (NULL);
5233 : 5630245 : auto_vec<vec_perm_simplify_seq, 8> vec_perm_simplify_seq_list;
5234 : 51031865 : for (int i = 0; i < postorder_num; ++i)
5235 : : {
5236 : 45401620 : gimple_stmt_iterator gsi;
5237 : 45401620 : basic_block bb = BASIC_BLOCK_FOR_FN (fun, postorder[i]);
5238 : 45401620 : edge_iterator ei;
5239 : 45401620 : edge e;
5240 : :
5241 : : /* Skip processing not executable blocks. We could improve
5242 : : single_use tracking by at least unlinking uses from unreachable
5243 : : blocks but since blocks with uses are not processed in a
5244 : : meaningful order this is probably not worth it. */
5245 : 45401620 : bool any = false;
5246 : 46557718 : FOR_EACH_EDGE (e, ei, bb->preds)
5247 : : {
5248 : 46545197 : if ((e->flags & EDGE_EXECUTABLE)
5249 : : /* We can handle backedges in natural loops correctly but
5250 : : for irreducible regions we have to take all backedges
5251 : : conservatively when we did not visit the source yet. */
5252 : 46545197 : || (bb_to_rpo[e->src->index] > i
5253 : 687871 : && !dominated_by_p (CDI_DOMINATORS, e->src, e->dest)))
5254 : : {
5255 : : any = true;
5256 : : break;
5257 : : }
5258 : : }
5259 : 45401620 : if (!any)
5260 : 13062 : continue;
5261 : :
5262 : : /* Remove conditions that go directly to unreachable when this is the last forwprop. */
5263 : 45389099 : if (last_p
5264 : 10094428 : && !(flag_sanitize & SANITIZE_UNREACHABLE))
5265 : : {
5266 : 10089445 : gimple_stmt_iterator gsi;
5267 : 10089445 : gsi = gsi_start_nondebug_after_labels_bb (bb);
5268 : 10089986 : if (!gsi_end_p (gsi)
5269 : 9161900 : && gimple_call_builtin_p (*gsi, BUILT_IN_UNREACHABLE)
5270 : 10093429 : && optimize_unreachable (bb))
5271 : : {
5272 : 541 : cfg_changed = true;
5273 : 541 : continue;
5274 : : }
5275 : : }
5276 : :
5277 : : /* Record degenerate PHIs in the lattice. */
5278 : 61610826 : for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
5279 : 16222268 : gsi_next (&si))
5280 : : {
5281 : 16222268 : gphi *phi = si.phi ();
5282 : 16222268 : tree res = gimple_phi_result (phi);
5283 : 32444536 : if (virtual_operand_p (res))
5284 : 7402724 : continue;
5285 : :
5286 : 8819544 : tree first = NULL_TREE;
5287 : 8819544 : bool all_same = true;
5288 : 8819544 : edge_iterator ei;
5289 : 8819544 : edge e;
5290 : 18210115 : FOR_EACH_EDGE (e, ei, bb->preds)
5291 : : {
5292 : : /* Ignore not executable forward edges. */
5293 : 17959035 : if (!(e->flags & EDGE_EXECUTABLE))
5294 : : {
5295 : 4107154 : if (bb_to_rpo[e->src->index] < i)
5296 : 5096 : continue;
5297 : : /* Avoid equivalences from backedges - while we might
5298 : : be able to make irreducible regions reducible and
5299 : : thus turning a back into a forward edge we do not
5300 : : want to deal with the intermediate SSA issues that
5301 : : exposes. */
5302 : : all_same = false;
5303 : : }
5304 : 17953939 : tree use = PHI_ARG_DEF_FROM_EDGE (phi, e);
5305 : 17953939 : if (use == res)
5306 : : /* The PHI result can also appear on a backedge, if so
5307 : : we can ignore this case for the purpose of determining
5308 : : the singular value. */
5309 : : ;
5310 : 17923265 : else if (! first)
5311 : : first = use;
5312 : 9103721 : else if (! operand_equal_p (first, use, 0))
5313 : : {
5314 : : all_same = false;
5315 : : break;
5316 : : }
5317 : : }
5318 : 8819544 : if (all_same)
5319 : : {
5320 : 244932 : if (may_propagate_copy (res, first))
5321 : 244479 : to_remove_defs.safe_push (SSA_NAME_VERSION (res));
5322 : 244932 : fwprop_set_lattice_val (res, first);
5323 : : }
5324 : : }
5325 : :
5326 : : /* Apply forward propagation to all stmts in the basic-block.
5327 : : Note we update GSI within the loop as necessary. */
5328 : 45388558 : unsigned int uid = 1;
5329 : 428365072 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
5330 : : {
5331 : 337587956 : gimple *stmt = gsi_stmt (gsi);
5332 : 337587956 : tree lhs, rhs;
5333 : 337587956 : enum tree_code code;
5334 : :
5335 : 337587956 : gimple_set_uid (stmt, uid++);
5336 : :
5337 : 337587956 : if (!is_gimple_assign (stmt))
5338 : : {
5339 : 232067174 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
5340 : 232067174 : gsi_next (&gsi);
5341 : 232067174 : continue;
5342 : : }
5343 : :
5344 : 105520782 : lhs = gimple_assign_lhs (stmt);
5345 : 105520782 : rhs = gimple_assign_rhs1 (stmt);
5346 : 105520782 : code = gimple_assign_rhs_code (stmt);
5347 : :
5348 : 144132856 : if (TREE_CODE (lhs) != SSA_NAME
5349 : 105520782 : || has_zero_uses (lhs))
5350 : : {
5351 : 38612074 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
5352 : 38612074 : gsi_next (&gsi);
5353 : 38612074 : continue;
5354 : : }
5355 : :
5356 : : /* If this statement sets an SSA_NAME to an address,
5357 : : try to propagate the address into the uses of the SSA_NAME. */
5358 : 66908708 : if ((code == ADDR_EXPR
5359 : : /* Handle pointer conversions on invariant addresses
5360 : : as well, as this is valid gimple. */
5361 : 64612688 : || (CONVERT_EXPR_CODE_P (code)
5362 : 8945872 : && TREE_CODE (rhs) == ADDR_EXPR
5363 : 359572 : && POINTER_TYPE_P (TREE_TYPE (lhs))))
5364 : 66908932 : && TREE_CODE (TREE_OPERAND (rhs, 0)) != TARGET_MEM_REF)
5365 : : {
5366 : 2295690 : tree base = get_base_address (TREE_OPERAND (rhs, 0));
5367 : 2295690 : if ((!base
5368 : 2295690 : || !DECL_P (base)
5369 : 130463 : || decl_address_invariant_p (base))
5370 : 2295690 : && !stmt_references_abnormal_ssa_name (stmt)
5371 : 4591364 : && forward_propagate_addr_expr (lhs, rhs, true))
5372 : : {
5373 : 484579 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
5374 : 484579 : release_defs (stmt);
5375 : 484579 : gsi_remove (&gsi, true);
5376 : : }
5377 : : else
5378 : 1811111 : gsi_next (&gsi);
5379 : : }
5380 : 64613018 : else if (code == POINTER_PLUS_EXPR)
5381 : : {
5382 : 3659990 : tree off = gimple_assign_rhs2 (stmt);
5383 : 3659990 : if (TREE_CODE (off) == INTEGER_CST
5384 : 1146789 : && can_propagate_from (stmt)
5385 : 1146436 : && !simple_iv_increment_p (stmt)
5386 : : /* ??? Better adjust the interface to that function
5387 : : instead of building new trees here. */
5388 : 4511103 : && forward_propagate_addr_expr
5389 : 2553339 : (lhs,
5390 : : build1_loc (gimple_location (stmt),
5391 : 851113 : ADDR_EXPR, TREE_TYPE (rhs),
5392 : 851113 : fold_build2 (MEM_REF,
5393 : : TREE_TYPE (TREE_TYPE (rhs)),
5394 : : rhs,
5395 : : fold_convert (ptr_type_node,
5396 : : off))), true))
5397 : : {
5398 : 298595 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
5399 : 298595 : release_defs (stmt);
5400 : 298595 : gsi_remove (&gsi, true);
5401 : : }
5402 : 3361395 : else if (is_gimple_min_invariant (rhs))
5403 : : {
5404 : : /* Make sure to fold &a[0] + off_1 here. */
5405 : 437546 : fold_stmt_inplace (&gsi);
5406 : 437546 : update_stmt (stmt);
5407 : 437546 : if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
5408 : 437528 : gsi_next (&gsi);
5409 : : }
5410 : : else
5411 : 2923849 : gsi_next (&gsi);
5412 : : }
5413 : 60953028 : else if (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE
5414 : 211407 : && gimple_assign_load_p (stmt)
5415 : 134312 : && !gimple_has_volatile_ops (stmt)
5416 : 40237 : && TREE_CODE (rhs) != TARGET_MEM_REF
5417 : 40212 : && TREE_CODE (rhs) != BIT_FIELD_REF
5418 : 60993236 : && !stmt_can_throw_internal (fun, stmt))
5419 : : {
5420 : : /* Rewrite loads used only in real/imagpart extractions to
5421 : : component-wise loads. */
5422 : 40083 : use_operand_p use_p;
5423 : 40083 : imm_use_iterator iter;
5424 : 40083 : tree vuse = gimple_vuse (stmt);
5425 : 40083 : bool rewrite = true;
5426 : 84886 : FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
5427 : : {
5428 : 42731 : gimple *use_stmt = USE_STMT (use_p);
5429 : 42731 : if (is_gimple_debug (use_stmt))
5430 : 690 : continue;
5431 : 42041 : if (!is_gimple_assign (use_stmt)
5432 : 27522 : || (gimple_assign_rhs_code (use_stmt) != REALPART_EXPR
5433 : 25492 : && gimple_assign_rhs_code (use_stmt) != IMAGPART_EXPR)
5434 : 46071 : || TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) != lhs)
5435 : : {
5436 : : rewrite = false;
5437 : : break;
5438 : : }
5439 : 40083 : }
5440 : 40083 : if (rewrite)
5441 : : {
5442 : 2072 : gimple *use_stmt;
5443 : 8608 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
5444 : : {
5445 : 4464 : if (is_gimple_debug (use_stmt))
5446 : : {
5447 : 453 : if (gimple_debug_bind_p (use_stmt))
5448 : : {
5449 : 453 : gimple_debug_bind_reset_value (use_stmt);
5450 : 453 : update_stmt (use_stmt);
5451 : : }
5452 : 453 : continue;
5453 : : }
5454 : :
5455 : 8022 : tree new_rhs = build1 (gimple_assign_rhs_code (use_stmt),
5456 : 4011 : TREE_TYPE (TREE_TYPE (rhs)),
5457 : : unshare_expr (rhs));
5458 : 4011 : gimple *new_stmt
5459 : 4011 : = gimple_build_assign (gimple_assign_lhs (use_stmt),
5460 : : new_rhs);
5461 : :
5462 : 4011 : location_t loc = gimple_location (use_stmt);
5463 : 4011 : gimple_set_location (new_stmt, loc);
5464 : 4011 : gimple_set_vuse (new_stmt, vuse);
5465 : 4011 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5466 : 4011 : unlink_stmt_vdef (use_stmt);
5467 : 4011 : gsi_remove (&gsi2, true);
5468 : :
5469 : 4011 : gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
5470 : 2072 : }
5471 : :
5472 : 2072 : release_defs (stmt);
5473 : 2072 : gsi_remove (&gsi, true);
5474 : : }
5475 : : else
5476 : 38011 : gsi_next (&gsi);
5477 : : }
5478 : 60912945 : else if (TREE_CODE (TREE_TYPE (lhs)) == VECTOR_TYPE
5479 : 1607254 : && (TYPE_MODE (TREE_TYPE (lhs)) == BLKmode
5480 : : /* After vector lowering rewrite all loads, but
5481 : : initially do not since this conflicts with
5482 : : vector CONSTRUCTOR to shuffle optimization. */
5483 : 1586432 : || (fun->curr_properties & PROP_gimple_lvec))
5484 : 884468 : && gimple_assign_load_p (stmt)
5485 : 294014 : && !gimple_has_volatile_ops (stmt)
5486 : 280169 : && !stmt_can_throw_internal (fun, stmt)
5487 : 61193114 : && (!VAR_P (rhs) || !DECL_HARD_REGISTER (rhs)))
5488 : 279667 : optimize_vector_load (&gsi);
5489 : :
5490 : 60633278 : else if (code == COMPLEX_EXPR)
5491 : : {
5492 : : /* Rewrite stores of a single-use complex build expression
5493 : : to component-wise stores. */
5494 : 36429 : use_operand_p use_p;
5495 : 36429 : gimple *use_stmt, *def1, *def2;
5496 : 36429 : tree rhs2;
5497 : 36429 : if (single_imm_use (lhs, &use_p, &use_stmt)
5498 : 34269 : && gimple_store_p (use_stmt)
5499 : 41026 : && !gimple_has_volatile_ops (use_stmt)
5500 : 2597 : && is_gimple_assign (use_stmt)
5501 : 2593 : && (TREE_CODE (TREE_TYPE (gimple_assign_lhs (use_stmt)))
5502 : : == COMPLEX_TYPE)
5503 : 39017 : && (TREE_CODE (gimple_assign_lhs (use_stmt))
5504 : : != TARGET_MEM_REF))
5505 : : {
5506 : 2584 : tree use_lhs = gimple_assign_lhs (use_stmt);
5507 : 2584 : if (auto_var_p (use_lhs))
5508 : 600 : DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
5509 : 5168 : tree new_lhs = build1 (REALPART_EXPR,
5510 : 2584 : TREE_TYPE (TREE_TYPE (use_lhs)),
5511 : : unshare_expr (use_lhs));
5512 : 2584 : gimple *new_stmt = gimple_build_assign (new_lhs, rhs);
5513 : 2584 : location_t loc = gimple_location (use_stmt);
5514 : 2584 : gimple_set_location (new_stmt, loc);
5515 : 5168 : gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
5516 : 2584 : gimple_set_vdef (new_stmt, make_ssa_name (gimple_vop (fun)));
5517 : 5168 : SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
5518 : 5168 : gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
5519 : 2584 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5520 : 2584 : gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
5521 : :
5522 : 5168 : new_lhs = build1 (IMAGPART_EXPR,
5523 : 2584 : TREE_TYPE (TREE_TYPE (use_lhs)),
5524 : : unshare_expr (use_lhs));
5525 : 2584 : gimple_assign_set_lhs (use_stmt, new_lhs);
5526 : 2584 : gimple_assign_set_rhs1 (use_stmt, gimple_assign_rhs2 (stmt));
5527 : 2584 : update_stmt (use_stmt);
5528 : :
5529 : 2584 : release_defs (stmt);
5530 : 2584 : gsi_remove (&gsi, true);
5531 : : }
5532 : : /* Rewrite a component-wise load of a complex to a complex
5533 : : load if the components are not used separately. */
5534 : 33845 : else if (TREE_CODE (rhs) == SSA_NAME
5535 : 33404 : && has_single_use (rhs)
5536 : 29956 : && ((rhs2 = gimple_assign_rhs2 (stmt)), true)
5537 : 29956 : && TREE_CODE (rhs2) == SSA_NAME
5538 : 28242 : && has_single_use (rhs2)
5539 : 27821 : && (def1 = SSA_NAME_DEF_STMT (rhs),
5540 : 27821 : gimple_assign_load_p (def1))
5541 : 1097 : && (def2 = SSA_NAME_DEF_STMT (rhs2),
5542 : 1097 : gimple_assign_load_p (def2))
5543 : 1606 : && (gimple_vuse (def1) == gimple_vuse (def2))
5544 : 800 : && !gimple_has_volatile_ops (def1)
5545 : 800 : && !gimple_has_volatile_ops (def2)
5546 : 800 : && !stmt_can_throw_internal (fun, def1)
5547 : 800 : && !stmt_can_throw_internal (fun, def2)
5548 : 800 : && gimple_assign_rhs_code (def1) == REALPART_EXPR
5549 : 542 : && gimple_assign_rhs_code (def2) == IMAGPART_EXPR
5550 : 34387 : && operand_equal_p (TREE_OPERAND (gimple_assign_rhs1
5551 : : (def1), 0),
5552 : 542 : TREE_OPERAND (gimple_assign_rhs1
5553 : : (def2), 0)))
5554 : : {
5555 : 542 : tree cl = TREE_OPERAND (gimple_assign_rhs1 (def1), 0);
5556 : 542 : gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (cl));
5557 : 542 : gcc_assert (gsi_stmt (gsi) == stmt);
5558 : 1084 : gimple_set_vuse (stmt, gimple_vuse (def1));
5559 : 542 : gimple_set_modified (stmt, true);
5560 : 542 : gimple_stmt_iterator gsi2 = gsi_for_stmt (def1);
5561 : 542 : gsi_remove (&gsi, false);
5562 : 542 : gsi_insert_after (&gsi2, stmt, GSI_SAME_STMT);
5563 : : }
5564 : : else
5565 : 33303 : gsi_next (&gsi);
5566 : : }
5567 : 60596849 : else if (code == CONSTRUCTOR
5568 : 153996 : && VECTOR_TYPE_P (TREE_TYPE (rhs))
5569 : 153996 : && TYPE_MODE (TREE_TYPE (rhs)) == BLKmode
5570 : 2832 : && CONSTRUCTOR_NELTS (rhs) > 0
5571 : 60599681 : && (!VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
5572 : 505 : || (TYPE_MODE (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
5573 : : != BLKmode)))
5574 : : {
5575 : : /* Rewrite stores of a single-use vector constructors
5576 : : to component-wise stores if the mode isn't supported. */
5577 : 2831 : use_operand_p use_p;
5578 : 2831 : gimple *use_stmt;
5579 : 2831 : if (single_imm_use (lhs, &use_p, &use_stmt)
5580 : 2418 : && gimple_store_p (use_stmt)
5581 : 2916 : && !gimple_has_volatile_ops (use_stmt)
5582 : 1452 : && !stmt_can_throw_internal (fun, use_stmt)
5583 : 4273 : && is_gimple_assign (use_stmt))
5584 : : {
5585 : 1442 : tree elt_t = TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value);
5586 : 1442 : unsigned HOST_WIDE_INT elt_w
5587 : 1442 : = tree_to_uhwi (TYPE_SIZE (elt_t));
5588 : 1442 : unsigned HOST_WIDE_INT n
5589 : 1442 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs)));
5590 : 1442 : tree use_lhs = gimple_assign_lhs (use_stmt);
5591 : 1442 : if (auto_var_p (use_lhs))
5592 : 539 : DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
5593 : 903 : else if (TREE_CODE (use_lhs) == TARGET_MEM_REF)
5594 : : {
5595 : 1 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5596 : 1 : use_lhs = prepare_target_mem_ref_lvalue (use_lhs, &gsi2);
5597 : : }
5598 : 32693 : for (unsigned HOST_WIDE_INT bi = 0; bi < n; bi += elt_w)
5599 : : {
5600 : 31251 : unsigned HOST_WIDE_INT ci = bi / elt_w;
5601 : 31251 : tree new_rhs;
5602 : 31251 : if (ci < CONSTRUCTOR_NELTS (rhs))
5603 : 30633 : new_rhs = CONSTRUCTOR_ELT (rhs, ci)->value;
5604 : : else
5605 : 618 : new_rhs = build_zero_cst (elt_t);
5606 : 31251 : tree new_lhs = build3 (BIT_FIELD_REF,
5607 : : elt_t,
5608 : : unshare_expr (use_lhs),
5609 : 31251 : bitsize_int (elt_w),
5610 : 31251 : bitsize_int (bi));
5611 : 31251 : gimple *new_stmt = gimple_build_assign (new_lhs, new_rhs);
5612 : 31251 : location_t loc = gimple_location (use_stmt);
5613 : 31251 : gimple_set_location (new_stmt, loc);
5614 : 62502 : gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
5615 : 31251 : gimple_set_vdef (new_stmt,
5616 : : make_ssa_name (gimple_vop (fun)));
5617 : 62502 : SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
5618 : 62502 : gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
5619 : 31251 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5620 : 31251 : gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
5621 : : }
5622 : 1442 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5623 : 1442 : unlink_stmt_vdef (use_stmt);
5624 : 1442 : release_defs (use_stmt);
5625 : 1442 : gsi_remove (&gsi2, true);
5626 : 1442 : release_defs (stmt);
5627 : 1442 : gsi_remove (&gsi, true);
5628 : : }
5629 : : else
5630 : 1389 : gsi_next (&gsi);
5631 : : }
5632 : 60594018 : else if (code == VEC_PERM_EXPR)
5633 : : {
5634 : : /* Find vectorized sequences, where we can reduce the lane
5635 : : utilization. The narrowing will be donw later and only
5636 : : if we find a pair of sequences that can be blended. */
5637 : 176591 : gassign *assign = dyn_cast <gassign *> (stmt);
5638 : 176591 : vec_perm_simplify_seq seq;
5639 : 176591 : if (recognise_vec_perm_simplify_seq (assign, &seq))
5640 : 99 : append_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list,
5641 : : seq);
5642 : :
5643 : 176591 : gsi_next (&gsi);
5644 : : }
5645 : : else
5646 : 60417427 : gsi_next (&gsi);
5647 : : }
5648 : :
5649 : 45388558 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
5650 : :
5651 : : /* Combine stmts with the stmts defining their operands.
5652 : : Note we update GSI within the loop as necessary. */
5653 : 428012484 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5654 : : {
5655 : 337235368 : gimple *stmt = gsi_stmt (gsi);
5656 : :
5657 : : /* Mark stmt as potentially needing revisiting. */
5658 : 337235368 : gimple_set_plf (stmt, GF_PLF_1, false);
5659 : :
5660 : 337235368 : bool can_make_abnormal_goto = (is_gimple_call (stmt)
5661 : 337235368 : && stmt_can_make_abnormal_goto (stmt));
5662 : :
5663 : : /* Substitute from our lattice. We need to do so only once. */
5664 : 337235368 : bool substituted_p = false;
5665 : 337235368 : use_operand_p usep;
5666 : 337235368 : ssa_op_iter iter;
5667 : 497706549 : FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
5668 : : {
5669 : 160471181 : tree use = USE_FROM_PTR (usep);
5670 : 160471181 : tree val = fwprop_ssa_val (use);
5671 : 160471181 : if (val && val != use)
5672 : : {
5673 : 1860377 : if (!is_gimple_debug (stmt))
5674 : 1551427 : bitmap_set_bit (simple_dce_worklist, SSA_NAME_VERSION (use));
5675 : 1860377 : if (may_propagate_copy (use, val))
5676 : : {
5677 : 1857272 : propagate_value (usep, val);
5678 : 1857272 : substituted_p = true;
5679 : : }
5680 : : }
5681 : : }
5682 : 337235368 : if (substituted_p)
5683 : 1804381 : update_stmt (stmt);
5684 : 1804381 : if (substituted_p
5685 : 1804381 : && is_gimple_assign (stmt)
5686 : 1108897 : && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
5687 : 19166 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
5688 : 337235368 : if (substituted_p
5689 : 337235368 : && can_make_abnormal_goto
5690 : 337235368 : && !stmt_can_make_abnormal_goto (stmt))
5691 : 3 : bitmap_set_bit (need_ab_cleanup, bb->index);
5692 : :
5693 : 340012985 : bool changed;
5694 : 680025970 : do
5695 : : {
5696 : 340012985 : gimple *orig_stmt = stmt = gsi_stmt (gsi);
5697 : 340012985 : bool was_call = is_gimple_call (stmt);
5698 : 340012985 : bool was_noreturn = (was_call
5699 : 340012985 : && gimple_call_noreturn_p (stmt));
5700 : 340012985 : changed = false;
5701 : :
5702 : 340012985 : auto_vec<tree, 8> uses;
5703 : 503450558 : FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
5704 : 163437573 : if (uses.space (1))
5705 : 163052212 : uses.quick_push (USE_FROM_PTR (usep));
5706 : :
5707 : 340012985 : if (fold_stmt (&gsi, fwprop_ssa_val, simple_dce_worklist))
5708 : : {
5709 : 2473037 : changed = true;
5710 : : /* There is no updating of the address
5711 : : taken after the last forwprop so update
5712 : : the addresses when a folding happened to a call.
5713 : : The va_* builtins can remove taking of the address so
5714 : : can the sincos->cexpi transformation. See PR 39643 and PR 20983. */
5715 : 2473037 : if (was_call && last_p)
5716 : 2473037 : todoflags |= TODO_update_address_taken;
5717 : 2473037 : stmt = gsi_stmt (gsi);
5718 : : /* Cleanup the CFG if we simplified a condition to
5719 : : true or false. */
5720 : 2473037 : if (gcond *cond = dyn_cast <gcond *> (stmt))
5721 : 976032 : if (gimple_cond_true_p (cond)
5722 : 976032 : || gimple_cond_false_p (cond))
5723 : 14081 : cfg_changed = true;
5724 : : /* Queue old uses for simple DCE if not debug statement. */
5725 : 2473037 : if (!is_gimple_debug (stmt))
5726 : 10483190 : for (tree use : uses)
5727 : 3080561 : if (TREE_CODE (use) == SSA_NAME
5728 : 3080561 : && !SSA_NAME_IS_DEFAULT_DEF (use))
5729 : 2886719 : bitmap_set_bit (simple_dce_worklist,
5730 : 2886719 : SSA_NAME_VERSION (use));
5731 : 2473037 : update_stmt (stmt);
5732 : : }
5733 : :
5734 : 340012985 : switch (gimple_code (stmt))
5735 : : {
5736 : 106501003 : case GIMPLE_ASSIGN:
5737 : 106501003 : {
5738 : 106501003 : tree rhs1 = gimple_assign_rhs1 (stmt);
5739 : 106501003 : enum tree_code code = gimple_assign_rhs_code (stmt);
5740 : 106501003 : if (gimple_clobber_p (stmt))
5741 : 7120367 : do_simple_agr_dse (as_a<gassign*>(stmt), full_walk);
5742 : 99380636 : else if (gimple_store_p (stmt))
5743 : : {
5744 : 30887746 : optimize_aggr_zeroprop (stmt, full_walk);
5745 : 30887746 : if (gimple_assign_load_p (stmt))
5746 : 3901150 : optimize_agr_copyprop (stmt);
5747 : : }
5748 : 68492890 : else if (TREE_CODE_CLASS (code) == tcc_comparison)
5749 : 2568359 : changed |= forward_propagate_into_comparison (&gsi);
5750 : 65924531 : else if ((code == PLUS_EXPR
5751 : 65924531 : || code == BIT_IOR_EXPR
5752 : 55955778 : || code == BIT_XOR_EXPR)
5753 : 66056752 : && simplify_rotate (&gsi))
5754 : : changed = true;
5755 : 65921857 : else if (code == VEC_PERM_EXPR)
5756 : 178620 : changed |= simplify_permutation (&gsi);
5757 : 65743237 : else if (code == CONSTRUCTOR
5758 : 65743237 : && TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
5759 : 151917 : changed |= simplify_vector_constructor (&gsi);
5760 : 65591320 : else if (code == ARRAY_REF)
5761 : 1967452 : changed |= simplify_count_zeroes (&gsi);
5762 : : break;
5763 : : }
5764 : :
5765 : 118956 : case GIMPLE_SWITCH:
5766 : 118956 : changed |= simplify_gimple_switch (as_a <gswitch *> (stmt),
5767 : : edges_to_remove,
5768 : : simple_dce_worklist);
5769 : 118956 : break;
5770 : :
5771 : 19411534 : case GIMPLE_COND:
5772 : 19411534 : {
5773 : 19411534 : int did_something = forward_propagate_into_gimple_cond
5774 : 19411534 : (as_a <gcond *> (stmt));
5775 : 19411534 : if (did_something == 2)
5776 : 1702 : cfg_changed = true;
5777 : 19411534 : changed |= did_something != 0;
5778 : 19411534 : break;
5779 : : }
5780 : :
5781 : 23299263 : case GIMPLE_CALL:
5782 : 23299263 : {
5783 : 23299263 : tree callee = gimple_call_fndecl (stmt);
5784 : 23299263 : if (callee != NULL_TREE
5785 : 23299263 : && fndecl_built_in_p (callee, BUILT_IN_NORMAL))
5786 : 6123926 : changed |= simplify_builtin_call (&gsi, callee, full_walk);
5787 : : break;
5788 : : }
5789 : :
5790 : 340010311 : default:;
5791 : : }
5792 : :
5793 : 340010311 : if (changed || substituted_p)
5794 : : {
5795 : 4074954 : substituted_p = false;
5796 : 4074954 : stmt = gsi_stmt (gsi);
5797 : 4074954 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
5798 : 70 : bitmap_set_bit (to_purge, bb->index);
5799 : 4074954 : if (!was_noreturn
5800 : 4074954 : && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
5801 : 12 : to_fixup.safe_push (stmt);
5802 : : }
5803 : 4074954 : if (changed)
5804 : : {
5805 : : /* If the stmt changed then re-visit it and the statements
5806 : : inserted before it. */
5807 : 8694849 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
5808 : 5514104 : if (gimple_plf (gsi_stmt (gsi), GF_PLF_1))
5809 : : break;
5810 : 2777617 : if (gsi_end_p (gsi))
5811 : 444258 : gsi = gsi_start_bb (bb);
5812 : : else
5813 : 2555488 : gsi_next (&gsi);
5814 : : }
5815 : 340012985 : }
5816 : : while (changed);
5817 : :
5818 : : /* Stmt no longer needs to be revisited. */
5819 : 337235368 : stmt = gsi_stmt (gsi);
5820 : 337235368 : gcc_checking_assert (!gimple_plf (stmt, GF_PLF_1));
5821 : 337235368 : gimple_set_plf (stmt, GF_PLF_1, true);
5822 : :
5823 : : /* Fill up the lattice. */
5824 : 337235368 : if (gimple_assign_single_p (stmt))
5825 : : {
5826 : 70570544 : tree lhs = gimple_assign_lhs (stmt);
5827 : 70570544 : tree rhs = gimple_assign_rhs1 (stmt);
5828 : 70570544 : if (TREE_CODE (lhs) == SSA_NAME)
5829 : : {
5830 : 32583082 : tree val = lhs;
5831 : 32583082 : if (TREE_CODE (rhs) == SSA_NAME)
5832 : 792980 : val = fwprop_ssa_val (rhs);
5833 : 31790102 : else if (is_gimple_min_invariant (rhs))
5834 : 420896 : val = rhs;
5835 : : /* If we can propagate the lattice-value mark the
5836 : : stmt for removal. */
5837 : 32583082 : if (val != lhs
5838 : 32583082 : && may_propagate_copy (lhs, val))
5839 : 1210584 : to_remove_defs.safe_push (SSA_NAME_VERSION (lhs));
5840 : 32583082 : fwprop_set_lattice_val (lhs, val);
5841 : : }
5842 : : }
5843 : 266664824 : else if (gimple_nop_p (stmt))
5844 : 87982 : to_remove.safe_push (stmt);
5845 : : }
5846 : :
5847 : : /* Substitute in destination PHI arguments. */
5848 : 109498646 : FOR_EACH_EDGE (e, ei, bb->succs)
5849 : 64110088 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
5850 : 106965082 : !gsi_end_p (gsi); gsi_next (&gsi))
5851 : : {
5852 : 42854994 : gphi *phi = gsi.phi ();
5853 : 42854994 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
5854 : 42854994 : tree arg = USE_FROM_PTR (use_p);
5855 : 70756109 : if (TREE_CODE (arg) != SSA_NAME
5856 : 42854994 : || virtual_operand_p (arg))
5857 : 27901115 : continue;
5858 : 14953879 : tree val = fwprop_ssa_val (arg);
5859 : 14953879 : if (val != arg
5860 : 14953879 : && may_propagate_copy (arg, val, !(e->flags & EDGE_ABNORMAL)))
5861 : 256445 : propagate_value (use_p, val);
5862 : : }
5863 : :
5864 : : /* Mark outgoing exectuable edges. */
5865 : 45388558 : if (edge e = find_taken_edge (bb, NULL))
5866 : : {
5867 : 19318864 : e->flags |= EDGE_EXECUTABLE;
5868 : 45408568 : if (EDGE_COUNT (bb->succs) > 1)
5869 : 20010 : cfg_changed = true;
5870 : : }
5871 : : else
5872 : : {
5873 : 70840907 : FOR_EACH_EDGE (e, ei, bb->succs)
5874 : 44771213 : e->flags |= EDGE_EXECUTABLE;
5875 : : }
5876 : : }
5877 : 5630245 : free (postorder);
5878 : 5630245 : free (bb_to_rpo);
5879 : 5630245 : lattice.release ();
5880 : :
5881 : : /* First remove chains of stmts where we check no uses remain. */
5882 : 5630245 : simple_dce_from_worklist (simple_dce_worklist, to_purge);
5883 : :
5884 : 5980293 : auto remove = [](gimple *stmt)
5885 : : {
5886 : 350048 : if (dump_file && (dump_flags & TDF_DETAILS))
5887 : : {
5888 : 1 : fprintf (dump_file, "Removing dead stmt ");
5889 : 1 : print_gimple_stmt (dump_file, stmt, 0);
5890 : 1 : fprintf (dump_file, "\n");
5891 : : }
5892 : 350048 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
5893 : 350048 : if (gimple_code (stmt) == GIMPLE_PHI)
5894 : 99670 : remove_phi_node (&gsi, true);
5895 : : else
5896 : : {
5897 : 250378 : unlink_stmt_vdef (stmt);
5898 : 250378 : gsi_remove (&gsi, true);
5899 : 250378 : release_defs (stmt);
5900 : : }
5901 : 350048 : };
5902 : :
5903 : : /* Then remove stmts we know we can remove even though we did not
5904 : : substitute in dead code regions, so uses can remain. Do so in reverse
5905 : : order to make debug stmt creation possible. */
5906 : 12715553 : while (!to_remove_defs.is_empty())
5907 : : {
5908 : 1455063 : tree def = ssa_name (to_remove_defs.pop ());
5909 : : /* For example remove_prop_source_from_use can remove stmts queued
5910 : : for removal. Deal with this gracefully. */
5911 : 1455063 : if (!def)
5912 : 1192997 : continue;
5913 : 262066 : gimple *stmt = SSA_NAME_DEF_STMT (def);
5914 : 262066 : remove (stmt);
5915 : : }
5916 : :
5917 : : /* Wipe other queued stmts that do not have SSA defs. */
5918 : 5718227 : while (!to_remove.is_empty())
5919 : : {
5920 : 87982 : gimple *stmt = to_remove.pop ();
5921 : 87982 : remove (stmt);
5922 : : }
5923 : :
5924 : : /* Fixup stmts that became noreturn calls. This may require splitting
5925 : : blocks and thus isn't possible during the walk. Do this
5926 : : in reverse order so we don't inadvertedly remove a stmt we want to
5927 : : fixup by visiting a dominating now noreturn call first. */
5928 : 5630257 : while (!to_fixup.is_empty ())
5929 : : {
5930 : 12 : gimple *stmt = to_fixup.pop ();
5931 : 12 : if (dump_file && dump_flags & TDF_DETAILS)
5932 : : {
5933 : 0 : fprintf (dump_file, "Fixing up noreturn call ");
5934 : 0 : print_gimple_stmt (dump_file, stmt, 0);
5935 : 0 : fprintf (dump_file, "\n");
5936 : : }
5937 : 12 : cfg_changed |= fixup_noreturn_call (stmt);
5938 : : }
5939 : :
5940 : 5630245 : cfg_changed |= gimple_purge_all_dead_eh_edges (to_purge);
5941 : 5630245 : cfg_changed |= gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
5942 : 5630245 : BITMAP_FREE (to_purge);
5943 : :
5944 : : /* Remove edges queued from switch stmt simplification. */
5945 : 16890735 : for (auto ep : edges_to_remove)
5946 : : {
5947 : 0 : basic_block src = BASIC_BLOCK_FOR_FN (fun, ep.first);
5948 : 0 : basic_block dest = BASIC_BLOCK_FOR_FN (fun, ep.second);
5949 : 0 : edge e;
5950 : 0 : if (src && dest && (e = find_edge (src, dest)))
5951 : : {
5952 : 0 : free_dominance_info (CDI_DOMINATORS);
5953 : 0 : remove_edge (e);
5954 : 0 : cfg_changed = true;
5955 : : }
5956 : : }
5957 : :
5958 : 11258950 : if (get_range_query (fun) != get_global_range_query ())
5959 : 1540 : disable_ranger (fun);
5960 : :
5961 : 5630245 : if (cfg_changed)
5962 : 9632 : todoflags |= TODO_cleanup_cfg;
5963 : :
5964 : 5630245 : return todoflags;
5965 : 5630245 : }
5966 : :
5967 : : } // anon namespace
5968 : :
5969 : : gimple_opt_pass *
5970 : 290033 : make_pass_forwprop (gcc::context *ctxt)
5971 : : {
5972 : 290033 : return new pass_forwprop (ctxt);
5973 : : }
|