Line data Source code
1 : /* Forward propagation of expressions for single use variables.
2 : Copyright (C) 2004-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify
7 : it under the terms of the GNU General Public License as published by
8 : the Free Software Foundation; either version 3, or (at your option)
9 : any later version.
10 :
11 : GCC is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : GNU General Public License for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 : #include "config.h"
21 : #include "system.h"
22 : #include "coretypes.h"
23 : #include "backend.h"
24 : #include "rtl.h"
25 : #include "tree.h"
26 : #include "gimple.h"
27 : #include "cfghooks.h"
28 : #include "tree-pass.h"
29 : #include "ssa.h"
30 : #include "expmed.h"
31 : #include "optabs-query.h"
32 : #include "gimple-pretty-print.h"
33 : #include "fold-const.h"
34 : #include "stor-layout.h"
35 : #include "gimple-iterator.h"
36 : #include "gimple-fold.h"
37 : #include "tree-eh.h"
38 : #include "gimplify.h"
39 : #include "gimplify-me.h"
40 : #include "tree-cfg.h"
41 : #include "expr.h"
42 : #include "tree-dfa.h"
43 : #include "tree-ssa-propagate.h"
44 : #include "tree-ssa-dom.h"
45 : #include "tree-ssa-strlen.h"
46 : #include "builtins.h"
47 : #include "tree-cfgcleanup.h"
48 : #include "cfganal.h"
49 : #include "optabs-tree.h"
50 : #include "insn-config.h"
51 : #include "recog.h"
52 : #include "cfgloop.h"
53 : #include "tree-vectorizer.h"
54 : #include "tree-vector-builder.h"
55 : #include "vec-perm-indices.h"
56 : #include "internal-fn.h"
57 : #include "cgraph.h"
58 : #include "tree-ssa.h"
59 : #include "gimple-range.h"
60 : #include "tree-ssa-dce.h"
61 : #include "tree-ssa-math-opts.h"
62 :
63 : /* This pass propagates the RHS of assignment statements into use
64 : sites of the LHS of the assignment. It's basically a specialized
65 : form of tree combination. It is hoped all of this can disappear
66 : when we have a generalized tree combiner.
67 :
68 : One class of common cases we handle is forward propagating a single use
69 : variable into a COND_EXPR.
70 :
71 : bb0:
72 : x = a COND b;
73 : if (x) goto ... else goto ...
74 :
75 : Will be transformed into:
76 :
77 : bb0:
78 : if (a COND b) goto ... else goto ...
79 :
80 : Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
81 :
82 : Or (assuming c1 and c2 are constants):
83 :
84 : bb0:
85 : x = a + c1;
86 : if (x EQ/NEQ c2) goto ... else goto ...
87 :
88 : Will be transformed into:
89 :
90 : bb0:
91 : if (a EQ/NEQ (c2 - c1)) goto ... else goto ...
92 :
93 : Similarly for x = a - c1.
94 :
95 : Or
96 :
97 : bb0:
98 : x = !a
99 : if (x) goto ... else goto ...
100 :
101 : Will be transformed into:
102 :
103 : bb0:
104 : if (a == 0) goto ... else goto ...
105 :
106 : Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
107 : For these cases, we propagate A into all, possibly more than one,
108 : COND_EXPRs that use X.
109 :
110 : Or
111 :
112 : bb0:
113 : x = (typecast) a
114 : if (x) goto ... else goto ...
115 :
116 : Will be transformed into:
117 :
118 : bb0:
119 : if (a != 0) goto ... else goto ...
120 :
121 : (Assuming a is an integral type and x is a boolean or x is an
122 : integral and a is a boolean.)
123 :
124 : Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
125 : For these cases, we propagate A into all, possibly more than one,
126 : COND_EXPRs that use X.
127 :
128 : In addition to eliminating the variable and the statement which assigns
129 : a value to the variable, we may be able to later thread the jump without
130 : adding insane complexity in the dominator optimizer.
131 :
132 : Also note these transformations can cascade. We handle this by having
133 : a worklist of COND_EXPR statements to examine. As we make a change to
134 : a statement, we put it back on the worklist to examine on the next
135 : iteration of the main loop.
136 :
137 : A second class of propagation opportunities arises for ADDR_EXPR
138 : nodes.
139 :
140 : ptr = &x->y->z;
141 : res = *ptr;
142 :
143 : Will get turned into
144 :
145 : res = x->y->z;
146 :
147 : Or
148 : ptr = (type1*)&type2var;
149 : res = *ptr
150 :
151 : Will get turned into (if type1 and type2 are the same size
152 : and neither have volatile on them):
153 : res = VIEW_CONVERT_EXPR<type1>(type2var)
154 :
155 : Or
156 :
157 : ptr = &x[0];
158 : ptr2 = ptr + <constant>;
159 :
160 : Will get turned into
161 :
162 : ptr2 = &x[constant/elementsize];
163 :
164 : Or
165 :
166 : ptr = &x[0];
167 : offset = index * element_size;
168 : offset_p = (pointer) offset;
169 : ptr2 = ptr + offset_p
170 :
171 : Will get turned into:
172 :
173 : ptr2 = &x[index];
174 :
175 : Or
176 : ssa = (int) decl
177 : res = ssa & 1
178 :
179 : Provided that decl has known alignment >= 2, will get turned into
180 :
181 : res = 0
182 :
183 : We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to
184 : allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent
185 : {NOT_EXPR,NEG_EXPR}.
186 :
187 : This will (of course) be extended as other needs arise. */
188 :
189 : /* Data structure that contains simplifiable vectorized permute sequences.
190 : See recognise_vec_perm_simplify_seq () for a description of the sequence. */
191 :
192 : struct _vec_perm_simplify_seq
193 : {
194 : /* Defining stmts of vectors in the sequence. */
195 : gassign *v_1_stmt;
196 : gassign *v_2_stmt;
197 : gassign *v_x_stmt;
198 : gassign *v_y_stmt;
199 : /* Final permute statement. */
200 : gassign *stmt;
201 : /* New selector indices for stmt. */
202 : tree new_sel;
203 : /* Elements of each vector and selector. */
204 : unsigned int nelts;
205 : };
206 : typedef struct _vec_perm_simplify_seq *vec_perm_simplify_seq;
207 :
208 : static bool forward_propagate_addr_expr (tree, tree, bool);
209 :
210 : /* Set to true if we delete dead edges during the optimization. */
211 : static bool cfg_changed;
212 :
213 : static tree rhs_to_tree (tree type, gimple *stmt);
214 :
215 : static bitmap to_purge;
216 :
217 : /* Const-and-copy lattice. */
218 : static vec<tree> lattice;
219 :
220 : /* Set the lattice entry for NAME to VAL. */
221 : static void
222 33777890 : fwprop_set_lattice_val (tree name, tree val)
223 : {
224 33777890 : if (TREE_CODE (name) == SSA_NAME)
225 : {
226 33777890 : if (SSA_NAME_VERSION (name) >= lattice.length ())
227 : {
228 31452 : lattice.reserve (num_ssa_names - lattice.length ());
229 20968 : lattice.quick_grow_cleared (num_ssa_names);
230 : }
231 33777890 : lattice[SSA_NAME_VERSION (name)] = val;
232 : /* As this now constitutes a copy duplicate points-to
233 : and range info appropriately. */
234 33777890 : if (TREE_CODE (val) == SSA_NAME)
235 33281616 : maybe_duplicate_ssa_info_at_copy (name, val);
236 : }
237 33777890 : }
238 :
239 : /* Invalidate the lattice entry for NAME, done when releasing SSA names. */
240 : static void
241 954796 : fwprop_invalidate_lattice (tree name)
242 : {
243 954796 : if (name
244 952283 : && TREE_CODE (name) == SSA_NAME
245 1906951 : && SSA_NAME_VERSION (name) < lattice.length ())
246 952124 : lattice[SSA_NAME_VERSION (name)] = NULL_TREE;
247 954796 : }
248 :
249 : /* Get the statement we can propagate from into NAME skipping
250 : trivial copies. Returns the statement which defines the
251 : propagation source or NULL_TREE if there is no such one.
252 : If SINGLE_USE_ONLY is set considers only sources which have
253 : a single use chain up to NAME. If SINGLE_USE_P is non-null,
254 : it is set to whether the chain to NAME is a single use chain
255 : or not. SINGLE_USE_P is not written to if SINGLE_USE_ONLY is set. */
256 :
257 : static gimple *
258 28715672 : get_prop_source_stmt (tree name, bool single_use_only, bool *single_use_p)
259 : {
260 28715672 : bool single_use = true;
261 :
262 28716654 : do {
263 28716163 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
264 :
265 28716163 : if (!has_single_use (name))
266 : {
267 15701512 : single_use = false;
268 15701512 : if (single_use_only)
269 : return NULL;
270 : }
271 :
272 : /* If name is defined by a PHI node or is the default def, bail out. */
273 28714603 : if (!is_gimple_assign (def_stmt))
274 : return NULL;
275 :
276 : /* If def_stmt is a simple copy, continue looking. */
277 20224385 : if (gimple_assign_rhs_code (def_stmt) == SSA_NAME)
278 491 : name = gimple_assign_rhs1 (def_stmt);
279 : else
280 : {
281 20223894 : if (!single_use_only && single_use_p)
282 19896955 : *single_use_p = single_use;
283 :
284 : return def_stmt;
285 : }
286 491 : } while (1);
287 : }
288 :
289 : /* Checks if the destination ssa name in DEF_STMT can be used as
290 : propagation source. Returns true if so, otherwise false. */
291 :
292 : static bool
293 28359097 : can_propagate_from (gimple *def_stmt)
294 : {
295 28359097 : gcc_assert (is_gimple_assign (def_stmt));
296 :
297 : /* If the rhs has side-effects we cannot propagate from it. */
298 28359097 : if (gimple_has_volatile_ops (def_stmt))
299 : return false;
300 :
301 : /* If the rhs is a load we cannot propagate from it. */
302 27766025 : if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_reference
303 27766025 : || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_declaration)
304 : return false;
305 :
306 : /* Constants can be always propagated. */
307 13603118 : if (gimple_assign_single_p (def_stmt)
308 13603118 : && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
309 : return true;
310 :
311 : /* We cannot propagate ssa names that occur in abnormal phi nodes. */
312 13603118 : if (stmt_references_abnormal_ssa_name (def_stmt))
313 : return false;
314 :
315 : /* If the definition is a conversion of a pointer to a function type,
316 : then we cannot apply optimizations as some targets require
317 : function pointers to be canonicalized and in this case this
318 : optimization could eliminate a necessary canonicalization. */
319 13602428 : if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
320 : {
321 3292302 : tree rhs = gimple_assign_rhs1 (def_stmt);
322 3292302 : if (FUNCTION_POINTER_TYPE_P (TREE_TYPE (rhs)))
323 818 : return false;
324 : }
325 :
326 : return true;
327 : }
328 :
329 : /* Remove a chain of dead statements starting at the definition of
330 : NAME. The chain is linked via the first operand of the defining statements.
331 : If NAME was replaced in its only use then this function can be used
332 : to clean up dead stmts. The function handles already released SSA
333 : names gracefully. */
334 :
335 : static void
336 233836 : remove_prop_source_from_use (tree name)
337 : {
338 295509 : gimple_stmt_iterator gsi;
339 295509 : gimple *stmt;
340 :
341 295509 : do {
342 295509 : basic_block bb;
343 :
344 295509 : if (SSA_NAME_IN_FREE_LIST (name)
345 295466 : || SSA_NAME_IS_DEFAULT_DEF (name)
346 589702 : || !has_zero_uses (name))
347 : break;
348 :
349 62132 : stmt = SSA_NAME_DEF_STMT (name);
350 62132 : if (gimple_code (stmt) == GIMPLE_PHI
351 62132 : || gimple_has_side_effects (stmt))
352 : break;
353 :
354 62132 : bb = gimple_bb (stmt);
355 62132 : gsi = gsi_for_stmt (stmt);
356 62132 : unlink_stmt_vdef (stmt);
357 62132 : if (gsi_remove (&gsi, true))
358 6 : bitmap_set_bit (to_purge, bb->index);
359 62132 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
360 62132 : release_defs (stmt);
361 :
362 62132 : name = is_gimple_assign (stmt) ? gimple_assign_rhs1 (stmt) : NULL_TREE;
363 62132 : } while (name && TREE_CODE (name) == SSA_NAME);
364 :
365 233836 : }
366 :
367 : /* Return the rhs of a gassign *STMT in a form of a single tree,
368 : converted to type TYPE.
369 :
370 : This should disappear, but is needed so we can combine expressions and use
371 : the fold() interfaces. Long term, we need to develop folding and combine
372 : routines that deal with gimple exclusively . */
373 :
374 : static tree
375 7437912 : rhs_to_tree (tree type, gimple *stmt)
376 : {
377 7437912 : location_t loc = gimple_location (stmt);
378 7437912 : enum tree_code code = gimple_assign_rhs_code (stmt);
379 7437912 : switch (get_gimple_rhs_class (code))
380 : {
381 13293 : case GIMPLE_TERNARY_RHS:
382 13293 : return fold_build3_loc (loc, code, type, gimple_assign_rhs1 (stmt),
383 : gimple_assign_rhs2 (stmt),
384 13293 : gimple_assign_rhs3 (stmt));
385 5049088 : case GIMPLE_BINARY_RHS:
386 5049088 : return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (stmt),
387 5049088 : gimple_assign_rhs2 (stmt));
388 2084820 : case GIMPLE_UNARY_RHS:
389 2084820 : return build1 (code, type, gimple_assign_rhs1 (stmt));
390 290711 : case GIMPLE_SINGLE_RHS:
391 290711 : return gimple_assign_rhs1 (stmt);
392 0 : default:
393 0 : gcc_unreachable ();
394 : }
395 : }
396 :
397 : /* Combine OP0 CODE OP1 in the context of a COND_EXPR. Returns
398 : the folded result in a form suitable for COND_EXPR_COND or
399 : NULL_TREE, if there is no suitable simplified form. If
400 : INVARIANT_ONLY is true only gimple_min_invariant results are
401 : considered simplified. */
402 :
403 : static tree
404 8372182 : combine_cond_expr_cond (gimple *stmt, enum tree_code code, tree type,
405 : tree op0, tree op1, bool invariant_only)
406 : {
407 8372182 : tree t;
408 :
409 8372182 : gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
410 :
411 8372182 : t = fold_binary_loc (gimple_location (stmt), code, type, op0, op1);
412 8372182 : if (!t)
413 : return NULL_TREE;
414 :
415 : /* Require that we got a boolean type out if we put one in. */
416 3650573 : gcc_assert (TREE_CODE (TREE_TYPE (t)) == TREE_CODE (type));
417 :
418 : /* Canonicalize the combined condition for use in a COND_EXPR. */
419 3650573 : t = canonicalize_cond_expr_cond (t);
420 :
421 : /* Bail out if we required an invariant but didn't get one. */
422 3650573 : if (!t || (invariant_only && !is_gimple_min_invariant (t)))
423 3419004 : return NULL_TREE;
424 :
425 : return t;
426 : }
427 :
428 : /* Combine the comparison OP0 CODE OP1 at LOC with the defining statements
429 : of its operand. Return a new comparison tree or NULL_TREE if there
430 : were no simplifying combines. */
431 :
432 : static tree
433 22633859 : forward_propagate_into_comparison_1 (gimple *stmt,
434 : enum tree_code code, tree type,
435 : tree op0, tree op1)
436 : {
437 22633859 : tree tmp = NULL_TREE;
438 22633859 : tree rhs0 = NULL_TREE, rhs1 = NULL_TREE;
439 22633859 : bool single_use0_p = false, single_use1_p = false;
440 :
441 : /* For comparisons use the first operand, that is likely to
442 : simplify comparisons against constants. */
443 22633859 : if (TREE_CODE (op0) == SSA_NAME)
444 : {
445 22561646 : gimple *def_stmt = get_prop_source_stmt (op0, false, &single_use0_p);
446 22561646 : if (def_stmt && can_propagate_from (def_stmt))
447 : {
448 5632767 : enum tree_code def_code = gimple_assign_rhs_code (def_stmt);
449 5632767 : bool invariant_only_p = !single_use0_p;
450 :
451 5632767 : rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt);
452 :
453 : /* Always combine comparisons or conversions from booleans. */
454 5632767 : if (TREE_CODE (op1) == INTEGER_CST
455 5632767 : && ((CONVERT_EXPR_CODE_P (def_code)
456 904895 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs0, 0)))
457 : == BOOLEAN_TYPE)
458 3598926 : || TREE_CODE_CLASS (def_code) == tcc_comparison))
459 : invariant_only_p = false;
460 :
461 5632767 : tmp = combine_cond_expr_cond (stmt, code, type,
462 : rhs0, op1, invariant_only_p);
463 5632767 : if (tmp)
464 : return tmp;
465 : }
466 : }
467 :
468 : /* If that wasn't successful, try the second operand. */
469 22410735 : if (TREE_CODE (op1) == SSA_NAME)
470 : {
471 5557963 : gimple *def_stmt = get_prop_source_stmt (op1, false, &single_use1_p);
472 5557963 : if (def_stmt && can_propagate_from (def_stmt))
473 : {
474 1805145 : rhs1 = rhs_to_tree (TREE_TYPE (op0), def_stmt);
475 1805145 : tmp = combine_cond_expr_cond (stmt, code, type,
476 : op0, rhs1, !single_use1_p);
477 1805145 : if (tmp)
478 : return tmp;
479 : }
480 : }
481 :
482 : /* If that wasn't successful either, try both operands. */
483 22404119 : if (rhs0 != NULL_TREE
484 22404119 : && rhs1 != NULL_TREE)
485 934270 : tmp = combine_cond_expr_cond (stmt, code, type,
486 : rhs0, rhs1,
487 934270 : !(single_use0_p && single_use1_p));
488 :
489 : return tmp;
490 : }
491 :
492 : /* Propagate from the ssa name definition statements of the assignment
493 : from a comparison at *GSI into the conditional if that simplifies it.
494 : Returns true if the stmt was modified. */
495 :
496 : static bool
497 2650594 : forward_propagate_into_comparison (gimple_stmt_iterator *gsi)
498 : {
499 2650594 : gimple *stmt = gsi_stmt (*gsi);
500 2650594 : tree tmp;
501 2650594 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
502 2650594 : tree rhs1 = gimple_assign_rhs1 (stmt);
503 2650594 : tree rhs2 = gimple_assign_rhs2 (stmt);
504 :
505 : /* Combine the comparison with defining statements. */
506 2650594 : tmp = forward_propagate_into_comparison_1 (stmt,
507 : gimple_assign_rhs_code (stmt),
508 : type, rhs1, rhs2);
509 2650594 : if (tmp && useless_type_conversion_p (type, TREE_TYPE (tmp)))
510 : {
511 7281 : if (dump_file)
512 : {
513 0 : fprintf (dump_file, " Replaced '");
514 0 : print_gimple_expr (dump_file, stmt, 0);
515 0 : fprintf (dump_file, "' with '");
516 0 : print_generic_expr (dump_file, tmp);
517 0 : fprintf (dump_file, "'\n");
518 : }
519 7281 : gimple_assign_set_rhs_from_tree (gsi, tmp);
520 7281 : fold_stmt (gsi);
521 7281 : update_stmt (gsi_stmt (*gsi));
522 :
523 7281 : if (TREE_CODE (rhs1) == SSA_NAME)
524 7281 : remove_prop_source_from_use (rhs1);
525 7281 : if (TREE_CODE (rhs2) == SSA_NAME)
526 3043 : remove_prop_source_from_use (rhs2);
527 : return true;
528 : }
529 :
530 : return false;
531 : }
532 :
533 : /* Propagate from the ssa name definition statements of COND_EXPR
534 : in GIMPLE_COND statement STMT into the conditional if that simplifies it.
535 : Returns zero if no statement was changed, one if there were
536 : changes and two if cfg_cleanup needs to run. */
537 :
538 : static int
539 19983265 : forward_propagate_into_gimple_cond (gcond *stmt)
540 : {
541 19983265 : tree tmp;
542 19983265 : enum tree_code code = gimple_cond_code (stmt);
543 19983265 : tree rhs1 = gimple_cond_lhs (stmt);
544 19983265 : tree rhs2 = gimple_cond_rhs (stmt);
545 :
546 : /* GIMPLE_COND will always be a comparison. */
547 19983265 : gcc_assert (TREE_CODE_CLASS (gimple_cond_code (stmt)) == tcc_comparison);
548 :
549 19983265 : tmp = forward_propagate_into_comparison_1 (stmt, code,
550 : boolean_type_node,
551 : rhs1, rhs2);
552 19983265 : if (tmp
553 19983265 : && is_gimple_condexpr_for_cond (tmp))
554 : {
555 217827 : if (dump_file)
556 : {
557 9 : fprintf (dump_file, " Replaced '");
558 9 : print_gimple_expr (dump_file, stmt, 0);
559 9 : fprintf (dump_file, "' with '");
560 9 : print_generic_expr (dump_file, tmp);
561 9 : fprintf (dump_file, "'\n");
562 : }
563 :
564 217827 : gimple_cond_set_condition_from_tree (stmt, unshare_expr (tmp));
565 217827 : update_stmt (stmt);
566 :
567 217827 : if (TREE_CODE (rhs1) == SSA_NAME)
568 217827 : remove_prop_source_from_use (rhs1);
569 217827 : if (TREE_CODE (rhs2) == SSA_NAME)
570 5684 : remove_prop_source_from_use (rhs2);
571 217827 : return is_gimple_min_invariant (tmp) ? 2 : 1;
572 : }
573 :
574 19765438 : if (canonicalize_bool_cond (stmt, gimple_bb (stmt)))
575 : return 1;
576 :
577 : return 0;
578 : }
579 :
580 : /* We've just substituted an ADDR_EXPR into stmt. Update all the
581 : relevant data structures to match. */
582 :
583 : static void
584 2053739 : tidy_after_forward_propagate_addr (gimple *stmt)
585 : {
586 : /* We may have turned a trapping insn into a non-trapping insn. */
587 2053739 : if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
588 39 : bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
589 :
590 2053739 : if (TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
591 261640 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
592 2053739 : }
593 :
594 : /* NAME is a SSA_NAME representing DEF_RHS which is of the form
595 : ADDR_EXPR <whatever>.
596 :
597 : Try to forward propagate the ADDR_EXPR into the use USE_STMT.
598 : Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
599 : node or for recovery of array indexing from pointer arithmetic.
600 :
601 : Return true if the propagation was successful (the propagation can
602 : be not totally successful, yet things may have been changed). */
603 :
604 : static bool
605 2919185 : forward_propagate_addr_expr_1 (tree name, tree def_rhs,
606 : gimple_stmt_iterator *use_stmt_gsi,
607 : bool single_use_p)
608 : {
609 2919185 : tree lhs, rhs, rhs2, array_ref;
610 2919185 : gimple *use_stmt = gsi_stmt (*use_stmt_gsi);
611 2919185 : enum tree_code rhs_code;
612 2919185 : bool res = true;
613 :
614 2919185 : gcc_assert (TREE_CODE (def_rhs) == ADDR_EXPR);
615 :
616 2919185 : lhs = gimple_assign_lhs (use_stmt);
617 2919185 : rhs_code = gimple_assign_rhs_code (use_stmt);
618 2919185 : rhs = gimple_assign_rhs1 (use_stmt);
619 :
620 : /* Do not perform copy-propagation but recurse through copy chains. */
621 2919185 : if (TREE_CODE (lhs) == SSA_NAME
622 1455261 : && rhs_code == SSA_NAME)
623 8786 : return forward_propagate_addr_expr (lhs, def_rhs, single_use_p);
624 :
625 : /* The use statement could be a conversion. Recurse to the uses of the
626 : lhs as copyprop does not copy through pointer to integer to pointer
627 : conversions and FRE does not catch all cases either.
628 : Treat the case of a single-use name and
629 : a conversion to def_rhs type separate, though. */
630 2910399 : if (TREE_CODE (lhs) == SSA_NAME
631 1446475 : && CONVERT_EXPR_CODE_P (rhs_code))
632 : {
633 : /* If there is a point in a conversion chain where the types match
634 : so we can remove a conversion re-materialize the address here
635 : and stop. */
636 24866 : if (single_use_p
637 24866 : && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs)))
638 : {
639 1 : gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs));
640 1 : gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs));
641 1 : return true;
642 : }
643 :
644 : /* Else recurse if the conversion preserves the address value. */
645 49730 : if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
646 2 : || POINTER_TYPE_P (TREE_TYPE (lhs)))
647 49730 : && (TYPE_PRECISION (TREE_TYPE (lhs))
648 24865 : >= TYPE_PRECISION (TREE_TYPE (def_rhs))))
649 24798 : return forward_propagate_addr_expr (lhs, def_rhs, single_use_p);
650 :
651 : return false;
652 : }
653 :
654 : /* If this isn't a conversion chain from this on we only can propagate
655 : into compatible pointer contexts. */
656 2885533 : if (!types_compatible_p (TREE_TYPE (name), TREE_TYPE (def_rhs)))
657 : return false;
658 :
659 : /* Propagate through constant pointer adjustments. */
660 2864282 : if (TREE_CODE (lhs) == SSA_NAME
661 1402048 : && rhs_code == POINTER_PLUS_EXPR
662 1402048 : && rhs == name
663 3032903 : && TREE_CODE (gimple_assign_rhs2 (use_stmt)) == INTEGER_CST)
664 : {
665 119395 : tree new_def_rhs;
666 : /* As we come here with non-invariant addresses in def_rhs we need
667 : to make sure we can build a valid constant offsetted address
668 : for further propagation. Simply rely on fold building that
669 : and check after the fact. */
670 119395 : new_def_rhs = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (rhs)),
671 : def_rhs,
672 : fold_convert (ptr_type_node,
673 : gimple_assign_rhs2 (use_stmt)));
674 119395 : if (TREE_CODE (new_def_rhs) == MEM_REF
675 119395 : && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs, 0)))
676 : return false;
677 115265 : new_def_rhs = build1 (ADDR_EXPR, TREE_TYPE (rhs), new_def_rhs);
678 :
679 : /* Recurse. If we could propagate into all uses of lhs do not
680 : bother to replace into the current use but just pretend we did. */
681 115265 : if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p))
682 : return true;
683 :
684 39192 : if (useless_type_conversion_p (TREE_TYPE (lhs),
685 39192 : TREE_TYPE (new_def_rhs)))
686 39192 : gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs),
687 : new_def_rhs);
688 0 : else if (is_gimple_min_invariant (new_def_rhs))
689 0 : gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR, new_def_rhs);
690 : else
691 : return false;
692 39192 : gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt);
693 39192 : update_stmt (use_stmt);
694 39192 : return true;
695 : }
696 :
697 : /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
698 : ADDR_EXPR will not appear on the LHS. */
699 2744887 : tree *lhsp = gimple_assign_lhs_ptr (use_stmt);
700 4159747 : while (handled_component_p (*lhsp))
701 1414860 : lhsp = &TREE_OPERAND (*lhsp, 0);
702 2744887 : lhs = *lhsp;
703 :
704 : /* Now see if the LHS node is a MEM_REF using NAME. If so,
705 : propagate the ADDR_EXPR into the use of NAME and fold the result. */
706 2744887 : if (TREE_CODE (lhs) == MEM_REF
707 2744887 : && TREE_OPERAND (lhs, 0) == name)
708 : {
709 919116 : tree def_rhs_base;
710 919116 : poly_int64 def_rhs_offset;
711 : /* If the address is invariant we can always fold it. */
712 919116 : if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
713 : &def_rhs_offset)))
714 : {
715 870826 : poly_offset_int off = mem_ref_offset (lhs);
716 870826 : tree new_ptr;
717 870826 : off += def_rhs_offset;
718 870826 : if (TREE_CODE (def_rhs_base) == MEM_REF)
719 : {
720 849146 : off += mem_ref_offset (def_rhs_base);
721 849146 : new_ptr = TREE_OPERAND (def_rhs_base, 0);
722 : }
723 : else
724 21680 : new_ptr = build_fold_addr_expr (def_rhs_base);
725 870826 : TREE_OPERAND (lhs, 0) = new_ptr;
726 870826 : TREE_OPERAND (lhs, 1)
727 870826 : = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs, 1)), off);
728 870826 : tidy_after_forward_propagate_addr (use_stmt);
729 : /* Continue propagating into the RHS if this was not the only use. */
730 870826 : if (single_use_p)
731 233610 : return true;
732 : }
733 : /* If the LHS is a plain dereference and the value type is the same as
734 : that of the pointed-to type of the address we can put the
735 : dereferenced address on the LHS preserving the original alias-type. */
736 48290 : else if (integer_zerop (TREE_OPERAND (lhs, 1))
737 18811 : && ((gimple_assign_lhs (use_stmt) == lhs
738 15176 : && useless_type_conversion_p
739 15176 : (TREE_TYPE (TREE_OPERAND (def_rhs, 0)),
740 15176 : TREE_TYPE (gimple_assign_rhs1 (use_stmt))))
741 13871 : || types_compatible_p (TREE_TYPE (lhs),
742 13871 : TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
743 : /* Don't forward anything into clobber stmts if it would result
744 : in the lhs no longer being a MEM_REF. */
745 56502 : && (!gimple_clobber_p (use_stmt)
746 166 : || TREE_CODE (TREE_OPERAND (def_rhs, 0)) == MEM_REF))
747 : {
748 8046 : tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
749 8046 : tree new_offset, new_base, saved, new_lhs;
750 29204 : while (handled_component_p (*def_rhs_basep))
751 13112 : def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
752 8046 : saved = *def_rhs_basep;
753 8046 : if (TREE_CODE (*def_rhs_basep) == MEM_REF)
754 : {
755 3960 : new_base = TREE_OPERAND (*def_rhs_basep, 0);
756 3960 : new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (lhs, 1)),
757 : TREE_OPERAND (*def_rhs_basep, 1));
758 : }
759 : else
760 : {
761 4086 : new_base = build_fold_addr_expr (*def_rhs_basep);
762 4086 : new_offset = TREE_OPERAND (lhs, 1);
763 : }
764 8046 : tree atype = TREE_TYPE (*def_rhs_basep);
765 8046 : if (TYPE_ALIGN (TREE_TYPE (lhs)) < TYPE_ALIGN (atype))
766 312 : atype = build_aligned_type (atype, TYPE_ALIGN (TREE_TYPE (lhs)));
767 8046 : *def_rhs_basep = build2 (MEM_REF, atype, new_base, new_offset);
768 8046 : TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (lhs);
769 8046 : TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (lhs);
770 8046 : TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (lhs);
771 8046 : new_lhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
772 8046 : *lhsp = new_lhs;
773 8046 : TREE_THIS_VOLATILE (new_lhs) = TREE_THIS_VOLATILE (lhs);
774 8046 : TREE_SIDE_EFFECTS (new_lhs) = TREE_SIDE_EFFECTS (lhs);
775 8046 : *def_rhs_basep = saved;
776 8046 : tidy_after_forward_propagate_addr (use_stmt);
777 : /* Continue propagating into the RHS if this was not the
778 : only use. */
779 8046 : if (single_use_p)
780 : return true;
781 : }
782 : else
783 : /* We can have a struct assignment dereferencing our name twice.
784 : Note that we didn't propagate into the lhs to not falsely
785 : claim we did when propagating into the rhs. */
786 : res = false;
787 : }
788 :
789 : /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
790 : nodes from the RHS. */
791 2507049 : tree *rhsp = gimple_assign_rhs1_ptr (use_stmt);
792 2507049 : if (TREE_CODE (*rhsp) == ADDR_EXPR)
793 249494 : rhsp = &TREE_OPERAND (*rhsp, 0);
794 3569644 : while (handled_component_p (*rhsp))
795 1062595 : rhsp = &TREE_OPERAND (*rhsp, 0);
796 2507049 : rhs = *rhsp;
797 :
798 : /* Now see if the RHS node is a MEM_REF using NAME. If so,
799 : propagate the ADDR_EXPR into the use of NAME and fold the result. */
800 2507049 : if (TREE_CODE (rhs) == MEM_REF
801 2507049 : && TREE_OPERAND (rhs, 0) == name)
802 : {
803 1196835 : tree def_rhs_base;
804 1196835 : poly_int64 def_rhs_offset;
805 1196835 : if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
806 : &def_rhs_offset)))
807 : {
808 1159519 : poly_offset_int off = mem_ref_offset (rhs);
809 1159519 : tree new_ptr;
810 1159519 : off += def_rhs_offset;
811 1159519 : if (TREE_CODE (def_rhs_base) == MEM_REF)
812 : {
813 1132115 : off += mem_ref_offset (def_rhs_base);
814 1132115 : new_ptr = TREE_OPERAND (def_rhs_base, 0);
815 : }
816 : else
817 27404 : new_ptr = build_fold_addr_expr (def_rhs_base);
818 1159519 : TREE_OPERAND (rhs, 0) = new_ptr;
819 1159519 : TREE_OPERAND (rhs, 1)
820 1159519 : = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs, 1)), off);
821 1159519 : fold_stmt_inplace (use_stmt_gsi);
822 1159519 : tidy_after_forward_propagate_addr (use_stmt);
823 1159519 : return res;
824 : }
825 : /* If the RHS is a plain dereference and the value type is the same as
826 : that of the pointed-to type of the address we can put the
827 : dereferenced address on the RHS preserving the original alias-type. */
828 37316 : else if (integer_zerop (TREE_OPERAND (rhs, 1))
829 37316 : && ((gimple_assign_rhs1 (use_stmt) == rhs
830 20332 : && useless_type_conversion_p
831 20332 : (TREE_TYPE (gimple_assign_lhs (use_stmt)),
832 20332 : TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
833 22599 : || types_compatible_p (TREE_TYPE (rhs),
834 22599 : TREE_TYPE (TREE_OPERAND (def_rhs, 0)))))
835 : {
836 15348 : tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
837 15348 : tree new_offset, new_base, saved, new_rhs;
838 54604 : while (handled_component_p (*def_rhs_basep))
839 23908 : def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
840 15348 : saved = *def_rhs_basep;
841 15348 : if (TREE_CODE (*def_rhs_basep) == MEM_REF)
842 : {
843 7370 : new_base = TREE_OPERAND (*def_rhs_basep, 0);
844 7370 : new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (rhs, 1)),
845 : TREE_OPERAND (*def_rhs_basep, 1));
846 : }
847 : else
848 : {
849 7978 : new_base = build_fold_addr_expr (*def_rhs_basep);
850 7978 : new_offset = TREE_OPERAND (rhs, 1);
851 : }
852 15348 : tree atype = TREE_TYPE (*def_rhs_basep);
853 15348 : if (TYPE_ALIGN (TREE_TYPE (rhs)) < TYPE_ALIGN (atype))
854 526 : atype = build_aligned_type (atype, TYPE_ALIGN (TREE_TYPE (rhs)));
855 15348 : *def_rhs_basep = build2 (MEM_REF, atype, new_base, new_offset);
856 15348 : TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (rhs);
857 15348 : TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (rhs);
858 15348 : TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (rhs);
859 15348 : new_rhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
860 15348 : *rhsp = new_rhs;
861 15348 : TREE_THIS_VOLATILE (new_rhs) = TREE_THIS_VOLATILE (rhs);
862 15348 : TREE_SIDE_EFFECTS (new_rhs) = TREE_SIDE_EFFECTS (rhs);
863 15348 : *def_rhs_basep = saved;
864 15348 : fold_stmt_inplace (use_stmt_gsi);
865 15348 : tidy_after_forward_propagate_addr (use_stmt);
866 15348 : return res;
867 : }
868 : }
869 :
870 : /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there
871 : is nothing to do. */
872 1332182 : if (gimple_assign_rhs_code (use_stmt) != POINTER_PLUS_EXPR
873 1332182 : || gimple_assign_rhs1 (use_stmt) != name)
874 : return false;
875 :
876 : /* The remaining cases are all for turning pointer arithmetic into
877 : array indexing. They only apply when we have the address of
878 : element zero in an array. If that is not the case then there
879 : is nothing to do. */
880 49226 : array_ref = TREE_OPERAND (def_rhs, 0);
881 49226 : if ((TREE_CODE (array_ref) != ARRAY_REF
882 4657 : || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
883 4657 : || TREE_CODE (TREE_OPERAND (array_ref, 1)) != INTEGER_CST)
884 50735 : && TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE)
885 : return false;
886 :
887 25541 : rhs2 = gimple_assign_rhs2 (use_stmt);
888 : /* Optimize &x[C1] p+ C2 to &x p+ C3 with C3 = C1 * element_size + C2. */
889 25541 : if (TREE_CODE (rhs2) == INTEGER_CST)
890 : {
891 0 : tree new_rhs = build1_loc (gimple_location (use_stmt),
892 0 : ADDR_EXPR, TREE_TYPE (def_rhs),
893 0 : fold_build2 (MEM_REF,
894 : TREE_TYPE (TREE_TYPE (def_rhs)),
895 : unshare_expr (def_rhs),
896 : fold_convert (ptr_type_node,
897 : rhs2)));
898 0 : gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs);
899 0 : use_stmt = gsi_stmt (*use_stmt_gsi);
900 0 : update_stmt (use_stmt);
901 0 : tidy_after_forward_propagate_addr (use_stmt);
902 0 : return true;
903 : }
904 :
905 : return false;
906 : }
907 :
908 : /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
909 :
910 : Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
911 : Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
912 : node or for recovery of array indexing from pointer arithmetic.
913 :
914 : PARENT_SINGLE_USE_P tells if, when in a recursive invocation, NAME was
915 : the single use in the previous invocation. Pass true when calling
916 : this as toplevel.
917 :
918 : Returns true, if all uses have been propagated into. */
919 :
920 : static bool
921 3386368 : forward_propagate_addr_expr (tree name, tree rhs, bool parent_single_use_p)
922 : {
923 3386368 : bool all = true;
924 3386368 : bool single_use_p = parent_single_use_p && has_single_use (name);
925 :
926 17929797 : for (gimple *use_stmt : gather_imm_use_stmts (name))
927 : {
928 7770693 : bool result;
929 7770693 : tree use_rhs;
930 :
931 : /* If the use is not in a simple assignment statement, then
932 : there is nothing we can do. */
933 7770693 : if (!is_gimple_assign (use_stmt))
934 : {
935 4851508 : if (!is_gimple_debug (use_stmt))
936 1981529 : all = false;
937 4851508 : continue;
938 : }
939 :
940 2919185 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
941 2919185 : result = forward_propagate_addr_expr_1 (name, rhs, &gsi,
942 : single_use_p);
943 : /* If the use has moved to a different statement adjust
944 : the update machinery for the old statement too. */
945 2919185 : if (use_stmt != gsi_stmt (gsi))
946 : {
947 0 : update_stmt (use_stmt);
948 0 : use_stmt = gsi_stmt (gsi);
949 : }
950 2919185 : update_stmt (use_stmt);
951 2919185 : all &= result;
952 :
953 : /* Remove intermediate now unused copy and conversion chains. */
954 2919185 : use_rhs = gimple_assign_rhs1 (use_stmt);
955 2919185 : if (result
956 1533227 : && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
957 1282405 : && TREE_CODE (use_rhs) == SSA_NAME
958 3000514 : && has_zero_uses (gimple_assign_lhs (use_stmt)))
959 : {
960 81329 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
961 81329 : fwprop_invalidate_lattice (gimple_get_lhs (use_stmt));
962 81329 : release_defs (use_stmt);
963 81329 : gsi_remove (&gsi, true);
964 : }
965 3386368 : }
966 :
967 3386368 : return all && has_zero_uses (name);
968 : }
969 :
970 :
971 : /* Helper function for simplify_gimple_switch. Remove case labels that
972 : have values outside the range of the new type. */
973 :
974 : static void
975 11871 : simplify_gimple_switch_label_vec (gswitch *stmt, tree index_type,
976 : vec<std::pair<int, int> > &edges_to_remove)
977 : {
978 11871 : unsigned int branch_num = gimple_switch_num_labels (stmt);
979 11871 : auto_vec<tree> labels (branch_num);
980 11871 : unsigned int i, len;
981 :
982 : /* Collect the existing case labels in a VEC, and preprocess it as if
983 : we are gimplifying a GENERIC SWITCH_EXPR. */
984 75115 : for (i = 1; i < branch_num; i++)
985 51373 : labels.quick_push (gimple_switch_label (stmt, i));
986 11871 : preprocess_case_label_vec_for_gimple (labels, index_type, NULL);
987 :
988 : /* If any labels were removed, replace the existing case labels
989 : in the GIMPLE_SWITCH statement with the correct ones.
990 : Note that the type updates were done in-place on the case labels,
991 : so we only have to replace the case labels in the GIMPLE_SWITCH
992 : if the number of labels changed. */
993 11871 : len = labels.length ();
994 11871 : if (len < branch_num - 1)
995 : {
996 0 : bitmap target_blocks;
997 0 : edge_iterator ei;
998 0 : edge e;
999 :
1000 : /* Corner case: *all* case labels have been removed as being
1001 : out-of-range for INDEX_TYPE. Push one label and let the
1002 : CFG cleanups deal with this further. */
1003 0 : if (len == 0)
1004 : {
1005 0 : tree label, elt;
1006 :
1007 0 : label = CASE_LABEL (gimple_switch_default_label (stmt));
1008 0 : elt = build_case_label (build_int_cst (index_type, 0), NULL, label);
1009 0 : labels.quick_push (elt);
1010 0 : len = 1;
1011 : }
1012 :
1013 0 : for (i = 0; i < labels.length (); i++)
1014 0 : gimple_switch_set_label (stmt, i + 1, labels[i]);
1015 0 : for (i++ ; i < branch_num; i++)
1016 0 : gimple_switch_set_label (stmt, i, NULL_TREE);
1017 0 : gimple_switch_set_num_labels (stmt, len + 1);
1018 :
1019 : /* Cleanup any edges that are now dead. */
1020 0 : target_blocks = BITMAP_ALLOC (NULL);
1021 0 : for (i = 0; i < gimple_switch_num_labels (stmt); i++)
1022 : {
1023 0 : tree elt = gimple_switch_label (stmt, i);
1024 0 : basic_block target = label_to_block (cfun, CASE_LABEL (elt));
1025 0 : bitmap_set_bit (target_blocks, target->index);
1026 : }
1027 0 : for (ei = ei_start (gimple_bb (stmt)->succs); (e = ei_safe_edge (ei)); )
1028 : {
1029 0 : if (! bitmap_bit_p (target_blocks, e->dest->index))
1030 0 : edges_to_remove.safe_push (std::make_pair (e->src->index,
1031 0 : e->dest->index));
1032 : else
1033 0 : ei_next (&ei);
1034 : }
1035 0 : BITMAP_FREE (target_blocks);
1036 : }
1037 11871 : }
1038 :
1039 : /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
1040 : the condition which we may be able to optimize better. */
1041 :
1042 : static bool
1043 111316 : simplify_gimple_switch (gswitch *stmt,
1044 : vec<std::pair<int, int> > &edges_to_remove,
1045 : bitmap simple_dce_worklist)
1046 : {
1047 : /* The optimization that we really care about is removing unnecessary
1048 : casts. That will let us do much better in propagating the inferred
1049 : constant at the switch target. */
1050 111316 : tree cond = gimple_switch_index (stmt);
1051 111316 : if (TREE_CODE (cond) == SSA_NAME)
1052 : {
1053 111315 : gimple *def_stmt = SSA_NAME_DEF_STMT (cond);
1054 111315 : if (gimple_assign_cast_p (def_stmt))
1055 : {
1056 12841 : tree def = gimple_assign_rhs1 (def_stmt);
1057 12841 : if (TREE_CODE (def) != SSA_NAME)
1058 : return false;
1059 :
1060 : /* If we have an extension or sign-change that preserves the
1061 : values we check against then we can copy the source value into
1062 : the switch. */
1063 12841 : tree ti = TREE_TYPE (def);
1064 12841 : if (INTEGRAL_TYPE_P (ti)
1065 12841 : && TYPE_PRECISION (ti) <= TYPE_PRECISION (TREE_TYPE (cond)))
1066 : {
1067 12578 : size_t n = gimple_switch_num_labels (stmt);
1068 12578 : tree min = NULL_TREE, max = NULL_TREE;
1069 12578 : if (n > 1)
1070 : {
1071 12578 : min = CASE_LOW (gimple_switch_label (stmt, 1));
1072 12578 : if (CASE_HIGH (gimple_switch_label (stmt, n - 1)))
1073 165 : max = CASE_HIGH (gimple_switch_label (stmt, n - 1));
1074 : else
1075 12413 : max = CASE_LOW (gimple_switch_label (stmt, n - 1));
1076 : }
1077 12578 : if ((!min || int_fits_type_p (min, ti))
1078 12574 : && (!max || int_fits_type_p (max, ti)))
1079 : {
1080 11871 : bitmap_set_bit (simple_dce_worklist,
1081 11871 : SSA_NAME_VERSION (cond));
1082 11871 : gimple_switch_set_index (stmt, def);
1083 11871 : simplify_gimple_switch_label_vec (stmt, ti,
1084 : edges_to_remove);
1085 11871 : update_stmt (stmt);
1086 11871 : return true;
1087 : }
1088 : }
1089 : }
1090 : }
1091 :
1092 : return false;
1093 : }
1094 :
1095 : /* For pointers p2 and p1 return p2 - p1 if the
1096 : difference is known and constant, otherwise return NULL. */
1097 :
1098 : static tree
1099 5485 : constant_pointer_difference (tree p1, tree p2)
1100 : {
1101 5485 : int i, j;
1102 : #define CPD_ITERATIONS 5
1103 5485 : tree exps[2][CPD_ITERATIONS];
1104 5485 : tree offs[2][CPD_ITERATIONS];
1105 5485 : int cnt[2];
1106 :
1107 16455 : for (i = 0; i < 2; i++)
1108 : {
1109 10970 : tree p = i ? p1 : p2;
1110 10970 : tree off = size_zero_node;
1111 10970 : gimple *stmt;
1112 10970 : enum tree_code code;
1113 :
1114 : /* For each of p1 and p2 we need to iterate at least
1115 : twice, to handle ADDR_EXPR directly in p1/p2,
1116 : SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc.
1117 : on definition's stmt RHS. Iterate a few extra times. */
1118 10970 : j = 0;
1119 12760 : do
1120 : {
1121 12760 : if (!POINTER_TYPE_P (TREE_TYPE (p)))
1122 : break;
1123 12754 : if (TREE_CODE (p) == ADDR_EXPR)
1124 : {
1125 9607 : tree q = TREE_OPERAND (p, 0);
1126 9607 : poly_int64 offset;
1127 9607 : tree base = get_addr_base_and_unit_offset (q, &offset);
1128 9607 : if (base)
1129 : {
1130 8815 : q = base;
1131 8815 : if (maybe_ne (offset, 0))
1132 3758 : off = size_binop (PLUS_EXPR, off, size_int (offset));
1133 : }
1134 9607 : if (TREE_CODE (q) == MEM_REF
1135 9607 : && TREE_CODE (TREE_OPERAND (q, 0)) == SSA_NAME)
1136 : {
1137 155 : p = TREE_OPERAND (q, 0);
1138 155 : off = size_binop (PLUS_EXPR, off,
1139 : wide_int_to_tree (sizetype,
1140 : mem_ref_offset (q)));
1141 : }
1142 : else
1143 : {
1144 9452 : exps[i][j] = q;
1145 9452 : offs[i][j++] = off;
1146 9452 : break;
1147 : }
1148 : }
1149 3302 : if (TREE_CODE (p) != SSA_NAME)
1150 : break;
1151 3302 : exps[i][j] = p;
1152 3302 : offs[i][j++] = off;
1153 3302 : if (j == CPD_ITERATIONS)
1154 : break;
1155 3302 : stmt = SSA_NAME_DEF_STMT (p);
1156 3302 : if (!is_gimple_assign (stmt) || gimple_assign_lhs (stmt) != p)
1157 : break;
1158 2643 : code = gimple_assign_rhs_code (stmt);
1159 2643 : if (code == POINTER_PLUS_EXPR)
1160 : {
1161 1390 : if (TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
1162 : break;
1163 863 : off = size_binop (PLUS_EXPR, off, gimple_assign_rhs2 (stmt));
1164 863 : p = gimple_assign_rhs1 (stmt);
1165 : }
1166 : else if (code == ADDR_EXPR || CONVERT_EXPR_CODE_P (code))
1167 927 : p = gimple_assign_rhs1 (stmt);
1168 : else
1169 : break;
1170 : }
1171 : while (1);
1172 10970 : cnt[i] = j;
1173 : }
1174 :
1175 7469 : for (i = 0; i < cnt[0]; i++)
1176 9724 : for (j = 0; j < cnt[1]; j++)
1177 7740 : if (exps[0][i] == exps[1][j])
1178 4600 : return size_binop (MINUS_EXPR, offs[0][i], offs[1][j]);
1179 :
1180 : return NULL_TREE;
1181 : }
1182 :
1183 : /* Helper function for optimize_aggr_zeroprop.
1184 : Props the zeroing (memset, VAL) that was done in DEST+OFFSET:LEN
1185 : (DEFSTMT) into the STMT. Returns true if the STMT was updated. */
1186 : static void
1187 22689757 : optimize_aggr_zeroprop_1 (gimple *defstmt, gimple *stmt,
1188 : tree dest, poly_int64 offset, tree val,
1189 : poly_offset_int len)
1190 : {
1191 22689757 : tree src2;
1192 22689757 : tree len2 = NULL_TREE;
1193 22689757 : poly_int64 offset2;
1194 :
1195 22689757 : if (gimple_call_builtin_p (stmt, BUILT_IN_MEMCPY)
1196 22568 : && TREE_CODE (gimple_call_arg (stmt, 1)) == ADDR_EXPR
1197 22703857 : && poly_int_tree_p (gimple_call_arg (stmt, 2)))
1198 : {
1199 13024 : src2 = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
1200 13024 : len2 = gimple_call_arg (stmt, 2);
1201 : }
1202 22676733 : else if (gimple_assign_load_p (stmt) && gimple_store_p (stmt))
1203 : {
1204 1913841 : src2 = gimple_assign_rhs1 (stmt);
1205 1913841 : len2 = (TREE_CODE (src2) == COMPONENT_REF
1206 1913841 : ? DECL_SIZE_UNIT (TREE_OPERAND (src2, 1))
1207 1745660 : : TYPE_SIZE_UNIT (TREE_TYPE (src2)));
1208 : /* Can only handle zero memsets. */
1209 1913841 : if (!integer_zerop (val))
1210 22667967 : return;
1211 : }
1212 : else
1213 : return;
1214 :
1215 1925839 : if (len2 == NULL_TREE
1216 1925839 : || !poly_int_tree_p (len2))
1217 : return;
1218 :
1219 1925839 : src2 = get_addr_base_and_unit_offset (src2, &offset2);
1220 1925839 : if (src2 == NULL_TREE
1221 1925839 : || maybe_lt (offset2, offset))
1222 : return;
1223 :
1224 876059 : if (!operand_equal_p (dest, src2, 0))
1225 : return;
1226 :
1227 : /* [ dest + offset, dest + offset + len - 1 ] is set to val.
1228 : Make sure that
1229 : [ dest + offset2, dest + offset2 + len2 - 1 ] is a subset of that. */
1230 131611 : if (maybe_gt (wi::to_poly_offset (len2) + (offset2 - offset),
1231 : len))
1232 : return;
1233 :
1234 21790 : if (dump_file && (dump_flags & TDF_DETAILS))
1235 : {
1236 32 : fprintf (dump_file, "Simplified\n ");
1237 32 : print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1238 32 : fprintf (dump_file, "after previous\n ");
1239 32 : print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
1240 : }
1241 21790 : gimple *orig_stmt = stmt;
1242 : /* For simplicity, don't change the kind of the stmt,
1243 : turn dest = src; into dest = {}; and memcpy (&dest, &src, len);
1244 : into memset (&dest, val, len);
1245 : In theory we could change dest = src into memset if dest
1246 : is addressable (maybe beneficial if val is not 0), or
1247 : memcpy (&dest, &src, len) into dest = {} if len is the size
1248 : of dest, dest isn't volatile. */
1249 21790 : if (is_gimple_assign (stmt))
1250 : {
1251 21785 : tree ctor_type = TREE_TYPE (gimple_assign_lhs (stmt));
1252 21785 : tree ctor = build_constructor (ctor_type, NULL);
1253 21785 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1254 21785 : gimple_assign_set_rhs_from_tree (&gsi, ctor);
1255 21785 : update_stmt (stmt);
1256 21785 : statistics_counter_event (cfun, "copy zeroing propagation of aggregate", 1);
1257 : }
1258 : else /* If stmt is memcpy, transform it into memset. */
1259 : {
1260 5 : gcall *call = as_a <gcall *> (stmt);
1261 5 : tree fndecl = builtin_decl_implicit (BUILT_IN_MEMSET);
1262 5 : gimple_call_set_fndecl (call, fndecl);
1263 5 : gimple_call_set_fntype (call, TREE_TYPE (fndecl));
1264 5 : gimple_call_set_arg (call, 1, val);
1265 5 : update_stmt (stmt);
1266 5 : statistics_counter_event (cfun, "memcpy to memset changed", 1);
1267 : }
1268 :
1269 21790 : if (dump_file && (dump_flags & TDF_DETAILS))
1270 : {
1271 32 : fprintf (dump_file, "into\n ");
1272 32 : print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1273 : }
1274 :
1275 : /* Mark the bb for eh cleanup if needed. */
1276 21790 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
1277 6 : bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
1278 : }
1279 :
1280 : /* Optimize
1281 : a = {}; // DEST = value ;; LEN(nullptr)
1282 : b = a;
1283 : into
1284 : a = {};
1285 : b = {};
1286 : Similarly for memset (&a, ..., sizeof (a)); instead of a = {};
1287 : and/or memcpy (&b, &a, sizeof (a)); instead of b = a; */
1288 :
1289 : static void
1290 31814809 : optimize_aggr_zeroprop (gimple *stmt, bool full_walk)
1291 : {
1292 31814809 : ao_ref read;
1293 63629618 : if (gimple_has_volatile_ops (stmt))
1294 27718974 : return;
1295 :
1296 30883987 : tree dest = NULL_TREE;
1297 30883987 : tree val = integer_zero_node;
1298 30883987 : tree len = NULL_TREE;
1299 30883987 : bool can_use_tbba = true;
1300 :
1301 30883987 : if (gimple_call_builtin_p (stmt, BUILT_IN_MEMSET)
1302 112887 : && TREE_CODE (gimple_call_arg (stmt, 0)) == ADDR_EXPR
1303 57516 : && TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST
1304 30939200 : && poly_int_tree_p (gimple_call_arg (stmt, 2)))
1305 : {
1306 52447 : dest = TREE_OPERAND (gimple_call_arg (stmt, 0), 0);
1307 52447 : len = gimple_call_arg (stmt, 2);
1308 52447 : val = gimple_call_arg (stmt, 1);
1309 52447 : ao_ref_init_from_ptr_and_size (&read, gimple_call_arg (stmt, 0), len);
1310 52447 : can_use_tbba = false;
1311 : }
1312 30831540 : else if (gimple_store_p (stmt)
1313 30770940 : && gimple_assign_single_p (stmt)
1314 61602480 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == STRING_CST)
1315 : {
1316 31006 : tree str = gimple_assign_rhs1 (stmt);
1317 31006 : dest = gimple_assign_lhs (stmt);
1318 31006 : ao_ref_init (&read, dest);
1319 : /* The string must contain all null char's for now. */
1320 67106 : for (int i = 0; i < TREE_STRING_LENGTH (str); i++)
1321 : {
1322 33705 : if (TREE_STRING_POINTER (str)[i] != 0)
1323 : {
1324 : dest = NULL_TREE;
1325 : break;
1326 : }
1327 : }
1328 : }
1329 : /* A store of integer (scalar, vector or complex) zeros is
1330 : a zero store. */
1331 30800534 : else if (gimple_store_p (stmt)
1332 30739934 : && gimple_assign_single_p (stmt)
1333 61540468 : && integer_zerop (gimple_assign_rhs1 (stmt)))
1334 : {
1335 3609477 : tree rhs = gimple_assign_rhs1 (stmt);
1336 3609477 : tree type = TREE_TYPE (rhs);
1337 3609477 : dest = gimple_assign_lhs (stmt);
1338 3609477 : ao_ref_init (&read, dest);
1339 : /* For integral types, the type precision needs to be a multiply of BITS_PER_UNIT. */
1340 3609477 : if (INTEGRAL_TYPE_P (type)
1341 3609477 : && (TYPE_PRECISION (type) % BITS_PER_UNIT) != 0)
1342 : dest = NULL_TREE;
1343 : }
1344 27191057 : else if (gimple_store_p (stmt)
1345 27130457 : && gimple_assign_single_p (stmt)
1346 27130457 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == CONSTRUCTOR
1347 27922478 : && !gimple_clobber_p (stmt))
1348 : {
1349 731421 : dest = gimple_assign_lhs (stmt);
1350 731421 : ao_ref_init (&read, dest);
1351 : }
1352 :
1353 4206239 : if (dest == NULL_TREE)
1354 : return;
1355 :
1356 4177628 : if (len == NULL_TREE)
1357 4125181 : len = (TREE_CODE (dest) == COMPONENT_REF
1358 4125181 : ? DECL_SIZE_UNIT (TREE_OPERAND (dest, 1))
1359 1803349 : : TYPE_SIZE_UNIT (TREE_TYPE (dest)));
1360 4125181 : if (len == NULL_TREE
1361 4177628 : || !poly_int_tree_p (len))
1362 : return;
1363 :
1364 : /* Sometimes memset can have no vdef due to invalid declaration of memset (const, etc.). */
1365 36074206 : if (!gimple_vdef (stmt))
1366 : return;
1367 :
1368 : /* This store needs to be on the byte boundary and pointing to an object. */
1369 4177604 : poly_int64 offset;
1370 4177604 : tree dest_base = get_addr_base_and_unit_offset (dest, &offset);
1371 4177604 : if (dest_base == NULL_TREE)
1372 : return;
1373 :
1374 : /* Setup the worklist. */
1375 4095835 : auto_vec<std::pair<tree, unsigned>> worklist;
1376 4095835 : unsigned limit = full_walk ? param_sccvn_max_alias_queries_per_access : 0;
1377 8191670 : worklist.safe_push (std::make_pair (gimple_vdef (stmt), limit));
1378 :
1379 28325498 : while (!worklist.is_empty ())
1380 : {
1381 20133828 : std::pair<tree, unsigned> top = worklist.pop ();
1382 20133828 : tree vdef = top.first;
1383 20133828 : limit = top.second;
1384 20133828 : gimple *use_stmt;
1385 20133828 : imm_use_iterator iter;
1386 44880815 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
1387 : {
1388 : /* Handling PHI nodes might not be worth it so don't. */
1389 24746987 : if (is_a <gphi*> (use_stmt))
1390 2057230 : continue;
1391 :
1392 : /* If this statement does not clobber add the vdef stmt to the
1393 : worklist.
1394 : After hitting the limit, allow clobbers to able to pass through. */
1395 2080322 : if ((limit != 0 || gimple_clobber_p (use_stmt))
1396 20646578 : && gimple_vdef (use_stmt)
1397 40183238 : && !stmt_may_clobber_ref_p_1 (use_stmt, &read,
1398 : /* tbaa_p = */ can_use_tbba))
1399 : {
1400 16037993 : unsigned new_limit = limit == 0 ? 0 : limit - 1;
1401 32075986 : worklist.safe_push (std::make_pair (gimple_vdef (use_stmt),
1402 : new_limit));
1403 : }
1404 :
1405 22689757 : optimize_aggr_zeroprop_1 (stmt, use_stmt, dest_base, offset,
1406 22689757 : val, wi::to_poly_offset (len));
1407 20133828 : }
1408 : }
1409 :
1410 4095835 : }
1411 :
1412 : /* Returns the pointer to the base of the object of the
1413 : reference EXPR and extracts the information about
1414 : the offset of the access, storing it to PBYTESIZE,
1415 : PBYTEPOS and PREVERSEP.
1416 : If the access is not a byte sized or position is not
1417 : on the byte, return NULL. */
1418 : static tree
1419 5337824 : split_core_and_offset_size (tree expr,
1420 : poly_int64 *pbytesize, poly_int64 *pbytepos,
1421 : tree *poffset, int *preversep)
1422 : {
1423 5337824 : tree core;
1424 5337824 : machine_mode mode;
1425 5337824 : int unsignedp, volatilep;
1426 5337824 : poly_int64 bitsize;
1427 5337824 : poly_int64 bitpos;
1428 5337824 : location_t loc = EXPR_LOCATION (expr);
1429 :
1430 5337824 : core = get_inner_reference (expr, &bitsize, &bitpos,
1431 : poffset, &mode, &unsignedp, preversep,
1432 : &volatilep);
1433 10675648 : if (!multiple_p (bitsize, BITS_PER_UNIT, pbytesize))
1434 : return NULL_TREE;
1435 5337824 : if (!multiple_p (bitpos, BITS_PER_UNIT, pbytepos))
1436 : return NULL_TREE;
1437 : /* If we are left with MEM[a + CST] strip that and add it to the
1438 : pbytepos and return a. */
1439 5337824 : if (TREE_CODE (core) == MEM_REF)
1440 : {
1441 1257054 : poly_offset_int tem;
1442 1257054 : tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
1443 1257054 : tem += *pbytepos;
1444 1257054 : if (tem.to_shwi (pbytepos))
1445 1254827 : return TREE_OPERAND (core, 0);
1446 : }
1447 4082997 : core = build_fold_addr_expr_loc (loc, core);
1448 4082997 : STRIP_NOPS (core);
1449 4082997 : return core;
1450 : }
1451 :
1452 : /* Returns a new src based on the
1453 : copy `DEST = SRC` and for the old SRC2.
1454 : Returns null if SRC2 is not related to DEST. */
1455 :
1456 : static tree
1457 1277770 : new_src_based_on_copy (tree src2, tree dest, tree src)
1458 : {
1459 : /* If the second src is not exactly the same as dest,
1460 : try to handle it separately; see it is address/size equivalent.
1461 : Handles `a` and `a.b` and `MEM<char[N]>(&a)` which all have
1462 : the same size and offsets as address/size equivalent.
1463 : This allows copying over a memcpy and also one for copying
1464 : where one field is the same size as the whole struct. */
1465 1277770 : if (operand_equal_p (dest, src2))
1466 : return src;
1467 : /* if both dest and src2 are decls, then we know these 2
1468 : accesses can't be the same. */
1469 725144 : if (DECL_P (dest) && DECL_P (src2))
1470 : return NULL_TREE;
1471 : /* A VCE can't be used with imag/real or BFR so reject them early. */
1472 380166 : if (TREE_CODE (src) == IMAGPART_EXPR
1473 380166 : || TREE_CODE (src) == REALPART_EXPR
1474 380166 : || TREE_CODE (src) == BIT_FIELD_REF)
1475 : return NULL_TREE;
1476 380166 : tree core1, core2;
1477 380166 : poly_int64 bytepos1, bytepos2;
1478 380166 : poly_int64 bytesize1, bytesize2;
1479 380166 : tree toffset1, toffset2;
1480 380166 : int reversep1 = 0;
1481 380166 : int reversep2 = 0;
1482 380166 : poly_int64 diff = 0;
1483 380166 : core1 = split_core_and_offset_size (dest, &bytesize1, &bytepos1,
1484 : &toffset1, &reversep1);
1485 380166 : core2 = split_core_and_offset_size (src2, &bytesize2, &bytepos2,
1486 : &toffset2, &reversep2);
1487 380166 : if (!core1 || !core2)
1488 : return NULL_TREE;
1489 380166 : if (reversep1 != reversep2)
1490 : return NULL_TREE;
1491 : /* The sizes of the 2 accesses need to be the same. */
1492 380166 : if (!known_eq (bytesize1, bytesize2))
1493 : return NULL_TREE;
1494 172765 : if (!operand_equal_p (core1, core2, 0))
1495 : return NULL_TREE;
1496 :
1497 23576 : if (toffset1 && toffset2)
1498 : {
1499 2 : tree type = TREE_TYPE (toffset1);
1500 2 : if (type != TREE_TYPE (toffset2))
1501 0 : toffset2 = fold_convert (type, toffset2);
1502 :
1503 2 : tree tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
1504 2 : if (!cst_and_fits_in_hwi (tdiff))
1505 : return NULL_TREE;
1506 :
1507 0 : diff = int_cst_value (tdiff);
1508 0 : }
1509 23574 : else if (toffset1 || toffset2)
1510 : {
1511 : /* If only one of the offsets is non-constant, the difference cannot
1512 : be a constant. */
1513 : return NULL_TREE;
1514 : }
1515 23542 : diff += bytepos1 - bytepos2;
1516 : /* The offset between the 2 need to be 0. */
1517 23542 : if (!known_eq (diff, 0))
1518 : return NULL_TREE;
1519 22704 : return fold_build1 (VIEW_CONVERT_EXPR,TREE_TYPE (src2), src);
1520 : }
1521 :
1522 : /* Returns true if SRC and DEST are the same address such that
1523 : `SRC == DEST;` is considered a nop. This is more than an
1524 : operand_equal_p check as it needs to be similar to
1525 : new_src_based_on_copy. */
1526 :
1527 : static bool
1528 4547734 : same_for_assignment (tree src, tree dest)
1529 : {
1530 4547734 : if (operand_equal_p (dest, src, 0))
1531 : return true;
1532 : /* if both dest and src2 are decls, then we know these 2
1533 : accesses can't be the same. */
1534 4544825 : if (DECL_P (dest) && DECL_P (src))
1535 : return false;
1536 :
1537 2288746 : tree core1, core2;
1538 2288746 : poly_int64 bytepos1, bytepos2;
1539 2288746 : poly_int64 bytesize1, bytesize2;
1540 2288746 : tree toffset1, toffset2;
1541 2288746 : int reversep1 = 0;
1542 2288746 : int reversep2 = 0;
1543 2288746 : poly_int64 diff = 0;
1544 2288746 : core1 = split_core_and_offset_size (dest, &bytesize1, &bytepos1,
1545 : &toffset1, &reversep1);
1546 2288746 : core2 = split_core_and_offset_size (src, &bytesize2, &bytepos2,
1547 : &toffset2, &reversep2);
1548 2288746 : if (!core1 || !core2)
1549 : return false;
1550 2288746 : if (reversep1 != reversep2)
1551 : return false;
1552 : /* The sizes of the 2 accesses need to be the same. */
1553 2288746 : if (!known_eq (bytesize1, bytesize2))
1554 : return false;
1555 2287845 : if (!operand_equal_p (core1, core2, 0))
1556 : return false;
1557 6176 : if (toffset1 && toffset2)
1558 : {
1559 313 : tree type = TREE_TYPE (toffset1);
1560 313 : if (type != TREE_TYPE (toffset2))
1561 0 : toffset2 = fold_convert (type, toffset2);
1562 :
1563 313 : tree tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
1564 313 : if (!cst_and_fits_in_hwi (tdiff))
1565 : return false;
1566 :
1567 0 : diff = int_cst_value (tdiff);
1568 0 : }
1569 5863 : else if (toffset1 || toffset2)
1570 : {
1571 : /* If only one of the offsets is non-constant, the difference cannot
1572 : be a constant. */
1573 : return false;
1574 : }
1575 5863 : diff += bytepos1 - bytepos2;
1576 : /* The offset between the 2 need to be 0. */
1577 5863 : if (!known_eq (diff, 0))
1578 5617 : return false;
1579 : return true;
1580 : }
1581 :
1582 : /* Helper function for optimize_agr_copyprop.
1583 : For aggregate copies in USE_STMT, see if DEST
1584 : is on the lhs of USE_STMT and replace it with SRC. */
1585 : static void
1586 1032367 : optimize_agr_copyprop_1 (gimple *stmt, gimple *use_stmt,
1587 : tree dest, tree src)
1588 : {
1589 1032367 : gcc_assert (gimple_assign_load_p (use_stmt)
1590 : && gimple_store_p (use_stmt));
1591 2064734 : if (gimple_has_volatile_ops (use_stmt))
1592 615154 : return;
1593 1032366 : tree dest2 = gimple_assign_lhs (use_stmt);
1594 1032366 : tree src2 = gimple_assign_rhs1 (use_stmt);
1595 : /* If the new store is `src2 = src2;` skip over it. */
1596 1032366 : if (same_for_assignment (src2, dest2))
1597 : return;
1598 1031803 : src = new_src_based_on_copy (src2, dest, src);
1599 1031803 : if (!src)
1600 : return;
1601 : /* For 2 memory references and using a temporary to do the copy,
1602 : don't remove the temporary as the 2 memory references might overlap.
1603 : Note t does not need to be decl as it could be field.
1604 : See PR 22237 for full details.
1605 : E.g.
1606 : t = *a; #DEST = SRC;
1607 : *b = t; #DEST2 = SRC2;
1608 : Cannot be convert into
1609 : t = *a;
1610 : *b = *a;
1611 : Though the following is allowed to be done:
1612 : t = *a;
1613 : *a = t;
1614 : And convert it into:
1615 : t = *a;
1616 : *a = *a;
1617 : */
1618 445907 : if (!operand_equal_p (dest2, src, 0)
1619 445907 : && !DECL_P (dest2) && !DECL_P (src))
1620 : {
1621 : /* If *a and *b have the same base see if
1622 : the offset between the two is greater than
1623 : or equal to the size of the type. */
1624 32039 : poly_int64 offset1, offset2;
1625 32039 : tree len = TYPE_SIZE_UNIT (TREE_TYPE (src));
1626 32039 : if (len == NULL_TREE
1627 32039 : || !tree_fits_poly_int64_p (len))
1628 28694 : return;
1629 32039 : tree base1 = get_addr_base_and_unit_offset (dest2, &offset1);
1630 32039 : tree base2 = get_addr_base_and_unit_offset (src, &offset2);
1631 32039 : poly_int64 size = tree_to_poly_int64 (len);
1632 : /* If the bases are 2 different decls,
1633 : then there can be no overlapping. */
1634 32039 : if (base1 && base2
1635 31118 : && DECL_P (base1) && DECL_P (base2)
1636 1862 : && base1 != base2)
1637 : ;
1638 : /* If we can't figure out the base or the bases are
1639 : not equal then fall back to an alignment check. */
1640 30406 : else if (!base1
1641 30406 : || !base2
1642 30406 : || !operand_equal_p (base1, base2))
1643 : {
1644 30034 : unsigned int align1 = get_object_alignment (src);
1645 30034 : unsigned int align2 = get_object_alignment (dest2);
1646 30034 : align1 /= BITS_PER_UNIT;
1647 30034 : align2 /= BITS_PER_UNIT;
1648 : /* If the alignment of either object is less
1649 : than the size then there is a possibility
1650 : of overlapping. */
1651 30034 : if (maybe_lt (align1, size)
1652 30034 : || maybe_lt (align2, size))
1653 28694 : return;
1654 : }
1655 : /* Make sure [offset1, offset1 + len - 1] does
1656 : not overlap with [offset2, offset2 + len - 1],
1657 : it is ok if they are at the same location though. */
1658 372 : else if (ranges_maybe_overlap_p (offset1, size, offset2, size)
1659 372 : && !known_eq (offset2, offset1))
1660 : return;
1661 : }
1662 :
1663 417213 : if (dump_file && (dump_flags & TDF_DETAILS))
1664 : {
1665 11 : fprintf (dump_file, "Simplified\n ");
1666 11 : print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
1667 11 : fprintf (dump_file, "after previous\n ");
1668 11 : print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1669 : }
1670 417213 : gimple *orig_stmt = use_stmt;
1671 417213 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1672 417213 : gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (src));
1673 417213 : update_stmt (use_stmt);
1674 :
1675 417213 : if (dump_file && (dump_flags & TDF_DETAILS))
1676 : {
1677 11 : fprintf (dump_file, "into\n ");
1678 11 : print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
1679 : }
1680 417213 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, use_stmt))
1681 0 : bitmap_set_bit (to_purge, gimple_bb (use_stmt)->index);
1682 417213 : statistics_counter_event (cfun, "copy prop for aggregate", 1);
1683 : }
1684 :
1685 : /* Helper function for optimize_agr_copyprop_1, propagate aggregates
1686 : into the arguments of USE_STMT if the argument matches with DEST;
1687 : replacing it with SRC. */
1688 : static void
1689 714722 : optimize_agr_copyprop_arg (gimple *defstmt, gcall *call,
1690 : tree dest, tree src)
1691 : {
1692 714722 : bool changed = false;
1693 2396381 : for (unsigned arg = 0; arg < gimple_call_num_args (call); arg++)
1694 : {
1695 1681659 : tree *argptr = gimple_call_arg_ptr (call, arg);
1696 3170720 : if (TREE_CODE (*argptr) == SSA_NAME
1697 958180 : || is_gimple_min_invariant (*argptr)
1698 1874257 : || TYPE_VOLATILE (TREE_TYPE (*argptr)))
1699 1489061 : continue;
1700 192598 : tree newsrc = new_src_based_on_copy (*argptr, dest, src);
1701 192598 : if (!newsrc)
1702 116454 : continue;
1703 :
1704 76144 : if (dump_file && (dump_flags & TDF_DETAILS))
1705 : {
1706 9 : fprintf (dump_file, "Simplified\n ");
1707 9 : print_gimple_stmt (dump_file, call, 0, dump_flags);
1708 9 : fprintf (dump_file, "after previous\n ");
1709 9 : print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
1710 : }
1711 76144 : *argptr = unshare_expr (newsrc);
1712 76144 : changed = true;
1713 76144 : if (dump_file && (dump_flags & TDF_DETAILS))
1714 : {
1715 9 : fprintf (dump_file, "into\n ");
1716 9 : print_gimple_stmt (dump_file, call, 0, dump_flags);
1717 : }
1718 : }
1719 714722 : if (changed)
1720 75966 : update_stmt (call);
1721 714722 : }
1722 :
1723 : /* Helper function for optimize_agr_copyprop, propagate aggregates
1724 : into the return stmt USE if the operand of the return matches DEST;
1725 : replacing it with SRC. */
1726 : static void
1727 129787 : optimize_agr_copyprop_return (gimple *defstmt, greturn *use,
1728 : tree dest, tree src)
1729 : {
1730 129787 : tree rvalue = gimple_return_retval (use);
1731 129787 : if (!rvalue
1732 84369 : || TREE_CODE (rvalue) == SSA_NAME
1733 75459 : || is_gimple_min_invariant (rvalue)
1734 204823 : || TYPE_VOLATILE (TREE_TYPE (rvalue)))
1735 : return;
1736 :
1737 : /* `return <retval>;` is already the best it could be.
1738 : Likewise `return *<retval>_N(D)`. */
1739 75035 : if (TREE_CODE (rvalue) == RESULT_DECL
1740 75035 : || (TREE_CODE (rvalue) == MEM_REF
1741 0 : && TREE_CODE (TREE_OPERAND (rvalue, 0)) == SSA_NAME
1742 0 : && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (rvalue, 0)))
1743 : == RESULT_DECL))
1744 : return;
1745 53369 : tree newsrc = new_src_based_on_copy (rvalue, dest, src);
1746 53369 : if (!newsrc)
1747 : return;
1748 : /* Currently only support non-global vars.
1749 : See PR 124099 on enumtls not supporting expanding for GIMPLE_RETURN.
1750 : FIXME: could support VCEs too? */
1751 53279 : if (!VAR_P (newsrc) || is_global_var (newsrc))
1752 : return;
1753 26871 : if (dump_file && (dump_flags & TDF_DETAILS))
1754 : {
1755 1 : fprintf (dump_file, "Simplified\n ");
1756 1 : print_gimple_stmt (dump_file, use, 0, dump_flags);
1757 1 : fprintf (dump_file, "after previous\n ");
1758 1 : print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
1759 : }
1760 26871 : gimple_return_set_retval (use, newsrc);
1761 26871 : if (dump_file && (dump_flags & TDF_DETAILS))
1762 : {
1763 1 : fprintf (dump_file, "into\n ");
1764 1 : print_gimple_stmt (dump_file, use, 0, dump_flags);
1765 : }
1766 26871 : update_stmt (use);
1767 : }
1768 :
1769 : /* Optimizes
1770 : DEST = SRC;
1771 : DEST2 = DEST; # DEST2 = SRC2;
1772 : into
1773 : DEST = SRC;
1774 : DEST2 = SRC;
1775 : STMT is the first statement and SRC is the common
1776 : between the statements.
1777 :
1778 : Also optimizes:
1779 : DEST = SRC;
1780 : call_func(..., DEST, ...);
1781 : into:
1782 : DEST = SRC;
1783 : call_func(..., SRC, ...);
1784 :
1785 : */
1786 : static void
1787 3924556 : optimize_agr_copyprop (gimple *stmt)
1788 : {
1789 7849112 : if (gimple_has_volatile_ops (stmt))
1790 411780 : return;
1791 :
1792 : /* Can't prop if the statement could throw. */
1793 3923345 : if (stmt_could_throw_p (cfun, stmt))
1794 : return;
1795 :
1796 3515368 : tree dest = gimple_assign_lhs (stmt);
1797 3515368 : tree src = gimple_assign_rhs1 (stmt);
1798 : /* If the statement is `src = src;` then ignore it. */
1799 3515368 : if (same_for_assignment (dest, src))
1800 : return;
1801 :
1802 3512776 : tree vdef = gimple_vdef (stmt);
1803 3512776 : imm_use_iterator iter;
1804 3512776 : gimple *use_stmt;
1805 10130910 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
1806 : {
1807 6618134 : if (gimple_assign_load_p (use_stmt)
1808 6618134 : && gimple_store_p (use_stmt))
1809 1032367 : optimize_agr_copyprop_1 (stmt, use_stmt, dest, src);
1810 5585767 : else if (is_gimple_call (use_stmt))
1811 714722 : optimize_agr_copyprop_arg (stmt, as_a<gcall*>(use_stmt), dest, src);
1812 4871045 : else if (is_a<greturn*> (use_stmt))
1813 129787 : optimize_agr_copyprop_return (stmt, as_a<greturn*>(use_stmt), dest, src);
1814 3512776 : }
1815 : }
1816 :
1817 : /* Simple DSE of the lhs from a clobber STMT.
1818 : This is used mostly to clean up from optimize_agr_copyprop and
1819 : to remove (exactly one) extra copy that might later on confuse SRA.
1820 : An example is:
1821 : ;; write to a and such.
1822 : b = a; // This statement is to be removed
1823 : b = {CLOBBER};
1824 : SRA will totally scalarize b (which means also a) here for the extra copy
1825 : which is not something welcomed. So removing the copy will
1826 : allow SRA to move the scalarization of a further down or not at all.
1827 : */
1828 : static void
1829 7651081 : do_simple_agr_dse (gassign *stmt, bool full_walk)
1830 : {
1831 : /* Don't do this while in -Og as we want to keep around the copy
1832 : for debuggability. */
1833 7651081 : if (optimize_debug)
1834 5290547 : return;
1835 7647536 : ao_ref read;
1836 7647536 : basic_block bb = gimple_bb (stmt);
1837 7647536 : tree lhs = gimple_assign_lhs (stmt);
1838 : /* Only handle clobbers of a full decl. */
1839 7647536 : if (!DECL_P (lhs))
1840 : return;
1841 6894988 : ao_ref_init (&read, lhs);
1842 6894988 : tree vuse = gimple_vuse (stmt);
1843 6894988 : unsigned limit = full_walk ? param_sccvn_max_alias_queries_per_access : 4;
1844 17427328 : while (limit)
1845 : {
1846 17413970 : gimple *ostmt = SSA_NAME_DEF_STMT (vuse);
1847 : /* Don't handle phis, just declare to be done. */
1848 17413970 : if (is_a<gphi*>(ostmt) || gimple_nop_p (ostmt))
1849 : break;
1850 15066794 : basic_block obb = gimple_bb (ostmt);
1851 : /* If the clobber is not fully dominating the statement define,
1852 : then it is not "simple" to detect if the define is fully clobbered. */
1853 15066794 : if (obb != bb && !dominated_by_p (CDI_DOMINATORS, bb, obb))
1854 4534454 : return;
1855 15066794 : gimple *use_stmt;
1856 15066794 : imm_use_iterator iter;
1857 45868342 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, gimple_vdef (ostmt))
1858 : {
1859 17752035 : basic_block ubb = gimple_bb (use_stmt);
1860 17752035 : if (stmt == use_stmt)
1861 5300871 : continue;
1862 : /* If the use is a clobber for lhs,
1863 : then it can be safely skipped; this happens with eh
1864 : and sometimes jump threading. */
1865 12451164 : if (gimple_clobber_p (use_stmt)
1866 12451164 : && lhs == gimple_assign_lhs (use_stmt))
1867 176629 : continue;
1868 : /* If the use is a phi and it is single use then check if that single use
1869 : is a clobber and lhs is the same. */
1870 12274535 : if (gphi *use_phi = dyn_cast<gphi*>(use_stmt))
1871 : {
1872 373652 : use_operand_p ou;
1873 373652 : gimple *ostmt;
1874 373652 : if (single_imm_use (gimple_phi_result (use_phi), &ou, &ostmt)
1875 312909 : && gimple_clobber_p (ostmt)
1876 626994 : && lhs == gimple_assign_lhs (ostmt))
1877 69417 : continue;
1878 : /* A phi node will never be dominating the clobber. */
1879 304235 : return;
1880 : }
1881 : /* The use needs to be dominating the clobber. */
1882 1519603 : if ((ubb != bb && !dominated_by_p (CDI_DOMINATORS, bb, ubb))
1883 12663645 : || ref_maybe_used_by_stmt_p (use_stmt, &read, false))
1884 : return;
1885 : /* Count the above alias lookup towards the limit. */
1886 10662010 : limit--;
1887 10662010 : if (limit == 0)
1888 : return;
1889 2017281 : }
1890 13049513 : vuse = gimple_vuse (ostmt);
1891 : /* This is a call with an assignment to the clobber decl,
1892 : remove the lhs or the whole stmt if it was pure/const. */
1893 13049513 : if (is_a <gcall*>(ostmt)
1894 13049513 : && lhs == gimple_call_lhs (ostmt))
1895 : {
1896 : /* Don't remove stores/statements that are needed for non-call
1897 : eh to work. */
1898 4042 : if (stmt_unremovable_because_of_non_call_eh_p (cfun, ostmt))
1899 : return;
1900 : /* If we delete a stmt that could throw, mark the block
1901 : in to_purge to cleanup afterwards. */
1902 4036 : if (stmt_could_throw_p (cfun, ostmt))
1903 1035 : bitmap_set_bit (to_purge, obb->index);
1904 4036 : int flags = gimple_call_flags (ostmt);
1905 4036 : if ((flags & (ECF_PURE|ECF_CONST|ECF_NOVOPS))
1906 205 : && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
1907 : {
1908 121 : gimple_stmt_iterator gsi = gsi_for_stmt (ostmt);
1909 121 : if (dump_file && (dump_flags & TDF_DETAILS))
1910 : {
1911 14 : fprintf (dump_file, "Removing dead call store stmt ");
1912 14 : print_gimple_stmt (dump_file, ostmt, 0);
1913 14 : fprintf (dump_file, "\n");
1914 : }
1915 121 : unlink_stmt_vdef (ostmt);
1916 121 : release_defs (ostmt);
1917 121 : gsi_remove (&gsi, true);
1918 121 : statistics_counter_event (cfun, "delete call dead store", 1);
1919 : /* Only remove the first store previous statement. */
1920 121 : return;
1921 : }
1922 : /* Make sure we do not remove a return slot we cannot reconstruct
1923 : later. */
1924 3915 : if (gimple_call_return_slot_opt_p (as_a <gcall *>(ostmt))
1925 3915 : && (TREE_ADDRESSABLE (TREE_TYPE (gimple_call_fntype (ostmt)))
1926 545 : || !poly_int_tree_p
1927 545 : (TYPE_SIZE (TREE_TYPE (gimple_call_fntype (ostmt))))))
1928 : return;
1929 682 : if (dump_file && (dump_flags & TDF_DETAILS))
1930 : {
1931 6 : fprintf (dump_file, "Removing lhs of call stmt ");
1932 6 : print_gimple_stmt (dump_file, ostmt, 0);
1933 6 : fprintf (dump_file, "\n");
1934 : }
1935 682 : gimple_call_set_lhs (ostmt, NULL_TREE);
1936 682 : update_stmt (ostmt);
1937 682 : statistics_counter_event (cfun, "removed lhs call", 1);
1938 682 : return;
1939 : }
1940 : /* This an assignment store to the clobbered decl,
1941 : then maybe remove it. */
1942 13045471 : if (is_a <gassign*>(ostmt)
1943 10992952 : && gimple_store_p (ostmt)
1944 10992952 : && !gimple_clobber_p (ostmt)
1945 16285860 : && lhs == gimple_assign_lhs (ostmt))
1946 : {
1947 : /* Don't remove stores/statements that are needed for non-call
1948 : eh to work. */
1949 169842 : if (stmt_unremovable_because_of_non_call_eh_p (cfun, ostmt))
1950 : return;
1951 : /* If we delete a stmt that could throw, mark the block
1952 : in to_purge to cleanup afterwards. */
1953 164760 : if (stmt_could_throw_p (cfun, ostmt))
1954 0 : bitmap_set_bit (to_purge, obb->index);
1955 164760 : gimple_stmt_iterator gsi = gsi_for_stmt (ostmt);
1956 164760 : if (dump_file && (dump_flags & TDF_DETAILS))
1957 : {
1958 12 : fprintf (dump_file, "Removing dead store stmt ");
1959 12 : print_gimple_stmt (dump_file, ostmt, 0);
1960 12 : fprintf (dump_file, "\n");
1961 : }
1962 164760 : unlink_stmt_vdef (ostmt);
1963 164760 : release_defs (ostmt);
1964 164760 : gsi_remove (&gsi, true);
1965 164760 : statistics_counter_event (cfun, "delete dead store", 1);
1966 : /* Only remove the first store previous statement. */
1967 164760 : return;
1968 : }
1969 : /* If the statement uses or maybe writes to the decl,
1970 : then nothing is to be removed. Don't know if the write
1971 : to the decl is partial write or a full one so the need
1972 : to stop.
1973 : e.g.
1974 : b.c = a;
1975 : Easier to stop here rather than do a full partial
1976 : dse of this statement.
1977 : b = {CLOBBER}; */
1978 12875629 : if (stmt_may_clobber_ref_p_1 (ostmt, &read, false)
1979 12875629 : || ref_maybe_used_by_stmt_p (ostmt, &read, false))
1980 : return;
1981 10532340 : limit--;
1982 : }
1983 : }
1984 :
1985 : /* Optimizes builtin memcmps for small constant sizes.
1986 : GSI_P is the GSI for the call. STMT is the call itself.
1987 : */
1988 :
1989 : static bool
1990 474690 : simplify_builtin_memcmp (gimple_stmt_iterator *gsi_p, gcall *stmt)
1991 : {
1992 : /* Make sure memcmp arguments are the correct type. */
1993 474690 : if (gimple_call_num_args (stmt) != 3)
1994 : return false;
1995 474690 : tree arg1 = gimple_call_arg (stmt, 0);
1996 474690 : tree arg2 = gimple_call_arg (stmt, 1);
1997 474690 : tree len = gimple_call_arg (stmt, 2);
1998 :
1999 474690 : if (!POINTER_TYPE_P (TREE_TYPE (arg1)))
2000 : return false;
2001 474690 : if (!POINTER_TYPE_P (TREE_TYPE (arg2)))
2002 : return false;
2003 474690 : if (!INTEGRAL_TYPE_P (TREE_TYPE (len)))
2004 : return false;
2005 :
2006 : /* The return value of the memcmp has to be used
2007 : equality comparison to zero. */
2008 474690 : tree res = gimple_call_lhs (stmt);
2009 :
2010 474690 : if (!res || !use_in_zero_equality (res))
2011 : return false;
2012 :
2013 458295 : unsigned HOST_WIDE_INT leni;
2014 :
2015 458295 : if (tree_fits_uhwi_p (len)
2016 636342 : && (leni = tree_to_uhwi (len)) <= GET_MODE_SIZE (word_mode)
2017 539883 : && pow2p_hwi (leni))
2018 : {
2019 19289 : leni *= CHAR_TYPE_SIZE;
2020 19289 : unsigned align1 = get_pointer_alignment (arg1);
2021 19289 : unsigned align2 = get_pointer_alignment (arg2);
2022 19289 : unsigned align = MIN (align1, align2);
2023 19289 : scalar_int_mode mode;
2024 38578 : if (int_mode_for_size (leni, 1).exists (&mode)
2025 19289 : && (align >= leni || !targetm.slow_unaligned_access (mode, align)))
2026 : {
2027 19289 : location_t loc = gimple_location (stmt);
2028 19289 : tree type, off;
2029 19289 : type = build_nonstandard_integer_type (leni, 1);
2030 38578 : gcc_assert (known_eq (GET_MODE_BITSIZE (TYPE_MODE (type)), leni));
2031 19289 : tree ptrtype = build_pointer_type_for_mode (char_type_node,
2032 : ptr_mode, true);
2033 19289 : off = build_int_cst (ptrtype, 0);
2034 :
2035 : /* Create unaligned types if needed. */
2036 19289 : tree type1 = type, type2 = type;
2037 19289 : if (TYPE_ALIGN (type1) > align1)
2038 7973 : type1 = build_aligned_type (type1, align1);
2039 19289 : if (TYPE_ALIGN (type2) > align2)
2040 8468 : type2 = build_aligned_type (type2, align2);
2041 :
2042 19289 : arg1 = build2_loc (loc, MEM_REF, type1, arg1, off);
2043 19289 : arg2 = build2_loc (loc, MEM_REF, type2, arg2, off);
2044 19289 : tree tem1 = fold_const_aggregate_ref (arg1);
2045 19289 : if (tem1)
2046 222 : arg1 = tem1;
2047 19289 : tree tem2 = fold_const_aggregate_ref (arg2);
2048 19289 : if (tem2)
2049 7622 : arg2 = tem2;
2050 19289 : res = fold_convert_loc (loc, TREE_TYPE (res),
2051 : fold_build2_loc (loc, NE_EXPR,
2052 : boolean_type_node,
2053 : arg1, arg2));
2054 19289 : gimplify_and_update_call_from_tree (gsi_p, res);
2055 19289 : return true;
2056 : }
2057 : }
2058 :
2059 : /* Replace memcmp with memcmp_eq if the above fails. */
2060 439006 : if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)) == BUILT_IN_MEMCMP_EQ)
2061 : return false;
2062 347485 : if (!fold_before_rtl_expansion_p ())
2063 : return false;
2064 91521 : gimple_call_set_fndecl (stmt, builtin_decl_explicit (BUILT_IN_MEMCMP_EQ));
2065 91521 : update_stmt (stmt);
2066 91521 : return true;
2067 : }
2068 :
2069 : /* Optimizes builtin memchrs for small constant sizes with a const string.
2070 : GSI_P is the GSI for the call. STMT is the call itself.
2071 : */
2072 :
2073 : static bool
2074 14799 : simplify_builtin_memchr (gimple_stmt_iterator *gsi_p, gcall *stmt)
2075 : {
2076 14799 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
2077 : return false;
2078 :
2079 14799 : if (gimple_call_num_args (stmt) != 3)
2080 : return false;
2081 :
2082 14799 : tree res = gimple_call_lhs (stmt);
2083 14799 : if (!res || !use_in_zero_equality (res))
2084 : return false;
2085 :
2086 1461 : tree ptr = gimple_call_arg (stmt, 0);
2087 1461 : if (TREE_CODE (ptr) != ADDR_EXPR
2088 1461 : || TREE_CODE (TREE_OPERAND (ptr, 0)) != STRING_CST)
2089 : return false;
2090 :
2091 454 : unsigned HOST_WIDE_INT slen
2092 454 : = TREE_STRING_LENGTH (TREE_OPERAND (ptr, 0));
2093 : /* It must be a non-empty string constant. */
2094 454 : if (slen < 2)
2095 : return false;
2096 :
2097 : /* For -Os, only simplify strings with a single character. */
2098 450 : if (!optimize_bb_for_speed_p (gimple_bb (stmt))
2099 450 : && slen > 2)
2100 : return false;
2101 :
2102 434 : tree size = gimple_call_arg (stmt, 2);
2103 : /* Size must be a constant which is <= UNITS_PER_WORD and
2104 : <= the string length. */
2105 434 : if (!tree_fits_uhwi_p (size))
2106 : return false;
2107 :
2108 434 : unsigned HOST_WIDE_INT sz = tree_to_uhwi (size);
2109 435 : if (sz == 0 || sz > UNITS_PER_WORD || sz >= slen)
2110 : return false;
2111 :
2112 382 : tree ch = gimple_call_arg (stmt, 1);
2113 382 : location_t loc = gimple_location (stmt);
2114 382 : if (!useless_type_conversion_p (char_type_node,
2115 382 : TREE_TYPE (ch)))
2116 382 : ch = fold_convert_loc (loc, char_type_node, ch);
2117 382 : const char *p = TREE_STRING_POINTER (TREE_OPERAND (ptr, 0));
2118 382 : unsigned int isize = sz;
2119 382 : tree *op = XALLOCAVEC (tree, isize);
2120 1377 : for (unsigned int i = 0; i < isize; i++)
2121 : {
2122 995 : op[i] = build_int_cst (char_type_node, p[i]);
2123 995 : op[i] = fold_build2_loc (loc, EQ_EXPR, boolean_type_node,
2124 : op[i], ch);
2125 : }
2126 995 : for (unsigned int i = isize - 1; i >= 1; i--)
2127 613 : op[i - 1] = fold_convert_loc (loc, boolean_type_node,
2128 : fold_build2_loc (loc,
2129 : BIT_IOR_EXPR,
2130 : boolean_type_node,
2131 613 : op[i - 1],
2132 613 : op[i]));
2133 382 : res = fold_convert_loc (loc, TREE_TYPE (res), op[0]);
2134 382 : gimplify_and_update_call_from_tree (gsi_p, res);
2135 382 : return true;
2136 : }
2137 :
2138 : /* *GSI_P is a GIMPLE_CALL to a builtin function.
2139 : Optimize
2140 : memcpy (p, "abcd", 4); // STMT1
2141 : memset (p + 4, ' ', 3); // STMT2
2142 : into
2143 : memcpy (p, "abcd ", 7);
2144 : call if the latter can be stored by pieces during expansion.
2145 : */
2146 :
2147 : static bool
2148 113047 : simplify_builtin_memcpy_memset (gimple_stmt_iterator *gsi_p, gcall *stmt2)
2149 : {
2150 113047 : if (gimple_call_num_args (stmt2) != 3
2151 113047 : || gimple_call_lhs (stmt2)
2152 : || CHAR_BIT != 8
2153 113047 : || BITS_PER_UNIT != 8)
2154 : return false;
2155 :
2156 106872 : tree vuse = gimple_vuse (stmt2);
2157 106872 : if (vuse == NULL)
2158 : return false;
2159 106856 : gimple *stmt1 = SSA_NAME_DEF_STMT (vuse);
2160 :
2161 106856 : tree callee1;
2162 106856 : tree ptr1, src1, str1, off1, len1, lhs1;
2163 106856 : tree ptr2 = gimple_call_arg (stmt2, 0);
2164 106856 : tree val2 = gimple_call_arg (stmt2, 1);
2165 106856 : tree len2 = gimple_call_arg (stmt2, 2);
2166 106856 : tree diff, vdef, new_str_cst;
2167 106856 : gimple *use_stmt;
2168 106856 : unsigned int ptr1_align;
2169 106856 : unsigned HOST_WIDE_INT src_len;
2170 106856 : char *src_buf;
2171 106856 : use_operand_p use_p;
2172 :
2173 106856 : if (!tree_fits_shwi_p (val2)
2174 102362 : || !tree_fits_uhwi_p (len2)
2175 171509 : || compare_tree_int (len2, 1024) == 1)
2176 : return false;
2177 :
2178 59082 : if (is_gimple_call (stmt1))
2179 : {
2180 : /* If first stmt is a call, it needs to be memcpy
2181 : or mempcpy, with string literal as second argument and
2182 : constant length. */
2183 30635 : callee1 = gimple_call_fndecl (stmt1);
2184 30635 : if (callee1 == NULL_TREE
2185 30519 : || !fndecl_built_in_p (callee1, BUILT_IN_NORMAL)
2186 57483 : || gimple_call_num_args (stmt1) != 3)
2187 : return false;
2188 25504 : if (DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMCPY
2189 25504 : && DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMPCPY)
2190 : return false;
2191 11109 : ptr1 = gimple_call_arg (stmt1, 0);
2192 11109 : src1 = gimple_call_arg (stmt1, 1);
2193 11109 : len1 = gimple_call_arg (stmt1, 2);
2194 11109 : lhs1 = gimple_call_lhs (stmt1);
2195 11109 : if (!tree_fits_uhwi_p (len1))
2196 : return false;
2197 11022 : str1 = string_constant (src1, &off1, NULL, NULL);
2198 11022 : if (str1 == NULL_TREE)
2199 : return false;
2200 5135 : if (!tree_fits_uhwi_p (off1)
2201 5135 : || compare_tree_int (off1, TREE_STRING_LENGTH (str1) - 1) > 0
2202 5135 : || compare_tree_int (len1, TREE_STRING_LENGTH (str1)
2203 5135 : - tree_to_uhwi (off1)) > 0
2204 5135 : || TREE_CODE (TREE_TYPE (str1)) != ARRAY_TYPE
2205 15405 : || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1)))
2206 5135 : != TYPE_MODE (char_type_node))
2207 : return false;
2208 : }
2209 28447 : else if (gimple_assign_single_p (stmt1))
2210 : {
2211 : /* Otherwise look for length 1 memcpy optimized into
2212 : assignment. */
2213 17410 : ptr1 = gimple_assign_lhs (stmt1);
2214 17410 : src1 = gimple_assign_rhs1 (stmt1);
2215 17410 : if (TREE_CODE (ptr1) != MEM_REF
2216 3482 : || TYPE_MODE (TREE_TYPE (ptr1)) != TYPE_MODE (char_type_node)
2217 18399 : || !tree_fits_shwi_p (src1))
2218 : return false;
2219 343 : ptr1 = build_fold_addr_expr (ptr1);
2220 343 : STRIP_USELESS_TYPE_CONVERSION (ptr1);
2221 343 : callee1 = NULL_TREE;
2222 343 : len1 = size_one_node;
2223 343 : lhs1 = NULL_TREE;
2224 343 : off1 = size_zero_node;
2225 343 : str1 = NULL_TREE;
2226 : }
2227 : else
2228 : return false;
2229 :
2230 5478 : diff = constant_pointer_difference (ptr1, ptr2);
2231 5478 : if (diff == NULL && lhs1 != NULL)
2232 : {
2233 7 : diff = constant_pointer_difference (lhs1, ptr2);
2234 7 : if (DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
2235 7 : && diff != NULL)
2236 7 : diff = size_binop (PLUS_EXPR, diff,
2237 : fold_convert (sizetype, len1));
2238 : }
2239 : /* If the difference between the second and first destination pointer
2240 : is not constant, or is bigger than memcpy length, bail out. */
2241 5478 : if (diff == NULL
2242 4600 : || !tree_fits_uhwi_p (diff)
2243 4600 : || tree_int_cst_lt (len1, diff)
2244 9822 : || compare_tree_int (diff, 1024) == 1)
2245 : return false;
2246 :
2247 : /* Use maximum of difference plus memset length and memcpy length
2248 : as the new memcpy length, if it is too big, bail out. */
2249 4344 : src_len = tree_to_uhwi (diff);
2250 4344 : src_len += tree_to_uhwi (len2);
2251 4344 : if (src_len < tree_to_uhwi (len1))
2252 : src_len = tree_to_uhwi (len1);
2253 4344 : if (src_len > 1024)
2254 : return false;
2255 :
2256 : /* If mempcpy value is used elsewhere, bail out, as mempcpy
2257 : with bigger length will return different result. */
2258 4344 : if (lhs1 != NULL_TREE
2259 64 : && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
2260 4351 : && (TREE_CODE (lhs1) != SSA_NAME
2261 7 : || !single_imm_use (lhs1, &use_p, &use_stmt)
2262 7 : || use_stmt != stmt2))
2263 : return false;
2264 :
2265 : /* If anything reads memory in between memcpy and memset
2266 : call, the modified memcpy call might change it. */
2267 4344 : vdef = gimple_vdef (stmt1);
2268 4344 : if (vdef != NULL
2269 4344 : && (!single_imm_use (vdef, &use_p, &use_stmt)
2270 3625 : || use_stmt != stmt2))
2271 : return false;
2272 :
2273 3625 : ptr1_align = get_pointer_alignment (ptr1);
2274 : /* Construct the new source string literal. */
2275 3625 : src_buf = XALLOCAVEC (char, src_len + 1);
2276 3625 : if (callee1)
2277 3459 : memcpy (src_buf,
2278 3459 : TREE_STRING_POINTER (str1) + tree_to_uhwi (off1),
2279 : tree_to_uhwi (len1));
2280 : else
2281 166 : src_buf[0] = tree_to_shwi (src1);
2282 3625 : memset (src_buf + tree_to_uhwi (diff),
2283 3625 : tree_to_shwi (val2), tree_to_uhwi (len2));
2284 3625 : src_buf[src_len] = '\0';
2285 : /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
2286 : handle embedded '\0's. */
2287 3625 : if (strlen (src_buf) != src_len)
2288 : return false;
2289 3531 : rtl_profile_for_bb (gimple_bb (stmt2));
2290 : /* If the new memcpy wouldn't be emitted by storing the literal
2291 : by pieces, this optimization might enlarge .rodata too much,
2292 : as commonly used string literals couldn't be shared any
2293 : longer. */
2294 3531 : if (!can_store_by_pieces (src_len,
2295 : builtin_strncpy_read_str,
2296 : src_buf, ptr1_align, false))
2297 : return false;
2298 :
2299 2641 : new_str_cst = build_string_literal (src_len, src_buf);
2300 2641 : if (callee1)
2301 : {
2302 : /* If STMT1 is a mem{,p}cpy call, adjust it and remove
2303 : memset call. */
2304 2513 : if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
2305 7 : gimple_call_set_lhs (stmt1, NULL_TREE);
2306 2513 : gimple_call_set_arg (stmt1, 1, new_str_cst);
2307 2513 : gimple_call_set_arg (stmt1, 2,
2308 2513 : build_int_cst (TREE_TYPE (len1), src_len));
2309 2513 : update_stmt (stmt1);
2310 2513 : unlink_stmt_vdef (stmt2);
2311 2513 : gsi_replace (gsi_p, gimple_build_nop (), false);
2312 2513 : fwprop_invalidate_lattice (gimple_get_lhs (stmt2));
2313 2513 : release_defs (stmt2);
2314 2513 : if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
2315 : {
2316 7 : fwprop_invalidate_lattice (lhs1);
2317 7 : release_ssa_name (lhs1);
2318 : }
2319 : return true;
2320 : }
2321 : else
2322 : {
2323 : /* Otherwise, if STMT1 is length 1 memcpy optimized into
2324 : assignment, remove STMT1 and change memset call into
2325 : memcpy call. */
2326 128 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt1);
2327 :
2328 128 : if (!is_gimple_val (ptr1))
2329 12 : ptr1 = force_gimple_operand_gsi (gsi_p, ptr1, true, NULL_TREE,
2330 : true, GSI_SAME_STMT);
2331 128 : tree fndecl = builtin_decl_explicit (BUILT_IN_MEMCPY);
2332 128 : gimple_call_set_fndecl (stmt2, fndecl);
2333 128 : gimple_call_set_fntype (stmt2,
2334 128 : TREE_TYPE (fndecl));
2335 128 : gimple_call_set_arg (stmt2, 0, ptr1);
2336 128 : gimple_call_set_arg (stmt2, 1, new_str_cst);
2337 128 : gimple_call_set_arg (stmt2, 2,
2338 128 : build_int_cst (TREE_TYPE (len2), src_len));
2339 128 : unlink_stmt_vdef (stmt1);
2340 128 : gsi_remove (&gsi, true);
2341 128 : fwprop_invalidate_lattice (gimple_get_lhs (stmt1));
2342 128 : release_defs (stmt1);
2343 128 : update_stmt (stmt2);
2344 128 : return false;
2345 : }
2346 : }
2347 :
2348 :
2349 : /* Try to optimize out __builtin_stack_restore. Optimize it out
2350 : if there is another __builtin_stack_restore in the same basic
2351 : block and no calls or ASM_EXPRs are in between, or if this block's
2352 : only outgoing edge is to EXIT_BLOCK and there are no calls or
2353 : ASM_EXPRs after this __builtin_stack_restore.
2354 : Note restore right before a noreturn function is not needed.
2355 : And skip some cheap calls that will most likely become an instruction.
2356 : Restoring the stack before a call is important to be able to keep
2357 : stack usage down so that call does not run out of stack. */
2358 :
2359 :
2360 : static bool
2361 10609 : optimize_stack_restore (gimple_stmt_iterator *gsi, gimple *call)
2362 : {
2363 10609 : if (!fold_before_rtl_expansion_p ())
2364 : return false;
2365 2587 : tree callee;
2366 2587 : gimple *stmt;
2367 :
2368 2587 : basic_block bb = gsi_bb (*gsi);
2369 :
2370 2587 : if (gimple_call_num_args (call) != 1
2371 2587 : || TREE_CODE (gimple_call_arg (call, 0)) != SSA_NAME
2372 5174 : || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
2373 : return false;
2374 :
2375 2587 : gimple_stmt_iterator i = *gsi;
2376 6481 : for (gsi_next (&i); !gsi_end_p (i); gsi_next (&i))
2377 : {
2378 4363 : stmt = gsi_stmt (i);
2379 4363 : if (is_a<gasm*> (stmt))
2380 : return false;
2381 4362 : gcall *call = dyn_cast<gcall*>(stmt);
2382 4362 : if (!call)
2383 3679 : continue;
2384 :
2385 : /* We can remove the restore in front of noreturn
2386 : calls. Since the restore will happen either
2387 : via an unwind/longjmp or not at all. */
2388 683 : if (gimple_call_noreturn_p (call))
2389 : break;
2390 :
2391 : /* Internal calls are ok, to bypass
2392 : check first since fndecl will be null. */
2393 667 : if (gimple_call_internal_p (call))
2394 1 : continue;
2395 :
2396 666 : callee = gimple_call_fndecl (call);
2397 : /* Non-builtin calls are not ok. */
2398 666 : if (!callee
2399 666 : || !fndecl_built_in_p (callee))
2400 : return false;
2401 :
2402 : /* Do not remove stack updates before strub leave. */
2403 586 : if (fndecl_built_in_p (callee, BUILT_IN___STRUB_LEAVE)
2404 : /* Alloca calls are not ok either. */
2405 586 : || fndecl_builtin_alloc_p (callee))
2406 : return false;
2407 :
2408 371 : if (fndecl_built_in_p (callee, BUILT_IN_STACK_RESTORE))
2409 57 : goto second_stack_restore;
2410 :
2411 : /* If not a simple or inexpensive builtin, then it is not ok either. */
2412 314 : if (!is_simple_builtin (callee)
2413 314 : && !is_inexpensive_builtin (callee))
2414 : return false;
2415 : }
2416 :
2417 : /* Allow one successor of the exit block, or zero successors. */
2418 2134 : switch (EDGE_COUNT (bb->succs))
2419 : {
2420 : case 0:
2421 : break;
2422 2044 : case 1:
2423 2044 : if (single_succ_edge (bb)->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
2424 : return false;
2425 : break;
2426 : default:
2427 : return false;
2428 : }
2429 1774 : second_stack_restore:
2430 :
2431 : /* If there's exactly one use, then zap the call to __builtin_stack_save.
2432 : If there are multiple uses, then the last one should remove the call.
2433 : In any case, whether the call to __builtin_stack_save can be removed
2434 : or not is irrelevant to removing the call to __builtin_stack_restore. */
2435 1774 : if (has_single_use (gimple_call_arg (call, 0)))
2436 : {
2437 1602 : gimple *stack_save = SSA_NAME_DEF_STMT (gimple_call_arg (call, 0));
2438 1602 : if (is_gimple_call (stack_save))
2439 : {
2440 1600 : callee = gimple_call_fndecl (stack_save);
2441 1600 : if (callee && fndecl_built_in_p (callee, BUILT_IN_STACK_SAVE))
2442 : {
2443 1600 : gimple_stmt_iterator stack_save_gsi;
2444 1600 : tree rhs;
2445 :
2446 1600 : stack_save_gsi = gsi_for_stmt (stack_save);
2447 1600 : rhs = build_int_cst (TREE_TYPE (gimple_call_arg (call, 0)), 0);
2448 1600 : replace_call_with_value (&stack_save_gsi, rhs);
2449 : }
2450 : }
2451 : }
2452 :
2453 : /* No effect, so the statement will be deleted. */
2454 1774 : replace_call_with_value (gsi, NULL_TREE);
2455 1774 : return true;
2456 : }
2457 :
2458 : /* Optimizes strlen (s) ==/!= 0 to *s ==/!= 0. */
2459 : static bool
2460 65200 : optimize_strlen_comp (gimple_stmt_iterator *gsi, gimple *call)
2461 : {
2462 65200 : if (!fold_before_rtl_expansion_p ())
2463 : return false;
2464 :
2465 14153 : tree lhs = gimple_call_lhs (call);
2466 14153 : if (lhs == NULL_TREE || use_in_zero_equality (lhs, true) == NULL)
2467 : return false;
2468 :
2469 : /* The string passed to strlen. */
2470 164 : tree ptr = gimple_call_arg (call, 0);
2471 :
2472 : /* Dereference the string. */
2473 164 : tree deref = fold_build2 (MEM_REF, char_type_node, ptr,
2474 : build_zero_cst (ptr_type_node));
2475 :
2476 : /* Perform a type conversion. */
2477 164 : deref = fold_convert_loc (gimple_location (call),
2478 164 : TREE_TYPE (lhs),
2479 : deref);
2480 :
2481 : /* Replace the original call to strlen with the dereference we just built. */
2482 164 : gimplify_and_update_call_from_tree (gsi, deref);
2483 :
2484 164 : return true;
2485 : }
2486 :
2487 : /* *GSI_P is a GIMPLE_CALL to a builtin function.
2488 : Optimize
2489 : memcpy (p, "abcd", 4);
2490 : memset (p + 4, ' ', 3);
2491 : into
2492 : memcpy (p, "abcd ", 7);
2493 : call if the latter can be stored by pieces during expansion.
2494 :
2495 : Optimize
2496 : memchr ("abcd", a, 4) == 0;
2497 : or
2498 : memchr ("abcd", a, 4) != 0;
2499 : to
2500 : (a == 'a' || a == 'b' || a == 'c' || a == 'd') == 0
2501 : or
2502 : (a == 'a' || a == 'b' || a == 'c' || a == 'd') != 0
2503 :
2504 : Also canonicalize __atomic_fetch_op (p, x, y) op x
2505 : to __atomic_op_fetch (p, x, y) or
2506 : __atomic_op_fetch (p, x, y) iop x
2507 : to __atomic_fetch_op (p, x, y) when possible (also __sync). */
2508 :
2509 : static bool
2510 6358982 : simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2, bool full_walk)
2511 : {
2512 6358982 : gimple *stmt2 = gsi_stmt (*gsi_p);
2513 6358982 : enum built_in_function other_atomic = END_BUILTINS;
2514 6358982 : enum tree_code atomic_op = ERROR_MARK;
2515 :
2516 6358982 : switch (DECL_FUNCTION_CODE (callee2))
2517 : {
2518 65200 : case BUILT_IN_STRLEN:
2519 65200 : return optimize_strlen_comp (gsi_p, as_a<gcall*>(stmt2));
2520 10609 : case BUILT_IN_STACK_RESTORE:
2521 10609 : return optimize_stack_restore (gsi_p, as_a<gcall*>(stmt2));
2522 474690 : case BUILT_IN_MEMCMP:
2523 474690 : case BUILT_IN_MEMCMP_EQ:
2524 474690 : return simplify_builtin_memcmp (gsi_p, as_a<gcall*>(stmt2));
2525 14799 : case BUILT_IN_MEMCHR:
2526 14799 : return simplify_builtin_memchr (gsi_p, as_a<gcall*>(stmt2));
2527 :
2528 113047 : case BUILT_IN_MEMSET:
2529 113047 : if (gimple_call_num_args (stmt2) == 3)
2530 : {
2531 : /* Try to prop the zeroing/value of the memset to memcpy
2532 : if the dest is an address and the value is a constant. */
2533 113047 : optimize_aggr_zeroprop (stmt2, full_walk);
2534 : }
2535 113047 : return simplify_builtin_memcpy_memset (gsi_p, as_a<gcall*>(stmt2));
2536 :
2537 : #define CASE_ATOMIC(NAME, OTHER, OP) \
2538 : case BUILT_IN_##NAME##_1: \
2539 : case BUILT_IN_##NAME##_2: \
2540 : case BUILT_IN_##NAME##_4: \
2541 : case BUILT_IN_##NAME##_8: \
2542 : case BUILT_IN_##NAME##_16: \
2543 : atomic_op = OP; \
2544 : other_atomic \
2545 : = (enum built_in_function) (BUILT_IN_##OTHER##_1 \
2546 : + (DECL_FUNCTION_CODE (callee2) \
2547 : - BUILT_IN_##NAME##_1)); \
2548 : goto handle_atomic_fetch_op;
2549 :
2550 48808 : CASE_ATOMIC (ATOMIC_FETCH_ADD, ATOMIC_ADD_FETCH, PLUS_EXPR)
2551 7133 : CASE_ATOMIC (ATOMIC_FETCH_SUB, ATOMIC_SUB_FETCH, MINUS_EXPR)
2552 2904 : CASE_ATOMIC (ATOMIC_FETCH_AND, ATOMIC_AND_FETCH, BIT_AND_EXPR)
2553 2923 : CASE_ATOMIC (ATOMIC_FETCH_XOR, ATOMIC_XOR_FETCH, BIT_XOR_EXPR)
2554 3874 : CASE_ATOMIC (ATOMIC_FETCH_OR, ATOMIC_OR_FETCH, BIT_IOR_EXPR)
2555 :
2556 2373 : CASE_ATOMIC (SYNC_FETCH_AND_ADD, SYNC_ADD_AND_FETCH, PLUS_EXPR)
2557 2012 : CASE_ATOMIC (SYNC_FETCH_AND_SUB, SYNC_SUB_AND_FETCH, MINUS_EXPR)
2558 1876 : CASE_ATOMIC (SYNC_FETCH_AND_AND, SYNC_AND_AND_FETCH, BIT_AND_EXPR)
2559 2144 : CASE_ATOMIC (SYNC_FETCH_AND_XOR, SYNC_XOR_AND_FETCH, BIT_XOR_EXPR)
2560 1987 : CASE_ATOMIC (SYNC_FETCH_AND_OR, SYNC_OR_AND_FETCH, BIT_IOR_EXPR)
2561 :
2562 14421 : CASE_ATOMIC (ATOMIC_ADD_FETCH, ATOMIC_FETCH_ADD, MINUS_EXPR)
2563 8560 : CASE_ATOMIC (ATOMIC_SUB_FETCH, ATOMIC_FETCH_SUB, PLUS_EXPR)
2564 2408 : CASE_ATOMIC (ATOMIC_XOR_FETCH, ATOMIC_FETCH_XOR, BIT_XOR_EXPR)
2565 :
2566 854 : CASE_ATOMIC (SYNC_ADD_AND_FETCH, SYNC_FETCH_AND_ADD, MINUS_EXPR)
2567 740 : CASE_ATOMIC (SYNC_SUB_AND_FETCH, SYNC_FETCH_AND_SUB, PLUS_EXPR)
2568 800 : CASE_ATOMIC (SYNC_XOR_AND_FETCH, SYNC_FETCH_AND_XOR, BIT_XOR_EXPR)
2569 :
2570 : #undef CASE_ATOMIC
2571 :
2572 103817 : handle_atomic_fetch_op:
2573 103817 : if (gimple_call_num_args (stmt2) >= 2 && gimple_call_lhs (stmt2))
2574 : {
2575 60294 : tree lhs2 = gimple_call_lhs (stmt2), lhsc = lhs2;
2576 60294 : tree arg = gimple_call_arg (stmt2, 1);
2577 60294 : gimple *use_stmt, *cast_stmt = NULL;
2578 60294 : use_operand_p use_p;
2579 60294 : tree ndecl = builtin_decl_explicit (other_atomic);
2580 :
2581 60294 : if (ndecl == NULL_TREE || !single_imm_use (lhs2, &use_p, &use_stmt))
2582 : break;
2583 :
2584 59173 : if (gimple_assign_cast_p (use_stmt))
2585 : {
2586 31597 : cast_stmt = use_stmt;
2587 31597 : lhsc = gimple_assign_lhs (cast_stmt);
2588 31597 : if (lhsc == NULL_TREE
2589 31597 : || !INTEGRAL_TYPE_P (TREE_TYPE (lhsc))
2590 31046 : || (TYPE_PRECISION (TREE_TYPE (lhsc))
2591 31046 : != TYPE_PRECISION (TREE_TYPE (lhs2)))
2592 61053 : || !single_imm_use (lhsc, &use_p, &use_stmt))
2593 : {
2594 2669 : use_stmt = cast_stmt;
2595 2669 : cast_stmt = NULL;
2596 2669 : lhsc = lhs2;
2597 : }
2598 : }
2599 :
2600 59173 : bool ok = false;
2601 59173 : tree oarg = NULL_TREE;
2602 59173 : enum tree_code ccode = ERROR_MARK;
2603 59173 : tree crhs1 = NULL_TREE, crhs2 = NULL_TREE;
2604 59173 : if (is_gimple_assign (use_stmt)
2605 59173 : && gimple_assign_rhs_code (use_stmt) == atomic_op)
2606 : {
2607 1416 : if (gimple_assign_rhs1 (use_stmt) == lhsc)
2608 1016 : oarg = gimple_assign_rhs2 (use_stmt);
2609 400 : else if (atomic_op != MINUS_EXPR)
2610 : oarg = gimple_assign_rhs1 (use_stmt);
2611 : }
2612 57757 : else if (atomic_op == MINUS_EXPR
2613 13291 : && is_gimple_assign (use_stmt)
2614 3638 : && gimple_assign_rhs_code (use_stmt) == PLUS_EXPR
2615 199 : && TREE_CODE (arg) == INTEGER_CST
2616 57956 : && (TREE_CODE (gimple_assign_rhs2 (use_stmt))
2617 : == INTEGER_CST))
2618 : {
2619 183 : tree a = fold_convert (TREE_TYPE (lhs2), arg);
2620 183 : tree o = fold_convert (TREE_TYPE (lhs2),
2621 : gimple_assign_rhs2 (use_stmt));
2622 183 : if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
2623 : ok = true;
2624 : }
2625 57574 : else if (atomic_op == BIT_AND_EXPR || atomic_op == BIT_IOR_EXPR)
2626 : ;
2627 52276 : else if (gimple_code (use_stmt) == GIMPLE_COND)
2628 : {
2629 19594 : ccode = gimple_cond_code (use_stmt);
2630 19594 : crhs1 = gimple_cond_lhs (use_stmt);
2631 19594 : crhs2 = gimple_cond_rhs (use_stmt);
2632 : }
2633 32682 : else if (is_gimple_assign (use_stmt))
2634 : {
2635 9583 : if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
2636 : {
2637 3935 : ccode = gimple_assign_rhs_code (use_stmt);
2638 3935 : crhs1 = gimple_assign_rhs1 (use_stmt);
2639 3935 : crhs2 = gimple_assign_rhs2 (use_stmt);
2640 : }
2641 : }
2642 24545 : if (ccode == EQ_EXPR || ccode == NE_EXPR)
2643 : {
2644 : /* Deal with x - y == 0 or x ^ y == 0
2645 : being optimized into x == y and x + cst == 0
2646 : into x == -cst. */
2647 22345 : tree o = NULL_TREE;
2648 22345 : if (crhs1 == lhsc)
2649 : o = crhs2;
2650 133 : else if (crhs2 == lhsc)
2651 133 : o = crhs1;
2652 22345 : if (o && atomic_op != PLUS_EXPR)
2653 : oarg = o;
2654 10117 : else if (o
2655 10117 : && TREE_CODE (o) == INTEGER_CST
2656 10117 : && TREE_CODE (arg) == INTEGER_CST)
2657 : {
2658 9407 : tree a = fold_convert (TREE_TYPE (lhs2), arg);
2659 9407 : o = fold_convert (TREE_TYPE (lhs2), o);
2660 9407 : if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
2661 59173 : ok = true;
2662 : }
2663 : }
2664 59173 : if (oarg && !ok)
2665 : {
2666 13644 : if (operand_equal_p (arg, oarg, 0))
2667 : ok = true;
2668 12315 : else if (TREE_CODE (arg) == SSA_NAME
2669 2215 : && TREE_CODE (oarg) == SSA_NAME)
2670 : {
2671 745 : tree oarg2 = oarg;
2672 745 : if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (oarg)))
2673 : {
2674 104 : gimple *g = SSA_NAME_DEF_STMT (oarg);
2675 104 : oarg2 = gimple_assign_rhs1 (g);
2676 104 : if (TREE_CODE (oarg2) != SSA_NAME
2677 104 : || !INTEGRAL_TYPE_P (TREE_TYPE (oarg2))
2678 208 : || (TYPE_PRECISION (TREE_TYPE (oarg2))
2679 104 : != TYPE_PRECISION (TREE_TYPE (oarg))))
2680 : oarg2 = oarg;
2681 : }
2682 745 : if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg)))
2683 : {
2684 544 : gimple *g = SSA_NAME_DEF_STMT (arg);
2685 544 : tree rhs1 = gimple_assign_rhs1 (g);
2686 : /* Handle e.g.
2687 : x.0_1 = (long unsigned int) x_4(D);
2688 : _2 = __atomic_fetch_add_8 (&vlong, x.0_1, 0);
2689 : _3 = (long int) _2;
2690 : _7 = x_4(D) + _3; */
2691 544 : if (rhs1 == oarg || rhs1 == oarg2)
2692 : ok = true;
2693 : /* Handle e.g.
2694 : x.18_1 = (short unsigned int) x_5(D);
2695 : _2 = (int) x.18_1;
2696 : _3 = __atomic_fetch_xor_2 (&vshort, _2, 0);
2697 : _4 = (short int) _3;
2698 : _8 = x_5(D) ^ _4;
2699 : This happens only for char/short. */
2700 160 : else if (TREE_CODE (rhs1) == SSA_NAME
2701 160 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
2702 320 : && (TYPE_PRECISION (TREE_TYPE (rhs1))
2703 160 : == TYPE_PRECISION (TREE_TYPE (lhs2))))
2704 : {
2705 160 : g = SSA_NAME_DEF_STMT (rhs1);
2706 160 : if (gimple_assign_cast_p (g)
2707 160 : && (gimple_assign_rhs1 (g) == oarg
2708 0 : || gimple_assign_rhs1 (g) == oarg2))
2709 : ok = true;
2710 : }
2711 : }
2712 745 : if (!ok && arg == oarg2)
2713 : /* Handle e.g.
2714 : _1 = __sync_fetch_and_add_4 (&v, x_5(D));
2715 : _2 = (int) _1;
2716 : x.0_3 = (int) x_5(D);
2717 : _7 = _2 + x.0_3; */
2718 : ok = true;
2719 : }
2720 : }
2721 :
2722 57844 : if (ok)
2723 : {
2724 2606 : tree new_lhs = make_ssa_name (TREE_TYPE (lhs2));
2725 2606 : gimple_call_set_lhs (stmt2, new_lhs);
2726 2606 : gimple_call_set_fndecl (stmt2, ndecl);
2727 2606 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
2728 2606 : if (ccode == ERROR_MARK)
2729 2000 : gimple_assign_set_rhs_with_ops (&gsi, cast_stmt
2730 : ? NOP_EXPR : SSA_NAME,
2731 : new_lhs);
2732 : else
2733 : {
2734 1383 : crhs1 = new_lhs;
2735 1383 : crhs2 = build_zero_cst (TREE_TYPE (lhs2));
2736 1383 : if (gimple_code (use_stmt) == GIMPLE_COND)
2737 : {
2738 1044 : gcond *cond_stmt = as_a <gcond *> (use_stmt);
2739 1044 : gimple_cond_set_lhs (cond_stmt, crhs1);
2740 1044 : gimple_cond_set_rhs (cond_stmt, crhs2);
2741 : }
2742 339 : else if (gimple_assign_rhs_class (use_stmt)
2743 : == GIMPLE_BINARY_RHS)
2744 : {
2745 339 : gimple_assign_set_rhs1 (use_stmt, crhs1);
2746 339 : gimple_assign_set_rhs2 (use_stmt, crhs2);
2747 : }
2748 : }
2749 2606 : update_stmt (use_stmt);
2750 2606 : if (atomic_op != BIT_AND_EXPR
2751 2606 : && atomic_op != BIT_IOR_EXPR
2752 2606 : && !stmt_ends_bb_p (stmt2))
2753 : {
2754 : /* For the benefit of debug stmts, emit stmt(s) to set
2755 : lhs2 to the value it had from the new builtin.
2756 : E.g. if it was previously:
2757 : lhs2 = __atomic_fetch_add_8 (ptr, arg, 0);
2758 : emit:
2759 : new_lhs = __atomic_add_fetch_8 (ptr, arg, 0);
2760 : lhs2 = new_lhs - arg;
2761 : We also keep cast_stmt if any in the IL for
2762 : the same reasons.
2763 : These stmts will be DCEd later and proper debug info
2764 : will be emitted.
2765 : This is only possible for reversible operations
2766 : (+/-/^) and without -fnon-call-exceptions. */
2767 2265 : gsi = gsi_for_stmt (stmt2);
2768 2265 : tree type = TREE_TYPE (lhs2);
2769 2265 : if (TREE_CODE (arg) == INTEGER_CST)
2770 1683 : arg = fold_convert (type, arg);
2771 582 : else if (!useless_type_conversion_p (type, TREE_TYPE (arg)))
2772 : {
2773 0 : tree narg = make_ssa_name (type);
2774 0 : gimple *g = gimple_build_assign (narg, NOP_EXPR, arg);
2775 0 : gsi_insert_after (&gsi, g, GSI_NEW_STMT);
2776 0 : arg = narg;
2777 : }
2778 2265 : enum tree_code rcode;
2779 2265 : switch (atomic_op)
2780 : {
2781 : case PLUS_EXPR: rcode = MINUS_EXPR; break;
2782 727 : case MINUS_EXPR: rcode = PLUS_EXPR; break;
2783 492 : case BIT_XOR_EXPR: rcode = atomic_op; break;
2784 : default: gcc_unreachable ();
2785 : }
2786 2265 : gimple *g = gimple_build_assign (lhs2, rcode, new_lhs, arg);
2787 2265 : gsi_insert_after (&gsi, g, GSI_NEW_STMT);
2788 2265 : update_stmt (stmt2);
2789 : }
2790 : else
2791 : {
2792 : /* For e.g.
2793 : lhs2 = __atomic_fetch_or_8 (ptr, arg, 0);
2794 : after we change it to
2795 : new_lhs = __atomic_or_fetch_8 (ptr, arg, 0);
2796 : there is no way to find out the lhs2 value (i.e.
2797 : what the atomic memory contained before the operation),
2798 : values of some bits are lost. We have checked earlier
2799 : that we don't have any non-debug users except for what
2800 : we are already changing, so we need to reset the
2801 : debug stmts and remove the cast_stmt if any. */
2802 341 : imm_use_iterator iter;
2803 676 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs2)
2804 335 : if (use_stmt != cast_stmt)
2805 : {
2806 168 : gcc_assert (is_gimple_debug (use_stmt));
2807 168 : gimple_debug_bind_reset_value (use_stmt);
2808 168 : update_stmt (use_stmt);
2809 341 : }
2810 341 : if (cast_stmt)
2811 : {
2812 167 : gsi = gsi_for_stmt (cast_stmt);
2813 167 : gsi_remove (&gsi, true);
2814 : }
2815 341 : update_stmt (stmt2);
2816 341 : release_ssa_name (lhs2);
2817 : }
2818 : }
2819 : }
2820 : break;
2821 :
2822 : default:
2823 : break;
2824 : }
2825 : return false;
2826 : }
2827 :
2828 : /* Given a ssa_name in NAME see if it was defined by an assignment and
2829 : set CODE to be the code and ARG1 to the first operand on the rhs and ARG2
2830 : to the second operand on the rhs. */
2831 :
2832 : static inline void
2833 17732676 : defcodefor_name (tree name, enum tree_code *code, tree *arg1, tree *arg2)
2834 : {
2835 17732676 : gimple *def;
2836 17732676 : enum tree_code code1;
2837 17732676 : tree arg11;
2838 17732676 : tree arg21;
2839 17732676 : tree arg31;
2840 17732676 : enum gimple_rhs_class grhs_class;
2841 :
2842 17732676 : code1 = TREE_CODE (name);
2843 17732676 : arg11 = name;
2844 17732676 : arg21 = NULL_TREE;
2845 17732676 : arg31 = NULL_TREE;
2846 17732676 : grhs_class = get_gimple_rhs_class (code1);
2847 :
2848 17732676 : if (code1 == SSA_NAME)
2849 : {
2850 11775256 : def = SSA_NAME_DEF_STMT (name);
2851 :
2852 11775256 : if (def && is_gimple_assign (def)
2853 19087348 : && can_propagate_from (def))
2854 : {
2855 4987912 : code1 = gimple_assign_rhs_code (def);
2856 4987912 : arg11 = gimple_assign_rhs1 (def);
2857 4987912 : arg21 = gimple_assign_rhs2 (def);
2858 4987912 : arg31 = gimple_assign_rhs3 (def);
2859 : }
2860 : }
2861 5957420 : else if (grhs_class != GIMPLE_SINGLE_RHS)
2862 0 : code1 = ERROR_MARK;
2863 :
2864 17732676 : *code = code1;
2865 17732676 : *arg1 = arg11;
2866 17732676 : if (arg2)
2867 17715417 : *arg2 = arg21;
2868 17732676 : if (arg31)
2869 2701 : *code = ERROR_MARK;
2870 17732676 : }
2871 :
2872 :
2873 : /* Recognize rotation patterns. Return true if a transformation
2874 : applied, otherwise return false.
2875 :
2876 : We are looking for X with unsigned type T with bitsize B, OP being
2877 : +, | or ^, some type T2 wider than T. For:
2878 : (X << CNT1) OP (X >> CNT2) iff CNT1 + CNT2 == B
2879 : ((T) ((T2) X << CNT1)) OP ((T) ((T2) X >> CNT2)) iff CNT1 + CNT2 == B
2880 :
2881 : transform these into:
2882 : X r<< CNT1
2883 :
2884 : Or for:
2885 : (X << Y) OP (X >> (B - Y))
2886 : (X << (int) Y) OP (X >> (int) (B - Y))
2887 : ((T) ((T2) X << Y)) OP ((T) ((T2) X >> (B - Y)))
2888 : ((T) ((T2) X << (int) Y)) OP ((T) ((T2) X >> (int) (B - Y)))
2889 : (X << Y) | (X >> ((-Y) & (B - 1)))
2890 : (X << (int) Y) | (X >> (int) ((-Y) & (B - 1)))
2891 : ((T) ((T2) X << Y)) | ((T) ((T2) X >> ((-Y) & (B - 1))))
2892 : ((T) ((T2) X << (int) Y)) | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
2893 :
2894 : transform these into (last 2 only if ranger can prove Y < B
2895 : or Y = N * B):
2896 : X r<< Y
2897 : or
2898 : X r<< (& & (B - 1))
2899 : The latter for the forms with T2 wider than T if ranger can't prove Y < B.
2900 :
2901 : Or for:
2902 : (X << (Y & (B - 1))) | (X >> ((-Y) & (B - 1)))
2903 : (X << (int) (Y & (B - 1))) | (X >> (int) ((-Y) & (B - 1)))
2904 : ((T) ((T2) X << (Y & (B - 1)))) | ((T) ((T2) X >> ((-Y) & (B - 1))))
2905 : ((T) ((T2) X << (int) (Y & (B - 1)))) \
2906 : | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
2907 :
2908 : transform these into:
2909 : X r<< (Y & (B - 1))
2910 :
2911 : Note, in the patterns with T2 type, the type of OP operands
2912 : might be even a signed type, but should have precision B.
2913 : Expressions with & (B - 1) should be recognized only if B is
2914 : a power of 2. */
2915 :
2916 : static bool
2917 10383418 : simplify_rotate (gimple_stmt_iterator *gsi)
2918 : {
2919 10383418 : gimple *stmt = gsi_stmt (*gsi);
2920 10383418 : tree arg[2], rtype, rotcnt = NULL_TREE;
2921 10383418 : tree def_arg1[2], def_arg2[2];
2922 10383418 : enum tree_code def_code[2];
2923 10383418 : tree lhs;
2924 10383418 : int i;
2925 10383418 : bool swapped_p = false;
2926 10383418 : gimple *g;
2927 10383418 : gimple *def_arg_stmt[2] = { NULL, NULL };
2928 10383418 : int wider_prec = 0;
2929 10383418 : bool add_masking = false;
2930 :
2931 10383418 : arg[0] = gimple_assign_rhs1 (stmt);
2932 10383418 : arg[1] = gimple_assign_rhs2 (stmt);
2933 10383418 : rtype = TREE_TYPE (arg[0]);
2934 :
2935 : /* Only create rotates in complete modes. Other cases are not
2936 : expanded properly. */
2937 10383418 : if (!INTEGRAL_TYPE_P (rtype)
2938 10383418 : || !type_has_mode_precision_p (rtype))
2939 : return false;
2940 :
2941 26455158 : for (i = 0; i < 2; i++)
2942 : {
2943 17636772 : defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
2944 17636772 : if (TREE_CODE (arg[i]) == SSA_NAME)
2945 11679352 : def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
2946 : }
2947 :
2948 : /* Look through narrowing (or same precision) conversions. */
2949 7854279 : if (CONVERT_EXPR_CODE_P (def_code[0])
2950 964107 : && CONVERT_EXPR_CODE_P (def_code[1])
2951 140995 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[0]))
2952 117112 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[1]))
2953 109208 : && TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
2954 109208 : == TYPE_PRECISION (TREE_TYPE (def_arg1[1]))
2955 63199 : && TYPE_PRECISION (TREE_TYPE (def_arg1[0])) >= TYPE_PRECISION (rtype)
2956 44288 : && has_single_use (arg[0])
2957 8850165 : && has_single_use (arg[1]))
2958 : {
2959 26631 : wider_prec = TYPE_PRECISION (TREE_TYPE (def_arg1[0]));
2960 79893 : for (i = 0; i < 2; i++)
2961 : {
2962 53262 : arg[i] = def_arg1[i];
2963 53262 : defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
2964 53262 : if (TREE_CODE (arg[i]) == SSA_NAME)
2965 53262 : def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
2966 : }
2967 : }
2968 : else
2969 : {
2970 : /* Handle signed rotate; the RSHIFT_EXPR has to be done
2971 : in unsigned type but LSHIFT_EXPR could be signed. */
2972 8791755 : i = (def_code[0] == LSHIFT_EXPR || def_code[0] == RSHIFT_EXPR);
2973 7836656 : if (CONVERT_EXPR_CODE_P (def_code[i])
2974 955099 : && (def_code[1 - i] == LSHIFT_EXPR || def_code[1 - i] == RSHIFT_EXPR)
2975 32236 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[i]))
2976 30970 : && TYPE_PRECISION (rtype) == TYPE_PRECISION (TREE_TYPE (def_arg1[i]))
2977 8796730 : && has_single_use (arg[i]))
2978 : {
2979 2118 : arg[i] = def_arg1[i];
2980 2118 : defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
2981 2118 : if (TREE_CODE (arg[i]) == SSA_NAME)
2982 2118 : def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
2983 : }
2984 : }
2985 :
2986 : /* One operand has to be LSHIFT_EXPR and one RSHIFT_EXPR. */
2987 9005570 : for (i = 0; i < 2; i++)
2988 8980756 : if (def_code[i] != LSHIFT_EXPR && def_code[i] != RSHIFT_EXPR)
2989 : return false;
2990 228311 : else if (!has_single_use (arg[i]))
2991 : return false;
2992 24814 : if (def_code[0] == def_code[1])
2993 : return false;
2994 :
2995 : /* If we've looked through narrowing conversions before, look through
2996 : widening conversions from unsigned type with the same precision
2997 : as rtype here. */
2998 20450 : if (TYPE_PRECISION (TREE_TYPE (def_arg1[0])) != TYPE_PRECISION (rtype))
2999 19345 : for (i = 0; i < 2; i++)
3000 : {
3001 12897 : tree tem;
3002 12897 : enum tree_code code;
3003 12897 : defcodefor_name (def_arg1[i], &code, &tem, NULL);
3004 1 : if (!CONVERT_EXPR_CODE_P (code)
3005 12896 : || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
3006 25793 : || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
3007 1 : return false;
3008 12896 : def_arg1[i] = tem;
3009 : }
3010 : /* Both shifts have to use the same first operand. */
3011 20449 : if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
3012 32373 : || !types_compatible_p (TREE_TYPE (def_arg1[0]),
3013 11924 : TREE_TYPE (def_arg1[1])))
3014 : {
3015 8525 : if ((TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
3016 8525 : != TYPE_PRECISION (TREE_TYPE (def_arg1[1])))
3017 8525 : || (TYPE_UNSIGNED (TREE_TYPE (def_arg1[0]))
3018 8525 : == TYPE_UNSIGNED (TREE_TYPE (def_arg1[1]))))
3019 8501 : return false;
3020 :
3021 : /* Handle signed rotate; the RSHIFT_EXPR has to be done
3022 : in unsigned type but LSHIFT_EXPR could be signed. */
3023 543 : i = def_code[0] != RSHIFT_EXPR;
3024 543 : if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[i])))
3025 : return false;
3026 :
3027 506 : tree tem;
3028 506 : enum tree_code code;
3029 506 : defcodefor_name (def_arg1[i], &code, &tem, NULL);
3030 303 : if (!CONVERT_EXPR_CODE_P (code)
3031 203 : || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
3032 709 : || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
3033 : return false;
3034 194 : def_arg1[i] = tem;
3035 194 : if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
3036 218 : || !types_compatible_p (TREE_TYPE (def_arg1[0]),
3037 24 : TREE_TYPE (def_arg1[1])))
3038 : return false;
3039 : }
3040 11924 : else if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[0])))
3041 : return false;
3042 :
3043 : /* CNT1 + CNT2 == B case above. */
3044 10693 : if (tree_fits_uhwi_p (def_arg2[0])
3045 1196 : && tree_fits_uhwi_p (def_arg2[1])
3046 10693 : && tree_to_uhwi (def_arg2[0])
3047 1196 : + tree_to_uhwi (def_arg2[1]) == TYPE_PRECISION (rtype))
3048 : rotcnt = def_arg2[0];
3049 9770 : else if (TREE_CODE (def_arg2[0]) != SSA_NAME
3050 9497 : || TREE_CODE (def_arg2[1]) != SSA_NAME)
3051 : return false;
3052 : else
3053 : {
3054 9497 : tree cdef_arg1[2], cdef_arg2[2], def_arg2_alt[2];
3055 9497 : enum tree_code cdef_code[2];
3056 9497 : gimple *def_arg_alt_stmt[2] = { NULL, NULL };
3057 9497 : int check_range = 0;
3058 9497 : gimple *check_range_stmt = NULL;
3059 : /* Look through conversion of the shift count argument.
3060 : The C/C++ FE cast any shift count argument to integer_type_node.
3061 : The only problem might be if the shift count type maximum value
3062 : is equal or smaller than number of bits in rtype. */
3063 28491 : for (i = 0; i < 2; i++)
3064 : {
3065 18994 : def_arg2_alt[i] = def_arg2[i];
3066 18994 : defcodefor_name (def_arg2[i], &cdef_code[i],
3067 : &cdef_arg1[i], &cdef_arg2[i]);
3068 14723 : if (CONVERT_EXPR_CODE_P (cdef_code[i])
3069 4271 : && INTEGRAL_TYPE_P (TREE_TYPE (cdef_arg1[i]))
3070 4271 : && TYPE_PRECISION (TREE_TYPE (cdef_arg1[i]))
3071 4271 : > floor_log2 (TYPE_PRECISION (rtype))
3072 23265 : && type_has_mode_precision_p (TREE_TYPE (cdef_arg1[i])))
3073 : {
3074 4271 : def_arg2_alt[i] = cdef_arg1[i];
3075 4271 : if (TREE_CODE (def_arg2[i]) == SSA_NAME)
3076 4271 : def_arg_alt_stmt[i] = SSA_NAME_DEF_STMT (def_arg2[i]);
3077 4271 : defcodefor_name (def_arg2_alt[i], &cdef_code[i],
3078 : &cdef_arg1[i], &cdef_arg2[i]);
3079 : }
3080 : else
3081 14723 : def_arg_alt_stmt[i] = def_arg_stmt[i];
3082 : }
3083 25816 : for (i = 0; i < 2; i++)
3084 : /* Check for one shift count being Y and the other B - Y,
3085 : with optional casts. */
3086 18643 : if (cdef_code[i] == MINUS_EXPR
3087 862 : && tree_fits_shwi_p (cdef_arg1[i])
3088 862 : && tree_to_shwi (cdef_arg1[i]) == TYPE_PRECISION (rtype)
3089 19465 : && TREE_CODE (cdef_arg2[i]) == SSA_NAME)
3090 : {
3091 822 : tree tem;
3092 822 : enum tree_code code;
3093 :
3094 822 : if (cdef_arg2[i] == def_arg2[1 - i]
3095 472 : || cdef_arg2[i] == def_arg2_alt[1 - i])
3096 : {
3097 350 : rotcnt = cdef_arg2[i];
3098 350 : check_range = -1;
3099 350 : if (cdef_arg2[i] == def_arg2[1 - i])
3100 350 : check_range_stmt = def_arg_stmt[1 - i];
3101 : else
3102 0 : check_range_stmt = def_arg_alt_stmt[1 - i];
3103 806 : break;
3104 : }
3105 472 : defcodefor_name (cdef_arg2[i], &code, &tem, NULL);
3106 16 : if (CONVERT_EXPR_CODE_P (code)
3107 456 : && INTEGRAL_TYPE_P (TREE_TYPE (tem))
3108 456 : && TYPE_PRECISION (TREE_TYPE (tem))
3109 456 : > floor_log2 (TYPE_PRECISION (rtype))
3110 456 : && type_has_mode_precision_p (TREE_TYPE (tem))
3111 928 : && (tem == def_arg2[1 - i]
3112 288 : || tem == def_arg2_alt[1 - i]))
3113 : {
3114 456 : rotcnt = tem;
3115 456 : check_range = -1;
3116 456 : if (tem == def_arg2[1 - i])
3117 168 : check_range_stmt = def_arg_stmt[1 - i];
3118 : else
3119 288 : check_range_stmt = def_arg_alt_stmt[1 - i];
3120 : break;
3121 : }
3122 : }
3123 : /* The above sequence isn't safe for Y being 0,
3124 : because then one of the shifts triggers undefined behavior.
3125 : This alternative is safe even for rotation count of 0.
3126 : One shift count is Y and the other (-Y) & (B - 1).
3127 : Or one shift count is Y & (B - 1) and the other (-Y) & (B - 1). */
3128 17821 : else if (cdef_code[i] == BIT_AND_EXPR
3129 28752 : && pow2p_hwi (TYPE_PRECISION (rtype))
3130 12433 : && tree_fits_shwi_p (cdef_arg2[i])
3131 24866 : && tree_to_shwi (cdef_arg2[i])
3132 12433 : == TYPE_PRECISION (rtype) - 1
3133 12373 : && TREE_CODE (cdef_arg1[i]) == SSA_NAME
3134 30194 : && gimple_assign_rhs_code (stmt) == BIT_IOR_EXPR)
3135 : {
3136 2313 : tree tem;
3137 2313 : enum tree_code code;
3138 :
3139 2313 : defcodefor_name (cdef_arg1[i], &code, &tem, NULL);
3140 2114 : if (CONVERT_EXPR_CODE_P (code)
3141 199 : && INTEGRAL_TYPE_P (TREE_TYPE (tem))
3142 199 : && TYPE_PRECISION (TREE_TYPE (tem))
3143 199 : > floor_log2 (TYPE_PRECISION (rtype))
3144 2512 : && type_has_mode_precision_p (TREE_TYPE (tem)))
3145 199 : defcodefor_name (tem, &code, &tem, NULL);
3146 :
3147 2313 : if (code == NEGATE_EXPR)
3148 : {
3149 1532 : if (tem == def_arg2[1 - i] || tem == def_arg2_alt[1 - i])
3150 : {
3151 853 : rotcnt = tem;
3152 853 : check_range = 1;
3153 853 : if (tem == def_arg2[1 - i])
3154 845 : check_range_stmt = def_arg_stmt[1 - i];
3155 : else
3156 8 : check_range_stmt = def_arg_alt_stmt[1 - i];
3157 1518 : break;
3158 : }
3159 679 : tree tem2;
3160 679 : defcodefor_name (tem, &code, &tem2, NULL);
3161 237 : if (CONVERT_EXPR_CODE_P (code)
3162 442 : && INTEGRAL_TYPE_P (TREE_TYPE (tem2))
3163 442 : && TYPE_PRECISION (TREE_TYPE (tem2))
3164 442 : > floor_log2 (TYPE_PRECISION (rtype))
3165 1121 : && type_has_mode_precision_p (TREE_TYPE (tem2)))
3166 : {
3167 442 : if (tem2 == def_arg2[1 - i]
3168 442 : || tem2 == def_arg2_alt[1 - i])
3169 : {
3170 228 : rotcnt = tem2;
3171 228 : check_range = 1;
3172 228 : if (tem2 == def_arg2[1 - i])
3173 0 : check_range_stmt = def_arg_stmt[1 - i];
3174 : else
3175 228 : check_range_stmt = def_arg_alt_stmt[1 - i];
3176 : break;
3177 : }
3178 : }
3179 : else
3180 237 : tem2 = NULL_TREE;
3181 :
3182 451 : if (cdef_code[1 - i] == BIT_AND_EXPR
3183 438 : && tree_fits_shwi_p (cdef_arg2[1 - i])
3184 876 : && tree_to_shwi (cdef_arg2[1 - i])
3185 438 : == TYPE_PRECISION (rtype) - 1
3186 889 : && TREE_CODE (cdef_arg1[1 - i]) == SSA_NAME)
3187 : {
3188 438 : if (tem == cdef_arg1[1 - i]
3189 213 : || tem2 == cdef_arg1[1 - i])
3190 : {
3191 : rotcnt = def_arg2[1 - i];
3192 437 : break;
3193 : }
3194 193 : tree tem3;
3195 193 : defcodefor_name (cdef_arg1[1 - i], &code, &tem3, NULL);
3196 0 : if (CONVERT_EXPR_CODE_P (code)
3197 193 : && INTEGRAL_TYPE_P (TREE_TYPE (tem3))
3198 193 : && TYPE_PRECISION (TREE_TYPE (tem3))
3199 193 : > floor_log2 (TYPE_PRECISION (rtype))
3200 386 : && type_has_mode_precision_p (TREE_TYPE (tem3)))
3201 : {
3202 193 : if (tem == tem3 || tem2 == tem3)
3203 : {
3204 : rotcnt = def_arg2[1 - i];
3205 : break;
3206 : }
3207 : }
3208 : }
3209 : }
3210 : }
3211 2324 : if (check_range && wider_prec > TYPE_PRECISION (rtype))
3212 : {
3213 1533 : if (TREE_CODE (rotcnt) != SSA_NAME)
3214 573 : return false;
3215 1533 : int_range_max r;
3216 1533 : range_query *q = get_range_query (cfun);
3217 1533 : if (q == get_global_range_query ())
3218 1522 : q = enable_ranger (cfun);
3219 1533 : if (!q->range_of_expr (r, rotcnt, check_range_stmt))
3220 : {
3221 0 : if (check_range > 0)
3222 : return false;
3223 0 : r.set_varying (TREE_TYPE (rotcnt));
3224 : }
3225 1533 : int prec = TYPE_PRECISION (TREE_TYPE (rotcnt));
3226 1533 : signop sign = TYPE_SIGN (TREE_TYPE (rotcnt));
3227 1533 : wide_int min = wide_int::from (TYPE_PRECISION (rtype), prec, sign);
3228 1533 : wide_int max = wide_int::from (wider_prec - 1, prec, sign);
3229 1533 : if (check_range < 0)
3230 616 : max = min;
3231 1533 : int_range<1> r2 (TREE_TYPE (rotcnt), min, max);
3232 1533 : r.intersect (r2);
3233 1533 : if (!r.undefined_p ())
3234 : {
3235 1181 : if (check_range > 0)
3236 : {
3237 589 : int_range_max r3;
3238 1844 : for (int i = TYPE_PRECISION (rtype) + 1; i < wider_prec;
3239 1255 : i += TYPE_PRECISION (rtype))
3240 : {
3241 1255 : int j = i + TYPE_PRECISION (rtype) - 2;
3242 1255 : min = wide_int::from (i, prec, sign);
3243 1255 : max = wide_int::from (MIN (j, wider_prec - 1),
3244 1255 : prec, sign);
3245 1255 : int_range<1> r4 (TREE_TYPE (rotcnt), min, max);
3246 1255 : r3.union_ (r4);
3247 1255 : }
3248 589 : r.intersect (r3);
3249 589 : if (!r.undefined_p ())
3250 573 : return false;
3251 589 : }
3252 : add_masking = true;
3253 : }
3254 1533 : }
3255 8924 : if (rotcnt == NULL_TREE)
3256 : return false;
3257 1751 : swapped_p = i != 1;
3258 : }
3259 :
3260 2674 : if (!useless_type_conversion_p (TREE_TYPE (def_arg2[0]),
3261 2674 : TREE_TYPE (rotcnt)))
3262 : {
3263 496 : g = gimple_build_assign (make_ssa_name (TREE_TYPE (def_arg2[0])),
3264 : NOP_EXPR, rotcnt);
3265 496 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
3266 496 : rotcnt = gimple_assign_lhs (g);
3267 : }
3268 2674 : if (add_masking)
3269 : {
3270 608 : g = gimple_build_assign (make_ssa_name (TREE_TYPE (rotcnt)),
3271 : BIT_AND_EXPR, rotcnt,
3272 608 : build_int_cst (TREE_TYPE (rotcnt),
3273 608 : TYPE_PRECISION (rtype) - 1));
3274 608 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
3275 608 : rotcnt = gimple_assign_lhs (g);
3276 : }
3277 2674 : lhs = gimple_assign_lhs (stmt);
3278 2674 : if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0])))
3279 1010 : lhs = make_ssa_name (TREE_TYPE (def_arg1[0]));
3280 2674 : g = gimple_build_assign (lhs,
3281 2674 : ((def_code[0] == LSHIFT_EXPR) ^ swapped_p)
3282 : ? LROTATE_EXPR : RROTATE_EXPR, def_arg1[0], rotcnt);
3283 2674 : if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0])))
3284 : {
3285 1010 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
3286 1010 : g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, lhs);
3287 : }
3288 2674 : gsi_replace (gsi, g, false);
3289 2674 : return true;
3290 : }
3291 :
3292 :
3293 : /* Check whether an array contains a valid table according to VALIDATE_FN. */
3294 : template<typename ValidateFn>
3295 : static bool
3296 34 : check_table_array (tree ctor, HOST_WIDE_INT &zero_val, unsigned bits,
3297 : ValidateFn validate_fn)
3298 : {
3299 : tree elt, idx;
3300 34 : unsigned HOST_WIDE_INT i, raw_idx = 0;
3301 34 : unsigned matched = 0;
3302 :
3303 34 : zero_val = 0;
3304 :
3305 1278 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, elt)
3306 : {
3307 1278 : if (!tree_fits_shwi_p (idx))
3308 : return false;
3309 1278 : if (!tree_fits_shwi_p (elt) && TREE_CODE (elt) != RAW_DATA_CST)
3310 : return false;
3311 :
3312 1278 : unsigned HOST_WIDE_INT index = tree_to_shwi (idx);
3313 : HOST_WIDE_INT val;
3314 :
3315 1278 : if (TREE_CODE (elt) == INTEGER_CST)
3316 1214 : val = tree_to_shwi (elt);
3317 : else
3318 : {
3319 64 : if (raw_idx == (unsigned) RAW_DATA_LENGTH (elt))
3320 : {
3321 0 : raw_idx = 0;
3322 0 : continue;
3323 : }
3324 64 : if (TYPE_UNSIGNED (TREE_TYPE (elt)))
3325 0 : val = RAW_DATA_UCHAR_ELT (elt, raw_idx);
3326 : else
3327 64 : val = RAW_DATA_SCHAR_ELT (elt, raw_idx);
3328 64 : index += raw_idx;
3329 64 : raw_idx++;
3330 64 : i--;
3331 : }
3332 :
3333 1278 : if (index > bits * 2)
3334 : return false;
3335 :
3336 1278 : if (index == 0)
3337 : {
3338 34 : zero_val = val;
3339 34 : matched++;
3340 : }
3341 :
3342 1278 : if (val >= 0 && val < bits && validate_fn (val, index))
3343 1216 : matched++;
3344 :
3345 1278 : if (matched > bits)
3346 : return true;
3347 : }
3348 :
3349 : return false;
3350 : }
3351 :
3352 : /* Check whether a string contains a valid table according to VALIDATE_FN. */
3353 : template<typename ValidateFn>
3354 : static bool
3355 21 : check_table_string (tree string, HOST_WIDE_INT &zero_val,unsigned bits,
3356 : ValidateFn validate_fn)
3357 : {
3358 21 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (string);
3359 21 : unsigned matched = 0;
3360 21 : const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (string);
3361 :
3362 21 : if (len < bits || len > bits * 2)
3363 : return false;
3364 :
3365 21 : zero_val = p[0];
3366 :
3367 725 : for (unsigned i = 0; i < len; i++)
3368 704 : if (p[i] < bits && validate_fn (p[i], i))
3369 704 : matched++;
3370 :
3371 21 : return matched == bits;
3372 : }
3373 :
3374 : /* Check whether CTOR contains a valid table according to VALIDATE_FN. */
3375 : template<typename ValidateFn>
3376 : static bool
3377 63 : check_table (tree ctor, tree type, HOST_WIDE_INT &zero_val, unsigned bits,
3378 : ValidateFn validate_fn)
3379 : {
3380 63 : if (TREE_CODE (ctor) == CONSTRUCTOR)
3381 34 : return check_table_array (ctor, zero_val, bits, validate_fn);
3382 : else if (TREE_CODE (ctor) == STRING_CST
3383 29 : && TYPE_PRECISION (type) == CHAR_TYPE_SIZE)
3384 21 : return check_table_string (ctor, zero_val, bits, validate_fn);
3385 : return false;
3386 : }
3387 :
3388 : /* Match.pd function to match the ctz expression. */
3389 : extern bool gimple_ctz_table_index (tree, tree *, tree (*)(tree));
3390 : extern bool gimple_clz_table_index (tree, tree *, tree (*)(tree));
3391 : extern bool gimple_clz_msb_iso_table_index (tree, tree *, tree (*)(tree));
3392 :
3393 : /* Recognize count leading and trailing zeroes idioms.
3394 : The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
3395 : constant which when multiplied by a power of 2 creates a unique value
3396 : in the top 5 or 6 bits. This is then indexed into a table which maps it
3397 : to the number of trailing zeroes. Array[0] is returned so the caller can
3398 : emit an appropriate sequence depending on whether ctz (0) is defined on
3399 : the target. */
3400 :
3401 : static bool
3402 2012230 : simplify_count_zeroes (gimple_stmt_iterator *gsi)
3403 : {
3404 2012230 : gimple *stmt = gsi_stmt (*gsi);
3405 2012230 : tree array_ref = gimple_assign_rhs1 (stmt);
3406 2012230 : tree res_ops[3];
3407 :
3408 2012230 : gcc_checking_assert (TREE_CODE (array_ref) == ARRAY_REF);
3409 :
3410 2012230 : internal_fn fn = IFN_LAST;
3411 : /* When true, the matched idiom is a CLZ using DeBruijn CTZ on the
3412 : isolated MSB -- see clz_msb_iso_table_index in match.pd. The
3413 : table stores MSB positions and must satisfy the direct CTZ
3414 : DeBruijn property, so we validate it with the CTZ checkfn even
3415 : though we emit IFN_CLZ code. */
3416 2012230 : bool clz_via_ctz = false;
3417 : /* For CTZ we recognize ((x & -x) * C) >> SHIFT where the array data
3418 : represents the number of trailing zeros. */
3419 2012230 : if (gimple_ctz_table_index (TREE_OPERAND (array_ref, 1), &res_ops[0], NULL))
3420 : fn = IFN_CTZ;
3421 : /* For CLZ we recognize
3422 : x |= x >> 1;
3423 : x |= x >> 2;
3424 : x |= x >> 4;
3425 : x |= x >> 8;
3426 : x |= x >> 16;
3427 : (x * C) >> SHIFT
3428 : where 31 minus the array data represents the number of leading zeros. */
3429 2012190 : else if (gimple_clz_table_index (TREE_OPERAND (array_ref, 1), &res_ops[0],
3430 : NULL))
3431 : fn = IFN_CLZ;
3432 : /* Variant CLZ idiom: after the OR-cascade sets all bits from 0 to
3433 : the original MSB, (value - (value >> 1)) isolates the MSB as a
3434 : power of two (2^k), and the subsequent DeBruijn multiply-and-shift
3435 : is a CTZ-style lookup on 2^k. The table stores MSB positions
3436 : directly. */
3437 2012163 : else if (gimple_clz_msb_iso_table_index (TREE_OPERAND (array_ref, 1),
3438 : &res_ops[0], NULL))
3439 : {
3440 : fn = IFN_CLZ;
3441 : clz_via_ctz = true;
3442 : }
3443 : else
3444 : return false;
3445 :
3446 68 : HOST_WIDE_INT zero_val;
3447 68 : tree type = TREE_TYPE (array_ref);
3448 68 : tree array = TREE_OPERAND (array_ref, 0);
3449 68 : tree input_type = TREE_TYPE (res_ops[0]);
3450 68 : unsigned input_bits = tree_to_shwi (TYPE_SIZE (input_type));
3451 :
3452 : /* Check the array element type is integral and not wider than 64 bits,
3453 : and the input is an unsigned 32-bit or 64-bit type. The table values
3454 : are bit positions in [0, input_bits - 1], so any integer element type
3455 : with at least 6 bits of precision suffices; the cap is just to keep
3456 : the transformation simple. */
3457 68 : if (!INTEGRAL_TYPE_P (type) || TYPE_PRECISION (type) > 64
3458 136 : || !TYPE_UNSIGNED (input_type))
3459 : return false;
3460 64 : if (input_bits != 32 && input_bits != 64)
3461 : return false;
3462 :
3463 64 : if (!direct_internal_fn_supported_p (fn, input_type, OPTIMIZE_FOR_BOTH))
3464 : return false;
3465 :
3466 : /* Check the lower bound of the array is zero. */
3467 64 : tree low = array_ref_low_bound (array_ref);
3468 64 : if (!low || !integer_zerop (low))
3469 : return false;
3470 :
3471 : /* Check the shift extracts the top 5..7 bits. */
3472 64 : unsigned shiftval = tree_to_shwi (res_ops[2]);
3473 64 : if (shiftval < input_bits - 7 || shiftval > input_bits - 5)
3474 : return false;
3475 :
3476 63 : tree ctor = ctor_for_folding (array);
3477 63 : if (!ctor)
3478 : return false;
3479 63 : unsigned HOST_WIDE_INT mulval = tree_to_uhwi (res_ops[1]);
3480 : /* CTZ and the MSB-isolation CLZ variant both use the direct CTZ
3481 : DeBruijn check (table[(magic << data) >> shift] == data). */
3482 63 : if (fn == IFN_CTZ || clz_via_ctz)
3483 : {
3484 1120 : auto checkfn = [&](unsigned data, unsigned i) -> bool
3485 : {
3486 1084 : unsigned HOST_WIDE_INT mask
3487 1084 : = ((HOST_WIDE_INT_1U << (input_bits - shiftval)) - 1) << shiftval;
3488 1084 : return (((mulval << data) & mask) >> shiftval) == i;
3489 36 : };
3490 36 : if (!check_table (ctor, type, zero_val, input_bits, checkfn))
3491 8 : return false;
3492 : }
3493 27 : else if (fn == IFN_CLZ)
3494 : {
3495 923 : auto checkfn = [&](unsigned data, unsigned i) -> bool
3496 : {
3497 896 : unsigned HOST_WIDE_INT mask
3498 896 : = ((HOST_WIDE_INT_1U << (input_bits - shiftval)) - 1) << shiftval;
3499 : /* The OR-cascade produces a value with all bits from 0 to the
3500 : original MSB set. Compute (1 << (data + 1)) - 1 to simulate
3501 : that value. When data + 1 equals HOST_BITS_PER_WIDE_INT
3502 : (i.e. data is the MSB position of a 64-bit input) the shift
3503 : is undefined behavior, so handle that case explicitly using
3504 : all-ones. Without this, any well-formed 64-bit DeBruijn CLZ
3505 : table is rejected because its entry for the all-ones input
3506 : correctly maps to the MSB (e.g. table[...] == 63).
3507 : PR tree-optimization/122569. */
3508 1791 : unsigned HOST_WIDE_INT all_bits_below
3509 : = (data + 1 == HOST_BITS_PER_WIDE_INT)
3510 896 : ? HOST_WIDE_INT_M1U
3511 895 : : ((HOST_WIDE_INT_1U << (data + 1)) - 1);
3512 896 : return (((all_bits_below * mulval) & mask) >> shiftval) == i;
3513 27 : };
3514 27 : if (!check_table (ctor, type, zero_val, input_bits, checkfn))
3515 0 : return false;
3516 : }
3517 :
3518 55 : HOST_WIDE_INT ctz_val = -1;
3519 55 : bool zero_ok;
3520 55 : if (fn == IFN_CTZ)
3521 : {
3522 27 : ctz_val = 0;
3523 54 : zero_ok = CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (input_type),
3524 : ctz_val) == 2;
3525 : }
3526 28 : else if (fn == IFN_CLZ)
3527 : {
3528 28 : ctz_val = 32;
3529 28 : zero_ok = CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (input_type),
3530 : ctz_val) == 2;
3531 28 : zero_val = input_bits - 1 - zero_val;
3532 : }
3533 : /* Pass the value at zero only if the target defines it; otherwise the
3534 : COND_EXPR below supplies the result for zero. */
3535 55 : int nargs = zero_ok ? 2 : 1;
3536 :
3537 : /* If the input value can't be zero, don't special case ctz (0). */
3538 55 : range_query *q = get_range_query (cfun);
3539 55 : if (q == get_global_range_query ())
3540 55 : q = enable_ranger (cfun);
3541 55 : int_range_max vr;
3542 55 : if (q->range_of_expr (vr, res_ops[0], stmt)
3543 55 : && !range_includes_zero_p (vr))
3544 : {
3545 6 : zero_ok = true;
3546 6 : zero_val = 0;
3547 6 : ctz_val = 0;
3548 6 : nargs = 1;
3549 : }
3550 :
3551 55 : gimple_seq seq = NULL;
3552 55 : gimple *g;
3553 63 : gcall *call = gimple_build_call_internal (fn, nargs, res_ops[0],
3554 : nargs == 1 ? NULL_TREE
3555 63 : : build_int_cst (integer_type_node,
3556 8 : ctz_val));
3557 55 : gimple_set_location (call, gimple_location (stmt));
3558 55 : gimple_set_lhs (call, make_ssa_name (integer_type_node));
3559 55 : gimple_seq_add_stmt (&seq, call);
3560 :
3561 55 : tree prev_lhs = gimple_call_lhs (call);
3562 :
3563 55 : if (zero_ok && zero_val == ctz_val)
3564 : ;
3565 : /* Emit ctz (x) & 31 if ctz (0) is 32 but we need to return 0. */
3566 6 : else if (zero_ok && zero_val == 0 && ctz_val == input_bits)
3567 : {
3568 5 : g = gimple_build_assign (make_ssa_name (integer_type_node),
3569 : BIT_AND_EXPR, prev_lhs,
3570 : build_int_cst (integer_type_node,
3571 5 : input_bits - 1));
3572 5 : gimple_set_location (g, gimple_location (stmt));
3573 5 : gimple_seq_add_stmt (&seq, g);
3574 5 : prev_lhs = gimple_assign_lhs (g);
3575 : }
3576 : /* As fallback emit a conditional move. */
3577 : else
3578 : {
3579 42 : g = gimple_build_assign (make_ssa_name (boolean_type_node), EQ_EXPR,
3580 : res_ops[0], build_zero_cst (input_type));
3581 42 : gimple_set_location (g, gimple_location (stmt));
3582 42 : gimple_seq_add_stmt (&seq, g);
3583 42 : tree cond = gimple_assign_lhs (g);
3584 42 : g = gimple_build_assign (make_ssa_name (integer_type_node),
3585 : COND_EXPR, cond,
3586 42 : build_int_cst (integer_type_node, zero_val),
3587 : prev_lhs);
3588 42 : gimple_set_location (g, gimple_location (stmt));
3589 42 : gimple_seq_add_stmt (&seq, g);
3590 42 : prev_lhs = gimple_assign_lhs (g);
3591 : }
3592 :
3593 55 : if (fn == IFN_CLZ)
3594 : {
3595 28 : g = gimple_build_assign (make_ssa_name (integer_type_node),
3596 : MINUS_EXPR,
3597 : build_int_cst (integer_type_node,
3598 28 : input_bits - 1),
3599 : prev_lhs);
3600 28 : gimple_set_location (g, gimple_location (stmt));
3601 28 : gimple_seq_add_stmt (&seq, g);
3602 28 : prev_lhs = gimple_assign_lhs (g);
3603 : }
3604 :
3605 55 : g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, prev_lhs);
3606 55 : gimple_seq_add_stmt (&seq, g);
3607 55 : gsi_replace_with_seq (gsi, seq, true);
3608 55 : return true;
3609 55 : }
3610 :
3611 : /* Long-multiply fold framework.
3612 :
3613 : Walks the outer addition or bit_ior chain on a candidate statement,
3614 : classifies each summand against the atom match patterns from
3615 : match.pd, and looks the resulting multiset of (kind, extract) tuples
3616 : up in a table. On a hit, three cross-summand consistency checks
3617 : decide whether the wide multiply is emitted. */
3618 :
3619 : /* Match.pd recognizers for the conditional carry-add pattern. */
3620 :
3621 : extern bool gimple_cond_carry_add (tree, tree *, tree (*)(tree));
3622 :
3623 : /* Match.pd functions to match long multiplication. */
3624 :
3625 : extern bool gimple_mul_hi (tree, tree *, tree (*)(tree));
3626 : extern bool gimple_mul_lo (tree, tree *, tree (*)(tree));
3627 : extern bool gimple_mul_hilo (tree, tree *, tree (*)(tree));
3628 : extern bool gimple_mul_lolo (tree, tree *, tree (*)(tree));
3629 : extern bool gimple_mul_hihi (tree, tree *, tree (*)(tree));
3630 : extern bool gimple_mul_cross_sum (tree, tree *, tree (*)(tree));
3631 : extern bool gimple_mul_low_sum (tree, tree *, tree (*)(tree));
3632 : extern bool gimple_mul_low_accum (tree, tree *, tree (*)(tree));
3633 : extern bool gimple_mul_carry_cross_sum (tree, tree *, tree (*)(tree));
3634 : extern bool gimple_mul_carry_low_sum (tree, tree *, tree (*)(tree));
3635 : extern bool gimple_mul_carry_low (tree, tree *, tree (*)(tree));
3636 : extern bool gimple_mul_ladder_sum1 (tree, tree *, tree (*)(tree));
3637 : extern bool gimple_mul_ladder_sum2 (tree, tree *, tree (*)(tree));
3638 : extern bool gimple_mul_ladder_sum3 (tree, tree *, tree (*)(tree));
3639 : extern bool gimple_mul_ladder_part_sum (tree, tree *, tree (*)(tree));
3640 :
3641 : /* Append to SEQ statements assigning DEST the high-part multiply of
3642 : OP1 and OP2, emitted as
3643 : (N)(((2N) op1 * (2N) op2) >> N).
3644 : pass_optimize_widening_mul's convert_mult_to_widen and
3645 : convert_mult_to_highpart later rewrite this to a single
3646 : WIDEN_MULT_EXPR or MULT_HIGHPART_EXPR when the target supports it,
3647 : otherwise the 2N multiply expands directly. Emitting the canonical
3648 : widening shape keeps target-capability decisions in the layer that
3649 : already owns them. */
3650 :
3651 : static void
3652 1919 : build_mul_high_seq (tree op1, tree op2, tree dest, location_t loc,
3653 : gimple_seq *seq)
3654 : {
3655 1919 : tree op_type = TREE_TYPE (op1);
3656 1919 : unsigned int width = TYPE_PRECISION (op_type);
3657 1919 : tree wide_type = build_nonstandard_integer_type (width * 2, 1);
3658 :
3659 1919 : tree wide_a = gimple_convert (seq, loc, wide_type, op1);
3660 1919 : tree wide_b = gimple_convert (seq, loc, wide_type, op2);
3661 1919 : tree wide_prod = gimple_build (seq, loc, MULT_EXPR, wide_type,
3662 : wide_a, wide_b);
3663 1919 : tree hi = gimple_build (seq, loc, RSHIFT_EXPR, wide_type, wide_prod,
3664 1919 : build_int_cst (integer_type_node, width));
3665 :
3666 1919 : gimple *prod = gimple_build_assign (dest, NOP_EXPR, hi);
3667 1919 : gimple_set_location (prod, loc);
3668 1919 : gimple_seq_add_stmt (seq, prod);
3669 1919 : }
3670 :
3671 : /* Append to SEQ statements combining ACC with each of EXTRAS under
3672 : OUTER, the last one assigning to STMT's lhs. EXTRAS are leaves of
3673 : STMT's own chain, so any combining order is valid. */
3674 :
3675 : static void
3676 361 : long_mul_apply_extras (tree acc, const vec<tree> &extras, tree_code outer,
3677 : gassign *stmt, gimple_seq *seq)
3678 : {
3679 361 : location_t loc = gimple_location (stmt);
3680 361 : tree lhs = gimple_assign_lhs (stmt);
3681 388 : for (unsigned i = 0; i + 1 < extras.length (); i++)
3682 27 : acc = gimple_build (seq, loc, outer, TREE_TYPE (lhs), acc, extras[i]);
3683 361 : gimple *last = gimple_build_assign (lhs, outer, acc, extras.last ());
3684 361 : gimple_set_location (last, loc);
3685 361 : gimple_seq_add_stmt (seq, last);
3686 361 : }
3687 :
3688 : /* Replace STMT with a high-part multiply of OP1 and OP2, combining any
3689 : EXTRAS back on top under OUTER. */
3690 :
3691 : static void
3692 1905 : create_mul_high_seq (tree op1, tree op2, gassign *stmt,
3693 : const vec<tree> &extras, tree_code outer)
3694 : {
3695 1905 : gimple_seq seq = NULL;
3696 1905 : tree lhs = gimple_assign_lhs (stmt);
3697 2266 : tree dest = extras.is_empty () ? lhs : make_ssa_name (TREE_TYPE (lhs));
3698 1905 : build_mul_high_seq (op1, op2, dest, gimple_location (stmt), &seq);
3699 1905 : if (!extras.is_empty ())
3700 361 : long_mul_apply_extras (dest, extras, outer, stmt, &seq);
3701 1905 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
3702 1905 : gsi_replace_with_seq (&gsi, seq, true);
3703 1905 : }
3704 :
3705 : /* Replace STMT with a low-part multiply of OP1 and OP2, combining any
3706 : EXTRAS back on top under OUTER. */
3707 :
3708 : static void
3709 32 : create_mul_low_seq (tree op1, tree op2, gassign *stmt,
3710 : const vec<tree> &extras, tree_code outer)
3711 : {
3712 32 : gimple_seq seq = NULL;
3713 32 : tree lhs = gimple_assign_lhs (stmt);
3714 32 : tree dest = extras.is_empty () ? lhs : make_ssa_name (TREE_TYPE (lhs));
3715 32 : gimple *prod = gimple_build_assign (dest, MULT_EXPR, op1, op2);
3716 32 : gimple_set_location (prod, gimple_location (stmt));
3717 32 : gimple_seq_add_stmt (&seq, prod);
3718 32 : if (!extras.is_empty ())
3719 0 : long_mul_apply_extras (dest, extras, outer, stmt, &seq);
3720 32 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
3721 32 : gsi_replace_with_seq (&gsi, seq, true);
3722 32 : }
3723 :
3724 : /* Widest match.pd atom (mul_carry_low_sum) takes 7 captures; round up
3725 : to 8 for the scratch buffers below. */
3726 : static constexpr unsigned LONG_MUL_MAX_CAPTURES = 8;
3727 :
3728 : /* Longest variant in long_mul_table has 4 summands. */
3729 : static constexpr unsigned LONG_MUL_MAX_SUMMANDS = 4;
3730 :
3731 : /* Cap on the leaves set aside as not part of the idiom, so an
3732 : arbitrarily long unrelated chain still bails early. */
3733 : static constexpr unsigned LONG_MUL_MAX_EXTRAS = 4;
3734 :
3735 : namespace {
3736 :
3737 : enum long_mul_kind : unsigned char {
3738 : LMK_INVALID,
3739 : LMK_MUL_HIHI,
3740 : LMK_MUL_LOLO,
3741 : LMK_MUL_HILO,
3742 : LMK_CROSS_SUM,
3743 : LMK_LOW_ACCUM,
3744 : LMK_LOW_SUM,
3745 : LMK_LADDER_SUM1,
3746 : LMK_LADDER_SUM2,
3747 : LMK_LADDER_SUM3,
3748 : LMK_LADDER_PART_SUM,
3749 : LMK_CARRY_LOW,
3750 : LMK_CARRY_CROSS_SUM,
3751 : LMK_CARRY_LOW_SUM,
3752 : };
3753 :
3754 : /* How the leaf wraps its inner kind. Carry kinds use LMX_NONE: their
3755 : match.pd pattern bakes the lshift in, so the leaf is already the
3756 : complete carry expression. */
3757 :
3758 : enum long_mul_extract : unsigned char {
3759 : LMX_NONE,
3760 : LMX_HI,
3761 : LMX_LO,
3762 : LMX_SHL_N,
3763 : };
3764 :
3765 : struct long_mul_summand {
3766 7065817 : long_mul_summand () {}
3767 : long_mul_summand (tree);
3768 : long_mul_summand (const long_mul_summand &) = default;
3769 : long_mul_kind kind = LMK_INVALID;
3770 : long_mul_extract extract = LMX_NONE;
3771 : tree op0 = NULL_TREE, op1 = NULL_TREE;
3772 : tree hilo[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
3773 : tree carry_a = NULL_TREE, carry_b = NULL_TREE;
3774 : unsigned HOST_WIDE_INT shift = 0;
3775 7046714 : operator bool () { return kind != LMK_INVALID; }
3776 : private:
3777 : bool classify_carry (tree leaf);
3778 : bool classify_hi_extract (tree, unsigned HOST_WIDE_INT);
3779 : bool classify_lo_extract (tree);
3780 : bool classify_shl_extract (tree, unsigned HOST_WIDE_INT);
3781 : bool classify_plus_kinds (tree);
3782 : bool classify_bare (tree);
3783 : void set (long_mul_kind kind, const tree *res_ops);
3784 : };
3785 :
3786 : }
3787 :
3788 : /* Walk the OUTER addition or BIT_IOR chain rooted at STMT and collect
3789 : the leaf operands into LEAVES. Descends through single-use
3790 : intermediate stmts of the same code. Returns false once the leaf
3791 : count exceeds LONG_MUL_MAX_SUMMANDS + LONG_MUL_MAX_EXTRAS, so an
3792 : overlong chain bails mid-walk instead of after a full traversal.
3793 :
3794 : If SHARED_DEF_OUT is non-NULL, record there the first inner stmt that
3795 : shares the outer code but has more than one use -- descending into it
3796 : would change semantics, so it stays as a leaf. Such a leaf often
3797 : classifies as something no row matches, silently disabling the fold;
3798 : the caller surfaces this as a dump-file hint. */
3799 :
3800 : static bool
3801 3444063 : long_mul_linearize_chain (gimple *stmt, tree_code outer, vec<tree> &leaves,
3802 : gimple **shared_def_out = NULL)
3803 : {
3804 3444063 : auto_vec<tree, 8> stack;
3805 3444063 : stack.safe_push (gimple_assign_rhs2 (stmt));
3806 3444063 : stack.safe_push (gimple_assign_rhs1 (stmt));
3807 :
3808 10678190 : while (!stack.is_empty ())
3809 : {
3810 7234922 : tree t = stack.pop ();
3811 7234922 : if (TREE_CODE (t) == SSA_NAME)
3812 : {
3813 4736171 : gimple *def = SSA_NAME_DEF_STMT (t);
3814 4736171 : if (def
3815 4736171 : && is_gimple_assign (def)
3816 8008688 : && gimple_assign_rhs_code (def) == outer)
3817 : {
3818 338399 : if (has_single_use (t))
3819 : {
3820 175743 : stack.safe_push (gimple_assign_rhs2 (def));
3821 175743 : stack.safe_push (gimple_assign_rhs1 (def));
3822 175743 : continue;
3823 : }
3824 162656 : if (shared_def_out && !*shared_def_out)
3825 147581 : *shared_def_out = def;
3826 : }
3827 : }
3828 7059179 : leaves.safe_push (t);
3829 7059179 : if (leaves.length () > LONG_MUL_MAX_SUMMANDS + LONG_MUL_MAX_EXTRAS)
3830 795 : return false;
3831 : }
3832 6886536 : return !leaves.is_empty ();
3833 3444063 : }
3834 :
3835 : /* If EXPR is defined by LSHIFT_EXPR with a uhwi-valued amount, return
3836 : the shifted input via *INNER_OUT and the amount via *SHIFT_OUT. */
3837 :
3838 : static bool
3839 6986908 : long_mul_is_lshift_def (tree expr, tree *inner_out,
3840 : unsigned HOST_WIDE_INT *shift_out)
3841 : {
3842 6986908 : if (TREE_CODE (expr) != SSA_NAME)
3843 : return false;
3844 4488643 : gimple *def = SSA_NAME_DEF_STMT (expr);
3845 4488643 : if (!def || !is_gimple_assign (def)
3846 7514393 : || gimple_assign_rhs_code (def) != LSHIFT_EXPR)
3847 : return false;
3848 84991 : tree amount = gimple_assign_rhs2 (def);
3849 84991 : if (!tree_fits_uhwi_p (amount))
3850 : return false;
3851 58742 : *inner_out = gimple_assign_rhs1 (def);
3852 58742 : *shift_out = tree_to_uhwi (amount);
3853 58742 : return true;
3854 : }
3855 :
3856 : /* Fill THIS's kind plus the captures from RES_OPS that the kind requires.
3857 : The kind itself determines how many (op0, op1) and hilo captures to
3858 : pick up from RES_OPS, and whether a baked-in shift is present. */
3859 :
3860 : void
3861 55479 : long_mul_summand::set (long_mul_kind kind, const tree *res_ops)
3862 : {
3863 55479 : this->kind = kind;
3864 55479 : unsigned n_ops = 0;
3865 55479 : unsigned n_hilos = 0;
3866 55479 : int shift_idx = -1;
3867 53032 : switch (kind)
3868 : {
3869 0 : default:
3870 0 : gcc_unreachable ();
3871 : case LMK_MUL_HIHI:
3872 : case LMK_MUL_LOLO:
3873 : case LMK_MUL_HILO:
3874 : n_ops = 2;
3875 : break;
3876 5737 : case LMK_CROSS_SUM:
3877 5737 : n_hilos = 2;
3878 5737 : break;
3879 18568 : case LMK_LOW_ACCUM:
3880 18568 : case LMK_LOW_SUM:
3881 18568 : case LMK_LADDER_SUM1:
3882 18568 : case LMK_LADDER_SUM2:
3883 18568 : case LMK_LADDER_SUM3:
3884 18568 : n_ops = 2;
3885 18568 : n_hilos = 2;
3886 18568 : break;
3887 135 : case LMK_LADDER_PART_SUM:
3888 135 : n_ops = 2;
3889 135 : n_hilos = 1;
3890 135 : break;
3891 : case LMK_CARRY_CROSS_SUM:
3892 : n_hilos = 3;
3893 : shift_idx = 3;
3894 : break;
3895 106 : case LMK_CARRY_LOW_SUM:
3896 106 : n_ops = 2;
3897 106 : n_hilos = 3;
3898 106 : shift_idx = 5;
3899 106 : break;
3900 2447 : case LMK_CARRY_LOW:
3901 2447 : this->carry_a = res_ops[0];
3902 2447 : this->carry_b = res_ops[1];
3903 0 : return;
3904 : }
3905 24546 : if (n_ops >= 1)
3906 47125 : this->op0 = res_ops[0];
3907 47125 : if (n_ops >= 2)
3908 47125 : this->op1 = res_ops[1];
3909 53032 : gcc_checking_assert (n_hilos <= 3);
3910 102605 : for (unsigned i = 0; i < n_hilos; i++)
3911 49573 : this->hilo[i] = res_ops[n_ops + i];
3912 53032 : if (shift_idx >= 0)
3913 : /* The carry atoms (mul_carry_cross_sum, mul_carry_low_sum) capture the
3914 : shift since it is always less than TYPE_PRECISION, using tree_to_uhwi is safe. */
3915 276 : this->shift = tree_to_uhwi (res_ops[shift_idx]);
3916 : }
3917 :
3918 : /* Classify LEAF as a carry-kind summand. The lshift amount is baked
3919 : into mul_carry_cross_sum / mul_carry_low_sum, so they're tried before
3920 : any branch that looks for a generic (X >> N) or (X << N) wrapper. */
3921 :
3922 : bool
3923 7046714 : long_mul_summand::classify_carry (tree leaf)
3924 : {
3925 7046714 : tree res_ops[LONG_MUL_MAX_CAPTURES];
3926 : /* mul_carry_low_sum's inner is constrained to mul_low_sum (cross_sum
3927 : + mul_hi(mul_lolo)); mul_carry_cross_sum's inner is just
3928 : mul_cross_sum (any plus); mul_carry_low matches gt:c (@0, plus(@0,
3929 : @1)) without a baked-in shift. Most specific first, so the
3930 : less-constrained pattern doesn't shadow the more-constrained one. */
3931 7046714 : if (gimple_mul_carry_low_sum (leaf, res_ops, NULL))
3932 : {
3933 106 : set (LMK_CARRY_LOW_SUM, res_ops);
3934 106 : return true;
3935 : }
3936 7046608 : if (gimple_mul_carry_cross_sum (leaf, res_ops, NULL))
3937 : {
3938 170 : set (LMK_CARRY_CROSS_SUM, res_ops);
3939 170 : return true;
3940 : }
3941 7046438 : if (gimple_mul_carry_low (leaf, res_ops, NULL))
3942 : {
3943 2447 : set (LMK_CARRY_LOW, res_ops);
3944 2447 : return true;
3945 : }
3946 : return false;
3947 : }
3948 :
3949 : /* Plus-based summand kinds shared by the (X >> SHIFT) and (X << SHIFT)
3950 : classifiers. Order is by specificity: mul_low_sum's first arm is any
3951 : plus, so mul_ladder_sum1/3 (which constrain that arm to a plus
3952 : containing a mul_lo) and mul_low_accum (which constrains both arms)
3953 : shadow it and must come first. */
3954 :
3955 : bool
3956 84941 : long_mul_summand::classify_plus_kinds (tree inner)
3957 : {
3958 84941 : tree res_ops[LONG_MUL_MAX_CAPTURES];
3959 84941 : if (gimple_mul_low_accum (inner, res_ops, NULL))
3960 : {
3961 19 : set (LMK_LOW_ACCUM, res_ops);
3962 19 : return true;
3963 : }
3964 84922 : if (gimple_mul_ladder_sum3 (inner, res_ops, NULL))
3965 : {
3966 15 : set (LMK_LADDER_SUM3, res_ops);
3967 15 : return true;
3968 : }
3969 84907 : if (gimple_mul_ladder_sum1 (inner, res_ops, NULL))
3970 : {
3971 18214 : set (LMK_LADDER_SUM1, res_ops);
3972 18214 : return true;
3973 : }
3974 66693 : if (gimple_mul_low_sum (inner, res_ops, NULL))
3975 : {
3976 230 : set (LMK_LOW_SUM, res_ops);
3977 230 : return true;
3978 : }
3979 66463 : if (gimple_mul_ladder_sum2 (inner, res_ops, NULL))
3980 : {
3981 90 : set (LMK_LADDER_SUM2, res_ops);
3982 90 : return true;
3983 : }
3984 : return false;
3985 : }
3986 :
3987 : /* Classify INNER -- already unwrapped from an outer (X >> SHIFT) -- as
3988 : a high-half-extracted summand. mul_hilo (mult-shape) is orthogonal
3989 : to the plus-based kinds and is tried first; ladder_part_sum (one arm
3990 : unconstrained) and mul_cross_sum (any plus) are the fallbacks after
3991 : the shared plus-based ladder. */
3992 :
3993 : bool
3994 35449 : long_mul_summand::classify_hi_extract (tree inner, unsigned HOST_WIDE_INT shift)
3995 : {
3996 35449 : tree res_ops[LONG_MUL_MAX_CAPTURES];
3997 35449 : this->extract = LMX_HI;
3998 35449 : this->shift = shift;
3999 35449 : if (gimple_mul_hilo (inner, res_ops, NULL))
4000 : {
4001 9238 : set (LMK_MUL_HILO, res_ops);
4002 9238 : return true;
4003 : }
4004 26211 : if (classify_plus_kinds (inner))
4005 : return true;
4006 16812 : if (gimple_mul_ladder_part_sum (inner, res_ops, NULL))
4007 : {
4008 135 : set (LMK_LADDER_PART_SUM, res_ops);
4009 135 : return true;
4010 : }
4011 16677 : if (gimple_mul_cross_sum (inner, res_ops, NULL))
4012 : {
4013 2808 : set (LMK_CROSS_SUM, res_ops);
4014 2808 : return true;
4015 : }
4016 : return false;
4017 : }
4018 :
4019 : /* Classify INNER -- already unwrapped from an outer (X & MASK) -- as
4020 : a low-half-masked summand. */
4021 :
4022 : bool
4023 21648 : long_mul_summand::classify_lo_extract (tree inner)
4024 : {
4025 21648 : tree res_ops[LONG_MUL_MAX_CAPTURES];
4026 21648 : this->extract = LMX_LO;
4027 21648 : if (gimple_mul_lolo (inner, res_ops, NULL))
4028 : {
4029 9329 : set (LMK_MUL_LOLO, res_ops);
4030 9329 : return true;
4031 : }
4032 : return false;
4033 : }
4034 :
4035 : /* Classify INNER -- already unwrapped from an outer (X << SHIFT) -- as
4036 : a left-shifted summand. No mul_hilo / ladder_part_sum here -- those
4037 : shapes appear only under (X >> SHIFT). */
4038 :
4039 : bool
4040 58730 : long_mul_summand::classify_shl_extract (tree inner,
4041 : unsigned HOST_WIDE_INT shift)
4042 : {
4043 58730 : tree res_ops[LONG_MUL_MAX_CAPTURES];
4044 58730 : this->extract = LMX_SHL_N;
4045 58730 : this->shift = shift;
4046 58730 : if (classify_plus_kinds (inner))
4047 : return true;
4048 49561 : if (gimple_mul_cross_sum (inner, res_ops, NULL))
4049 : {
4050 2929 : set (LMK_CROSS_SUM, res_ops);
4051 2929 : return true;
4052 : }
4053 : return false;
4054 : }
4055 :
4056 : /* Classify LEAF as one of the bare-kind summands (no extraction
4057 : wrapper): mul_hihi or mul_lolo standing on their own. */
4058 :
4059 : bool
4060 6928164 : long_mul_summand::classify_bare (tree leaf)
4061 : {
4062 6928164 : tree res_ops[LONG_MUL_MAX_CAPTURES];
4063 6928164 : if (gimple_mul_hihi (leaf, res_ops, NULL))
4064 : {
4065 9686 : set (LMK_MUL_HIHI, res_ops);
4066 9686 : return true;
4067 : }
4068 6918478 : if (gimple_mul_lolo (leaf, res_ops, NULL))
4069 : {
4070 63 : set (LMK_MUL_LOLO, res_ops);
4071 63 : return true;
4072 : }
4073 : return false;
4074 : }
4075 :
4076 : /* Classify LEAF as one of the long-multiply summand shapes. On success,
4077 : fill *INFO with the kind, extract, captured operands and shift.
4078 : Dispatches to per-extract helpers; the order matters because the
4079 : carry kinds bake an lshift into the pattern and would otherwise be
4080 : misread by the (X << N) branch. */
4081 :
4082 7046714 : long_mul_summand::long_mul_summand (tree leaf)
4083 7046714 : : long_mul_summand()
4084 : {
4085 7046714 : tree res_ops[LONG_MUL_MAX_CAPTURES];
4086 :
4087 7046714 : if (classify_carry (leaf))
4088 118550 : return;
4089 :
4090 7043991 : if (gimple_mul_hi (leaf, res_ops, NULL))
4091 : {
4092 35449 : classify_hi_extract (res_ops[0], tree_to_uhwi (res_ops[1]));
4093 35449 : return;
4094 : }
4095 :
4096 7008542 : if (gimple_mul_lo (leaf, res_ops, NULL))
4097 : {
4098 21648 : classify_lo_extract (res_ops[0]);
4099 21648 : return;
4100 : }
4101 :
4102 6986894 : tree inner;
4103 6986894 : unsigned HOST_WIDE_INT shift;
4104 6986894 : if (long_mul_is_lshift_def (leaf, &inner, &shift))
4105 : {
4106 58730 : classify_shl_extract (inner, shift);
4107 58730 : return;
4108 : }
4109 :
4110 6928164 : classify_bare (leaf);
4111 : }
4112 :
4113 : /* qsort comparator: sort summands by (kind, extract) to put a multiset
4114 : into canonical order for table lookup. Unstable sort within a tie is
4115 : harmless: no row in long_mul_table pairs distinct subterms under the
4116 : same (kind, extract), and long_mul_check_consistency cross-validates
4117 : that matching summands share one canonical (op0, op1). */
4118 :
4119 : static int
4120 139514 : long_mul_summand_compare (const void *a, const void *b)
4121 : {
4122 139514 : const long_mul_summand *sa = (const long_mul_summand *) a;
4123 139514 : const long_mul_summand *sb = (const long_mul_summand *) b;
4124 139514 : gcc_checking_assert (sa->kind != LMK_INVALID);
4125 139514 : gcc_checking_assert (sb->kind != LMK_INVALID);
4126 139514 : if (sa->kind != sb->kind)
4127 138651 : return (int) sa->kind - (int) sb->kind;
4128 863 : return (int) sa->extract - (int) sb->extract;
4129 : }
4130 :
4131 : /* One row of the long-multiply variant table. COUNT is how many entries
4132 : of SIG carry the row's signature (2 to LONG_MUL_MAX_SUMMANDS); a row
4133 : with fewer summands leaves the remaining SIG entries zero-initialized.
4134 : Those zeros are not a terminator -- {LMK_MUL_HIHI, LMX_NONE} is itself a
4135 : valid signature -- so long_mul_signature_matches is bounded by COUNT,
4136 : never by a sentinel entry. */
4137 :
4138 : struct long_mul_row {
4139 : enum long_mul_row_part { HIGH_PART, LOW_PART } part;
4140 : tree_code outer;
4141 : unsigned char count;
4142 : struct {
4143 : long_mul_kind kind;
4144 : long_mul_extract extract;
4145 : } sig[LONG_MUL_MAX_SUMMANDS];
4146 : bool (*extra_check) (const vec<long_mul_summand> &, gimple *);
4147 : };
4148 :
4149 : /* True if (A, B) is the same pair as (OP0, OP1) in either order. */
4150 :
4151 : static inline bool
4152 23145 : long_mul_same_ops (tree a, tree b, tree op0, tree op1)
4153 : {
4154 32492 : return (a == op0 && b == op1) || (a == op1 && b == op0);
4155 : }
4156 :
4157 : /* True if H is a cross-half product of (OP0, OP1) -- gimple_mul_hilo
4158 : recognizes it and its captured operands match the pair. */
4159 :
4160 : static bool
4161 4374 : long_mul_is_cross_half (tree h, tree op0, tree op1)
4162 : {
4163 4374 : tree scratch[LONG_MUL_MAX_CAPTURES];
4164 4374 : return gimple_mul_hilo (h, scratch, NULL)
4165 4374 : && long_mul_same_ops (scratch[0], scratch[1], op0, op1);
4166 : }
4167 :
4168 : /* Orientation of the mul_hilo capture H relative to (OP0, OP1):
4169 : returns 0 for high(OP0)*low(OP1), 1 for high(OP1)*low(OP0), or -1
4170 : if H does not decompose that way. A cross-sum of two mul_hilos must
4171 : see one of each orientation -- otherwise a doubled factor would fold
4172 : to the wrong value. (In a self-multiply the two orientations
4173 : coincide; see the OP0 == OP1 bypass in long_mul_check_consistency.) */
4174 :
4175 : static int
4176 4146 : long_mul_hilo_orientation (tree h, tree op0, tree op1)
4177 : {
4178 4146 : tree scratch[LONG_MUL_MAX_CAPTURES];
4179 4146 : if (!gimple_mul_hilo (h, scratch, NULL))
4180 : return -1;
4181 4146 : if (scratch[0] == op0 && scratch[1] == op1)
4182 : return 0;
4183 2073 : if (scratch[0] == op1 && scratch[1] == op0)
4184 2073 : return 1;
4185 : return -1;
4186 : }
4187 :
4188 : /* Find the first summand that carries operand captures, and return its
4189 : (op0, op1) pair in *OP0_OUT / *OP1_OUT. Returns false if no summand
4190 : provides them. */
4191 :
4192 : static bool
4193 8103 : long_mul_canonical_ops (const vec<long_mul_summand> &summands,
4194 : tree *op0_out, tree *op1_out)
4195 : {
4196 24309 : for (const long_mul_summand &s : summands)
4197 8103 : if (s.op0)
4198 : {
4199 8103 : *op0_out = s.op0;
4200 8103 : *op1_out = s.op1;
4201 8103 : return true;
4202 : }
4203 : return false;
4204 : }
4205 :
4206 : /* Return the first summand in SUMMANDS whose kind matches KIND, or NULL. */
4207 :
4208 : static const long_mul_summand *
4209 12 : long_mul_find_summand (const vec<long_mul_summand> &summands,
4210 : long_mul_kind kind)
4211 : {
4212 12 : gcc_checking_assert (kind != LMK_INVALID);
4213 60 : for (const long_mul_summand &s : summands)
4214 36 : if (s.kind == kind)
4215 : return &s;
4216 : return NULL;
4217 : }
4218 :
4219 : /* Run the cross-summand validation invariants and return the canonical
4220 : (op0, op1). Returns false unless all summands that carry operands use
4221 : the same (op0, op1) pair (in either order), every LMX_HI/LMX_SHL_N shift
4222 : equals halfwidth, every captured hilo is a true cross-half product of
4223 : (op0, op1), and every cross-half pair (both those inside a single
4224 : mul_cross_sum-bearing summand and those spread across separate
4225 : LMK_MUL_HILO summands) contains one of each orientation. */
4226 :
4227 : static bool
4228 8091 : long_mul_check_consistency (const vec<long_mul_summand> &summands,
4229 : tree *op0_out, tree *op1_out)
4230 : {
4231 8091 : tree op0, op1;
4232 8091 : if (!long_mul_canonical_ops (summands, &op0, &op1))
4233 : return false;
4234 :
4235 8091 : tree op_type = TREE_TYPE (op0);
4236 8091 : if (!INTEGRAL_TYPE_P (op_type)
4237 8091 : || TYPE_PRECISION (op_type) % 2 != 0)
4238 : return false;
4239 8091 : unsigned int halfwidth = TYPE_PRECISION (op_type) / 2;
4240 :
4241 : /* Self-multiply (x*x) collapses the two cross-halves onto one value,
4242 : so the complementarity constraint is a trivial no-op there. */
4243 8091 : bool need_orient = op0 != op1;
4244 8091 : int mul_hilo_orient[2] = { 0, 0 };
4245 :
4246 37049 : for (const long_mul_summand &s : summands)
4247 : {
4248 18880 : if (s.op0 && !long_mul_same_ops (s.op0, s.op1, op0, op1))
4249 : return false;
4250 12802 : if ((s.extract == LMX_HI || s.extract == LMX_SHL_N)
4251 4626 : && s.shift != halfwidth)
4252 : return false;
4253 51154 : for (tree h : s.hilo)
4254 38378 : if (h && !long_mul_is_cross_half (h, op0, op1))
4255 : return false;
4256 :
4257 12776 : if (!need_orient)
4258 0 : continue;
4259 :
4260 : /* The two cross-sum operands are the last two non-null hilos:
4261 : (hilo[1], hilo[2]) for the CARRY_*_SUM kinds, (hilo[0], hilo[1]) for
4262 : the CROSS_SUM / SUM / ACCUM / LADDER_SUM kinds, none for the
4263 : rest. */
4264 12776 : tree a = NULL_TREE;
4265 12776 : tree b = NULL_TREE;
4266 12776 : if (s.hilo[2])
4267 : {
4268 73 : a = s.hilo[1];
4269 73 : b = s.hilo[2];
4270 : }
4271 12703 : else if (s.hilo[1])
4272 : {
4273 2000 : a = s.hilo[0];
4274 2000 : b = s.hilo[1];
4275 : }
4276 12776 : if (a && b
4277 14849 : && (long_mul_hilo_orientation (a, op0, op1)
4278 2073 : == long_mul_hilo_orientation (b, op0, op1)))
4279 : return false;
4280 :
4281 : /* Two LMK_MUL_HILO summands (the two-hilos ladder form) stand for
4282 : the two cross-halves separately; count orientations and require
4283 : the pair to be complementary. s.op0/op1 is already validated to
4284 : match (op0, op1) in some order above. */
4285 12776 : if (s.kind == LMK_MUL_HILO && s.op0)
4286 2519 : mul_hilo_orient[s.op0 == op1]++;
4287 : }
4288 :
4289 1987 : if (mul_hilo_orient[0] + mul_hilo_orient[1] >= 2
4290 10 : && (mul_hilo_orient[0] == 0 || mul_hilo_orient[1] == 0))
4291 : return false;
4292 :
4293 1987 : *op0_out = op0;
4294 1987 : *op1_out = op1;
4295 1987 : return true;
4296 : }
4297 :
4298 : /* Compare the (already-sorted) SUMMANDS multiset against ROW.sig. */
4299 :
4300 : static bool
4301 124433 : long_mul_signature_matches (const vec<long_mul_summand> &summands,
4302 : const long_mul_row &row)
4303 : {
4304 248866 : if (row.count != summands.length ())
4305 : return false;
4306 75408 : for (unsigned i = 0; i < row.count; i++)
4307 67317 : if (summands[i].kind != row.sig[i].kind
4308 67317 : || summands[i].extract != row.sig[i].extract)
4309 : return false;
4310 : return true;
4311 : }
4312 :
4313 : /* Extra check for the two-carries high-part row: the LMK_CARRY_LOW summand's
4314 : two operands (carry_a, carry_b) must be a (cross_shifted, mul_lolo) pair
4315 : consistent with the multiset's canonical (op0, op1). */
4316 :
4317 : static bool
4318 12 : long_mul_check_two_carries (const vec<long_mul_summand> &summands,
4319 : gimple *)
4320 : {
4321 12 : tree op0, op1;
4322 12 : if (!long_mul_canonical_ops (summands, &op0, &op1))
4323 : return false;
4324 12 : unsigned int halfwidth = TYPE_PRECISION (TREE_TYPE (op0)) / 2;
4325 :
4326 12 : const long_mul_summand *cl = long_mul_find_summand (summands, LMK_CARRY_LOW);
4327 12 : if (!cl)
4328 : return false;
4329 :
4330 : /* The two carry_low operands must be (cross_shifted, mul_lolo) in either
4331 : order. cross_shifted = LSHIFT_EXPR (mul_cross_sum, halfwidth). */
4332 12 : tree cs = cl->carry_a, lolo = cl->carry_b;
4333 12 : tree inner;
4334 12 : unsigned HOST_WIDE_INT shift;
4335 12 : if (!long_mul_is_lshift_def (cs, &inner, &shift))
4336 : {
4337 2 : std::swap (cs, lolo);
4338 2 : if (!long_mul_is_lshift_def (cs, &inner, &shift))
4339 : return false;
4340 : }
4341 12 : if (shift != halfwidth)
4342 : return false;
4343 :
4344 12 : tree scratch[LONG_MUL_MAX_CAPTURES];
4345 12 : if (!gimple_mul_cross_sum (inner, scratch, NULL))
4346 : return false;
4347 36 : for (int i = 0; i < 2; i++)
4348 24 : if (!long_mul_is_cross_half (scratch[i], op0, op1))
4349 : return false;
4350 12 : if (!gimple_mul_lolo (lolo, scratch, NULL)
4351 12 : || !long_mul_same_ops (scratch[0], scratch[1], op0, op1))
4352 : return false;
4353 :
4354 : return true;
4355 : }
4356 :
4357 : /* The lolo + cross_shifted shape is also the low half of a two-carry
4358 : long-multiply, where an unsigned overflow compare against one of
4359 : the PLUS operands is the low-carry term consumed by the matching
4360 : high-part fold. Folding to mul_lo here destroys cross_shifted,
4361 : which both the compare and the high-part match still need; defer
4362 : so the high-part fold runs first. After it does, the compare is
4363 : dead and the surviving lolo + cross_shifted is picked up by this
4364 : row in the next forwprop instance. Returns false to defer. */
4365 :
4366 : static bool
4367 51 : long_mul_check_low_plus_defer (const vec<long_mul_summand> &, gimple *stmt)
4368 : {
4369 : /* The PHI entry passes its gphi as the candidate but commits only to
4370 : HIGH_PART rows, so a LOW_PART row never folds from there. Guard the
4371 : gimple_assign accessors regardless, so this stays correct if a future
4372 : PLUS-shaped row reachable from the PHI path uses it. */
4373 51 : if (!is_gimple_assign (stmt))
4374 : return false;
4375 :
4376 51 : tree lhs = gimple_assign_lhs (stmt);
4377 51 : tree rhs1 = gimple_assign_rhs1 (stmt);
4378 51 : tree rhs2 = gimple_assign_rhs2 (stmt);
4379 :
4380 51 : imm_use_iterator iter;
4381 51 : gimple *use_stmt;
4382 79 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
4383 : {
4384 64 : tree cmp_op1 = NULL_TREE, cmp_op2 = NULL_TREE;
4385 64 : enum tree_code use_code = ERROR_MARK;
4386 64 : if (is_gimple_assign (use_stmt))
4387 : {
4388 42 : use_code = gimple_assign_rhs_code (use_stmt);
4389 42 : cmp_op1 = gimple_assign_rhs1 (use_stmt);
4390 42 : cmp_op2 = gimple_assign_rhs2 (use_stmt);
4391 : }
4392 22 : else if (gcond *cond = dyn_cast<gcond *> (use_stmt))
4393 : {
4394 9 : use_code = gimple_cond_code (cond);
4395 9 : cmp_op1 = gimple_cond_lhs (cond);
4396 9 : cmp_op2 = gimple_cond_rhs (cond);
4397 : }
4398 64 : if (use_code == GT_EXPR || use_code == LT_EXPR
4399 28 : || use_code == GE_EXPR || use_code == LE_EXPR)
4400 : {
4401 36 : tree other = (cmp_op1 == lhs) ? cmp_op2
4402 23 : : (cmp_op2 == lhs) ? cmp_op1 : NULL_TREE;
4403 36 : if (other && (other == rhs1 || other == rhs2))
4404 36 : return false;
4405 : }
4406 36 : }
4407 15 : return true;
4408 : }
4409 :
4410 : /* Long-multiply variant table. Each row enumerates the multiset of
4411 : (kind, extract) summands that compose one long-multiply form. Rows
4412 : are sorted by long_mul_summand_compare, matching the input summands'
4413 : sort order, so a plain element-wise compare suffices. Rows describe
4414 : unsigned schoolbook expansions on an even-width 2N-bit type split at
4415 : half-width N; EXTRA_CHECK carries invariants the (kind, extract)
4416 : signature cannot express.
4417 :
4418 : The formula on each row uses xh, xl, yh, yl for the half-width pieces
4419 : of x and y, cross_sum for xh*yl + xl*yh, and hilo for either cross-half
4420 : product (consumers validate the operand shape). */
4421 :
4422 : static const long_mul_row long_mul_table[] = {
4423 : /* HIGH-PART folds. */
4424 : /* xh*yh + (low_sum >> N) + ((hilo > low_sum) << N),
4425 : low_sum = cross_sum + (xl*yl >> N). */
4426 : { long_mul_row::HIGH_PART, PLUS_EXPR, 3,
4427 : { { LMK_MUL_HIHI, LMX_NONE },
4428 : { LMK_LOW_SUM, LMX_HI },
4429 : { LMK_CARRY_LOW_SUM, LMX_NONE } },
4430 : NULL },
4431 : /* xh*yh + (low_accum >> N) + (cross_sum >> N) + ((hilo > cross_sum) << N),
4432 : low_accum = (xl*yl >> N) + (cross_sum & mask). */
4433 : { long_mul_row::HIGH_PART, PLUS_EXPR, 4,
4434 : { { LMK_MUL_HIHI, LMX_NONE },
4435 : { LMK_CROSS_SUM, LMX_HI },
4436 : { LMK_LOW_ACCUM, LMX_HI },
4437 : { LMK_CARRY_CROSS_SUM, LMX_NONE } },
4438 : NULL },
4439 : /* xh*yh + (cross_sum >> N) + carry_low + ((hilo > cross_sum) << N),
4440 : carry_low = (xl*yl + (cross_sum << N)) < (cross_sum << N). */
4441 : { long_mul_row::HIGH_PART, PLUS_EXPR, 4,
4442 : { { LMK_MUL_HIHI, LMX_NONE },
4443 : { LMK_CROSS_SUM, LMX_HI },
4444 : { LMK_CARRY_LOW, LMX_NONE },
4445 : { LMK_CARRY_CROSS_SUM, LMX_NONE } },
4446 : long_mul_check_two_carries },
4447 : /* xh*yh + (hilo >> N) + (ladder_sum1 >> N),
4448 : ladder_sum1 = (hilo & mask) + hilo' + (xl*yl >> N),
4449 : hilo, hilo' the two cross-half products. */
4450 : { long_mul_row::HIGH_PART, PLUS_EXPR, 3,
4451 : { { LMK_MUL_HIHI, LMX_NONE },
4452 : { LMK_MUL_HILO, LMX_HI },
4453 : { LMK_LADDER_SUM1, LMX_HI } },
4454 : NULL },
4455 : /* xh*yh + (ladder_sum2 >> N) + (ladder_part_sum >> N),
4456 : ladder_part_sum = (xl*yl >> N) + hilo,
4457 : ladder_sum2 = (ladder_part_sum & mask) + hilo'. */
4458 : { long_mul_row::HIGH_PART, PLUS_EXPR, 3,
4459 : { { LMK_MUL_HIHI, LMX_NONE },
4460 : { LMK_LADDER_SUM2, LMX_HI },
4461 : { LMK_LADDER_PART_SUM, LMX_HI } },
4462 : NULL },
4463 : /* xh*yh + (hilo >> N) + (hilo' >> N) + (ladder_sum3 >> N),
4464 : ladder_sum3 = (hilo & mask) + (hilo' & mask) + (xl*yl >> N). */
4465 : { long_mul_row::HIGH_PART, PLUS_EXPR, 4,
4466 : { { LMK_MUL_HIHI, LMX_NONE },
4467 : { LMK_MUL_HILO, LMX_HI },
4468 : { LMK_MUL_HILO, LMX_HI },
4469 : { LMK_LADDER_SUM3, LMX_HI } },
4470 : NULL },
4471 : /* LOW-PART folds. Recover the lower 2N bits from xl*yl plus a
4472 : shifted cross-half term. */
4473 : /* xl*yl + (cross_sum << N). */
4474 : { long_mul_row::LOW_PART, PLUS_EXPR, 2,
4475 : { { LMK_MUL_LOLO, LMX_NONE },
4476 : { LMK_CROSS_SUM, LMX_SHL_N } },
4477 : long_mul_check_low_plus_defer },
4478 : /* (xl*yl & mask) | (low_accum << N),
4479 : low_accum = (xl*yl >> N) + (cross_sum & mask). */
4480 : { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
4481 : { { LMK_MUL_LOLO, LMX_LO },
4482 : { LMK_LOW_ACCUM, LMX_SHL_N } },
4483 : NULL },
4484 : /* (xl*yl & mask) | (low_sum << N),
4485 : low_sum = cross_sum + (xl*yl >> N). */
4486 : { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
4487 : { { LMK_MUL_LOLO, LMX_LO },
4488 : { LMK_LOW_SUM, LMX_SHL_N } },
4489 : NULL },
4490 : /* (xl*yl & mask) | (ladder_sum1 << N),
4491 : ladder_sum1 as in the high ladder row above. */
4492 : { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
4493 : { { LMK_MUL_LOLO, LMX_LO },
4494 : { LMK_LADDER_SUM1, LMX_SHL_N } },
4495 : NULL },
4496 : /* (xl*yl & mask) | (ladder_sum2 << N),
4497 : ladder_sum2 as in the high ladder row above. */
4498 : { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
4499 : { { LMK_MUL_LOLO, LMX_LO },
4500 : { LMK_LADDER_SUM2, LMX_SHL_N } },
4501 : NULL },
4502 : /* (xl*yl & mask) | (ladder_sum3 << N),
4503 : ladder_sum3 as in the high ladder-long row above. */
4504 : { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
4505 : { { LMK_MUL_LOLO, LMX_LO },
4506 : { LMK_LADDER_SUM3, LMX_SHL_N } },
4507 : NULL },
4508 : };
4509 :
4510 : /* If a multi-used inner addition (sharing the chain's outer code) blocked
4511 : linearization of a long-mul candidate, emit a dump-file hint pointing
4512 : at it. */
4513 :
4514 : static void
4515 21328 : long_mul_hint_shared_intermediate (gimple *shared_def)
4516 : {
4517 21328 : if (!shared_def || !dump_file || !(dump_flags & TDF_DETAILS))
4518 : return;
4519 0 : fprintf (dump_file, "long-mul fold rejected: shared intermediate at ");
4520 0 : print_gimple_stmt (dump_file, shared_def, 0, TDF_SLIM);
4521 : }
4522 :
4523 : /* Search long_mul_table for a row whose multiset matches SUMMANDS for
4524 : outer kind OUTER on a result of type LHS_TYPE. CANDIDATE_STMT is
4525 : passed to per-row extra_check predicates. On a hit, returns the
4526 : matching row and writes the half-width operands via OUT_OP0/OUT_OP1.
4527 : No IR mutation. */
4528 :
4529 : static const long_mul_row *
4530 18719 : long_mul_classify_match (const vec<long_mul_summand> &summands,
4531 : tree lhs_type, tree_code outer,
4532 : gimple *candidate_stmt,
4533 : tree *out_op0, tree *out_op1)
4534 : {
4535 : /* HIGH_PART rows emit a 2N-bit multiply that pass_optimize_widening_mul
4536 : consumes -- either via WIDEN_MULT_EXPR / MULT_HIGHPART conversion when
4537 : the target has a native 2N multiply, or via lower_long_mul_high_chain
4538 : when it does not. LOW_PART rows emit a plain MULT_EXPR. Emission
4539 : needs a 2N mode to exist in the mode table AND the widening_mul pass
4540 : to be active: without the pass, the emit could reach RTL expand as an
4541 : unexpandable 2N multiply (e.g. OImode). BITINT_TYPE is
4542 : refused -- the long_mul_high_chain atom excludes it. */
4543 18719 : scalar_int_mode mode, wide_mode;
4544 18719 : bool can_emit_high
4545 18719 : = optimize_widening_mul_active_p ()
4546 18628 : && TREE_CODE (lhs_type) != BITINT_TYPE
4547 37256 : && is_a <scalar_int_mode> (TYPE_MODE (lhs_type), &mode)
4548 37347 : && GET_MODE_2XWIDER_MODE (mode).exists (&wide_mode);
4549 :
4550 225854 : for (const long_mul_row &row : long_mul_table)
4551 : {
4552 410081 : if (row.outer != outer
4553 124979 : || (row.part == long_mul_row::HIGH_PART && !can_emit_high)
4554 333519 : || !long_mul_signature_matches (summands, row))
4555 207135 : continue;
4556 :
4557 8091 : tree op0, op1;
4558 8091 : if (!long_mul_check_consistency (summands, &op0, &op1))
4559 6104 : continue;
4560 :
4561 : /* Do not emit the wide chain when an operand is subject to
4562 : abnormal coalescing: the widening_mul-side consumers refuse
4563 : such operands (see convert_mult_to_widen), which would leave
4564 : the chain without a consumer. */
4565 1987 : if (row.part == long_mul_row::HIGH_PART
4566 1987 : && ((TREE_CODE (op0) == SSA_NAME
4567 1919 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
4568 1919 : || (TREE_CODE (op1) == SSA_NAME
4569 1919 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1))))
4570 0 : continue;
4571 :
4572 1987 : if (row.extra_check && !row.extra_check (summands, candidate_stmt))
4573 36 : continue;
4574 :
4575 1951 : *out_op0 = op0;
4576 1951 : *out_op1 = op1;
4577 1951 : return &row;
4578 : }
4579 : return NULL;
4580 : }
4581 :
4582 : /* Walk STMT's outer chain (kind OUTER), classify each leaf as a
4583 : long-multiply summand, optionally add the already-classified EXTRA,
4584 : and look the multiset up in long_mul_table for a result of type
4585 : LHS_TYPE. CANDIDATE is passed to per-row extra_check predicates.
4586 :
4587 : If EXTRAS_OUT is non-NULL, leaves matching no summand are set aside
4588 : there instead of failing the match, and the caller must re-apply
4589 : them on top of the folded multiply. A leaf that does match is
4590 : always consumed: if that makes the signature miss every row the
4591 : match fails, rather than retrying with the leaf demoted to an extra
4592 : (subset search would be exponential).
4593 :
4594 : Returns the matched row and the half-width operands via
4595 : OUT_OP0/OUT_OP1, or NULL on a miss. No IR mutation. */
4596 :
4597 : static const long_mul_row *
4598 3444063 : long_mul_classify_chain (gimple *stmt, tree_code outer, tree lhs_type,
4599 : gimple *candidate, const long_mul_summand *extra,
4600 : vec<tree> *extras_out,
4601 : tree *out_op0, tree *out_op1)
4602 : {
4603 3444063 : auto_vec<tree, LONG_MUL_MAX_SUMMANDS + LONG_MUL_MAX_EXTRAS> leaves;
4604 3444063 : gimple *shared_def = NULL;
4605 3444063 : if (!long_mul_linearize_chain (stmt, outer, leaves, &shared_def))
4606 : return NULL;
4607 :
4608 3443268 : auto_vec<long_mul_summand,
4609 3443268 : LONG_MUL_MAX_SUMMANDS + LONG_MUL_MAX_EXTRAS + 1> summands;
4610 17371958 : for (tree leaf : leaves)
4611 : {
4612 7046714 : if (long_mul_summand s{leaf})
4613 : {
4614 55479 : gcc_checking_assert (s.kind != LMK_INVALID);
4615 55479 : summands.quick_push (s);
4616 : }
4617 13982194 : else if (extras_out && extras_out->length () < LONG_MUL_MAX_EXTRAS)
4618 6986675 : extras_out->safe_push (leaf);
4619 : else
4620 : {
4621 4560 : long_mul_hint_shared_intermediate (shared_def);
4622 4560 : return NULL;
4623 : }
4624 : }
4625 3438708 : if (extra)
4626 : {
4627 354 : gcc_checking_assert (extra->kind != LMK_INVALID);
4628 354 : summands.quick_push (*extra);
4629 : }
4630 6881976 : if (summands.length () < 2
4631 3438708 : || summands.length () > LONG_MUL_MAX_SUMMANDS)
4632 : return NULL;
4633 18719 : summands.qsort (long_mul_summand_compare);
4634 :
4635 18719 : const long_mul_row *row
4636 18719 : = long_mul_classify_match (summands, lhs_type, outer, candidate,
4637 : out_op0, out_op1);
4638 18719 : if (!row)
4639 16768 : long_mul_hint_shared_intermediate (shared_def);
4640 : return row;
4641 3443268 : }
4642 :
4643 : /* Top-level entry for long-multiply folding. Walks STMT's outer
4644 : addition or BIT_IOR chain, classifies the summands, and dispatches
4645 : to create_mul_high_seq / create_mul_low_seq if the multiset matches
4646 : a known long-multiply form. Returns true on success. */
4647 :
4648 : static bool
4649 10239215 : match_long_mul (gassign *stmt)
4650 : {
4651 10239215 : tree_code outer = gimple_assign_rhs_code (stmt);
4652 10239215 : if (outer != PLUS_EXPR && outer != BIT_IOR_EXPR)
4653 : return false;
4654 :
4655 : /* Skip non-candidate adds (signed, pointer, odd-width) before walking the
4656 : chain. No legitimate long-mul leaf has a type the atoms would reject.
4657 : This just avoids the linearize/classify work on every other PLUS/IOR. */
4658 10239215 : tree lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
4659 10239215 : if (!INTEGRAL_TYPE_P (lhs_type)
4660 8950610 : || !TYPE_UNSIGNED (lhs_type)
4661 14086110 : || TYPE_PRECISION (lhs_type) % 2 != 0)
4662 : return false;
4663 :
4664 : /* Only start at the end of a chain: a consumer with the same code
4665 : linearizes through this statement anyway, so starting here is
4666 : redundant. A consumer in another block does not count -- folding
4667 : at the later use could sink a loop-invariant multiply into a
4668 : loop. */
4669 3610598 : use_operand_p use_p;
4670 3610598 : gimple *use_stmt;
4671 3610598 : if (single_imm_use (gimple_assign_lhs (stmt), &use_p, &use_stmt)
4672 2560164 : && is_gimple_assign (use_stmt)
4673 1382652 : && gimple_assign_rhs_code (use_stmt) == outer
4674 3784618 : && gimple_bb (use_stmt) == gimple_bb (stmt))
4675 : return false;
4676 :
4677 3443433 : auto_vec<tree, LONG_MUL_MAX_EXTRAS> extras;
4678 3443433 : tree op0, op1;
4679 3443433 : const long_mul_row *row
4680 3443433 : = long_mul_classify_chain (stmt, outer, lhs_type, stmt, NULL, &extras,
4681 : &op0, &op1);
4682 3443433 : if (!row)
4683 : return false;
4684 :
4685 1937 : if (row->part == long_mul_row::HIGH_PART)
4686 : {
4687 1905 : create_mul_high_seq (op0, op1, stmt, extras, outer);
4688 1905 : if (dump_file && (dump_flags & TDF_DETAILS))
4689 32 : fprintf (dump_file, "Long multiplication high part folded.\n");
4690 : return true;
4691 : }
4692 32 : create_mul_low_seq (op0, op1, stmt, extras, outer);
4693 32 : if (dump_file && (dump_flags & TDF_DETAILS))
4694 8 : fprintf (dump_file, "Long multiplication low part folded.\n");
4695 : return true;
4696 3443433 : }
4697 :
4698 : /* PHI-driven entry for long-multiply folding. When PHI's value
4699 : flattens to base + (carry << N), probe sum to classify the carry
4700 : kind, linearize base for the remaining high-part summands, and run
4701 : the long-multiply table. On a hit, emit a 2N-bit multiply at the
4702 : top of the join block with PHI_RES as its LHS and remove the PHI.
4703 : Otherwise leave the IR untouched. Only HIGH_PART rows are
4704 : reachable. LOW_PART rows are BIT_IOR-shaped and never produce a
4705 : carry PHI. */
4706 :
4707 : static bool
4708 8843774 : match_long_mul_phi (gphi *phi)
4709 : {
4710 8843774 : tree phi_res = gimple_phi_result (phi);
4711 8843774 : tree lhs_type = TREE_TYPE (phi_res);
4712 6241642 : if (!INTEGRAL_TYPE_P (lhs_type) || !TYPE_UNSIGNED (lhs_type)
4713 11622065 : || TYPE_PRECISION (lhs_type) % 2 != 0)
4714 : return false;
4715 :
4716 2045085 : tree cca_ops[4];
4717 2045085 : if (!gimple_cond_carry_add (phi_res, cca_ops, NULL))
4718 : return false;
4719 19103 : tree cmp_lhs = cca_ops[0];
4720 19103 : tree sum = cca_ops[1];
4721 19103 : tree base = cca_ops[2];
4722 :
4723 : /* Classify sum and populate the carry summand directly. Most
4724 : specific first, mirroring long_mul_classify_carry's order. */
4725 19103 : long_mul_summand carry{};
4726 19103 : tree sum_ops[LONG_MUL_MAX_CAPTURES];
4727 19103 : unsigned HOST_WIDE_INT shift_amt
4728 19103 : = wi::exact_log2 (wi::to_wide (cca_ops[3]));
4729 19103 : unsigned HOST_WIDE_INT halfwidth = TYPE_PRECISION (lhs_type) / 2;
4730 19103 : carry.shift = shift_amt;
4731 :
4732 19103 : if (shift_amt == halfwidth
4733 19103 : && gimple_mul_low_sum (sum, sum_ops, NULL))
4734 : {
4735 : /* mul_carry_low_sum's flat form ties the outer lshift amount to
4736 : the inner mul_hi's INTEGER_CST@0 via match.pd capture re-use;
4737 : the PHI form has no such tie, so gate on shift_amt explicitly. */
4738 14 : carry.kind = LMK_CARRY_LOW_SUM;
4739 14 : carry.op0 = sum_ops[0];
4740 14 : carry.op1 = sum_ops[1];
4741 14 : carry.hilo[0] = cmp_lhs;
4742 14 : carry.hilo[1] = sum_ops[2];
4743 14 : carry.hilo[2] = sum_ops[3];
4744 : }
4745 19089 : else if (shift_amt == halfwidth
4746 19089 : && gimple_mul_cross_sum (sum, sum_ops, NULL))
4747 : {
4748 : /* mul_cross_sum is just (plus:c @0 @1) with no half-width
4749 : constraint. Gate here to mirror mul_carry_cross_sum;
4750 : a mismatch falls through to the LMK_CARRY_LOW branch. */
4751 1 : carry.kind = LMK_CARRY_CROSS_SUM;
4752 1 : carry.hilo[0] = cmp_lhs;
4753 1 : carry.hilo[1] = sum_ops[0];
4754 1 : carry.hilo[2] = sum_ops[1];
4755 : }
4756 19088 : else if (shift_amt == 0 && TREE_CODE (sum) == SSA_NAME)
4757 : {
4758 17950 : gimple *def = SSA_NAME_DEF_STMT (sum);
4759 17950 : if (!is_gimple_assign (def)
4760 17950 : || gimple_assign_rhs_code (def) != PLUS_EXPR)
4761 : return false;
4762 4734 : tree p1 = gimple_assign_rhs1 (def);
4763 4734 : tree p2 = gimple_assign_rhs2 (def);
4764 4734 : if (p1 != cmp_lhs && p2 != cmp_lhs)
4765 : return false;
4766 4009 : carry.kind = LMK_CARRY_LOW;
4767 4009 : carry.carry_a = cmp_lhs;
4768 4009 : carry.carry_b = p1 == cmp_lhs ? p2 : p1;
4769 4009 : }
4770 : else
4771 : return false;
4772 :
4773 : /* Linearize base, the rest of the high-part chain. */
4774 4024 : if (TREE_CODE (base) != SSA_NAME)
4775 : return false;
4776 4024 : gimple *base_def = SSA_NAME_DEF_STMT (base);
4777 4024 : if (!is_gimple_assign (base_def)
4778 4024 : || gimple_assign_rhs_code (base_def) != PLUS_EXPR)
4779 : return false;
4780 :
4781 630 : tree op0, op1;
4782 630 : const long_mul_row *row
4783 630 : = long_mul_classify_chain (base_def, PLUS_EXPR, lhs_type, phi, &carry,
4784 : NULL, &op0, &op1);
4785 630 : if (!row || row->part != long_mul_row::HIGH_PART)
4786 : return false;
4787 :
4788 14 : gimple_seq seq = NULL;
4789 14 : build_mul_high_seq (op0, op1, phi_res, gimple_location (phi), &seq);
4790 14 : gimple_stmt_iterator gsi = gsi_after_labels (gimple_bb (phi));
4791 14 : gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
4792 14 : gimple_stmt_iterator psi = gsi_for_stmt (phi);
4793 14 : remove_phi_node (&psi, false);
4794 14 : if (dump_file && (dump_flags & TDF_DETAILS))
4795 4 : fprintf (dump_file,
4796 : "Long multiplication high part folded (carry PHI).\n");
4797 : return true;
4798 : }
4799 :
4800 : /* Verify if we have the following structure:
4801 :
4802 : iftmp1 = PHI <pow2a, pow2b, pow2c, ...>
4803 : _ssa1 = _ssa2 MOD|DIV iftmp1;
4804 : _ssa3 = _ssa1 EQ|NE 0;
4805 :
4806 : And, if the right conditions are met, change the PHI args
4807 : and "_ssa1" stmt to a cheaper alternative.
4808 :
4809 : - for MOD, if either "_ssa2" is known to be positive or
4810 : "_ssa1" is used just in zero comparisons:
4811 :
4812 : iftmp1 = PHI <(pow2a - 1), (pow2b - 1), (pow2c - 1), ...>
4813 : _ssa1 = _ssa2 & iftmp1;
4814 :
4815 : - for DIV, if "_ssa2" is known to be positive:
4816 :
4817 : iftmp1 = PHI <log2 (pow2a), log2 (pow2b), log2 (pow2c), ...>
4818 : _ssa1 = _ssa2 >> iftmp1; */
4819 : static bool
4820 133595 : simplify_phi_result_movdiv (gimple *stmt, tree_code code)
4821 : {
4822 133595 : tree rhs1 = gimple_assign_rhs1 (stmt);
4823 133595 : tree_code new_code;
4824 133595 : bool needs_reset = false;
4825 :
4826 : /* Skip complex types (PR127163) */
4827 133595 : if (!INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4828 : return false;
4829 :
4830 67288 : switch (code)
4831 : {
4832 228 : case TRUNC_MOD_EXPR:
4833 228 : case FLOOR_MOD_EXPR:
4834 228 : if (!tree_expr_nonnegative_p (rhs1))
4835 : {
4836 : /* For a negative X, the truncating X % 2^k and X & (2^k - 1)
4837 : differ (e.g. -3 % 4 is -3 but -3 & 3 is 1), though both are
4838 : zero for the same X. That is enough when the result is only
4839 : compared with zero, but the range recorded for the MOD result
4840 : no longer holds for the AND, so reset it. */
4841 143 : needs_reset = true;
4842 143 : if (!use_in_zero_equality (gimple_assign_lhs (stmt), true))
4843 : return false;
4844 : }
4845 :
4846 : new_code = BIT_AND_EXPR;
4847 : break;
4848 :
4849 703 : case TRUNC_DIV_EXPR:
4850 703 : case FLOOR_DIV_EXPR:
4851 703 : case EXACT_DIV_EXPR:
4852 703 : if (!tree_expr_nonnegative_p (rhs1))
4853 : return false;
4854 :
4855 : new_code = RSHIFT_EXPR;
4856 : break;
4857 :
4858 : default:
4859 : return false;
4860 : }
4861 :
4862 552 : gphi *phi = as_a<gphi *> (SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt)));
4863 :
4864 1159 : for (unsigned int i = 0; i < gimple_phi_num_args (phi); i++)
4865 594 : if (!integer_pow2p (gimple_phi_arg_def (phi, i))
4866 594 : || tree_int_cst_sgn (gimple_phi_arg_def (phi, i)) < 0)
4867 : return false;
4868 :
4869 13 : tree type = TREE_TYPE (gimple_phi_result (phi));
4870 13 : tree new_phires = make_ssa_name (type);
4871 13 : gphi *new_phi = create_phi_node (new_phires, phi->bb);
4872 :
4873 54 : for (unsigned int i = 0; i < gimple_phi_num_args (phi); i++)
4874 : {
4875 28 : tree phi_arg = gimple_phi_arg_def (phi, i);
4876 28 : edge e = gimple_phi_arg_edge (phi, i);
4877 28 : tree arg;
4878 :
4879 28 : if (new_code == RSHIFT_EXPR)
4880 4 : arg = wide_int_to_tree (type, wi::exact_log2 (wi::to_wide (phi_arg)));
4881 : else
4882 24 : arg = wide_int_to_tree (type, wi::to_wide (phi_arg) - 1);
4883 :
4884 28 : add_phi_arg (new_phi, arg, e,
4885 28 : gimple_phi_arg_location (phi, e->dest_idx));
4886 : }
4887 :
4888 : /* Add a gimple_convert to integer_type_node for new_phires
4889 : since it might be a long long which we want to convert
4890 : into an integer or a bit_int that we want to convert into
4891 : an integer. */
4892 13 : gimple_stmt_iterator gsi;
4893 13 : if (new_code == RSHIFT_EXPR)
4894 : {
4895 2 : gsi = gsi_for_stmt (stmt);
4896 2 : new_phires = gimple_convert (&gsi, true, GSI_SAME_STMT,
4897 : gimple_location (stmt),
4898 : integer_type_node, new_phires);
4899 : }
4900 :
4901 13 : gimple_assign_set_rhs2 (stmt, new_phires);
4902 13 : gimple_assign_set_rhs_code (stmt, new_code);
4903 13 : if (needs_reset)
4904 8 : reset_flow_sensitive_info (gimple_assign_lhs (stmt));
4905 13 : update_stmt (stmt);
4906 :
4907 13 : gsi = gsi_for_phi (phi);
4908 13 : remove_phi_node (&gsi, true);
4909 :
4910 13 : return true;
4911 : }
4912 :
4913 : /* Determine whether applying the 2 permutations (mask1 then mask2)
4914 : gives back one of the input. */
4915 :
4916 : static int
4917 46 : is_combined_permutation_identity (tree mask1, tree mask2)
4918 : {
4919 46 : tree mask;
4920 46 : unsigned HOST_WIDE_INT nelts, i, j;
4921 46 : bool maybe_identity1 = true;
4922 46 : bool maybe_identity2 = true;
4923 :
4924 46 : gcc_checking_assert (TREE_CODE (mask1) == VECTOR_CST
4925 : && TREE_CODE (mask2) == VECTOR_CST);
4926 :
4927 : /* For VLA masks, check for the following pattern:
4928 : v1 = VEC_PERM_EXPR (v0, ..., mask1)
4929 : v2 = VEC_PERM_EXPR (v1, ..., mask2)
4930 : -->
4931 : v2 = v0
4932 : if mask1 == mask2 == {nelts - 1, nelts - 2, ...}. */
4933 :
4934 46 : if (operand_equal_p (mask1, mask2, 0)
4935 46 : && !VECTOR_CST_NELTS (mask1).is_constant ())
4936 : {
4937 : vec_perm_builder builder;
4938 : if (tree_to_vec_perm_builder (&builder, mask1))
4939 : {
4940 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask1));
4941 : vec_perm_indices sel (builder, 1, nelts);
4942 : if (sel.series_p (0, 1, nelts - 1, -1))
4943 : return 1;
4944 : }
4945 : }
4946 :
4947 46 : mask = fold_ternary (VEC_PERM_EXPR, TREE_TYPE (mask1), mask1, mask1, mask2);
4948 46 : if (mask == NULL_TREE || TREE_CODE (mask) != VECTOR_CST)
4949 : return 0;
4950 :
4951 46 : if (!VECTOR_CST_NELTS (mask).is_constant (&nelts))
4952 : return 0;
4953 80 : for (i = 0; i < nelts; i++)
4954 : {
4955 80 : tree val = VECTOR_CST_ELT (mask, i);
4956 80 : gcc_assert (TREE_CODE (val) == INTEGER_CST);
4957 80 : j = TREE_INT_CST_LOW (val) & (2 * nelts - 1);
4958 80 : if (j == i)
4959 : maybe_identity2 = false;
4960 63 : else if (j == i + nelts)
4961 : maybe_identity1 = false;
4962 : else
4963 : return 0;
4964 : }
4965 0 : return maybe_identity1 ? 1 : maybe_identity2 ? 2 : 0;
4966 : }
4967 :
4968 : /* Combine a shuffle with its arguments. Returns true if there were any
4969 : changes made. */
4970 :
4971 : static bool
4972 191400 : simplify_permutation (gimple_stmt_iterator *gsi)
4973 : {
4974 191400 : gimple *stmt = gsi_stmt (*gsi);
4975 191400 : gimple *def_stmt = NULL;
4976 191400 : tree op0, op1, op2, op3, arg0, arg1;
4977 191400 : enum tree_code code, code2 = ERROR_MARK;
4978 191400 : bool single_use_op0 = false;
4979 :
4980 191400 : gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
4981 :
4982 191400 : op0 = gimple_assign_rhs1 (stmt);
4983 191400 : op1 = gimple_assign_rhs2 (stmt);
4984 191400 : op2 = gimple_assign_rhs3 (stmt);
4985 :
4986 191400 : if (TREE_CODE (op2) != VECTOR_CST)
4987 : return false;
4988 :
4989 188631 : if (TREE_CODE (op0) == VECTOR_CST)
4990 : {
4991 : code = VECTOR_CST;
4992 : arg0 = op0;
4993 : }
4994 186460 : else if (TREE_CODE (op0) == SSA_NAME)
4995 : {
4996 186460 : def_stmt = get_prop_source_stmt (op0, false, &single_use_op0);
4997 186460 : if (!def_stmt)
4998 : return false;
4999 177992 : code = gimple_assign_rhs_code (def_stmt);
5000 177992 : if (code == VIEW_CONVERT_EXPR)
5001 : {
5002 1640 : tree rhs = gimple_assign_rhs1 (def_stmt);
5003 1640 : tree name = TREE_OPERAND (rhs, 0);
5004 1640 : if (TREE_CODE (name) != SSA_NAME)
5005 : return false;
5006 1640 : if (!has_single_use (name))
5007 254 : single_use_op0 = false;
5008 : /* Here we update the def_stmt through this VIEW_CONVERT_EXPR,
5009 : but still keep the code to indicate it comes from
5010 : VIEW_CONVERT_EXPR. */
5011 1640 : def_stmt = SSA_NAME_DEF_STMT (name);
5012 1640 : if (!def_stmt || !is_gimple_assign (def_stmt))
5013 : return false;
5014 853 : if (gimple_assign_rhs_code (def_stmt) != CONSTRUCTOR)
5015 : return false;
5016 : }
5017 176618 : if (!can_propagate_from (def_stmt))
5018 : return false;
5019 24713 : arg0 = gimple_assign_rhs1 (def_stmt);
5020 : }
5021 : else
5022 : return false;
5023 :
5024 : /* Two consecutive shuffles. */
5025 24713 : if (code == VEC_PERM_EXPR)
5026 : {
5027 6817 : tree orig;
5028 6817 : int ident;
5029 :
5030 6817 : if (op0 != op1)
5031 : return false;
5032 46 : op3 = gimple_assign_rhs3 (def_stmt);
5033 46 : if (TREE_CODE (op3) != VECTOR_CST)
5034 : return false;
5035 46 : ident = is_combined_permutation_identity (op3, op2);
5036 46 : if (!ident)
5037 : return false;
5038 0 : orig = (ident == 1) ? gimple_assign_rhs1 (def_stmt)
5039 0 : : gimple_assign_rhs2 (def_stmt);
5040 0 : gimple_assign_set_rhs1 (stmt, unshare_expr (orig));
5041 0 : gimple_assign_set_rhs_code (stmt, TREE_CODE (orig));
5042 0 : gimple_set_num_ops (stmt, 2);
5043 0 : update_stmt (stmt);
5044 0 : remove_prop_source_from_use (op0);
5045 0 : return true;
5046 : }
5047 20067 : else if (code == CONSTRUCTOR
5048 20067 : || code == VECTOR_CST
5049 : || code == VIEW_CONVERT_EXPR)
5050 : {
5051 4987 : if (op0 != op1)
5052 : {
5053 4795 : if (TREE_CODE (op0) == SSA_NAME && !single_use_op0)
5054 : return false;
5055 :
5056 4132 : if (TREE_CODE (op1) == VECTOR_CST)
5057 : arg1 = op1;
5058 3499 : else if (TREE_CODE (op1) == SSA_NAME)
5059 : {
5060 3499 : gimple *def_stmt2 = get_prop_source_stmt (op1, true, NULL);
5061 3499 : if (!def_stmt2)
5062 : return false;
5063 1698 : code2 = gimple_assign_rhs_code (def_stmt2);
5064 1698 : if (code2 == VIEW_CONVERT_EXPR)
5065 : {
5066 0 : tree rhs = gimple_assign_rhs1 (def_stmt2);
5067 0 : tree name = TREE_OPERAND (rhs, 0);
5068 0 : if (TREE_CODE (name) != SSA_NAME)
5069 : return false;
5070 0 : if (!has_single_use (name))
5071 : return false;
5072 0 : def_stmt2 = SSA_NAME_DEF_STMT (name);
5073 0 : if (!def_stmt2 || !is_gimple_assign (def_stmt2))
5074 : return false;
5075 0 : if (gimple_assign_rhs_code (def_stmt2) != CONSTRUCTOR)
5076 : return false;
5077 : }
5078 1698 : else if (code2 != CONSTRUCTOR && code2 != VECTOR_CST)
5079 : return false;
5080 1485 : if (!can_propagate_from (def_stmt2))
5081 : return false;
5082 1485 : arg1 = gimple_assign_rhs1 (def_stmt2);
5083 : }
5084 : else
5085 : return false;
5086 : }
5087 : else
5088 : {
5089 : /* Already used twice in this statement. */
5090 192 : if (TREE_CODE (op0) == SSA_NAME && num_imm_uses (op0) > 2)
5091 : return false;
5092 : arg1 = arg0;
5093 : }
5094 :
5095 : /* If there are any VIEW_CONVERT_EXPRs found when finding permutation
5096 : operands source, check whether it's valid to transform and prepare
5097 : the required new operands. */
5098 2242 : if (code == VIEW_CONVERT_EXPR || code2 == VIEW_CONVERT_EXPR)
5099 : {
5100 : /* Figure out the target vector type to which operands should be
5101 : converted. If both are CONSTRUCTOR, the types should be the
5102 : same, otherwise, use the one of CONSTRUCTOR. */
5103 24 : tree tgt_type = NULL_TREE;
5104 24 : if (code == VIEW_CONVERT_EXPR)
5105 : {
5106 24 : gcc_assert (gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR);
5107 24 : code = CONSTRUCTOR;
5108 24 : tgt_type = TREE_TYPE (arg0);
5109 : }
5110 24 : if (code2 == VIEW_CONVERT_EXPR)
5111 : {
5112 0 : tree arg1_type = TREE_TYPE (arg1);
5113 0 : if (tgt_type == NULL_TREE)
5114 : tgt_type = arg1_type;
5115 0 : else if (tgt_type != arg1_type)
5116 23 : return false;
5117 : }
5118 :
5119 24 : if (!VECTOR_TYPE_P (tgt_type))
5120 : return false;
5121 24 : tree op2_type = TREE_TYPE (op2);
5122 :
5123 : /* Figure out the shrunk factor. */
5124 24 : poly_uint64 tgt_units = TYPE_VECTOR_SUBPARTS (tgt_type);
5125 24 : poly_uint64 op2_units = TYPE_VECTOR_SUBPARTS (op2_type);
5126 24 : if (maybe_gt (tgt_units, op2_units))
5127 : return false;
5128 24 : unsigned int factor;
5129 47 : if (!constant_multiple_p (op2_units, tgt_units, &factor))
5130 : return false;
5131 :
5132 : /* Build the new permutation control vector as target vector. */
5133 24 : vec_perm_builder builder;
5134 24 : if (!tree_to_vec_perm_builder (&builder, op2))
5135 : return false;
5136 24 : vec_perm_indices indices (builder, 2, op2_units);
5137 24 : vec_perm_indices new_indices;
5138 24 : if (new_indices.new_shrunk_vector (indices, factor))
5139 : {
5140 1 : tree mask_type = tgt_type;
5141 1 : if (!VECTOR_INTEGER_TYPE_P (mask_type))
5142 : {
5143 0 : tree elem_type = TREE_TYPE (mask_type);
5144 0 : unsigned elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type));
5145 0 : tree int_type = build_nonstandard_integer_type (elem_size, 0);
5146 0 : mask_type = build_vector_type (int_type, tgt_units);
5147 : }
5148 1 : op2 = vec_perm_indices_to_tree (mask_type, new_indices);
5149 : }
5150 : else
5151 23 : return false;
5152 :
5153 : /* Convert the VECTOR_CST to the appropriate vector type. */
5154 1 : if (tgt_type != TREE_TYPE (arg0))
5155 0 : arg0 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg0);
5156 1 : else if (tgt_type != TREE_TYPE (arg1))
5157 0 : arg1 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg1);
5158 47 : }
5159 :
5160 : /* VIEW_CONVERT_EXPR should be updated to CONSTRUCTOR before. */
5161 2219 : gcc_assert (code == CONSTRUCTOR || code == VECTOR_CST);
5162 :
5163 : /* Shuffle of a constructor. */
5164 2219 : tree res_type
5165 2219 : = build_vector_type (TREE_TYPE (TREE_TYPE (arg0)),
5166 2219 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (op2)));
5167 2219 : tree opt = fold_ternary (VEC_PERM_EXPR, res_type, arg0, arg1, op2);
5168 2219 : if (!opt
5169 280 : || (TREE_CODE (opt) != CONSTRUCTOR && TREE_CODE (opt) != VECTOR_CST))
5170 : return false;
5171 : /* Found VIEW_CONVERT_EXPR before, need one explicit conversion. */
5172 280 : if (res_type != TREE_TYPE (op0))
5173 : {
5174 1 : tree name = make_ssa_name (TREE_TYPE (opt));
5175 1 : gimple *ass_stmt = gimple_build_assign (name, opt);
5176 1 : gsi_insert_before (gsi, ass_stmt, GSI_SAME_STMT);
5177 1 : opt = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op0), name);
5178 : }
5179 280 : gimple_assign_set_rhs_from_tree (gsi, opt);
5180 280 : update_stmt (gsi_stmt (*gsi));
5181 280 : if (TREE_CODE (op0) == SSA_NAME)
5182 1 : remove_prop_source_from_use (op0);
5183 280 : if (op0 != op1 && TREE_CODE (op1) == SSA_NAME)
5184 0 : remove_prop_source_from_use (op1);
5185 : return true;
5186 : }
5187 :
5188 : return false;
5189 : }
5190 :
5191 : /* Get the BIT_FIELD_REF definition of VAL, if any, looking through
5192 : conversions with code CONV_CODE or update it if still ERROR_MARK.
5193 : Return NULL_TREE if no such matching def was found. */
5194 :
5195 : static tree
5196 436963 : get_bit_field_ref_def (tree val, enum tree_code &conv_code)
5197 : {
5198 436963 : if (TREE_CODE (val) != SSA_NAME)
5199 : return NULL_TREE ;
5200 406104 : gimple *def_stmt = get_prop_source_stmt (val, false, NULL);
5201 406104 : if (!def_stmt)
5202 : return NULL_TREE;
5203 325241 : enum tree_code code = gimple_assign_rhs_code (def_stmt);
5204 325241 : if (code == FLOAT_EXPR
5205 325241 : || code == FIX_TRUNC_EXPR
5206 : || CONVERT_EXPR_CODE_P (code))
5207 : {
5208 188450 : tree op1 = gimple_assign_rhs1 (def_stmt);
5209 188450 : if (conv_code == ERROR_MARK)
5210 90275 : conv_code = code;
5211 98175 : else if (conv_code != code)
5212 : return NULL_TREE;
5213 188425 : if (TREE_CODE (op1) != SSA_NAME)
5214 : return NULL_TREE;
5215 79631 : def_stmt = SSA_NAME_DEF_STMT (op1);
5216 79631 : if (! is_gimple_assign (def_stmt))
5217 : return NULL_TREE;
5218 63611 : code = gimple_assign_rhs_code (def_stmt);
5219 : }
5220 200402 : if (code != BIT_FIELD_REF)
5221 : return NULL_TREE;
5222 23446 : return gimple_assign_rhs1 (def_stmt);
5223 : }
5224 :
5225 : /* Recognize a VEC_PERM_EXPR. Returns true if there were any changes. */
5226 :
5227 : static bool
5228 167838 : simplify_vector_constructor (gimple_stmt_iterator *gsi)
5229 : {
5230 167838 : gimple *stmt = gsi_stmt (*gsi);
5231 167838 : tree op, orig[2], type;
5232 167838 : unsigned i;
5233 167838 : unsigned HOST_WIDE_INT nelts;
5234 167838 : unsigned HOST_WIDE_INT refnelts;
5235 167838 : enum tree_code conv_code;
5236 167838 : constructor_elt *elt;
5237 :
5238 167838 : op = gimple_assign_rhs1 (stmt);
5239 167838 : type = TREE_TYPE (op);
5240 167838 : gcc_checking_assert (TREE_CODE (op) == CONSTRUCTOR
5241 : && TREE_CODE (type) == VECTOR_TYPE);
5242 :
5243 167838 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
5244 : return false;
5245 :
5246 167838 : orig[0] = NULL;
5247 167838 : orig[1] = NULL;
5248 167838 : tree orig_elem_type[2] = {};
5249 167838 : conv_code = ERROR_MARK;
5250 167838 : bool maybe_ident = true;
5251 167838 : bool maybe_blend[2] = { true, true };
5252 167838 : tree one_constant = NULL_TREE;
5253 167838 : tree one_nonconstant = NULL_TREE;
5254 167838 : tree subelt;
5255 167838 : auto_vec<tree> constants;
5256 167838 : constants.safe_grow_cleared (nelts, true);
5257 167838 : auto_vec<std::pair<unsigned, unsigned>, 64> elts;
5258 167838 : unsigned int tsubelts = 0;
5259 470480 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (op), i, elt)
5260 : {
5261 436963 : tree ref, op1;
5262 436963 : unsigned int elem, src_elem_size;
5263 436963 : unsigned HOST_WIDE_INT nsubelts = 1;
5264 :
5265 436963 : if (i >= nelts)
5266 167838 : return false;
5267 :
5268 : /* Look for elements extracted and possibly converted from
5269 : another vector. */
5270 436963 : op1 = get_bit_field_ref_def (elt->value, conv_code);
5271 441266 : if (op1
5272 23446 : && TREE_CODE ((ref = TREE_OPERAND (op1, 0))) == SSA_NAME
5273 4998 : && VECTOR_TYPE_P (TREE_TYPE (ref))
5274 4983 : && (tree_nop_conversion_p (TREE_TYPE (op1),
5275 4983 : TREE_TYPE (TREE_TYPE (ref)))
5276 813 : || (VECTOR_TYPE_P (TREE_TYPE (op1))
5277 133 : && tree_nop_conversion_p (TREE_TYPE (TREE_TYPE (op1)),
5278 133 : TREE_TYPE (TREE_TYPE (ref)))
5279 133 : && TYPE_VECTOR_SUBPARTS (TREE_TYPE (op1))
5280 133 : .is_constant (&nsubelts)))
5281 4303 : && constant_multiple_p (bit_field_size (op1), nsubelts,
5282 : &src_elem_size)
5283 441266 : && constant_multiple_p (bit_field_offset (op1), src_elem_size, &elem)
5284 441266 : && TYPE_VECTOR_SUBPARTS (TREE_TYPE (ref)).is_constant (&refnelts))
5285 : {
5286 : unsigned int j;
5287 4663 : for (j = 0; j < 2; ++j)
5288 : {
5289 4638 : if (!orig[j])
5290 : {
5291 1877 : if (j == 0
5292 2077 : || useless_type_conversion_p (TREE_TYPE (orig[0]),
5293 200 : TREE_TYPE (ref)))
5294 : break;
5295 : }
5296 2761 : else if (ref == orig[j])
5297 : break;
5298 : }
5299 : /* Found a suitable vector element. */
5300 4303 : if (j < 2)
5301 : {
5302 4278 : orig[j] = ref;
5303 : /* Track what element type was actually extracted (which may
5304 : differ in signedness from the vector's element type due to
5305 : tree_nop_conversion_p). */
5306 4278 : if (!orig_elem_type[j])
5307 1873 : orig_elem_type[j] = TREE_TYPE (op1);
5308 4278 : if (elem != i || j != 0)
5309 2238 : maybe_ident = false;
5310 4278 : if (elem != i)
5311 2177 : maybe_blend[j] = false;
5312 8815 : for (unsigned int k = 0; k < nsubelts; ++k)
5313 4537 : elts.safe_push (std::make_pair (j, elem + k));
5314 4278 : tsubelts += nsubelts;
5315 4278 : continue;
5316 4278 : }
5317 : /* Else fallthru. */
5318 : }
5319 : /* Handle elements not extracted from a vector.
5320 : 1. constants by permuting with constant vector
5321 : 2. a unique non-constant element by permuting with a splat vector */
5322 432685 : if (orig[1]
5323 265697 : && orig[1] != error_mark_node)
5324 : return false;
5325 432660 : orig[1] = error_mark_node;
5326 432660 : if (VECTOR_TYPE_P (TREE_TYPE (elt->value))
5327 432660 : && !TYPE_VECTOR_SUBPARTS (TREE_TYPE (elt->value))
5328 5781 : .is_constant (&nsubelts))
5329 : return false;
5330 432660 : if (CONSTANT_CLASS_P (elt->value))
5331 : {
5332 30855 : if (one_nonconstant)
5333 : return false;
5334 21852 : if (!one_constant)
5335 9785 : one_constant = TREE_CODE (elt->value) == VECTOR_CST
5336 9785 : ? VECTOR_CST_ELT (elt->value, 0)
5337 : : elt->value;
5338 21852 : if (TREE_CODE (elt->value) == VECTOR_CST)
5339 : {
5340 777 : for (unsigned int k = 0; k < nsubelts; k++)
5341 587 : constants[tsubelts + k] = VECTOR_CST_ELT (elt->value, k);
5342 : }
5343 : else
5344 21662 : constants[tsubelts] = elt->value;
5345 : }
5346 : else
5347 : {
5348 401805 : if (one_constant)
5349 : return false;
5350 392541 : subelt = VECTOR_TYPE_P (TREE_TYPE (elt->value))
5351 392541 : ? ssa_uniform_vector_p (elt->value)
5352 : : elt->value;
5353 392541 : if (!subelt)
5354 : return false;
5355 387202 : if (!one_nonconstant)
5356 : one_nonconstant = subelt;
5357 235338 : else if (!operand_equal_p (one_nonconstant, subelt, 0))
5358 : return false;
5359 : }
5360 597127 : for (unsigned int k = 0; k < nsubelts; ++k)
5361 298763 : elts.safe_push (std::make_pair (1, tsubelts + k));
5362 298364 : tsubelts += nsubelts;
5363 298364 : maybe_ident = false;
5364 : }
5365 :
5366 67034 : if (elts.length () < nelts)
5367 : return false;
5368 :
5369 32345 : if (! orig[0]
5370 32345 : || ! VECTOR_TYPE_P (TREE_TYPE (orig[0])))
5371 : return false;
5372 1196 : refnelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (orig[0])).to_constant ();
5373 : /* We currently do not handle larger destination vectors. */
5374 1196 : if (refnelts < nelts)
5375 : return false;
5376 :
5377 : /* Determine the element type for the conversion source.
5378 : As orig_elem_type keeps track of the original type, check
5379 : if we need to perform a sign swap after permuting.
5380 : We need to be able to construct a vector type from the element
5381 : type which is not possible for e.g. BitInt or pointers
5382 : so pun with an integer type if needed. */
5383 982 : tree perm_eltype = TREE_TYPE (TREE_TYPE (orig[0]));
5384 982 : bool sign_change_p = false;
5385 982 : if (conv_code != ERROR_MARK
5386 213 : && orig_elem_type[0]
5387 1195 : && TYPE_SIGN (orig_elem_type[0]) != TYPE_SIGN (perm_eltype))
5388 : {
5389 35 : perm_eltype = signed_or_unsigned_type_for
5390 35 : (TYPE_UNSIGNED (orig_elem_type[0]), perm_eltype);
5391 35 : sign_change_p = true;
5392 : }
5393 982 : tree conv_src_type = build_vector_type (perm_eltype, nelts);
5394 :
5395 982 : if (maybe_ident)
5396 : {
5397 : /* When there is no conversion, use the target type directly. */
5398 194 : if (conv_code == ERROR_MARK && nelts != refnelts)
5399 194 : conv_src_type = type;
5400 194 : if (conv_code != ERROR_MARK
5401 194 : && !supportable_convert_operation (conv_code, type, conv_src_type))
5402 : {
5403 : /* Only few targets implement direct conversion patterns so try
5404 : some simple special cases via VEC_[UN]PACK[_FLOAT]_LO_EXPR. */
5405 103 : optab optab;
5406 103 : insn_code icode;
5407 103 : tree halfvectype, dblvectype;
5408 103 : enum tree_code unpack_op;
5409 :
5410 103 : if (!BYTES_BIG_ENDIAN)
5411 183 : unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
5412 103 : ? VEC_UNPACK_FLOAT_LO_EXPR
5413 : : VEC_UNPACK_LO_EXPR);
5414 : else
5415 : unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
5416 : ? VEC_UNPACK_FLOAT_HI_EXPR
5417 : : VEC_UNPACK_HI_EXPR);
5418 :
5419 : /* Conversions between DFP and FP have no special tree code
5420 : but we cannot handle those since all relevant vector conversion
5421 : optabs only have a single mode. */
5422 15 : if (CONVERT_EXPR_CODE_P (conv_code)
5423 88 : && FLOAT_TYPE_P (TREE_TYPE (type))
5424 119 : && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (type))
5425 8 : != DECIMAL_FLOAT_TYPE_P (TREE_TYPE (conv_src_type))))
5426 : return false;
5427 :
5428 15 : if (CONVERT_EXPR_CODE_P (conv_code)
5429 87 : && (2 * TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
5430 87 : == TYPE_PRECISION (TREE_TYPE (type)))
5431 6 : && orig_elem_type[0]
5432 6 : && useless_type_conversion_p (orig_elem_type[0],
5433 6 : TREE_TYPE (TREE_TYPE (orig[0])))
5434 6 : && mode_for_vector (as_a <scalar_mode>
5435 6 : (TYPE_MODE (TREE_TYPE (TREE_TYPE (orig[0])))),
5436 12 : nelts * 2).exists ()
5437 6 : && (dblvectype
5438 6 : = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
5439 6 : nelts * 2))
5440 : /* Only use it for vector modes or for vector booleans
5441 : represented as scalar bitmasks. See PR95528. */
5442 6 : && (VECTOR_MODE_P (TYPE_MODE (dblvectype))
5443 0 : || VECTOR_BOOLEAN_TYPE_P (dblvectype))
5444 6 : && (optab = optab_for_tree_code (unpack_op,
5445 : dblvectype,
5446 : optab_default))
5447 6 : && ((icode = optab_handler (optab, TYPE_MODE (dblvectype)))
5448 : != CODE_FOR_nothing)
5449 102 : && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
5450 : {
5451 0 : gimple_seq stmts = NULL;
5452 0 : tree dbl;
5453 0 : if (refnelts == nelts)
5454 : {
5455 : /* ??? Paradoxical subregs don't exist, so insert into
5456 : the lower half of a wider zero vector. */
5457 0 : dbl = gimple_build (&stmts, BIT_INSERT_EXPR, dblvectype,
5458 : build_zero_cst (dblvectype), orig[0],
5459 0 : bitsize_zero_node);
5460 : }
5461 0 : else if (refnelts == 2 * nelts)
5462 : dbl = orig[0];
5463 : else
5464 0 : dbl = gimple_build (&stmts, BIT_FIELD_REF, dblvectype,
5465 0 : orig[0], TYPE_SIZE (dblvectype),
5466 0 : bitsize_zero_node);
5467 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
5468 0 : gimple_assign_set_rhs_with_ops (gsi, unpack_op, dbl);
5469 : }
5470 15 : else if (CONVERT_EXPR_CODE_P (conv_code)
5471 87 : && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
5472 87 : == 2 * TYPE_PRECISION (TREE_TYPE (type)))
5473 5 : && orig_elem_type[0]
5474 5 : && useless_type_conversion_p (orig_elem_type[0],
5475 5 : TREE_TYPE (TREE_TYPE (orig[0])))
5476 1 : && mode_for_vector (as_a <scalar_mode>
5477 1 : (TYPE_MODE
5478 : (TREE_TYPE (TREE_TYPE (orig[0])))),
5479 2 : nelts / 2).exists ()
5480 1 : && (halfvectype
5481 1 : = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
5482 1 : nelts / 2))
5483 : /* Only use it for vector modes or for vector booleans
5484 : represented as scalar bitmasks. See PR95528. */
5485 1 : && (VECTOR_MODE_P (TYPE_MODE (halfvectype))
5486 0 : || VECTOR_BOOLEAN_TYPE_P (halfvectype))
5487 1 : && (optab = optab_for_tree_code (VEC_PACK_TRUNC_EXPR,
5488 : halfvectype,
5489 : optab_default))
5490 1 : && ((icode = optab_handler (optab, TYPE_MODE (halfvectype)))
5491 : != CODE_FOR_nothing)
5492 103 : && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
5493 : {
5494 0 : gimple_seq stmts = NULL;
5495 0 : tree low = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
5496 0 : orig[0], TYPE_SIZE (halfvectype),
5497 0 : bitsize_zero_node);
5498 0 : tree hig = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
5499 0 : orig[0], TYPE_SIZE (halfvectype),
5500 0 : TYPE_SIZE (halfvectype));
5501 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
5502 0 : gimple_assign_set_rhs_with_ops (gsi, VEC_PACK_TRUNC_EXPR,
5503 : low, hig);
5504 : }
5505 : else
5506 102 : return false;
5507 0 : update_stmt (gsi_stmt (*gsi));
5508 0 : return true;
5509 : }
5510 91 : if (nelts != refnelts)
5511 : {
5512 14 : gassign *lowpart
5513 14 : = gimple_build_assign (make_ssa_name (conv_src_type),
5514 : build3 (BIT_FIELD_REF, conv_src_type,
5515 14 : orig[0], TYPE_SIZE (conv_src_type),
5516 : bitsize_zero_node));
5517 14 : gsi_insert_before (gsi, lowpart, GSI_SAME_STMT);
5518 14 : orig[0] = gimple_assign_lhs (lowpart);
5519 : }
5520 77 : else if (sign_change_p)
5521 : {
5522 0 : gassign *conv
5523 0 : = gimple_build_assign (make_ssa_name (conv_src_type),
5524 : build1 (VIEW_CONVERT_EXPR, conv_src_type,
5525 : orig[0]));
5526 0 : gsi_insert_before (gsi, conv, GSI_SAME_STMT);
5527 0 : orig[0] = gimple_assign_lhs (conv);
5528 : }
5529 91 : if (conv_code == ERROR_MARK)
5530 : {
5531 74 : tree src_type = TREE_TYPE (orig[0]);
5532 74 : if (!useless_type_conversion_p (type, src_type))
5533 : {
5534 0 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
5535 : TYPE_VECTOR_SUBPARTS (src_type))
5536 : && tree_nop_conversion_p (TREE_TYPE (type),
5537 : TREE_TYPE (src_type)));
5538 0 : tree rhs = build1 (VIEW_CONVERT_EXPR, type, orig[0]);
5539 0 : orig[0] = make_ssa_name (type);
5540 0 : gassign *assign = gimple_build_assign (orig[0], rhs);
5541 0 : gsi_insert_before (gsi, assign, GSI_SAME_STMT);
5542 : }
5543 74 : gimple_assign_set_rhs_from_tree (gsi, orig[0]);
5544 : }
5545 : else
5546 17 : gimple_assign_set_rhs_with_ops (gsi, conv_code, orig[0],
5547 : NULL_TREE, NULL_TREE);
5548 : }
5549 : else
5550 : {
5551 : /* If we combine a vector with a non-vector avoid cases where
5552 : we'll obviously end up with more GIMPLE stmts which is when
5553 : we'll later not fold this to a single insert into the vector
5554 : and we had a single extract originally. See PR92819. */
5555 788 : if (nelts == 2
5556 334 : && refnelts > 2
5557 122 : && orig[1] == error_mark_node
5558 31 : && !maybe_blend[0])
5559 92 : return false;
5560 763 : tree mask_type, perm_type;
5561 763 : perm_type = TREE_TYPE (orig[0]);
5562 763 : if (conv_code != ERROR_MARK
5563 763 : && !supportable_convert_operation (conv_code, type, conv_src_type))
5564 : return false;
5565 :
5566 : /* Now that we know the number of elements of the source build the
5567 : permute vector.
5568 : ??? When the second vector has constant values we can shuffle
5569 : it and its source indexes to make the permutation supported.
5570 : For now it mimics a blend. */
5571 753 : vec_perm_builder sel (refnelts, refnelts, 1);
5572 753 : bool all_same_p = true;
5573 8410 : for (i = 0; i < elts.length (); ++i)
5574 : {
5575 3452 : sel.quick_push (elts[i].second + elts[i].first * refnelts);
5576 3452 : all_same_p &= known_eq (sel[i], sel[0]);
5577 : }
5578 : /* And fill the tail with "something". It's really don't care,
5579 : and ideally we'd allow VEC_PERM to have a smaller destination
5580 : vector. As a heuristic:
5581 :
5582 : (a) if what we have so far duplicates a single element, make the
5583 : tail do the same
5584 :
5585 : (b) otherwise preserve a uniform orig[0]. This facilitates
5586 : later pattern-matching of VEC_PERM_EXPR to a BIT_INSERT_EXPR. */
5587 1365 : for (; i < refnelts; ++i)
5588 1224 : sel.quick_push (all_same_p
5589 1836 : ? sel[0]
5590 156 : : (elts[0].second == 0 && elts[0].first == 0
5591 908 : ? 0 : refnelts) + i);
5592 951 : vec_perm_indices indices (sel, orig[1] ? 2 : 1, refnelts);
5593 753 : machine_mode vmode = TYPE_MODE (perm_type);
5594 753 : if ((cfun->curr_properties & PROP_gimple_lvec)
5595 753 : && !can_vec_perm_const_p (vmode, vmode, indices))
5596 : return false;
5597 696 : mask_type = build_vector_type (ssizetype, refnelts);
5598 696 : tree op2 = vec_perm_indices_to_tree (mask_type, indices);
5599 696 : bool converted_orig1 = false;
5600 696 : gimple_seq stmts = NULL;
5601 696 : if (!orig[1])
5602 173 : orig[1] = orig[0];
5603 523 : else if (orig[1] == error_mark_node
5604 401 : && one_nonconstant)
5605 : {
5606 : /* ??? We can see if we can safely convert to the original
5607 : element type. */
5608 91 : converted_orig1 = conv_code != ERROR_MARK;
5609 91 : tree target_type = converted_orig1 ? type : perm_type;
5610 91 : tree nonconstant_for_splat = one_nonconstant;
5611 : /* If there's a nop conversion between the target element type and
5612 : the nonconstant's type, convert it. */
5613 91 : if (!useless_type_conversion_p (TREE_TYPE (target_type),
5614 91 : TREE_TYPE (one_nonconstant)))
5615 0 : nonconstant_for_splat
5616 0 : = gimple_build (&stmts, NOP_EXPR, TREE_TYPE (target_type),
5617 : one_nonconstant);
5618 91 : orig[1] = gimple_build_vector_from_val (&stmts, UNKNOWN_LOCATION,
5619 : target_type,
5620 : nonconstant_for_splat);
5621 91 : }
5622 432 : else if (orig[1] == error_mark_node)
5623 : {
5624 : /* ??? See if we can convert the vector to the original type. */
5625 310 : converted_orig1 = conv_code != ERROR_MARK;
5626 310 : unsigned n = converted_orig1 ? nelts : refnelts;
5627 293 : tree target_type = converted_orig1 ? type : perm_type;
5628 310 : tree_vector_builder vec (target_type, n, 1);
5629 2508 : for (unsigned i = 0; i < n; ++i)
5630 3652 : if (i < nelts && constants[i])
5631 : {
5632 1056 : tree constant = constants[i];
5633 : /* If there's a nop conversion, convert the constant. */
5634 1056 : if (!useless_type_conversion_p (TREE_TYPE (target_type),
5635 1056 : TREE_TYPE (constant)))
5636 2 : constant = fold_convert (TREE_TYPE (target_type), constant);
5637 1056 : vec.quick_push (constant);
5638 : }
5639 : else
5640 : {
5641 : /* ??? Push a don't-care value. */
5642 832 : tree constant = one_constant;
5643 832 : if (!useless_type_conversion_p (TREE_TYPE (target_type),
5644 832 : TREE_TYPE (constant)))
5645 2 : constant = fold_convert (TREE_TYPE (target_type), constant);
5646 832 : vec.quick_push (constant);
5647 : }
5648 310 : orig[1] = vec.build ();
5649 310 : }
5650 574 : tree blend_op2 = NULL_TREE;
5651 574 : if (converted_orig1)
5652 : {
5653 : /* Make sure we can do a blend in the target type. */
5654 34 : vec_perm_builder sel (nelts, nelts, 1);
5655 136 : for (i = 0; i < elts.length (); ++i)
5656 102 : sel.quick_push (elts[i].first
5657 102 : ? elts[i].second + nelts : i);
5658 34 : vec_perm_indices indices (sel, 2, nelts);
5659 34 : machine_mode vmode = TYPE_MODE (type);
5660 34 : if ((cfun->curr_properties & PROP_gimple_lvec)
5661 34 : && !can_vec_perm_const_p (vmode, vmode, indices))
5662 0 : return false;
5663 34 : mask_type = build_vector_type (ssizetype, nelts);
5664 34 : blend_op2 = vec_perm_indices_to_tree (mask_type, indices);
5665 34 : }
5666 :
5667 : /* For a real orig[1] (no splat, constant etc.) we might need to
5668 : nop-convert it. Do so here. */
5669 696 : if (orig[1] && orig[1] != error_mark_node
5670 696 : && !converted_orig1
5671 662 : && !useless_type_conversion_p (perm_type, TREE_TYPE (orig[1]))
5672 696 : && tree_nop_conversion_p (TREE_TYPE (perm_type),
5673 0 : TREE_TYPE (TREE_TYPE (orig[1]))))
5674 0 : orig[1] = gimple_build (&stmts, VIEW_CONVERT_EXPR, perm_type,
5675 : orig[1]);
5676 :
5677 696 : tree orig1_for_perm
5678 696 : = converted_orig1 ? build_zero_cst (perm_type) : orig[1];
5679 696 : tree res = gimple_build (&stmts, VEC_PERM_EXPR, perm_type,
5680 : orig[0], orig1_for_perm, op2);
5681 : /* If we're building a smaller vector, extract the element
5682 : with the proper type. */
5683 696 : if (nelts != refnelts)
5684 244 : res = gimple_build (&stmts, BIT_FIELD_REF,
5685 : conv_code != ERROR_MARK ? conv_src_type : type,
5686 : res,
5687 122 : TYPE_SIZE (conv_code != ERROR_MARK ? conv_src_type
5688 : : type),
5689 122 : bitsize_zero_node);
5690 : /* Otherwise, we can still have an intermediate sign change.
5691 : ??? In that case we have two subsequent conversions.
5692 : We should be able to merge them. */
5693 574 : else if (sign_change_p)
5694 15 : res = gimple_build (&stmts, VIEW_CONVERT_EXPR, conv_src_type, res);
5695 : /* Finally, apply the conversion. */
5696 696 : if (conv_code != ERROR_MARK)
5697 68 : res = gimple_build (&stmts, conv_code, type, res);
5698 628 : else if (!useless_type_conversion_p (type, TREE_TYPE (res)))
5699 : {
5700 3 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
5701 : TYPE_VECTOR_SUBPARTS (perm_type))
5702 : && tree_nop_conversion_p (TREE_TYPE (type),
5703 : TREE_TYPE (perm_type)));
5704 3 : res = gimple_build (&stmts, VIEW_CONVERT_EXPR, type, res);
5705 : }
5706 : /* Blend in the actual constant. */
5707 696 : if (converted_orig1)
5708 34 : res = gimple_build (&stmts, VEC_PERM_EXPR, type,
5709 34 : res, orig[1], blend_op2);
5710 696 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
5711 696 : gimple_assign_set_rhs_with_ops (gsi, SSA_NAME, res);
5712 753 : }
5713 787 : update_stmt (gsi_stmt (*gsi));
5714 787 : return true;
5715 167838 : }
5716 :
5717 : /* Prepare a TARGET_MEM_REF ref so that it can be subsetted as
5718 : lvalue. This splits out an address computation stmt before *GSI
5719 : and returns a MEM_REF wrapping the address. */
5720 :
5721 : static tree
5722 1115 : prepare_target_mem_ref_lvalue (tree ref, gimple_stmt_iterator *gsi)
5723 : {
5724 1115 : if (TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR)
5725 189 : mark_addressable (TREE_OPERAND (TREE_OPERAND (ref, 0), 0));
5726 1115 : tree ptrtype = build_pointer_type (TREE_TYPE (ref));
5727 1115 : tree tem = make_ssa_name (ptrtype);
5728 1115 : gimple *new_stmt
5729 1115 : = gimple_build_assign (tem, build1 (ADDR_EXPR, TREE_TYPE (tem),
5730 : unshare_expr (ref)));
5731 1115 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
5732 2230 : ref = build2_loc (EXPR_LOCATION (ref),
5733 1115 : MEM_REF, TREE_TYPE (ref), tem,
5734 1115 : build_int_cst (TREE_TYPE (TREE_OPERAND (ref, 1)), 0));
5735 1115 : return ref;
5736 : }
5737 :
5738 : /* Rewrite the vector load at *GSI to component-wise loads if the load
5739 : is only used in BIT_FIELD_REF extractions with eventual intermediate
5740 : widening. */
5741 :
5742 : static void
5743 279356 : optimize_vector_load (gimple_stmt_iterator *gsi)
5744 : {
5745 279356 : gimple *stmt = gsi_stmt (*gsi);
5746 279356 : tree lhs = gimple_assign_lhs (stmt);
5747 279356 : tree rhs = gimple_assign_rhs1 (stmt);
5748 279356 : tree vuse = gimple_vuse (stmt);
5749 :
5750 : /* Gather BIT_FIELD_REFs to rewrite, looking through
5751 : VEC_UNPACK_{LO,HI}_EXPR. */
5752 279356 : use_operand_p use_p;
5753 279356 : imm_use_iterator iter;
5754 279356 : bool rewrite = true;
5755 279356 : bool scalar_use = false;
5756 279356 : bool unpack_use = false;
5757 279356 : auto_vec<gimple *, 8> bf_stmts;
5758 279356 : auto_vec<tree, 8> worklist;
5759 279356 : worklist.quick_push (lhs);
5760 281135 : do
5761 : {
5762 281135 : tree def = worklist.pop ();
5763 281135 : unsigned HOST_WIDE_INT def_eltsize
5764 281135 : = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (def))));
5765 356659 : FOR_EACH_IMM_USE_FAST (use_p, iter, def)
5766 : {
5767 336131 : gimple *use_stmt = USE_STMT (use_p);
5768 336131 : if (is_gimple_debug (use_stmt))
5769 75524 : continue;
5770 334693 : tree use_lhs;
5771 334693 : if (!is_gimple_assign (use_stmt)
5772 : /* For alias reasons we move the use to the place of the
5773 : load. Avoid this when abnormals are involved. */
5774 334693 : || ((TREE_CODE ((use_lhs = gimple_assign_lhs (use_stmt)))
5775 : == SSA_NAME)
5776 234600 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs)))
5777 : {
5778 : rewrite = false;
5779 260607 : break;
5780 : }
5781 304784 : enum tree_code use_code = gimple_assign_rhs_code (use_stmt);
5782 304784 : tree use_rhs = gimple_assign_rhs1 (use_stmt);
5783 375319 : if (use_code == BIT_FIELD_REF
5784 70536 : && TREE_OPERAND (use_rhs, 0) == def
5785 : /* If its on the VEC_UNPACK_{HI,LO}_EXPR
5786 : def need to verify it is element aligned. */
5787 375320 : && (def == lhs
5788 153 : || (known_eq (bit_field_size (use_rhs), def_eltsize)
5789 70688 : && constant_multiple_p (bit_field_offset (use_rhs),
5790 : def_eltsize)
5791 : /* We can simulate the VEC_UNPACK_{HI,LO}_EXPR
5792 : via a NOP_EXPR only for integral types.
5793 : ??? Support VEC_UNPACK_FLOAT_{HI,LO}_EXPR. */
5794 153 : && INTEGRAL_TYPE_P (TREE_TYPE (use_rhs)))))
5795 : {
5796 70535 : if (!VECTOR_TYPE_P (TREE_TYPE (gimple_assign_lhs (use_stmt))))
5797 68228 : scalar_use = true;
5798 70535 : bf_stmts.safe_push (use_stmt);
5799 70535 : continue;
5800 : }
5801 : /* Walk through one level of VEC_UNPACK_{LO,HI}_EXPR. */
5802 234249 : if (def == lhs
5803 232551 : && (use_code == VEC_UNPACK_HI_EXPR
5804 232551 : || use_code == VEC_UNPACK_LO_EXPR)
5805 3551 : && use_rhs == lhs)
5806 : {
5807 3551 : unpack_use = true;
5808 3551 : worklist.safe_push (gimple_assign_lhs (use_stmt));
5809 3551 : continue;
5810 : }
5811 : rewrite = false;
5812 : break;
5813 281135 : }
5814 281135 : if (!rewrite)
5815 : break;
5816 : }
5817 41056 : while (!worklist.is_empty ());
5818 :
5819 279356 : rewrite = rewrite && (scalar_use
5820 18749 : || unpack_use
5821 626 : || !can_implement_p (mov_optab,
5822 626 : TYPE_MODE (TREE_TYPE (lhs))));
5823 260789 : if (!rewrite)
5824 : {
5825 260789 : gsi_next (gsi);
5826 260789 : return;
5827 : }
5828 : /* We now have all ultimate uses of the load to rewrite in bf_stmts. */
5829 :
5830 : /* Prepare the original ref to be wrapped in adjusted BIT_FIELD_REFs.
5831 : For TARGET_MEM_REFs we have to separate the LEA from the reference. */
5832 18567 : tree load_rhs = rhs;
5833 18567 : if (TREE_CODE (load_rhs) == TARGET_MEM_REF)
5834 1114 : load_rhs = prepare_target_mem_ref_lvalue (load_rhs, gsi);
5835 :
5836 : /* Rewrite the BIT_FIELD_REFs to be actual loads, re-emitting them at
5837 : the place of the original load. */
5838 122045 : for (gimple *use_stmt : bf_stmts)
5839 : {
5840 66344 : tree bfr = gimple_assign_rhs1 (use_stmt);
5841 66344 : tree new_rhs = unshare_expr (load_rhs);
5842 66344 : if (TREE_OPERAND (bfr, 0) != lhs)
5843 : {
5844 : /* When the BIT_FIELD_REF is on the promoted vector we have to
5845 : adjust it and emit a conversion afterwards. */
5846 152 : gimple *def_stmt
5847 152 : = SSA_NAME_DEF_STMT (TREE_OPERAND (bfr, 0));
5848 152 : enum tree_code def_code
5849 152 : = gimple_assign_rhs_code (def_stmt);
5850 :
5851 : /* The adjusted BIT_FIELD_REF is of the promotion source
5852 : vector size and at half of the offset... */
5853 152 : new_rhs = fold_build3 (BIT_FIELD_REF,
5854 : TREE_TYPE (TREE_TYPE (lhs)),
5855 : new_rhs,
5856 : TYPE_SIZE (TREE_TYPE (TREE_TYPE (lhs))),
5857 : size_binop (EXACT_DIV_EXPR,
5858 : TREE_OPERAND (bfr, 2),
5859 : bitsize_int (2)));
5860 : /* ... and offsetted by half of the vector if VEC_UNPACK_HI_EXPR. */
5861 152 : if (def_code == (!BYTES_BIG_ENDIAN
5862 : ? VEC_UNPACK_HI_EXPR : VEC_UNPACK_LO_EXPR))
5863 76 : TREE_OPERAND (new_rhs, 2)
5864 152 : = size_binop (PLUS_EXPR, TREE_OPERAND (new_rhs, 2),
5865 : size_binop (EXACT_DIV_EXPR,
5866 : TYPE_SIZE (TREE_TYPE (lhs)),
5867 : bitsize_int (2)));
5868 152 : tree tem = make_ssa_name (TREE_TYPE (TREE_TYPE (lhs)));
5869 152 : gimple *new_stmt = gimple_build_assign (tem, new_rhs);
5870 152 : location_t loc = gimple_location (use_stmt);
5871 152 : gimple_set_location (new_stmt, loc);
5872 152 : gimple_set_vuse (new_stmt, vuse);
5873 152 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
5874 : /* Perform scalar promotion. */
5875 152 : new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
5876 : NOP_EXPR, tem);
5877 152 : gimple_set_location (new_stmt, loc);
5878 152 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
5879 : }
5880 : else
5881 : {
5882 : /* When the BIT_FIELD_REF is on the original load result
5883 : we can just wrap that. */
5884 66192 : tree new_rhs = fold_build3 (BIT_FIELD_REF, TREE_TYPE (bfr),
5885 : unshare_expr (load_rhs),
5886 : TREE_OPERAND (bfr, 1),
5887 : TREE_OPERAND (bfr, 2));
5888 66192 : gimple *new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
5889 : new_rhs);
5890 66192 : location_t loc = gimple_location (use_stmt);
5891 66192 : gimple_set_location (new_stmt, loc);
5892 66192 : gimple_set_vuse (new_stmt, vuse);
5893 66192 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
5894 : }
5895 66344 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5896 66344 : unlink_stmt_vdef (use_stmt);
5897 66344 : gsi_remove (&gsi2, true);
5898 : }
5899 :
5900 : /* Finally get rid of the intermediate stmts. */
5901 18567 : gimple *use_stmt;
5902 19189 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
5903 : {
5904 622 : if (is_gimple_debug (use_stmt))
5905 : {
5906 560 : if (gimple_debug_bind_p (use_stmt))
5907 : {
5908 560 : gimple_debug_bind_reset_value (use_stmt);
5909 560 : update_stmt (use_stmt);
5910 : }
5911 560 : continue;
5912 : }
5913 62 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5914 62 : unlink_stmt_vdef (use_stmt);
5915 62 : release_defs (use_stmt);
5916 62 : gsi_remove (&gsi2, true);
5917 18567 : }
5918 : /* And the original load. */
5919 18567 : release_defs (stmt);
5920 18567 : gsi_remove (gsi, true);
5921 279356 : }
5922 :
5923 :
5924 : /* Primitive "lattice" function for gimple_simplify. */
5925 :
5926 : static tree
5927 1792259568 : fwprop_ssa_val (tree name)
5928 : {
5929 : /* First valueize NAME. */
5930 1792259568 : if (TREE_CODE (name) == SSA_NAME
5931 1792259568 : && SSA_NAME_VERSION (name) < lattice.length ())
5932 : {
5933 1791333624 : tree val = lattice[SSA_NAME_VERSION (name)];
5934 1791333624 : if (val)
5935 1792259568 : name = val;
5936 : }
5937 : /* We continue matching along SSA use-def edges for SSA names
5938 : that are not single-use. Currently there are no patterns
5939 : that would cause any issues with that. */
5940 1792259568 : return name;
5941 : }
5942 :
5943 : /* Search for opportunities to free half of the lanes in the following pattern:
5944 :
5945 : v_in = {e0, e1, e2, e3}
5946 : v_1 = VEC_PERM <v_in, v_in, {0, 2, 0, 2}>
5947 : // v_1 = {e0, e2, e0, e2}
5948 : v_2 = VEC_PERM <v_in, v_in, {1, 3, 1, 3}>
5949 : // v_2 = {e1, e3, e1, e3}
5950 :
5951 : v_x = v_1 + v_2
5952 : // v_x = {e0+e1, e2+e3, e0+e1, e2+e3}
5953 : v_y = v_1 - v_2
5954 : // v_y = {e0-e1, e2-e3, e0-e1, e2-e3}
5955 :
5956 : v_out = VEC_PERM <v_x, v_y, {0, 1, 6, 7}>
5957 : // v_out = {e0+e1, e2+e3, e0-e1, e2-e3}
5958 :
5959 : The last statement could be simplified to:
5960 : v_out' = VEC_PERM <v_x, v_y, {0, 1, 4, 5}>
5961 : // v_out' = {e0+e1, e2+e3, e0-e1, e2-e3}
5962 :
5963 : Characteristic properties:
5964 : - v_1 and v_2 are created from the same input vector v_in and introduce the
5965 : lane duplication (in the selection operand) that we can eliminate.
5966 : - v_x and v_y are results from lane-preserving operations that use v_1 and
5967 : v_2 as inputs.
5968 : - v_out is created by selecting from duplicated lanes. */
5969 :
5970 : static bool
5971 189051 : recognise_vec_perm_simplify_seq (gassign *stmt, vec_perm_simplify_seq *seq)
5972 : {
5973 189051 : unsigned HOST_WIDE_INT nelts;
5974 :
5975 189051 : gcc_checking_assert (stmt);
5976 189051 : gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
5977 189051 : basic_block bb = gimple_bb (stmt);
5978 :
5979 : /* Decompose the final vec permute statement. */
5980 189051 : tree v_x = gimple_assign_rhs1 (stmt);
5981 189051 : tree v_y = gimple_assign_rhs2 (stmt);
5982 189051 : tree sel = gimple_assign_rhs3 (stmt);
5983 :
5984 189051 : if (TREE_CODE (sel) != VECTOR_CST
5985 263417 : || !VECTOR_CST_NELTS (sel).is_constant (&nelts)
5986 186282 : || TREE_CODE (v_x) != SSA_NAME
5987 184190 : || TREE_CODE (v_y) != SSA_NAME
5988 178692 : || !has_single_use (v_x)
5989 300671 : || !has_single_use (v_y))
5990 : return false;
5991 :
5992 : /* Don't analyse sequences with many lanes. */
5993 109824 : if (nelts > 4)
5994 : return false;
5995 :
5996 : /* Lookup the definition of v_x and v_y. */
5997 106090 : gassign *v_x_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x));
5998 106090 : gassign *v_y_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_y));
5999 105713 : if (!v_x_stmt || gimple_bb (v_x_stmt) != bb
6000 211803 : || !v_y_stmt || gimple_bb (v_y_stmt) != bb)
6001 : return false;
6002 :
6003 : /* Check the operations that define v_x and v_y. */
6004 105706 : if (TREE_CODE_CLASS (gimple_assign_rhs_code (v_x_stmt)) != tcc_binary
6005 107816 : || TREE_CODE_CLASS (gimple_assign_rhs_code (v_y_stmt)) != tcc_binary)
6006 : return false;
6007 :
6008 2110 : tree v_x_1 = gimple_assign_rhs1 (v_x_stmt);
6009 2110 : tree v_x_2 = gimple_assign_rhs2 (v_x_stmt);
6010 2110 : tree v_y_1 = gimple_assign_rhs1 (v_y_stmt);
6011 2110 : tree v_y_2 = gimple_assign_rhs2 (v_y_stmt);
6012 :
6013 2110 : if (v_x_stmt == v_y_stmt
6014 2110 : || TREE_CODE (v_x_1) != SSA_NAME
6015 2107 : || TREE_CODE (v_x_2) != SSA_NAME
6016 2083 : || num_imm_uses (v_x_1) != 2
6017 4034 : || num_imm_uses (v_x_2) != 2)
6018 : return false;
6019 :
6020 1883 : if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
6021 : {
6022 : /* Allow operands of commutative operators to swap. */
6023 663 : if (commutative_tree_code (gimple_assign_rhs_code (v_x_stmt)))
6024 : {
6025 : /* Keep v_x_1 the first operand for non-commutative operators. */
6026 257 : std::swap (v_x_1, v_x_2);
6027 257 : if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
6028 : return false;
6029 : }
6030 406 : else if (commutative_tree_code (gimple_assign_rhs_code (v_y_stmt)))
6031 : {
6032 406 : if (v_x_1 != v_y_2 || v_x_2 != v_y_1)
6033 : return false;
6034 : }
6035 : else
6036 : return false;
6037 : }
6038 1883 : gassign *v_1_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_1));
6039 1883 : gassign *v_2_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_2));
6040 1819 : if (!v_1_stmt || gimple_bb (v_1_stmt) != bb
6041 3702 : || !v_2_stmt || gimple_bb (v_2_stmt) != bb)
6042 : return false;
6043 :
6044 1813 : if (gimple_assign_rhs_code (v_1_stmt) != VEC_PERM_EXPR
6045 1939 : || gimple_assign_rhs_code (v_2_stmt) != VEC_PERM_EXPR)
6046 : return false;
6047 :
6048 : /* Decompose initial VEC_PERM_EXPRs. */
6049 112 : tree v_in = gimple_assign_rhs1 (v_1_stmt);
6050 112 : tree v_1_sel = gimple_assign_rhs3 (v_1_stmt);
6051 112 : tree v_2_sel = gimple_assign_rhs3 (v_2_stmt);
6052 112 : if (v_in != gimple_assign_rhs2 (v_1_stmt)
6053 107 : || v_in != gimple_assign_rhs1 (v_2_stmt)
6054 217 : || v_in != gimple_assign_rhs2 (v_2_stmt))
6055 : return false;
6056 :
6057 105 : unsigned HOST_WIDE_INT v_1_nelts, v_2_nelts;
6058 105 : if (TREE_CODE (v_1_sel) != VECTOR_CST
6059 105 : || !VECTOR_CST_NELTS (v_1_sel).is_constant (&v_1_nelts)
6060 105 : || TREE_CODE (v_2_sel) != VECTOR_CST
6061 210 : || !VECTOR_CST_NELTS (v_2_sel).is_constant (&v_2_nelts))
6062 : return false;
6063 :
6064 105 : if (nelts != v_1_nelts || nelts != v_2_nelts)
6065 : return false;
6066 :
6067 : /* Create the new selector. */
6068 105 : vec_perm_builder new_sel_perm (nelts, nelts, 1);
6069 105 : auto_vec<bool> lanes (nelts);
6070 105 : lanes.quick_grow_cleared (nelts);
6071 630 : for (unsigned int i = 0; i < nelts; i++)
6072 : {
6073 : /* Extract the i-th value from the selector. */
6074 420 : unsigned int sel_cst = TREE_INT_CST_LOW (VECTOR_CST_ELT (sel, i));
6075 420 : unsigned int lane = sel_cst % nelts;
6076 420 : unsigned int offs = sel_cst / nelts;
6077 :
6078 : /* Check what's in the lane. */
6079 420 : unsigned int e_1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, lane));
6080 420 : unsigned int e_2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, lane));
6081 :
6082 : /* Reuse previous lane (if any). */
6083 420 : unsigned int l = 0;
6084 711 : for (; l < lane; l++)
6085 : {
6086 489 : if ((TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, l)) == e_1)
6087 489 : && (TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, l)) == e_2))
6088 : break;
6089 : }
6090 :
6091 : /* Add to narrowed selector. */
6092 420 : new_sel_perm.quick_push (l + offs * nelts);
6093 :
6094 : /* Mark lane as used. */
6095 420 : lanes[l] = true;
6096 : }
6097 :
6098 : /* Count how many lanes are need. */
6099 : unsigned int cnt = 0;
6100 525 : for (unsigned int i = 0; i < nelts; i++)
6101 420 : cnt += lanes[i];
6102 :
6103 : /* If more than (nelts/2) lanes are needed, skip the sequence. */
6104 105 : if (cnt > nelts / 2)
6105 : return false;
6106 :
6107 : /* Check if the resulting permutation is cheap. */
6108 105 : vec_perm_indices new_indices (new_sel_perm, 2, nelts);
6109 105 : tree vectype = TREE_TYPE (gimple_assign_lhs (stmt));
6110 105 : machine_mode vmode = TYPE_MODE (vectype);
6111 105 : if (!can_vec_perm_const_p (vmode, vmode, new_indices, false))
6112 : return false;
6113 :
6114 105 : *seq = XNEW (struct _vec_perm_simplify_seq);
6115 105 : (*seq)->stmt = stmt;
6116 105 : (*seq)->v_1_stmt = v_1_stmt;
6117 105 : (*seq)->v_2_stmt = v_2_stmt;
6118 105 : (*seq)->v_x_stmt = v_x_stmt;
6119 105 : (*seq)->v_y_stmt = v_y_stmt;
6120 105 : (*seq)->nelts = nelts;
6121 105 : (*seq)->new_sel = vect_gen_perm_mask_checked (vectype, new_indices);
6122 :
6123 105 : if (dump_file)
6124 : {
6125 32 : fprintf (dump_file, "Found vec perm simplify sequence ending with:\n\t");
6126 32 : print_gimple_stmt (dump_file, stmt, 0);
6127 :
6128 32 : if (dump_flags & TDF_DETAILS)
6129 : {
6130 32 : fprintf (dump_file, "\tNarrowed vec_perm selector: ");
6131 32 : print_generic_expr (dump_file, (*seq)->new_sel);
6132 32 : fprintf (dump_file, "\n");
6133 : }
6134 : }
6135 :
6136 : return true;
6137 210 : }
6138 :
6139 : /* Reduce the lane consumption of a simplifiable vec perm sequence. */
6140 :
6141 : static void
6142 78 : narrow_vec_perm_simplify_seq (const vec_perm_simplify_seq &seq)
6143 : {
6144 78 : gassign *stmt = seq->stmt;
6145 78 : if (dump_file && (dump_flags & TDF_DETAILS))
6146 : {
6147 26 : fprintf (dump_file, "Updating VEC_PERM statement:\n");
6148 26 : fprintf (dump_file, "Old stmt: ");
6149 26 : print_gimple_stmt (dump_file, stmt, 0);
6150 : }
6151 :
6152 : /* Update the last VEC_PERM statement. */
6153 78 : gimple_assign_set_rhs3 (stmt, seq->new_sel);
6154 78 : update_stmt (stmt);
6155 :
6156 78 : if (dump_file && (dump_flags & TDF_DETAILS))
6157 : {
6158 26 : fprintf (dump_file, "New stmt: ");
6159 26 : print_gimple_stmt (dump_file, stmt, 0);
6160 : }
6161 78 : }
6162 :
6163 : /* Test if we can blend two simplifiable vec permute sequences.
6164 : NEED_SWAP will be set, if sequences must be swapped for blending. */
6165 :
6166 : static bool
6167 49 : can_blend_vec_perm_simplify_seqs_p (vec_perm_simplify_seq seq1,
6168 : vec_perm_simplify_seq seq2,
6169 : bool *need_swap)
6170 : {
6171 49 : unsigned int nelts = seq1->nelts;
6172 49 : basic_block bb = gimple_bb (seq1->stmt);
6173 :
6174 49 : gcc_assert (gimple_bb (seq2->stmt) == bb);
6175 :
6176 : /* BBs and number of elements must be equal. */
6177 49 : if (gimple_bb (seq2->stmt) != bb || seq2->nelts != nelts)
6178 : return false;
6179 :
6180 : /* We need vectors of the same type. */
6181 49 : if (TREE_TYPE (gimple_assign_lhs (seq1->stmt))
6182 49 : != TREE_TYPE (gimple_assign_lhs (seq2->stmt)))
6183 : return false;
6184 :
6185 : /* We require isomorphic operators. */
6186 43 : if (((gimple_assign_rhs_code (seq1->v_x_stmt)
6187 43 : != gimple_assign_rhs_code (seq2->v_x_stmt))
6188 43 : || (gimple_assign_rhs_code (seq1->v_y_stmt)
6189 43 : != gimple_assign_rhs_code (seq2->v_y_stmt))))
6190 : return false;
6191 :
6192 : /* We cannot have any dependencies between the sequences.
6193 :
6194 : For merging, we will reuse seq1->v_1_stmt and seq1->v_2_stmt.
6195 : seq1's v_in is defined before these statements, but we need
6196 : to check if seq2's v_in is defined before them as well.
6197 :
6198 : Further, we will reuse seq2->stmt. We need to ensure that
6199 : seq1->v_x_stmt and seq1->v_y_stmt are before it.
6200 :
6201 : Note, that we don't need to check the BBs here, because all
6202 : statements of both sequences have to be in the same BB. */
6203 :
6204 43 : tree seq2_v_in = gimple_assign_rhs1 (seq2->v_1_stmt);
6205 43 : if (TREE_CODE (seq2_v_in) != SSA_NAME)
6206 : return false;
6207 :
6208 43 : gassign *seq2_v_in_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq2_v_in));
6209 43 : if (!seq2_v_in_stmt || gimple_bb (seq2_v_in_stmt) != bb
6210 43 : || (gimple_uid (seq2_v_in_stmt) > gimple_uid (seq1->v_1_stmt))
6211 39 : || (gimple_uid (seq1->v_x_stmt) > gimple_uid (seq2->stmt))
6212 39 : || (gimple_uid (seq1->v_y_stmt) > gimple_uid (seq2->stmt)))
6213 : {
6214 4 : tree seq1_v_in = gimple_assign_rhs1 (seq1->v_1_stmt);
6215 4 : if (TREE_CODE (seq1_v_in) != SSA_NAME)
6216 : return false;
6217 :
6218 4 : gassign *seq1_v_in_stmt
6219 4 : = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq1_v_in));
6220 : /* Let's try to see if we succeed when swapping the sequences. */
6221 4 : if (!seq1_v_in_stmt || gimple_bb (seq1_v_in_stmt)
6222 0 : || (gimple_uid (seq1_v_in_stmt) > gimple_uid (seq2->v_1_stmt))
6223 0 : || (gimple_uid (seq2->v_x_stmt) > gimple_uid (seq1->stmt))
6224 0 : || (gimple_uid (seq2->v_y_stmt) > gimple_uid (seq1->stmt)))
6225 : return false;
6226 0 : *need_swap = true;
6227 : }
6228 : else
6229 39 : *need_swap = false;
6230 :
6231 39 : if (dump_file && (dump_flags & TDF_DETAILS))
6232 13 : fprintf (dump_file, "Found vec perm simplify sequence pair.\n");
6233 :
6234 : return true;
6235 : }
6236 :
6237 : /* Calculate the permutations for blending the two given vec permute
6238 : sequences. This may fail if the resulting permutation is not
6239 : supported. */
6240 :
6241 : static bool
6242 39 : calc_perm_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
6243 : vec_perm_simplify_seq seq2,
6244 : vec_perm_indices *seq2_stmt_indices,
6245 : vec_perm_indices *seq1_v_1_stmt_indices,
6246 : vec_perm_indices *seq1_v_2_stmt_indices)
6247 : {
6248 39 : unsigned int i;
6249 39 : unsigned int nelts = seq1->nelts;
6250 39 : auto_vec<unsigned int> lane_assignment;
6251 39 : lane_assignment.create (nelts);
6252 :
6253 : /* Mark all lanes as free. */
6254 39 : lane_assignment.quick_grow_cleared (nelts);
6255 :
6256 : /* Allocate lanes for seq1. */
6257 234 : for (i = 0; i < nelts; i++)
6258 : {
6259 156 : unsigned int l = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq1->new_sel, i));
6260 156 : l %= nelts;
6261 156 : lane_assignment[l] = 1;
6262 : }
6263 :
6264 : /* Allocate lanes for seq2 and calculate selector for seq2->stmt. */
6265 39 : vec_perm_builder seq2_stmt_sel_perm (nelts, nelts, 1);
6266 195 : for (i = 0; i < nelts; i++)
6267 : {
6268 156 : unsigned int sel = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, i));
6269 156 : unsigned int lane = sel % nelts;
6270 156 : unsigned int offs = sel / nelts;
6271 156 : unsigned int new_sel;
6272 :
6273 : /* Check if we already allocated the lane for seq2. */
6274 156 : unsigned int j = 0;
6275 279 : for (; j < i; j++)
6276 : {
6277 201 : unsigned int sel_old;
6278 201 : sel_old = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, j));
6279 201 : unsigned int lane_old = sel_old % nelts;
6280 201 : if (lane == lane_old)
6281 : {
6282 78 : new_sel = seq2_stmt_sel_perm[j].to_constant ();
6283 78 : new_sel = (new_sel % nelts) + offs * nelts;
6284 78 : break;
6285 : }
6286 : }
6287 :
6288 : /* If the lane is not allocated, we need to do that now. */
6289 156 : if (j == i)
6290 : {
6291 : unsigned int l_orig = lane;
6292 194 : while (lane_assignment[lane] != 0)
6293 : {
6294 116 : lane = (lane + 1) % nelts;
6295 :
6296 : /* This should not happen if both sequences utilize no more than
6297 : half of the lanes. Test anyway to guarantee termination. */
6298 116 : if (lane == l_orig)
6299 39 : return false;
6300 : }
6301 :
6302 : /* Allocate lane. */
6303 78 : lane_assignment[lane] = 2 + l_orig;
6304 78 : new_sel = lane + offs * nelts;
6305 : }
6306 :
6307 156 : seq2_stmt_sel_perm.quick_push (new_sel);
6308 : }
6309 :
6310 : /* Check if the resulting permutation is cheap. */
6311 39 : seq2_stmt_indices->new_vector (seq2_stmt_sel_perm, 2, nelts);
6312 39 : tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
6313 39 : machine_mode vmode = TYPE_MODE (vectype);
6314 39 : if (!can_vec_perm_const_p (vmode, vmode, *seq2_stmt_indices, false))
6315 : return false;
6316 :
6317 : /* Calculate selectors for seq1->v_1_stmt and seq1->v_2_stmt. */
6318 39 : vec_perm_builder seq1_v_1_stmt_sel_perm (nelts, nelts, 1);
6319 39 : vec_perm_builder seq1_v_2_stmt_sel_perm (nelts, nelts, 1);
6320 195 : for (i = 0; i < nelts; i++)
6321 : {
6322 156 : bool use_seq1 = lane_assignment[i] < 2;
6323 156 : unsigned int l1, l2;
6324 :
6325 156 : if (use_seq1)
6326 : {
6327 : /* Just reuse the selector indices. */
6328 78 : tree s1 = gimple_assign_rhs3 (seq1->v_1_stmt);
6329 78 : tree s2 = gimple_assign_rhs3 (seq1->v_2_stmt);
6330 78 : l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, i));
6331 78 : l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, i));
6332 : }
6333 : else
6334 : {
6335 : /* We moved the lanes for seq2, so we need to adjust for that. */
6336 78 : tree s1 = gimple_assign_rhs3 (seq2->v_1_stmt);
6337 78 : tree s2 = gimple_assign_rhs3 (seq2->v_2_stmt);
6338 78 : l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, lane_assignment[i] - 2));
6339 78 : l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, lane_assignment[i] - 2));
6340 : }
6341 :
6342 156 : l1 %= nelts;
6343 156 : l2 %= nelts;
6344 234 : seq1_v_1_stmt_sel_perm.quick_push (l1 + (use_seq1 ? 0 : nelts));
6345 156 : seq1_v_2_stmt_sel_perm.quick_push (l2 + (use_seq1 ? 0 : nelts));
6346 : }
6347 :
6348 39 : seq1_v_1_stmt_indices->new_vector (seq1_v_1_stmt_sel_perm, 2, nelts);
6349 39 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
6350 39 : vmode = TYPE_MODE (vectype);
6351 39 : if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_1_stmt_indices, false))
6352 : return false;
6353 :
6354 39 : seq1_v_2_stmt_indices->new_vector (seq1_v_2_stmt_sel_perm, 2, nelts);
6355 39 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
6356 39 : vmode = TYPE_MODE (vectype);
6357 39 : if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_2_stmt_indices, false))
6358 : return false;
6359 :
6360 : return true;
6361 78 : }
6362 :
6363 : /* Blend the two given simplifiable vec permute sequences using the
6364 : given permutations. */
6365 :
6366 : static void
6367 39 : blend_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
6368 : vec_perm_simplify_seq seq2,
6369 : const vec_perm_indices &seq2_stmt_indices,
6370 : const vec_perm_indices &seq1_v_1_stmt_indices,
6371 : const vec_perm_indices &seq1_v_2_stmt_indices)
6372 : {
6373 : /* We don't need to adjust seq1->stmt because its lanes consumption
6374 : was already narrowed before entering this function. */
6375 :
6376 : /* Adjust seq2->stmt: copy RHS1/RHS2 from seq1->stmt and set new sel. */
6377 39 : if (dump_file && (dump_flags & TDF_DETAILS))
6378 : {
6379 13 : fprintf (dump_file, "Updating VEC_PERM statement:\n");
6380 13 : fprintf (dump_file, "Old stmt: ");
6381 13 : print_gimple_stmt (dump_file, seq2->stmt, 0);
6382 : }
6383 :
6384 39 : gimple_assign_set_rhs1 (seq2->stmt, gimple_assign_rhs1 (seq1->stmt));
6385 78 : gimple_assign_set_rhs2 (seq2->stmt, gimple_assign_rhs2 (seq1->stmt));
6386 39 : tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
6387 39 : tree sel = vect_gen_perm_mask_checked (vectype, seq2_stmt_indices);
6388 39 : gimple_assign_set_rhs3 (seq2->stmt, sel);
6389 39 : update_stmt (seq2->stmt);
6390 :
6391 39 : if (dump_file && (dump_flags & TDF_DETAILS))
6392 : {
6393 13 : fprintf (dump_file, "New stmt: ");
6394 13 : print_gimple_stmt (dump_file, seq2->stmt, 0);
6395 : }
6396 :
6397 : /* Adjust seq1->v_1_stmt: copy RHS2 from seq2->v_1_stmt and set new sel. */
6398 39 : if (dump_file && (dump_flags & TDF_DETAILS))
6399 : {
6400 13 : fprintf (dump_file, "Updating VEC_PERM statement:\n");
6401 13 : fprintf (dump_file, "Old stmt: ");
6402 13 : print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
6403 : }
6404 :
6405 39 : gimple_assign_set_rhs2 (seq1->v_1_stmt, gimple_assign_rhs1 (seq2->v_1_stmt));
6406 39 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
6407 39 : sel = vect_gen_perm_mask_checked (vectype, seq1_v_1_stmt_indices);
6408 39 : gimple_assign_set_rhs3 (seq1->v_1_stmt, sel);
6409 39 : update_stmt (seq1->v_1_stmt);
6410 :
6411 39 : if (dump_file && (dump_flags & TDF_DETAILS))
6412 : {
6413 13 : fprintf (dump_file, "New stmt: ");
6414 13 : print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
6415 : }
6416 :
6417 : /* Adjust seq1->v_2_stmt: copy RHS2 from seq2->v_2_stmt and set new sel. */
6418 39 : if (dump_file && (dump_flags & TDF_DETAILS))
6419 : {
6420 13 : fprintf (dump_file, "Updating VEC_PERM statement:\n");
6421 13 : fprintf (dump_file, "Old stmt: ");
6422 13 : print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
6423 : }
6424 :
6425 39 : gimple_assign_set_rhs2 (seq1->v_2_stmt, gimple_assign_rhs1 (seq2->v_2_stmt));
6426 39 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
6427 39 : sel = vect_gen_perm_mask_checked (vectype, seq1_v_2_stmt_indices);
6428 39 : gimple_assign_set_rhs3 (seq1->v_2_stmt, sel);
6429 39 : update_stmt (seq1->v_2_stmt);
6430 :
6431 39 : if (dump_file && (dump_flags & TDF_DETAILS))
6432 : {
6433 13 : fprintf (dump_file, "New stmt: ");
6434 13 : print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
6435 : }
6436 :
6437 : /* At this point, we have four unmodified seq2 stmts, which will be
6438 : eliminated by DCE. */
6439 :
6440 39 : if (dump_file)
6441 13 : fprintf (dump_file, "Vec perm simplify sequences have been blended.\n\n");
6442 39 : }
6443 :
6444 : /* Try to blend narrowed vec_perm_simplify_seqs pairwise.
6445 : The provided list will be empty after this call. */
6446 :
6447 : static void
6448 140509302 : process_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l)
6449 : {
6450 140509302 : unsigned int i, j;
6451 140509302 : vec_perm_simplify_seq seq1, seq2;
6452 :
6453 140509302 : if (l->is_empty ())
6454 140509255 : return;
6455 :
6456 47 : if (dump_file && (dump_flags & TDF_DETAILS))
6457 15 : fprintf (dump_file, "\nProcessing %u vec perm simplify sequences.\n",
6458 : l->length ());
6459 :
6460 160 : FOR_EACH_VEC_ELT (*l, i, seq1)
6461 : {
6462 66 : if (i + 1 < l->length ())
6463 : {
6464 119 : FOR_EACH_VEC_ELT_FROM (*l, j, seq2, i + 1)
6465 : {
6466 49 : bool swap = false;
6467 49 : if (can_blend_vec_perm_simplify_seqs_p (seq1, seq2, &swap))
6468 : {
6469 39 : vec_perm_indices seq2_stmt_indices;
6470 39 : vec_perm_indices seq1_v_1_stmt_indices;
6471 39 : vec_perm_indices seq1_v_2_stmt_indices;
6472 117 : if (calc_perm_vec_perm_simplify_seqs (swap ? seq2 : seq1,
6473 : swap ? seq1 : seq2,
6474 : &seq2_stmt_indices,
6475 : &seq1_v_1_stmt_indices,
6476 : &seq1_v_2_stmt_indices))
6477 : {
6478 : /* Narrow lane usage. */
6479 39 : narrow_vec_perm_simplify_seq (seq1);
6480 39 : narrow_vec_perm_simplify_seq (seq2);
6481 :
6482 : /* Blend sequences. */
6483 39 : blend_vec_perm_simplify_seqs (swap ? seq2 : seq1,
6484 : swap ? seq1 : seq2,
6485 : seq2_stmt_indices,
6486 : seq1_v_1_stmt_indices,
6487 : seq1_v_2_stmt_indices);
6488 :
6489 : /* We can use unordered_remove as we break the loop. */
6490 39 : l->unordered_remove (j);
6491 39 : XDELETE (seq2);
6492 39 : break;
6493 : }
6494 39 : }
6495 : }
6496 : }
6497 :
6498 : /* We don't need to call l->remove for seq1. */
6499 66 : XDELETE (seq1);
6500 : }
6501 :
6502 47 : l->truncate (0);
6503 : }
6504 :
6505 : static void
6506 105 : append_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l,
6507 : const vec_perm_simplify_seq &seq)
6508 : {
6509 : /* If no space on list left, then process the list. */
6510 105 : if (!l->space (1))
6511 0 : process_vec_perm_simplify_seq_list (l);
6512 :
6513 105 : l->quick_push (seq);
6514 105 : }
6515 :
6516 : /* Main entry point for the forward propagation and statement combine
6517 : optimizer. */
6518 :
6519 : namespace {
6520 :
6521 : const pass_data pass_data_forwprop =
6522 : {
6523 : GIMPLE_PASS, /* type */
6524 : "forwprop", /* name */
6525 : OPTGROUP_NONE, /* optinfo_flags */
6526 : TV_TREE_FORWPROP, /* tv_id */
6527 : ( PROP_cfg | PROP_ssa ), /* properties_required */
6528 : 0, /* properties_provided */
6529 : 0, /* properties_destroyed */
6530 : 0, /* todo_flags_start */
6531 : 0, /* todo_flags_finish */
6532 : };
6533 :
6534 : class pass_forwprop : public gimple_opt_pass
6535 : {
6536 : public:
6537 1472680 : pass_forwprop (gcc::context *ctxt)
6538 2945360 : : gimple_opt_pass (pass_data_forwprop, ctxt), last_p (false)
6539 : {}
6540 :
6541 : /* opt_pass methods: */
6542 1178144 : opt_pass * clone () final override { return new pass_forwprop (m_ctxt); }
6543 1767216 : void set_pass_param (unsigned int n, bool param) final override
6544 : {
6545 1767216 : switch (n)
6546 : {
6547 1178144 : case 0:
6548 1178144 : m_full_walk = param;
6549 1178144 : break;
6550 589072 : case 1:
6551 589072 : last_p = param;
6552 589072 : break;
6553 0 : default:
6554 0 : gcc_unreachable();
6555 : }
6556 1767216 : }
6557 5776388 : bool gate (function *) final override { return flag_tree_forwprop; }
6558 : unsigned int execute (function *) final override;
6559 :
6560 : private:
6561 : /* Determines whether the pass instance should set PROP_last_full_fold. */
6562 : bool last_p;
6563 :
6564 : /* True if the aggregate props are doing a full walk or not. */
6565 : bool m_full_walk = false;
6566 : }; // class pass_forwprop
6567 :
6568 : /* Attempt to make the BB block of __builtin_unreachable unreachable by changing
6569 : the incoming jumps. Return true if at least one jump was changed. */
6570 :
6571 : static bool
6572 1225 : optimize_unreachable (basic_block bb)
6573 : {
6574 1225 : gimple_stmt_iterator gsi;
6575 1225 : gimple *stmt;
6576 1225 : edge_iterator ei;
6577 1225 : edge e;
6578 1225 : bool ret;
6579 :
6580 1225 : ret = false;
6581 2524 : FOR_EACH_EDGE (e, ei, bb->preds)
6582 : {
6583 1299 : gsi = gsi_last_bb (e->src);
6584 1299 : if (gsi_end_p (gsi))
6585 327 : continue;
6586 :
6587 972 : stmt = gsi_stmt (gsi);
6588 972 : if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
6589 : {
6590 : /* If the condition is already true/false
6591 : ignore it. This can happen during copy prop of forwprop. */
6592 708 : if (gimple_cond_true_p (cond_stmt)
6593 700 : || gimple_cond_false_p (cond_stmt))
6594 8 : continue;
6595 692 : else if (e->flags & EDGE_TRUE_VALUE)
6596 600 : gimple_cond_make_false (cond_stmt);
6597 92 : else if (e->flags & EDGE_FALSE_VALUE)
6598 92 : gimple_cond_make_true (cond_stmt);
6599 : else
6600 0 : gcc_unreachable ();
6601 692 : update_stmt (cond_stmt);
6602 : }
6603 : else
6604 : {
6605 : /* Todo: handle other cases. e.g. switch. */
6606 272 : continue;
6607 : }
6608 :
6609 692 : ret = true;
6610 : }
6611 :
6612 1225 : return ret;
6613 : }
6614 :
6615 : unsigned int
6616 5773622 : pass_forwprop::execute (function *fun)
6617 : {
6618 5773622 : unsigned int todoflags = 0;
6619 : /* Handle a full walk only when expensive optimizations are on. */
6620 5773622 : bool full_walk = m_full_walk && flag_expensive_optimizations;
6621 :
6622 5773622 : cfg_changed = false;
6623 5773622 : if (last_p)
6624 1070111 : fun->curr_properties |= PROP_last_full_fold;
6625 :
6626 5773622 : calculate_dominance_info (CDI_DOMINATORS);
6627 :
6628 : /* Combine stmts with the stmts defining their operands. Do that
6629 : in an order that guarantees visiting SSA defs before SSA uses. */
6630 11547244 : lattice.create (num_ssa_names);
6631 11547244 : lattice.quick_grow_cleared (num_ssa_names);
6632 5773622 : int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (fun));
6633 5773622 : int postorder_num = pre_and_rev_post_order_compute_fn (fun, NULL,
6634 : postorder, false);
6635 5773622 : int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
6636 58007987 : for (int i = 0; i < postorder_num; ++i)
6637 : {
6638 46460743 : bb_to_rpo[postorder[i]] = i;
6639 46460743 : edge_iterator ei;
6640 46460743 : edge e;
6641 111853043 : FOR_EACH_EDGE (e, ei, BASIC_BLOCK_FOR_FN (fun, postorder[i])->succs)
6642 65392300 : e->flags &= ~EDGE_EXECUTABLE;
6643 : }
6644 5773622 : single_succ_edge (BASIC_BLOCK_FOR_FN (fun, ENTRY_BLOCK))->flags
6645 5773622 : |= EDGE_EXECUTABLE;
6646 5773622 : auto_vec<gimple *, 4> to_fixup;
6647 5773622 : auto_vec<gimple *, 32> to_remove;
6648 5773622 : auto_vec<unsigned, 32> to_remove_defs;
6649 5773622 : auto_vec<std::pair<int, int>, 10> edges_to_remove;
6650 5773622 : auto_bitmap simple_dce_worklist;
6651 5773622 : auto_bitmap need_ab_cleanup;
6652 5773622 : to_purge = BITMAP_ALLOC (NULL);
6653 5773622 : auto_vec<vec_perm_simplify_seq, 8> vec_perm_simplify_seq_list;
6654 52234365 : for (int i = 0; i < postorder_num; ++i)
6655 : {
6656 46460743 : gimple_stmt_iterator gsi;
6657 46460743 : basic_block bb = BASIC_BLOCK_FOR_FN (fun, postorder[i]);
6658 46460743 : edge_iterator ei;
6659 46460743 : edge e;
6660 :
6661 : /* Skip processing not executable blocks. We could improve
6662 : single_use tracking by at least unlinking uses from unreachable
6663 : blocks but since blocks with uses are not processed in a
6664 : meaningful order this is probably not worth it. */
6665 46460743 : bool any = false;
6666 47656852 : FOR_EACH_EDGE (e, ei, bb->preds)
6667 : {
6668 47628594 : if ((e->flags & EDGE_EXECUTABLE)
6669 : /* We can handle backedges in natural loops correctly but
6670 : for irreducible regions we have to take all backedges
6671 : conservatively when we did not visit the source yet. */
6672 47628594 : || (bb_to_rpo[e->src->index] > i
6673 686305 : && !dominated_by_p (CDI_DOMINATORS, e->src, e->dest)))
6674 : {
6675 : any = true;
6676 : break;
6677 : }
6678 : }
6679 46460743 : if (!any)
6680 28912 : continue;
6681 :
6682 : /* Remove conditions that go directly to unreachable when this is the last forwprop. */
6683 46432485 : if (last_p
6684 10165308 : && !(flag_sanitize & SANITIZE_UNREACHABLE))
6685 : {
6686 10160357 : gimple_stmt_iterator gsi;
6687 10160357 : gsi = gsi_start_nondebug_after_labels_bb (bb);
6688 10161011 : if (!gsi_end_p (gsi)
6689 9261991 : && gimple_call_builtin_p (*gsi, BUILT_IN_UNREACHABLE)
6690 10161582 : && optimize_unreachable (bb))
6691 : {
6692 654 : cfg_changed = true;
6693 654 : continue;
6694 : }
6695 : }
6696 :
6697 : /* Fold PHI-form long-multiply carries and record degenerate
6698 : PHIs in the lattice. Iterator advanced up front so a folded
6699 : PHI can be removed in-flight; a long-mul carry PHI is never
6700 : degenerate, so the two cases are disjoint. */
6701 62791855 : for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);)
6702 : {
6703 16360024 : gphi *phi = si.phi ();
6704 16360024 : gsi_next (&si);
6705 16360024 : tree res = gimple_phi_result (phi);
6706 32720048 : if (virtual_operand_p (res))
6707 7516264 : continue;
6708 8843774 : if (match_long_mul_phi (phi))
6709 14 : continue;
6710 :
6711 8843760 : tree first = NULL_TREE;
6712 8843760 : bool all_same = true;
6713 8843760 : edge_iterator ei;
6714 8843760 : edge e;
6715 18218372 : FOR_EACH_EDGE (e, ei, bb->preds)
6716 : {
6717 : /* Ignore not executable forward edges. */
6718 17979864 : if (!(e->flags & EDGE_EXECUTABLE))
6719 : {
6720 4163861 : if (bb_to_rpo[e->src->index] < i)
6721 18411 : continue;
6722 : /* Avoid equivalences from backedges - while we might
6723 : be able to make irreducible regions reducible and
6724 : thus turning a back into a forward edge we do not
6725 : want to deal with the intermediate SSA issues that
6726 : exposes. */
6727 : all_same = false;
6728 : }
6729 17961453 : tree use = PHI_ARG_DEF_FROM_EDGE (phi, e);
6730 17961453 : if (use == res)
6731 : /* The PHI result can also appear on a backedge, if so
6732 : we can ignore this case for the purpose of determining
6733 : the singular value. */
6734 : ;
6735 17948306 : else if (! first)
6736 : first = use;
6737 9104546 : else if (! operand_equal_p (first, use, 0))
6738 : {
6739 : all_same = false;
6740 : break;
6741 : }
6742 : }
6743 8843760 : if (all_same)
6744 : {
6745 233830 : if (may_propagate_copy (res, first))
6746 233170 : to_remove_defs.safe_push (SSA_NAME_VERSION (res));
6747 233830 : fwprop_set_lattice_val (res, first);
6748 : }
6749 : }
6750 :
6751 : /* Apply forward propagation to all stmts in the basic-block.
6752 : Note we update GSI within the loop as necessary. */
6753 46431831 : unsigned int uid = 1;
6754 458929930 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
6755 : {
6756 366066268 : gimple *stmt = gsi_stmt (gsi);
6757 366066268 : tree lhs, rhs;
6758 366066268 : enum tree_code code;
6759 :
6760 366066268 : if (is_gimple_debug (stmt))
6761 : {
6762 203133933 : gsi_next (&gsi);
6763 203133933 : continue;
6764 : }
6765 :
6766 162932335 : gimple_set_uid (stmt, uid++);
6767 :
6768 162932335 : if (!is_gimple_assign (stmt))
6769 : {
6770 54094211 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
6771 54094211 : gsi_next (&gsi);
6772 54094211 : continue;
6773 : }
6774 :
6775 108838124 : lhs = gimple_assign_lhs (stmt);
6776 108838124 : rhs = gimple_assign_rhs1 (stmt);
6777 108838124 : code = gimple_assign_rhs_code (stmt);
6778 :
6779 148821384 : if (TREE_CODE (lhs) != SSA_NAME
6780 108838124 : || has_zero_uses (lhs))
6781 : {
6782 39983260 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
6783 39983260 : gsi_next (&gsi);
6784 39983260 : continue;
6785 : }
6786 :
6787 : /* If this statement sets an SSA_NAME to an address,
6788 : try to propagate the address into the uses of the SSA_NAME. */
6789 68854864 : if ((code == ADDR_EXPR
6790 : /* Handle pointer conversions on invariant addresses
6791 : as well, as this is valid gimple. */
6792 66465317 : || (CONVERT_EXPR_CODE_P (code)
6793 9163542 : && TREE_CODE (rhs) == ADDR_EXPR
6794 363770 : && POINTER_TYPE_P (TREE_TYPE (lhs))))
6795 68855088 : && TREE_CODE (TREE_OPERAND (rhs, 0)) != TARGET_MEM_REF)
6796 : {
6797 2389174 : tree base = get_base_address (TREE_OPERAND (rhs, 0));
6798 2389174 : if ((!base
6799 2389174 : || !DECL_P (base)
6800 135760 : || decl_address_invariant_p (base))
6801 2389174 : && !stmt_references_abnormal_ssa_name (stmt)
6802 4778332 : && forward_propagate_addr_expr (lhs, rhs, true))
6803 : {
6804 488218 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
6805 488218 : release_defs (stmt);
6806 488218 : gsi_remove (&gsi, true);
6807 : }
6808 : else
6809 1900956 : gsi_next (&gsi);
6810 : }
6811 66465690 : else if (code == POINTER_PLUS_EXPR)
6812 : {
6813 3726543 : tree off = gimple_assign_rhs2 (stmt);
6814 3726543 : if (TREE_CODE (off) == INTEGER_CST
6815 1149939 : && can_propagate_from (stmt)
6816 1149588 : && !simple_iv_increment_p (stmt)
6817 : /* ??? Better adjust the interface to that function
6818 : instead of building new trees here. */
6819 4574904 : && forward_propagate_addr_expr
6820 2545083 : (lhs,
6821 : build1_loc (gimple_location (stmt),
6822 848361 : ADDR_EXPR, TREE_TYPE (rhs),
6823 848361 : fold_build2 (MEM_REF,
6824 : TREE_TYPE (TREE_TYPE (rhs)),
6825 : rhs,
6826 : fold_convert (ptr_type_node,
6827 : off))), true))
6828 : {
6829 320469 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
6830 320469 : release_defs (stmt);
6831 320469 : gsi_remove (&gsi, true);
6832 : }
6833 3406074 : else if (is_gimple_min_invariant (rhs))
6834 : {
6835 : /* Make sure to fold &a[0] + off_1 here. */
6836 420657 : fold_stmt_inplace (&gsi);
6837 420657 : update_stmt (stmt);
6838 420657 : if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
6839 420633 : gsi_next (&gsi);
6840 : }
6841 : else
6842 2985417 : gsi_next (&gsi);
6843 : }
6844 62739147 : else if (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE
6845 214380 : && gimple_assign_load_p (stmt)
6846 135142 : && !gimple_has_volatile_ops (stmt)
6847 41067 : && TREE_CODE (rhs) != TARGET_MEM_REF
6848 41037 : && TREE_CODE (rhs) != BIT_FIELD_REF
6849 62780180 : && !stmt_can_throw_internal (fun, stmt))
6850 : {
6851 : /* Rewrite loads used only in real/imagpart extractions to
6852 : component-wise loads. */
6853 40915 : use_operand_p use_p;
6854 40915 : imm_use_iterator iter;
6855 40915 : tree vuse = gimple_vuse (stmt);
6856 40915 : bool rewrite = true;
6857 46101 : FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
6858 : {
6859 43972 : gimple *use_stmt = USE_STMT (use_p);
6860 43972 : if (is_gimple_debug (use_stmt))
6861 1011 : continue;
6862 42961 : if (!is_gimple_assign (use_stmt)
6863 28251 : || (gimple_assign_rhs_code (use_stmt) != REALPART_EXPR
6864 26147 : && gimple_assign_rhs_code (use_stmt) != IMAGPART_EXPR)
6865 47136 : || TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) != lhs)
6866 : {
6867 : rewrite = false;
6868 : break;
6869 : }
6870 40915 : }
6871 40915 : if (rewrite)
6872 : {
6873 2129 : gimple *use_stmt;
6874 6769 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6875 : {
6876 4640 : if (is_gimple_debug (use_stmt))
6877 : {
6878 498 : if (gimple_debug_bind_p (use_stmt))
6879 : {
6880 498 : gimple_debug_bind_reset_value (use_stmt);
6881 498 : update_stmt (use_stmt);
6882 : }
6883 498 : continue;
6884 : }
6885 :
6886 8284 : tree new_rhs = build1 (gimple_assign_rhs_code (use_stmt),
6887 4142 : TREE_TYPE (TREE_TYPE (rhs)),
6888 : unshare_expr (rhs));
6889 4142 : gimple *new_stmt
6890 4142 : = gimple_build_assign (gimple_assign_lhs (use_stmt),
6891 : new_rhs);
6892 :
6893 4142 : location_t loc = gimple_location (use_stmt);
6894 4142 : gimple_set_location (new_stmt, loc);
6895 4142 : gimple_set_vuse (new_stmt, vuse);
6896 4142 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
6897 4142 : unlink_stmt_vdef (use_stmt);
6898 4142 : gsi_remove (&gsi2, true);
6899 :
6900 4142 : gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
6901 2129 : }
6902 :
6903 2129 : release_defs (stmt);
6904 2129 : gsi_remove (&gsi, true);
6905 : }
6906 : else
6907 38786 : gsi_next (&gsi);
6908 : }
6909 62698232 : else if (TREE_CODE (TREE_TYPE (lhs)) == VECTOR_TYPE
6910 1806068 : && (TYPE_MODE (TREE_TYPE (lhs)) == BLKmode
6911 : /* After vector lowering rewrite all loads, but
6912 : initially do not since this conflicts with
6913 : vector CONSTRUCTOR to shuffle optimization. */
6914 1780980 : || (fun->curr_properties & PROP_gimple_lvec))
6915 919830 : && gimple_assign_load_p (stmt)
6916 293991 : && !gimple_has_volatile_ops (stmt)
6917 279859 : && !stmt_can_throw_internal (fun, stmt)
6918 62978091 : && (!VAR_P (rhs) || !DECL_HARD_REGISTER (rhs)))
6919 279356 : optimize_vector_load (&gsi);
6920 :
6921 62418876 : else if (code == COMPLEX_EXPR)
6922 : {
6923 : /* Rewrite stores of a single-use complex build expression
6924 : to component-wise stores. */
6925 37928 : use_operand_p use_p;
6926 37928 : gimple *use_stmt, *def1, *def2;
6927 37928 : tree rhs2;
6928 37928 : if (single_imm_use (lhs, &use_p, &use_stmt)
6929 35745 : && gimple_store_p (use_stmt)
6930 42098 : && !gimple_has_volatile_ops (use_stmt)
6931 3133 : && is_gimple_assign (use_stmt)
6932 3129 : && (TREE_CODE (TREE_TYPE (gimple_assign_lhs (use_stmt)))
6933 : == COMPLEX_TYPE)
6934 41052 : && (TREE_CODE (gimple_assign_lhs (use_stmt))
6935 : != TARGET_MEM_REF))
6936 : {
6937 3120 : tree use_lhs = gimple_assign_lhs (use_stmt);
6938 3120 : if (auto_var_p (use_lhs))
6939 601 : DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
6940 6240 : tree new_lhs = build1 (REALPART_EXPR,
6941 3120 : TREE_TYPE (TREE_TYPE (use_lhs)),
6942 : unshare_expr (use_lhs));
6943 3120 : gimple *new_stmt = gimple_build_assign (new_lhs, rhs);
6944 3120 : location_t loc = gimple_location (use_stmt);
6945 3120 : gimple_set_location (new_stmt, loc);
6946 6240 : gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
6947 3120 : gimple_set_vdef (new_stmt, make_ssa_name (gimple_vop (fun)));
6948 6240 : SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
6949 6240 : gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
6950 3120 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
6951 3120 : gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
6952 :
6953 6240 : new_lhs = build1 (IMAGPART_EXPR,
6954 3120 : TREE_TYPE (TREE_TYPE (use_lhs)),
6955 : unshare_expr (use_lhs));
6956 3120 : gimple_assign_set_lhs (use_stmt, new_lhs);
6957 3120 : gimple_assign_set_rhs1 (use_stmt, gimple_assign_rhs2 (stmt));
6958 3120 : update_stmt (use_stmt);
6959 :
6960 3120 : release_defs (stmt);
6961 3120 : gsi_remove (&gsi, true);
6962 : }
6963 : /* Rewrite a component-wise load of a complex to a complex
6964 : load if the components are not used separately. */
6965 34808 : else if (TREE_CODE (rhs) == SSA_NAME
6966 34368 : && has_single_use (rhs)
6967 30818 : && ((rhs2 = gimple_assign_rhs2 (stmt)), true)
6968 30818 : && TREE_CODE (rhs2) == SSA_NAME
6969 28997 : && has_single_use (rhs2)
6970 28577 : && (def1 = SSA_NAME_DEF_STMT (rhs),
6971 28577 : gimple_assign_load_p (def1))
6972 1093 : && (def2 = SSA_NAME_DEF_STMT (rhs2),
6973 1093 : gimple_assign_load_p (def2))
6974 1598 : && (gimple_vuse (def1) == gimple_vuse (def2))
6975 796 : && !gimple_has_volatile_ops (def1)
6976 796 : && !gimple_has_volatile_ops (def2)
6977 796 : && !stmt_can_throw_internal (fun, def1)
6978 796 : && !stmt_can_throw_internal (fun, def2)
6979 796 : && gimple_assign_rhs_code (def1) == REALPART_EXPR
6980 550 : && gimple_assign_rhs_code (def2) == IMAGPART_EXPR
6981 35358 : && operand_equal_p (TREE_OPERAND (gimple_assign_rhs1
6982 : (def1), 0),
6983 550 : TREE_OPERAND (gimple_assign_rhs1
6984 : (def2), 0)))
6985 : {
6986 550 : tree cl = TREE_OPERAND (gimple_assign_rhs1 (def1), 0);
6987 550 : gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (cl));
6988 550 : gcc_assert (gsi_stmt (gsi) == stmt);
6989 1100 : gimple_set_vuse (stmt, gimple_vuse (def1));
6990 550 : gimple_set_modified (stmt, true);
6991 550 : gimple_stmt_iterator gsi2 = gsi_for_stmt (def1);
6992 550 : gsi_remove (&gsi, false);
6993 550 : gsi_insert_after (&gsi2, stmt, GSI_SAME_STMT);
6994 : }
6995 : else
6996 34258 : gsi_next (&gsi);
6997 : }
6998 62380948 : else if (code == CONSTRUCTOR
6999 170079 : && VECTOR_TYPE_P (TREE_TYPE (rhs))
7000 170079 : && TYPE_MODE (TREE_TYPE (rhs)) == BLKmode
7001 4589 : && CONSTRUCTOR_NELTS (rhs) > 0
7002 62385537 : && (!VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
7003 2096 : || (TYPE_MODE (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
7004 : != BLKmode)))
7005 : {
7006 : /* Rewrite stores of a single-use vector constructors
7007 : to component-wise stores if the mode isn't supported. */
7008 4232 : use_operand_p use_p;
7009 4232 : gimple *use_stmt;
7010 4232 : if (single_imm_use (lhs, &use_p, &use_stmt)
7011 3768 : && gimple_store_p (use_stmt)
7012 3108 : && !gimple_has_volatile_ops (use_stmt)
7013 1548 : && !stmt_can_throw_internal (fun, use_stmt)
7014 5773 : && is_gimple_assign (use_stmt))
7015 : {
7016 1541 : tree elt_t = TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value);
7017 1541 : unsigned HOST_WIDE_INT elt_w
7018 1541 : = tree_to_uhwi (TYPE_SIZE (elt_t));
7019 1541 : unsigned HOST_WIDE_INT n
7020 1541 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs)));
7021 1541 : tree use_lhs = gimple_assign_lhs (use_stmt);
7022 1541 : if (auto_var_p (use_lhs))
7023 575 : DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
7024 966 : else if (TREE_CODE (use_lhs) == TARGET_MEM_REF)
7025 : {
7026 1 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
7027 1 : use_lhs = prepare_target_mem_ref_lvalue (use_lhs, &gsi2);
7028 : }
7029 33716 : for (unsigned HOST_WIDE_INT bi = 0; bi < n; bi += elt_w)
7030 : {
7031 32175 : unsigned HOST_WIDE_INT ci = bi / elt_w;
7032 32175 : tree new_rhs;
7033 32175 : if (ci < CONSTRUCTOR_NELTS (rhs))
7034 31557 : new_rhs = CONSTRUCTOR_ELT (rhs, ci)->value;
7035 : else
7036 618 : new_rhs = build_zero_cst (elt_t);
7037 32175 : tree new_lhs = build3 (BIT_FIELD_REF,
7038 : elt_t,
7039 : unshare_expr (use_lhs),
7040 32175 : bitsize_int (elt_w),
7041 32175 : bitsize_int (bi));
7042 32175 : gimple *new_stmt = gimple_build_assign (new_lhs, new_rhs);
7043 32175 : location_t loc = gimple_location (use_stmt);
7044 32175 : gimple_set_location (new_stmt, loc);
7045 64350 : gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
7046 32175 : gimple_set_vdef (new_stmt,
7047 : make_ssa_name (gimple_vop (fun)));
7048 64350 : SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
7049 64350 : gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
7050 32175 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
7051 32175 : gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
7052 : }
7053 1541 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
7054 1541 : unlink_stmt_vdef (use_stmt);
7055 1541 : release_defs (use_stmt);
7056 1541 : gsi_remove (&gsi2, true);
7057 1541 : release_defs (stmt);
7058 1541 : gsi_remove (&gsi, true);
7059 : }
7060 : else
7061 2691 : gsi_next (&gsi);
7062 : }
7063 62376716 : else if (code == VEC_PERM_EXPR)
7064 : {
7065 : /* Find vectorized sequences, where we can reduce the lane
7066 : utilization. The narrowing will be donw later and only
7067 : if we find a pair of sequences that can be blended. */
7068 189051 : gassign *assign = dyn_cast <gassign *> (stmt);
7069 189051 : vec_perm_simplify_seq seq;
7070 189051 : if (recognise_vec_perm_simplify_seq (assign, &seq))
7071 105 : append_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list,
7072 : seq);
7073 :
7074 189051 : gsi_next (&gsi);
7075 : }
7076 : else
7077 62187665 : gsi_next (&gsi);
7078 : }
7079 :
7080 46431831 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
7081 :
7082 : /* Combine stmts with the stmts defining their operands.
7083 : Note we update GSI within the loop as necessary. */
7084 458593719 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7085 : {
7086 365730057 : gimple *stmt = gsi_stmt (gsi);
7087 :
7088 : /* Mark stmt as potentially needing revisiting. */
7089 365730057 : gimple_set_plf (stmt, GF_PLF_1, false);
7090 :
7091 365730057 : bool can_make_abnormal_goto = (is_gimple_call (stmt)
7092 365730057 : && stmt_can_make_abnormal_goto (stmt));
7093 :
7094 : /* Substitute from our lattice. We need to do so only once. */
7095 365730057 : bool substituted_p = false;
7096 365730057 : use_operand_p usep;
7097 365730057 : ssa_op_iter iter;
7098 534332167 : FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
7099 : {
7100 168602110 : tree use = USE_FROM_PTR (usep);
7101 168602110 : tree val = fwprop_ssa_val (use);
7102 168602110 : if (val && val != use)
7103 : {
7104 1955961 : if (!is_gimple_debug (stmt))
7105 1617385 : bitmap_set_bit (simple_dce_worklist, SSA_NAME_VERSION (use));
7106 1955961 : if (may_propagate_copy (use, val))
7107 : {
7108 1952717 : propagate_value (usep, val);
7109 1952717 : substituted_p = true;
7110 : }
7111 : }
7112 : }
7113 365730057 : if (substituted_p)
7114 1897209 : update_stmt (stmt);
7115 1897209 : if (substituted_p
7116 1897209 : && is_gimple_assign (stmt)
7117 1141389 : && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
7118 20681 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
7119 365730057 : if (substituted_p
7120 365730057 : && can_make_abnormal_goto
7121 365730057 : && !stmt_can_make_abnormal_goto (stmt))
7122 3 : bitmap_set_bit (need_ab_cleanup, bb->index);
7123 :
7124 368666925 : bool changed;
7125 737333850 : do
7126 : {
7127 368666925 : gimple *orig_stmt = stmt = gsi_stmt (gsi);
7128 368666925 : bool was_call = is_gimple_call (stmt);
7129 368666925 : bool was_noreturn = (was_call
7130 368666925 : && gimple_call_noreturn_p (stmt));
7131 368666925 : changed = false;
7132 :
7133 368666925 : auto_vec<tree, 8> uses;
7134 540318039 : FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
7135 342910367 : if (uses.space (1))
7136 171259253 : uses.quick_push (USE_FROM_PTR (usep));
7137 :
7138 368666925 : if (fold_stmt (&gsi, fwprop_ssa_val, simple_dce_worklist))
7139 : {
7140 2620726 : changed = true;
7141 : /* There is no updating of the address
7142 : taken after the last forwprop so update
7143 : the addresses when a folding happened to a call.
7144 : The va_* builtins can remove taking of the address so
7145 : can the sincos->cexpi transformation. See PR 39643 and PR 20983. */
7146 2620726 : if (was_call && last_p)
7147 2620726 : todoflags |= TODO_update_address_taken;
7148 2620726 : stmt = gsi_stmt (gsi);
7149 : /* Cleanup the CFG if we simplified a condition to
7150 : true or false. */
7151 2620726 : if (gcond *cond = dyn_cast <gcond *> (stmt))
7152 1058799 : if (gimple_cond_true_p (cond)
7153 1058799 : || gimple_cond_false_p (cond))
7154 31890 : cfg_changed = true;
7155 : /* Queue old uses for simple DCE if not debug statement. */
7156 2620726 : if (!is_gimple_debug (stmt))
7157 11043138 : for (tree use : uses)
7158 3202842 : if (TREE_CODE (use) == SSA_NAME
7159 3202842 : && !SSA_NAME_IS_DEFAULT_DEF (use))
7160 3003284 : bitmap_set_bit (simple_dce_worklist,
7161 3003284 : SSA_NAME_VERSION (use));
7162 2620726 : update_stmt (stmt);
7163 : }
7164 :
7165 368666925 : switch (gimple_code (stmt))
7166 : {
7167 109893601 : case GIMPLE_ASSIGN:
7168 109893601 : {
7169 109893601 : tree rhs1 = gimple_assign_rhs1 (stmt);
7170 109893601 : enum tree_code code = gimple_assign_rhs_code (stmt);
7171 109893601 : if (gimple_clobber_p (stmt))
7172 7651081 : do_simple_agr_dse (as_a<gassign*>(stmt), full_walk);
7173 102242520 : else if (gimple_store_p (stmt))
7174 : {
7175 31701762 : optimize_aggr_zeroprop (stmt, full_walk);
7176 31701762 : if (gimple_assign_load_p (stmt))
7177 3924556 : optimize_agr_copyprop (stmt);
7178 : }
7179 70540758 : else if (TREE_CODE_CLASS (code) == tcc_comparison)
7180 2650594 : changed |= forward_propagate_into_comparison (&gsi);
7181 67890164 : else if ((code == PLUS_EXPR || code == BIT_IOR_EXPR))
7182 : {
7183 10239215 : bool folded = match_long_mul (as_a <gassign *> (stmt));
7184 10239215 : if (!folded)
7185 10237278 : folded = simplify_rotate (&gsi);
7186 10239215 : changed |= folded;
7187 : }
7188 : else if (code == BIT_XOR_EXPR)
7189 146140 : changed |= simplify_rotate (&gsi);
7190 : else if (code == VEC_PERM_EXPR)
7191 191400 : changed |= simplify_permutation (&gsi);
7192 : else if (code == CONSTRUCTOR
7193 167838 : && TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
7194 167838 : changed |= simplify_vector_constructor (&gsi);
7195 57145571 : else if (code == ARRAY_REF)
7196 2012230 : changed |= simplify_count_zeroes (&gsi);
7197 55133341 : else if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
7198 12708627 : && TREE_CODE (
7199 : gimple_assign_rhs2 (stmt)) == SSA_NAME
7200 6750564 : && has_single_use (gimple_assign_rhs2 (stmt))
7201 59275739 : && is_a<gphi*> (SSA_NAME_DEF_STMT (
7202 : gimple_assign_rhs2 (stmt))))
7203 133595 : changed |= simplify_phi_result_movdiv (stmt, code);
7204 : break;
7205 : }
7206 :
7207 111316 : case GIMPLE_SWITCH:
7208 111316 : changed |= simplify_gimple_switch (as_a <gswitch *> (stmt),
7209 : edges_to_remove,
7210 : simple_dce_worklist);
7211 111316 : break;
7212 :
7213 19983265 : case GIMPLE_COND:
7214 19983265 : {
7215 19983265 : int did_something = forward_propagate_into_gimple_cond
7216 19983265 : (as_a <gcond *> (stmt));
7217 19983265 : if (did_something == 2)
7218 1624 : cfg_changed = true;
7219 19983265 : changed |= did_something != 0;
7220 19983265 : break;
7221 : }
7222 :
7223 24244637 : case GIMPLE_CALL:
7224 24244637 : {
7225 24244637 : tree callee = gimple_call_fndecl (stmt);
7226 24244637 : if (callee != NULL_TREE
7227 24244637 : && fndecl_built_in_p (callee, BUILT_IN_NORMAL))
7228 6358982 : changed |= simplify_builtin_call (&gsi, callee, full_walk);
7229 : break;
7230 : }
7231 :
7232 368666925 : default:;
7233 : }
7234 :
7235 368666925 : if (changed || substituted_p)
7236 : {
7237 4263652 : substituted_p = false;
7238 4263652 : stmt = gsi_stmt (gsi);
7239 4263652 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
7240 70 : bitmap_set_bit (to_purge, bb->index);
7241 4263652 : if (!was_noreturn
7242 4263652 : && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
7243 12 : to_fixup.safe_push (stmt);
7244 : }
7245 4263652 : if (changed)
7246 : {
7247 : /* If the stmt changed then re-visit it and the statements
7248 : inserted before it. */
7249 6320592 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
7250 5857790 : if (gimple_plf (gsi_stmt (gsi), GF_PLF_1))
7251 : break;
7252 2936868 : if (gsi_end_p (gsi))
7253 462802 : gsi = gsi_start_bb (bb);
7254 : else
7255 2705467 : gsi_next (&gsi);
7256 : }
7257 368666925 : }
7258 : while (changed);
7259 :
7260 : /* Stmt no longer needs to be revisited. */
7261 365730057 : stmt = gsi_stmt (gsi);
7262 365730057 : gcc_checking_assert (!gimple_plf (stmt, GF_PLF_1));
7263 365730057 : gimple_set_plf (stmt, GF_PLF_1, true);
7264 :
7265 : /* Fill up the lattice. */
7266 365730057 : if (gimple_assign_single_p (stmt))
7267 : {
7268 72884238 : tree lhs = gimple_assign_lhs (stmt);
7269 72884238 : tree rhs = gimple_assign_rhs1 (stmt);
7270 72884238 : if (TREE_CODE (lhs) == SSA_NAME)
7271 : {
7272 33544060 : tree val = lhs;
7273 33544060 : if (TREE_CODE (rhs) == SSA_NAME)
7274 786738 : val = fwprop_ssa_val (rhs);
7275 32757322 : else if (is_gimple_min_invariant (rhs))
7276 472586 : val = rhs;
7277 : /* If we can propagate the lattice-value mark the
7278 : stmt for removal. */
7279 33544060 : if (val != lhs
7280 33544060 : && may_propagate_copy (lhs, val))
7281 1255923 : to_remove_defs.safe_push (SSA_NAME_VERSION (lhs));
7282 33544060 : fwprop_set_lattice_val (lhs, val);
7283 : }
7284 : }
7285 292845819 : else if (gimple_nop_p (stmt))
7286 103614 : to_remove.safe_push (stmt);
7287 : }
7288 :
7289 : /* Substitute in destination PHI arguments. */
7290 111807927 : FOR_EACH_EDGE (e, ei, bb->succs)
7291 65376096 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
7292 108765695 : !gsi_end_p (gsi); gsi_next (&gsi))
7293 : {
7294 43389599 : gphi *phi = gsi.phi ();
7295 43389599 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
7296 43389599 : tree arg = USE_FROM_PTR (use_p);
7297 71478487 : if (TREE_CODE (arg) != SSA_NAME
7298 43389599 : || virtual_operand_p (arg))
7299 28088888 : continue;
7300 15300711 : tree val = fwprop_ssa_val (arg);
7301 15300711 : if (val != arg
7302 15300711 : && may_propagate_copy (arg, val, !(e->flags & EDGE_ABNORMAL)))
7303 259317 : propagate_value (use_p, val);
7304 : }
7305 :
7306 : /* Mark outgoing executable edges. */
7307 46431831 : if (edge e = find_taken_edge (bb, NULL))
7308 : {
7309 19734663 : e->flags |= EDGE_EXECUTABLE;
7310 46469654 : if (EDGE_COUNT (bb->succs) > 1)
7311 37823 : cfg_changed = true;
7312 : }
7313 : else
7314 : {
7315 72300777 : FOR_EACH_EDGE (e, ei, bb->succs)
7316 45603609 : e->flags |= EDGE_EXECUTABLE;
7317 : }
7318 : }
7319 5773622 : free (postorder);
7320 5773622 : free (bb_to_rpo);
7321 5773622 : lattice.release ();
7322 :
7323 : /* First remove chains of stmts where we check no uses remain. */
7324 5773622 : simple_dce_from_worklist (simple_dce_worklist, to_purge);
7325 :
7326 6139206 : auto remove = [](gimple *stmt)
7327 : {
7328 365584 : if (dump_file && (dump_flags & TDF_DETAILS))
7329 : {
7330 1 : fprintf (dump_file, "Removing dead stmt ");
7331 1 : print_gimple_stmt (dump_file, stmt, 0);
7332 1 : fprintf (dump_file, "\n");
7333 : }
7334 365584 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
7335 365584 : if (gimple_code (stmt) == GIMPLE_PHI)
7336 97979 : remove_phi_node (&gsi, true);
7337 : else
7338 : {
7339 267605 : unlink_stmt_vdef (stmt);
7340 267605 : gsi_remove (&gsi, true);
7341 267605 : release_defs (stmt);
7342 : }
7343 365584 : };
7344 :
7345 : /* Then remove stmts we know we can remove even though we did not
7346 : substitute in dead code regions, so uses can remain. Do so in reverse
7347 : order to make debug stmt creation possible. */
7348 13036337 : while (!to_remove_defs.is_empty())
7349 : {
7350 1489093 : tree def = ssa_name (to_remove_defs.pop ());
7351 : /* For example remove_prop_source_from_use can remove stmts queued
7352 : for removal. Deal with this gracefully. */
7353 1489093 : if (!def)
7354 1227123 : continue;
7355 261970 : gimple *stmt = SSA_NAME_DEF_STMT (def);
7356 261970 : remove (stmt);
7357 : }
7358 :
7359 : /* Wipe other queued stmts that do not have SSA defs. */
7360 5877236 : while (!to_remove.is_empty())
7361 : {
7362 103614 : gimple *stmt = to_remove.pop ();
7363 103614 : remove (stmt);
7364 : }
7365 :
7366 : /* Fixup stmts that became noreturn calls. This may require splitting
7367 : blocks and thus isn't possible during the walk. Do this
7368 : in reverse order so we don't inadvertently remove a stmt we want to
7369 : fixup by visiting a dominating now noreturn call first. */
7370 5773634 : while (!to_fixup.is_empty ())
7371 : {
7372 12 : gimple *stmt = to_fixup.pop ();
7373 12 : if (dump_file && dump_flags & TDF_DETAILS)
7374 : {
7375 0 : fprintf (dump_file, "Fixing up noreturn call ");
7376 0 : print_gimple_stmt (dump_file, stmt, 0);
7377 0 : fprintf (dump_file, "\n");
7378 : }
7379 12 : cfg_changed |= fixup_noreturn_call (stmt);
7380 : }
7381 :
7382 5773622 : cfg_changed |= gimple_purge_all_dead_eh_edges (to_purge);
7383 5773622 : cfg_changed |= gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
7384 5773622 : BITMAP_FREE (to_purge);
7385 :
7386 : /* Remove edges queued from switch stmt simplification. */
7387 17320866 : for (auto ep : edges_to_remove)
7388 : {
7389 0 : basic_block src = BASIC_BLOCK_FOR_FN (fun, ep.first);
7390 0 : basic_block dest = BASIC_BLOCK_FOR_FN (fun, ep.second);
7391 0 : edge e;
7392 0 : if (src && dest && (e = find_edge (src, dest)))
7393 : {
7394 0 : free_dominance_info (CDI_DOMINATORS);
7395 0 : remove_edge (e);
7396 0 : cfg_changed = true;
7397 : }
7398 : }
7399 :
7400 5773625 : if (get_range_query (fun) != get_global_range_query ())
7401 1577 : disable_ranger (fun);
7402 :
7403 5773622 : if (cfg_changed)
7404 13371 : todoflags |= TODO_cleanup_cfg;
7405 :
7406 5773622 : return todoflags;
7407 5773622 : }
7408 :
7409 : } // anon namespace
7410 :
7411 : gimple_opt_pass *
7412 294536 : make_pass_forwprop (gcc::context *ctxt)
7413 : {
7414 294536 : return new pass_forwprop (ctxt);
7415 : }
|