Branch data Line data Source code
1 : : /* Forward propagation of expressions for single use variables.
2 : : Copyright (C) 2004-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify
7 : : it under the terms of the GNU General Public License as published by
8 : : the Free Software Foundation; either version 3, or (at your option)
9 : : any later version.
10 : :
11 : : GCC is distributed in the hope that it will be useful,
12 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : GNU General Public License for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along with GCC; see the file COPYING3. If not see
18 : : <http://www.gnu.org/licenses/>. */
19 : :
20 : : #include "config.h"
21 : : #include "system.h"
22 : : #include "coretypes.h"
23 : : #include "backend.h"
24 : : #include "rtl.h"
25 : : #include "tree.h"
26 : : #include "gimple.h"
27 : : #include "cfghooks.h"
28 : : #include "tree-pass.h"
29 : : #include "ssa.h"
30 : : #include "expmed.h"
31 : : #include "optabs-query.h"
32 : : #include "gimple-pretty-print.h"
33 : : #include "fold-const.h"
34 : : #include "stor-layout.h"
35 : : #include "gimple-iterator.h"
36 : : #include "gimple-fold.h"
37 : : #include "tree-eh.h"
38 : : #include "gimplify.h"
39 : : #include "gimplify-me.h"
40 : : #include "tree-cfg.h"
41 : : #include "expr.h"
42 : : #include "tree-dfa.h"
43 : : #include "tree-ssa-propagate.h"
44 : : #include "tree-ssa-dom.h"
45 : : #include "tree-ssa-strlen.h"
46 : : #include "builtins.h"
47 : : #include "tree-cfgcleanup.h"
48 : : #include "cfganal.h"
49 : : #include "optabs-tree.h"
50 : : #include "insn-config.h"
51 : : #include "recog.h"
52 : : #include "cfgloop.h"
53 : : #include "tree-vectorizer.h"
54 : : #include "tree-vector-builder.h"
55 : : #include "vec-perm-indices.h"
56 : : #include "internal-fn.h"
57 : : #include "cgraph.h"
58 : : #include "tree-ssa.h"
59 : : #include "gimple-range.h"
60 : : #include "tree-ssa-dce.h"
61 : :
62 : : /* This pass propagates the RHS of assignment statements into use
63 : : sites of the LHS of the assignment. It's basically a specialized
64 : : form of tree combination. It is hoped all of this can disappear
65 : : when we have a generalized tree combiner.
66 : :
67 : : One class of common cases we handle is forward propagating a single use
68 : : variable into a COND_EXPR.
69 : :
70 : : bb0:
71 : : x = a COND b;
72 : : if (x) goto ... else goto ...
73 : :
74 : : Will be transformed into:
75 : :
76 : : bb0:
77 : : if (a COND b) goto ... else goto ...
78 : :
79 : : Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
80 : :
81 : : Or (assuming c1 and c2 are constants):
82 : :
83 : : bb0:
84 : : x = a + c1;
85 : : if (x EQ/NEQ c2) goto ... else goto ...
86 : :
87 : : Will be transformed into:
88 : :
89 : : bb0:
90 : : if (a EQ/NEQ (c2 - c1)) goto ... else goto ...
91 : :
92 : : Similarly for x = a - c1.
93 : :
94 : : Or
95 : :
96 : : bb0:
97 : : x = !a
98 : : if (x) goto ... else goto ...
99 : :
100 : : Will be transformed into:
101 : :
102 : : bb0:
103 : : if (a == 0) goto ... else goto ...
104 : :
105 : : Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
106 : : For these cases, we propagate A into all, possibly more than one,
107 : : COND_EXPRs that use X.
108 : :
109 : : Or
110 : :
111 : : bb0:
112 : : x = (typecast) a
113 : : if (x) goto ... else goto ...
114 : :
115 : : Will be transformed into:
116 : :
117 : : bb0:
118 : : if (a != 0) goto ... else goto ...
119 : :
120 : : (Assuming a is an integral type and x is a boolean or x is an
121 : : integral and a is a boolean.)
122 : :
123 : : Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
124 : : For these cases, we propagate A into all, possibly more than one,
125 : : COND_EXPRs that use X.
126 : :
127 : : In addition to eliminating the variable and the statement which assigns
128 : : a value to the variable, we may be able to later thread the jump without
129 : : adding insane complexity in the dominator optimizer.
130 : :
131 : : Also note these transformations can cascade. We handle this by having
132 : : a worklist of COND_EXPR statements to examine. As we make a change to
133 : : a statement, we put it back on the worklist to examine on the next
134 : : iteration of the main loop.
135 : :
136 : : A second class of propagation opportunities arises for ADDR_EXPR
137 : : nodes.
138 : :
139 : : ptr = &x->y->z;
140 : : res = *ptr;
141 : :
142 : : Will get turned into
143 : :
144 : : res = x->y->z;
145 : :
146 : : Or
147 : : ptr = (type1*)&type2var;
148 : : res = *ptr
149 : :
150 : : Will get turned into (if type1 and type2 are the same size
151 : : and neither have volatile on them):
152 : : res = VIEW_CONVERT_EXPR<type1>(type2var)
153 : :
154 : : Or
155 : :
156 : : ptr = &x[0];
157 : : ptr2 = ptr + <constant>;
158 : :
159 : : Will get turned into
160 : :
161 : : ptr2 = &x[constant/elementsize];
162 : :
163 : : Or
164 : :
165 : : ptr = &x[0];
166 : : offset = index * element_size;
167 : : offset_p = (pointer) offset;
168 : : ptr2 = ptr + offset_p
169 : :
170 : : Will get turned into:
171 : :
172 : : ptr2 = &x[index];
173 : :
174 : : Or
175 : : ssa = (int) decl
176 : : res = ssa & 1
177 : :
178 : : Provided that decl has known alignment >= 2, will get turned into
179 : :
180 : : res = 0
181 : :
182 : : We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to
183 : : allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent
184 : : {NOT_EXPR,NEG_EXPR}.
185 : :
186 : : This will (of course) be extended as other needs arise. */
187 : :
188 : : /* Data structure that contains simplifiable vectorized permute sequences.
189 : : See recognise_vec_perm_simplify_seq () for a description of the sequence. */
190 : :
191 : : struct _vec_perm_simplify_seq
192 : : {
193 : : /* Defining stmts of vectors in the sequence. */
194 : : gassign *v_1_stmt;
195 : : gassign *v_2_stmt;
196 : : gassign *v_x_stmt;
197 : : gassign *v_y_stmt;
198 : : /* Final permute statment. */
199 : : gassign *stmt;
200 : : /* New selector indices for stmt. */
201 : : tree new_sel;
202 : : /* Elements of each vector and selector. */
203 : : unsigned int nelts;
204 : : };
205 : : typedef struct _vec_perm_simplify_seq *vec_perm_simplify_seq;
206 : :
207 : : static bool forward_propagate_addr_expr (tree, tree, bool);
208 : : static void optimize_vector_load (gimple_stmt_iterator *);
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 : 30762831 : fwprop_set_lattice_val (tree name, tree val)
223 : : {
224 : 30762831 : if (TREE_CODE (name) == SSA_NAME)
225 : : {
226 : 30762831 : if (SSA_NAME_VERSION (name) >= lattice.length ())
227 : : {
228 : 1803 : lattice.reserve (num_ssa_names - lattice.length ());
229 : 1202 : lattice.quick_grow_cleared (num_ssa_names);
230 : : }
231 : 30762831 : lattice[SSA_NAME_VERSION (name)] = val;
232 : : /* As this now constitutes a copy duplicate points-to
233 : : and range info appropriately. */
234 : 30762831 : if (TREE_CODE (val) == SSA_NAME)
235 : 30332845 : maybe_duplicate_ssa_info_at_copy (name, val);
236 : : }
237 : 30762831 : }
238 : :
239 : : /* Invalidate the lattice entry for NAME, done when releasing SSA names. */
240 : : static void
241 : 830012 : fwprop_invalidate_lattice (tree name)
242 : : {
243 : 830012 : if (name
244 : 827705 : && TREE_CODE (name) == SSA_NAME
245 : 1657612 : && SSA_NAME_VERSION (name) < lattice.length ())
246 : 827571 : lattice[SSA_NAME_VERSION (name)] = NULL_TREE;
247 : 830012 : }
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 : 24347351 : get_prop_source_stmt (tree name, bool single_use_only, bool *single_use_p)
259 : : {
260 : 24347351 : bool single_use = true;
261 : :
262 : 24348323 : do {
263 : 24347837 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
264 : :
265 : 24347837 : if (!has_single_use (name))
266 : : {
267 : 12632462 : single_use = false;
268 : 12632462 : 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 : 24346371 : if (!is_gimple_assign (def_stmt))
274 : : return NULL;
275 : :
276 : : /* If def_stmt is a simple copy, continue looking. */
277 : 16912812 : if (gimple_assign_rhs_code (def_stmt) == SSA_NAME)
278 : 486 : name = gimple_assign_rhs1 (def_stmt);
279 : : else
280 : : {
281 : 16912326 : if (!single_use_only && single_use_p)
282 : 16608965 : *single_use_p = single_use;
283 : :
284 : 16912326 : return def_stmt;
285 : : }
286 : 486 : } 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 : 24455514 : can_propagate_from (gimple *def_stmt)
294 : : {
295 : 24455514 : gcc_assert (is_gimple_assign (def_stmt));
296 : :
297 : : /* If the rhs has side-effects we cannot propagate from it. */
298 : 24455514 : if (gimple_has_volatile_ops (def_stmt))
299 : : return false;
300 : :
301 : : /* If the rhs is a load we cannot propagate from it. */
302 : 23876854 : if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_reference
303 : 23876854 : || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_declaration)
304 : : return false;
305 : :
306 : : /* Constants can be always propagated. */
307 : 11942508 : if (gimple_assign_single_p (def_stmt)
308 : 11942508 : && 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 : 11942508 : 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 : 11941888 : if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
320 : : {
321 : 2867401 : tree rhs = gimple_assign_rhs1 (def_stmt);
322 : 2867401 : if (FUNCTION_POINTER_TYPE_P (TREE_TYPE (rhs)))
323 : : 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 : : Returns true if cleanup-cfg has to run. */
335 : :
336 : : static bool
337 : 207215 : remove_prop_source_from_use (tree name)
338 : : {
339 : 207215 : gimple_stmt_iterator gsi;
340 : 207215 : gimple *stmt;
341 : 207215 : bool cfg_changed = false;
342 : :
343 : 264325 : do {
344 : 264325 : basic_block bb;
345 : :
346 : 264325 : if (SSA_NAME_IN_FREE_LIST (name)
347 : 264282 : || SSA_NAME_IS_DEFAULT_DEF (name)
348 : 525290 : || !has_zero_uses (name))
349 : : return cfg_changed;
350 : :
351 : 58132 : stmt = SSA_NAME_DEF_STMT (name);
352 : 58132 : if (gimple_code (stmt) == GIMPLE_PHI
353 : 58132 : || gimple_has_side_effects (stmt))
354 : 0 : return cfg_changed;
355 : :
356 : 58132 : bb = gimple_bb (stmt);
357 : 58132 : gsi = gsi_for_stmt (stmt);
358 : 58132 : unlink_stmt_vdef (stmt);
359 : 58132 : if (gsi_remove (&gsi, true))
360 : 6 : bitmap_set_bit (to_purge, bb->index);
361 : 58132 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
362 : 58132 : release_defs (stmt);
363 : :
364 : 58132 : name = is_gimple_assign (stmt) ? gimple_assign_rhs1 (stmt) : NULL_TREE;
365 : 58132 : } while (name && TREE_CODE (name) == SSA_NAME);
366 : :
367 : : return cfg_changed;
368 : : }
369 : :
370 : : /* Return the rhs of a gassign *STMT in a form of a single tree,
371 : : converted to type TYPE.
372 : :
373 : : This should disappear, but is needed so we can combine expressions and use
374 : : the fold() interfaces. Long term, we need to develop folding and combine
375 : : routines that deal with gimple exclusively . */
376 : :
377 : : static tree
378 : 6146259 : rhs_to_tree (tree type, gimple *stmt)
379 : : {
380 : 6146259 : location_t loc = gimple_location (stmt);
381 : 6146259 : enum tree_code code = gimple_assign_rhs_code (stmt);
382 : 6146259 : switch (get_gimple_rhs_class (code))
383 : : {
384 : 10591 : case GIMPLE_TERNARY_RHS:
385 : 10591 : return fold_build3_loc (loc, code, type, gimple_assign_rhs1 (stmt),
386 : : gimple_assign_rhs2 (stmt),
387 : 10591 : gimple_assign_rhs3 (stmt));
388 : 4265703 : case GIMPLE_BINARY_RHS:
389 : 4265703 : return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (stmt),
390 : 4265703 : gimple_assign_rhs2 (stmt));
391 : 1715396 : case GIMPLE_UNARY_RHS:
392 : 1715396 : return build1 (code, type, gimple_assign_rhs1 (stmt));
393 : 154569 : case GIMPLE_SINGLE_RHS:
394 : 154569 : return gimple_assign_rhs1 (stmt);
395 : 0 : default:
396 : 0 : gcc_unreachable ();
397 : : }
398 : : }
399 : :
400 : : /* Combine OP0 CODE OP1 in the context of a COND_EXPR. Returns
401 : : the folded result in a form suitable for COND_EXPR_COND or
402 : : NULL_TREE, if there is no suitable simplified form. If
403 : : INVARIANT_ONLY is true only gimple_min_invariant results are
404 : : considered simplified. */
405 : :
406 : : static tree
407 : 6927839 : combine_cond_expr_cond (gimple *stmt, enum tree_code code, tree type,
408 : : tree op0, tree op1, bool invariant_only)
409 : : {
410 : 6927839 : tree t;
411 : :
412 : 6927839 : gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
413 : :
414 : 6927839 : fold_defer_overflow_warnings ();
415 : 6927839 : t = fold_binary_loc (gimple_location (stmt), code, type, op0, op1);
416 : 6927839 : if (!t)
417 : : {
418 : 3764485 : fold_undefer_overflow_warnings (false, NULL, 0);
419 : 3764485 : return NULL_TREE;
420 : : }
421 : :
422 : : /* Require that we got a boolean type out if we put one in. */
423 : 3163354 : gcc_assert (TREE_CODE (TREE_TYPE (t)) == TREE_CODE (type));
424 : :
425 : : /* Canonicalize the combined condition for use in a COND_EXPR. */
426 : 3163354 : t = canonicalize_cond_expr_cond (t);
427 : :
428 : : /* Bail out if we required an invariant but didn't get one. */
429 : 3163354 : if (!t || (invariant_only && !is_gimple_min_invariant (t)))
430 : : {
431 : 2958278 : fold_undefer_overflow_warnings (false, NULL, 0);
432 : 2958278 : return NULL_TREE;
433 : : }
434 : :
435 : 205076 : bool nowarn = warning_suppressed_p (stmt, OPT_Wstrict_overflow);
436 : 205076 : fold_undefer_overflow_warnings (!nowarn, stmt, 0);
437 : :
438 : 205076 : return t;
439 : : }
440 : :
441 : : /* Combine the comparison OP0 CODE OP1 at LOC with the defining statements
442 : : of its operand. Return a new comparison tree or NULL_TREE if there
443 : : were no simplifying combines. */
444 : :
445 : : static tree
446 : 19335353 : forward_propagate_into_comparison_1 (gimple *stmt,
447 : : enum tree_code code, tree type,
448 : : tree op0, tree op1)
449 : : {
450 : 19335353 : tree tmp = NULL_TREE;
451 : 19335353 : tree rhs0 = NULL_TREE, rhs1 = NULL_TREE;
452 : 19335353 : bool single_use0_p = false, single_use1_p = false;
453 : :
454 : : /* For comparisons use the first operand, that is likely to
455 : : simplify comparisons against constants. */
456 : 19335353 : if (TREE_CODE (op0) == SSA_NAME)
457 : : {
458 : 19069703 : gimple *def_stmt = get_prop_source_stmt (op0, false, &single_use0_p);
459 : 19069703 : if (def_stmt && can_propagate_from (def_stmt))
460 : : {
461 : 4667669 : enum tree_code def_code = gimple_assign_rhs_code (def_stmt);
462 : 4667669 : bool invariant_only_p = !single_use0_p;
463 : :
464 : 4667669 : rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt);
465 : :
466 : : /* Always combine comparisons or conversions from booleans. */
467 : 4667669 : if (TREE_CODE (op1) == INTEGER_CST
468 : 4667669 : && ((CONVERT_EXPR_CODE_P (def_code)
469 : 746071 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs0, 0)))
470 : : == BOOLEAN_TYPE)
471 : 3072348 : || TREE_CODE_CLASS (def_code) == tcc_comparison))
472 : : invariant_only_p = false;
473 : :
474 : 4667669 : tmp = combine_cond_expr_cond (stmt, code, type,
475 : : rhs0, op1, invariant_only_p);
476 : 4667669 : if (tmp)
477 : : return tmp;
478 : : }
479 : : }
480 : :
481 : : /* If that wasn't successful, try the second operand. */
482 : 19137298 : if (TREE_CODE (op1) == SSA_NAME)
483 : : {
484 : 4732607 : gimple *def_stmt = get_prop_source_stmt (op1, false, &single_use1_p);
485 : 4732607 : if (def_stmt && can_propagate_from (def_stmt))
486 : : {
487 : 1478590 : rhs1 = rhs_to_tree (TREE_TYPE (op0), def_stmt);
488 : 2957180 : tmp = combine_cond_expr_cond (stmt, code, type,
489 : 1478590 : op0, rhs1, !single_use1_p);
490 : 1478590 : if (tmp)
491 : : return tmp;
492 : : }
493 : : }
494 : :
495 : : /* If that wasn't successful either, try both operands. */
496 : 19132231 : if (rhs0 != NULL_TREE
497 : 19132231 : && rhs1 != NULL_TREE)
498 : 781580 : tmp = combine_cond_expr_cond (stmt, code, type,
499 : : rhs0, rhs1,
500 : 781580 : !(single_use0_p && single_use1_p));
501 : :
502 : : return tmp;
503 : : }
504 : :
505 : : /* Propagate from the ssa name definition statements of the assignment
506 : : from a comparison at *GSI into the conditional if that simplifies it.
507 : : Returns 1 if the stmt was modified and 2 if the CFG needs cleanup,
508 : : otherwise returns 0. */
509 : :
510 : : static int
511 : 2293067 : forward_propagate_into_comparison (gimple_stmt_iterator *gsi)
512 : : {
513 : 2293067 : gimple *stmt = gsi_stmt (*gsi);
514 : 2293067 : tree tmp;
515 : 2293067 : bool cfg_changed = false;
516 : 2293067 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
517 : 2293067 : tree rhs1 = gimple_assign_rhs1 (stmt);
518 : 2293067 : tree rhs2 = gimple_assign_rhs2 (stmt);
519 : :
520 : : /* Combine the comparison with defining statements. */
521 : 2293067 : tmp = forward_propagate_into_comparison_1 (stmt,
522 : : gimple_assign_rhs_code (stmt),
523 : : type, rhs1, rhs2);
524 : 2293067 : if (tmp && useless_type_conversion_p (type, TREE_TYPE (tmp)))
525 : : {
526 : 6987 : gimple_assign_set_rhs_from_tree (gsi, tmp);
527 : 6987 : fold_stmt (gsi);
528 : 6987 : update_stmt (gsi_stmt (*gsi));
529 : :
530 : 6987 : if (TREE_CODE (rhs1) == SSA_NAME)
531 : 6733 : cfg_changed |= remove_prop_source_from_use (rhs1);
532 : 6987 : if (TREE_CODE (rhs2) == SSA_NAME)
533 : 2997 : cfg_changed |= remove_prop_source_from_use (rhs2);
534 : 6987 : return cfg_changed ? 2 : 1;
535 : : }
536 : :
537 : : return 0;
538 : : }
539 : :
540 : : /* Propagate from the ssa name definition statements of COND_EXPR
541 : : in GIMPLE_COND statement STMT into the conditional if that simplifies it.
542 : : Returns zero if no statement was changed, one if there were
543 : : changes and two if cfg_cleanup needs to run. */
544 : :
545 : : static int
546 : 17042286 : forward_propagate_into_gimple_cond (gcond *stmt)
547 : : {
548 : 17042286 : tree tmp;
549 : 17042286 : enum tree_code code = gimple_cond_code (stmt);
550 : 17042286 : bool cfg_changed = false;
551 : 17042286 : tree rhs1 = gimple_cond_lhs (stmt);
552 : 17042286 : tree rhs2 = gimple_cond_rhs (stmt);
553 : :
554 : : /* We can do tree combining on SSA_NAME and comparison expressions. */
555 : 17042286 : if (TREE_CODE_CLASS (gimple_cond_code (stmt)) != tcc_comparison)
556 : : return 0;
557 : :
558 : 17042286 : tmp = forward_propagate_into_comparison_1 (stmt, code,
559 : : boolean_type_node,
560 : : rhs1, rhs2);
561 : 17042286 : if (tmp
562 : 17042286 : && is_gimple_condexpr_for_cond (tmp))
563 : : {
564 : 191889 : if (dump_file)
565 : : {
566 : 6 : fprintf (dump_file, " Replaced '");
567 : 6 : print_gimple_expr (dump_file, stmt, 0);
568 : 6 : fprintf (dump_file, "' with '");
569 : 6 : print_generic_expr (dump_file, tmp);
570 : 6 : fprintf (dump_file, "'\n");
571 : : }
572 : :
573 : 191889 : gimple_cond_set_condition_from_tree (stmt, unshare_expr (tmp));
574 : 191889 : update_stmt (stmt);
575 : :
576 : 191889 : if (TREE_CODE (rhs1) == SSA_NAME)
577 : 191861 : cfg_changed |= remove_prop_source_from_use (rhs1);
578 : 191889 : if (TREE_CODE (rhs2) == SSA_NAME)
579 : 5623 : cfg_changed |= remove_prop_source_from_use (rhs2);
580 : 191889 : return (cfg_changed || is_gimple_min_invariant (tmp)) ? 2 : 1;
581 : : }
582 : :
583 : : /* Canonicalize _Bool == 0 and _Bool != 1 to _Bool != 0 by swapping edges. */
584 : 16850397 : if ((TREE_CODE (TREE_TYPE (rhs1)) == BOOLEAN_TYPE
585 : 14792995 : || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
586 : 11102979 : && TYPE_PRECISION (TREE_TYPE (rhs1)) == 1))
587 : 16852853 : && ((code == EQ_EXPR
588 : 7269 : && integer_zerop (rhs2))
589 : 2059213 : || (code == NE_EXPR
590 : 2052271 : && integer_onep (rhs2))))
591 : : {
592 : 135365 : basic_block bb = gimple_bb (stmt);
593 : 135365 : gimple_cond_set_code (stmt, NE_EXPR);
594 : 135365 : gimple_cond_set_rhs (stmt, build_zero_cst (TREE_TYPE (rhs1)));
595 : 135365 : EDGE_SUCC (bb, 0)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
596 : 135365 : EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
597 : 135365 : return 1;
598 : : }
599 : :
600 : : return 0;
601 : : }
602 : :
603 : : /* We've just substituted an ADDR_EXPR into stmt. Update all the
604 : : relevant data structures to match. */
605 : :
606 : : static void
607 : 1983955 : tidy_after_forward_propagate_addr (gimple *stmt)
608 : : {
609 : : /* We may have turned a trapping insn into a non-trapping insn. */
610 : 1983955 : if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
611 : 8 : bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
612 : :
613 : 1983955 : if (TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
614 : 237290 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
615 : 1983955 : }
616 : :
617 : : /* NAME is a SSA_NAME representing DEF_RHS which is of the form
618 : : ADDR_EXPR <whatever>.
619 : :
620 : : Try to forward propagate the ADDR_EXPR into the use USE_STMT.
621 : : Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
622 : : node or for recovery of array indexing from pointer arithmetic.
623 : :
624 : : Return true if the propagation was successful (the propagation can
625 : : be not totally successful, yet things may have been changed). */
626 : :
627 : : static bool
628 : 2739879 : forward_propagate_addr_expr_1 (tree name, tree def_rhs,
629 : : gimple_stmt_iterator *use_stmt_gsi,
630 : : bool single_use_p)
631 : : {
632 : 2739879 : tree lhs, rhs, rhs2, array_ref;
633 : 2739879 : gimple *use_stmt = gsi_stmt (*use_stmt_gsi);
634 : 2739879 : enum tree_code rhs_code;
635 : 2739879 : bool res = true;
636 : :
637 : 2739879 : gcc_assert (TREE_CODE (def_rhs) == ADDR_EXPR);
638 : :
639 : 2739879 : lhs = gimple_assign_lhs (use_stmt);
640 : 2739879 : rhs_code = gimple_assign_rhs_code (use_stmt);
641 : 2739879 : rhs = gimple_assign_rhs1 (use_stmt);
642 : :
643 : : /* Do not perform copy-propagation but recurse through copy chains. */
644 : 2739879 : if (TREE_CODE (lhs) == SSA_NAME
645 : 1288669 : && rhs_code == SSA_NAME)
646 : 5016 : return forward_propagate_addr_expr (lhs, def_rhs, single_use_p);
647 : :
648 : : /* The use statement could be a conversion. Recurse to the uses of the
649 : : lhs as copyprop does not copy through pointer to integer to pointer
650 : : conversions and FRE does not catch all cases either.
651 : : Treat the case of a single-use name and
652 : : a conversion to def_rhs type separate, though. */
653 : 2734863 : if (TREE_CODE (lhs) == SSA_NAME
654 : 1283653 : && CONVERT_EXPR_CODE_P (rhs_code))
655 : : {
656 : : /* If there is a point in a conversion chain where the types match
657 : : so we can remove a conversion re-materialize the address here
658 : : and stop. */
659 : 27391 : if (single_use_p
660 : 27391 : && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs)))
661 : : {
662 : 1 : gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs));
663 : 1 : gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs));
664 : 1 : return true;
665 : : }
666 : :
667 : : /* Else recurse if the conversion preserves the address value. */
668 : 54780 : if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
669 : 1 : || POINTER_TYPE_P (TREE_TYPE (lhs)))
670 : 54780 : && (TYPE_PRECISION (TREE_TYPE (lhs))
671 : 27390 : >= TYPE_PRECISION (TREE_TYPE (def_rhs))))
672 : 27323 : return forward_propagate_addr_expr (lhs, def_rhs, single_use_p);
673 : :
674 : : return false;
675 : : }
676 : :
677 : : /* If this isn't a conversion chain from this on we only can propagate
678 : : into compatible pointer contexts. */
679 : 2707472 : if (!types_compatible_p (TREE_TYPE (name), TREE_TYPE (def_rhs)))
680 : : return false;
681 : :
682 : : /* Propagate through constant pointer adjustments. */
683 : 2683335 : if (TREE_CODE (lhs) == SSA_NAME
684 : 1233333 : && rhs_code == POINTER_PLUS_EXPR
685 : 1233333 : && rhs == name
686 : 2840544 : && TREE_CODE (gimple_assign_rhs2 (use_stmt)) == INTEGER_CST)
687 : : {
688 : 119390 : tree new_def_rhs;
689 : : /* As we come here with non-invariant addresses in def_rhs we need
690 : : to make sure we can build a valid constant offsetted address
691 : : for further propagation. Simply rely on fold building that
692 : : and check after the fact. */
693 : 119390 : new_def_rhs = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (rhs)),
694 : : def_rhs,
695 : : fold_convert (ptr_type_node,
696 : : gimple_assign_rhs2 (use_stmt)));
697 : 119390 : if (TREE_CODE (new_def_rhs) == MEM_REF
698 : 119390 : && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs, 0)))
699 : : return false;
700 : 115862 : new_def_rhs = build1 (ADDR_EXPR, TREE_TYPE (rhs), new_def_rhs);
701 : :
702 : : /* Recurse. If we could propagate into all uses of lhs do not
703 : : bother to replace into the current use but just pretend we did. */
704 : 115862 : if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p))
705 : : return true;
706 : :
707 : 37942 : if (useless_type_conversion_p (TREE_TYPE (lhs),
708 : 37942 : TREE_TYPE (new_def_rhs)))
709 : 37942 : gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs),
710 : : new_def_rhs);
711 : 0 : else if (is_gimple_min_invariant (new_def_rhs))
712 : 0 : gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR, new_def_rhs);
713 : : else
714 : : return false;
715 : 37942 : gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt);
716 : 37942 : update_stmt (use_stmt);
717 : 37942 : return true;
718 : : }
719 : :
720 : : /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
721 : : ADDR_EXPR will not appear on the LHS. */
722 : 2563945 : tree *lhsp = gimple_assign_lhs_ptr (use_stmt);
723 : 3831346 : while (handled_component_p (*lhsp))
724 : 1267401 : lhsp = &TREE_OPERAND (*lhsp, 0);
725 : 2563945 : lhs = *lhsp;
726 : :
727 : : /* Now see if the LHS node is a MEM_REF using NAME. If so,
728 : : propagate the ADDR_EXPR into the use of NAME and fold the result. */
729 : 2563945 : if (TREE_CODE (lhs) == MEM_REF
730 : 2563945 : && TREE_OPERAND (lhs, 0) == name)
731 : : {
732 : 963764 : tree def_rhs_base;
733 : 963764 : poly_int64 def_rhs_offset;
734 : : /* If the address is invariant we can always fold it. */
735 : 963764 : if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
736 : : &def_rhs_offset)))
737 : : {
738 : 944711 : poly_offset_int off = mem_ref_offset (lhs);
739 : 944711 : tree new_ptr;
740 : 944711 : off += def_rhs_offset;
741 : 944711 : if (TREE_CODE (def_rhs_base) == MEM_REF)
742 : : {
743 : 913186 : off += mem_ref_offset (def_rhs_base);
744 : 913186 : new_ptr = TREE_OPERAND (def_rhs_base, 0);
745 : : }
746 : : else
747 : 31525 : new_ptr = build_fold_addr_expr (def_rhs_base);
748 : 944711 : TREE_OPERAND (lhs, 0) = new_ptr;
749 : 944711 : TREE_OPERAND (lhs, 1)
750 : 944711 : = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs, 1)), off);
751 : 944711 : tidy_after_forward_propagate_addr (use_stmt);
752 : : /* Continue propagating into the RHS if this was not the only use. */
753 : 944711 : if (single_use_p)
754 : 170941 : return true;
755 : : }
756 : : /* If the LHS is a plain dereference and the value type is the same as
757 : : that of the pointed-to type of the address we can put the
758 : : dereferenced address on the LHS preserving the original alias-type. */
759 : 19053 : else if (integer_zerop (TREE_OPERAND (lhs, 1))
760 : 14304 : && ((gimple_assign_lhs (use_stmt) == lhs
761 : 10993 : && useless_type_conversion_p
762 : 10993 : (TREE_TYPE (TREE_OPERAND (def_rhs, 0)),
763 : 10993 : TREE_TYPE (gimple_assign_rhs1 (use_stmt))))
764 : 10786 : || types_compatible_p (TREE_TYPE (lhs),
765 : 10786 : TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
766 : : /* Don't forward anything into clobber stmts if it would result
767 : : in the lhs no longer being a MEM_REF. */
768 : 25538 : && (!gimple_clobber_p (use_stmt)
769 : 320 : || TREE_CODE (TREE_OPERAND (def_rhs, 0)) == MEM_REF))
770 : : {
771 : 6165 : tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
772 : 6165 : tree new_offset, new_base, saved, new_lhs;
773 : 21665 : while (handled_component_p (*def_rhs_basep))
774 : 9335 : def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
775 : 6165 : saved = *def_rhs_basep;
776 : 6165 : if (TREE_CODE (*def_rhs_basep) == MEM_REF)
777 : : {
778 : 3338 : new_base = TREE_OPERAND (*def_rhs_basep, 0);
779 : 3338 : new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (lhs, 1)),
780 : : TREE_OPERAND (*def_rhs_basep, 1));
781 : : }
782 : : else
783 : : {
784 : 2827 : new_base = build_fold_addr_expr (*def_rhs_basep);
785 : 2827 : new_offset = TREE_OPERAND (lhs, 1);
786 : : }
787 : 6165 : *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep),
788 : : new_base, new_offset);
789 : 6165 : TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (lhs);
790 : 6165 : TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (lhs);
791 : 6165 : TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (lhs);
792 : 6165 : new_lhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
793 : 6165 : *lhsp = new_lhs;
794 : 6165 : TREE_THIS_VOLATILE (new_lhs) = TREE_THIS_VOLATILE (lhs);
795 : 6165 : TREE_SIDE_EFFECTS (new_lhs) = TREE_SIDE_EFFECTS (lhs);
796 : 6165 : *def_rhs_basep = saved;
797 : 6165 : tidy_after_forward_propagate_addr (use_stmt);
798 : : /* Continue propagating into the RHS if this was not the
799 : : only use. */
800 : 6165 : if (single_use_p)
801 : : return true;
802 : : }
803 : : else
804 : : /* We can have a struct assignment dereferencing our name twice.
805 : : Note that we didn't propagate into the lhs to not falsely
806 : : claim we did when propagating into the rhs. */
807 : : res = false;
808 : : }
809 : :
810 : : /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
811 : : nodes from the RHS. */
812 : 2390777 : tree *rhsp = gimple_assign_rhs1_ptr (use_stmt);
813 : 2390777 : if (TREE_CODE (*rhsp) == ADDR_EXPR)
814 : 222158 : rhsp = &TREE_OPERAND (*rhsp, 0);
815 : 3322957 : while (handled_component_p (*rhsp))
816 : 932180 : rhsp = &TREE_OPERAND (*rhsp, 0);
817 : 2390777 : rhs = *rhsp;
818 : :
819 : : /* Now see if the RHS node is a MEM_REF using NAME. If so,
820 : : propagate the ADDR_EXPR into the use of NAME and fold the result. */
821 : 2390777 : if (TREE_CODE (rhs) == MEM_REF
822 : 2390777 : && TREE_OPERAND (rhs, 0) == name)
823 : : {
824 : 1048560 : tree def_rhs_base;
825 : 1048560 : poly_int64 def_rhs_offset;
826 : 1048560 : if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
827 : : &def_rhs_offset)))
828 : : {
829 : 1019584 : poly_offset_int off = mem_ref_offset (rhs);
830 : 1019584 : tree new_ptr;
831 : 1019584 : off += def_rhs_offset;
832 : 1019584 : if (TREE_CODE (def_rhs_base) == MEM_REF)
833 : : {
834 : 987968 : off += mem_ref_offset (def_rhs_base);
835 : 987968 : new_ptr = TREE_OPERAND (def_rhs_base, 0);
836 : : }
837 : : else
838 : 31616 : new_ptr = build_fold_addr_expr (def_rhs_base);
839 : 1019584 : TREE_OPERAND (rhs, 0) = new_ptr;
840 : 1019584 : TREE_OPERAND (rhs, 1)
841 : 1019584 : = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs, 1)), off);
842 : 1019584 : fold_stmt_inplace (use_stmt_gsi);
843 : 1019584 : tidy_after_forward_propagate_addr (use_stmt);
844 : 1019584 : return res;
845 : : }
846 : : /* If the RHS is a plain dereference and the value type is the same as
847 : : that of the pointed-to type of the address we can put the
848 : : dereferenced address on the RHS preserving the original alias-type. */
849 : 28976 : else if (integer_zerop (TREE_OPERAND (rhs, 1))
850 : 28976 : && ((gimple_assign_rhs1 (use_stmt) == rhs
851 : 16676 : && useless_type_conversion_p
852 : 16676 : (TREE_TYPE (gimple_assign_lhs (use_stmt)),
853 : 16676 : TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
854 : 21090 : || types_compatible_p (TREE_TYPE (rhs),
855 : 21090 : TREE_TYPE (TREE_OPERAND (def_rhs, 0)))))
856 : : {
857 : 13495 : tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
858 : 13495 : tree new_offset, new_base, saved, new_rhs;
859 : 47204 : while (handled_component_p (*def_rhs_basep))
860 : 20214 : def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
861 : 13495 : saved = *def_rhs_basep;
862 : 13495 : if (TREE_CODE (*def_rhs_basep) == MEM_REF)
863 : : {
864 : 6318 : new_base = TREE_OPERAND (*def_rhs_basep, 0);
865 : 6318 : new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (rhs, 1)),
866 : : TREE_OPERAND (*def_rhs_basep, 1));
867 : : }
868 : : else
869 : : {
870 : 7177 : new_base = build_fold_addr_expr (*def_rhs_basep);
871 : 7177 : new_offset = TREE_OPERAND (rhs, 1);
872 : : }
873 : 13495 : *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep),
874 : : new_base, new_offset);
875 : 13495 : TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (rhs);
876 : 13495 : TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (rhs);
877 : 13495 : TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (rhs);
878 : 13495 : new_rhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
879 : 13495 : *rhsp = new_rhs;
880 : 13495 : TREE_THIS_VOLATILE (new_rhs) = TREE_THIS_VOLATILE (rhs);
881 : 13495 : TREE_SIDE_EFFECTS (new_rhs) = TREE_SIDE_EFFECTS (rhs);
882 : 13495 : *def_rhs_basep = saved;
883 : 13495 : fold_stmt_inplace (use_stmt_gsi);
884 : 13495 : tidy_after_forward_propagate_addr (use_stmt);
885 : 13495 : return res;
886 : : }
887 : : }
888 : :
889 : : /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there
890 : : is nothing to do. */
891 : 1357698 : if (gimple_assign_rhs_code (use_stmt) != POINTER_PLUS_EXPR
892 : 1357698 : || gimple_assign_rhs1 (use_stmt) != name)
893 : : return false;
894 : :
895 : : /* The remaining cases are all for turning pointer arithmetic into
896 : : array indexing. They only apply when we have the address of
897 : : element zero in an array. If that is not the case then there
898 : : is nothing to do. */
899 : 37819 : array_ref = TREE_OPERAND (def_rhs, 0);
900 : 37819 : if ((TREE_CODE (array_ref) != ARRAY_REF
901 : 4434 : || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
902 : 4434 : || TREE_CODE (TREE_OPERAND (array_ref, 1)) != INTEGER_CST)
903 : 39238 : && TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE)
904 : : return false;
905 : :
906 : 14027 : rhs2 = gimple_assign_rhs2 (use_stmt);
907 : : /* Optimize &x[C1] p+ C2 to &x p+ C3 with C3 = C1 * element_size + C2. */
908 : 14027 : if (TREE_CODE (rhs2) == INTEGER_CST)
909 : : {
910 : 0 : tree new_rhs = build1_loc (gimple_location (use_stmt),
911 : 0 : ADDR_EXPR, TREE_TYPE (def_rhs),
912 : 0 : fold_build2 (MEM_REF,
913 : : TREE_TYPE (TREE_TYPE (def_rhs)),
914 : : unshare_expr (def_rhs),
915 : : fold_convert (ptr_type_node,
916 : : rhs2)));
917 : 0 : gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs);
918 : 0 : use_stmt = gsi_stmt (*use_stmt_gsi);
919 : 0 : update_stmt (use_stmt);
920 : 0 : tidy_after_forward_propagate_addr (use_stmt);
921 : 0 : return true;
922 : : }
923 : :
924 : : return false;
925 : : }
926 : :
927 : : /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
928 : :
929 : : Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
930 : : Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
931 : : node or for recovery of array indexing from pointer arithmetic.
932 : :
933 : : PARENT_SINGLE_USE_P tells if, when in a recursive invocation, NAME was
934 : : the single use in the previous invocation. Pass true when calling
935 : : this as toplevel.
936 : :
937 : : Returns true, if all uses have been propagated into. */
938 : :
939 : : static bool
940 : 3058549 : forward_propagate_addr_expr (tree name, tree rhs, bool parent_single_use_p)
941 : : {
942 : 3058549 : imm_use_iterator iter;
943 : 3058549 : gimple *use_stmt;
944 : 3058549 : bool all = true;
945 : 3058549 : bool single_use_p = parent_single_use_p && has_single_use (name);
946 : :
947 : 10121203 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, name)
948 : : {
949 : 7062654 : bool result;
950 : 7062654 : tree use_rhs;
951 : :
952 : : /* If the use is not in a simple assignment statement, then
953 : : there is nothing we can do. */
954 : 7062654 : if (!is_gimple_assign (use_stmt))
955 : : {
956 : 4322775 : if (!is_gimple_debug (use_stmt))
957 : 1777301 : all = false;
958 : 4322775 : continue;
959 : : }
960 : :
961 : 2739879 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
962 : 2739879 : result = forward_propagate_addr_expr_1 (name, rhs, &gsi,
963 : : single_use_p);
964 : : /* If the use has moved to a different statement adjust
965 : : the update machinery for the old statement too. */
966 : 2739879 : if (use_stmt != gsi_stmt (gsi))
967 : : {
968 : 0 : update_stmt (use_stmt);
969 : 0 : use_stmt = gsi_stmt (gsi);
970 : : }
971 : 2739879 : update_stmt (use_stmt);
972 : 2739879 : all &= result;
973 : :
974 : : /* Remove intermediate now unused copy and conversion chains. */
975 : 2739879 : use_rhs = gimple_assign_rhs1 (use_stmt);
976 : 2739879 : if (result
977 : 1324033 : && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
978 : 1139372 : && TREE_CODE (use_rhs) == SSA_NAME
979 : 2819722 : && has_zero_uses (gimple_assign_lhs (use_stmt)))
980 : : {
981 : 79843 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
982 : 79843 : fwprop_invalidate_lattice (gimple_get_lhs (use_stmt));
983 : 79843 : release_defs (use_stmt);
984 : 79843 : gsi_remove (&gsi, true);
985 : : }
986 : 3058549 : }
987 : :
988 : 3058549 : return all && has_zero_uses (name);
989 : : }
990 : :
991 : :
992 : : /* Helper function for simplify_gimple_switch. Remove case labels that
993 : : have values outside the range of the new type. */
994 : :
995 : : static void
996 : 12998 : simplify_gimple_switch_label_vec (gswitch *stmt, tree index_type,
997 : : vec<std::pair<int, int> > &edges_to_remove)
998 : : {
999 : 12998 : unsigned int branch_num = gimple_switch_num_labels (stmt);
1000 : 12998 : auto_vec<tree> labels (branch_num);
1001 : 12998 : unsigned int i, len;
1002 : :
1003 : : /* Collect the existing case labels in a VEC, and preprocess it as if
1004 : : we are gimplifying a GENERIC SWITCH_EXPR. */
1005 : 98529 : for (i = 1; i < branch_num; i++)
1006 : 72533 : labels.quick_push (gimple_switch_label (stmt, i));
1007 : 12998 : preprocess_case_label_vec_for_gimple (labels, index_type, NULL);
1008 : :
1009 : : /* If any labels were removed, replace the existing case labels
1010 : : in the GIMPLE_SWITCH statement with the correct ones.
1011 : : Note that the type updates were done in-place on the case labels,
1012 : : so we only have to replace the case labels in the GIMPLE_SWITCH
1013 : : if the number of labels changed. */
1014 : 12998 : len = labels.length ();
1015 : 12998 : if (len < branch_num - 1)
1016 : : {
1017 : 0 : bitmap target_blocks;
1018 : 0 : edge_iterator ei;
1019 : 0 : edge e;
1020 : :
1021 : : /* Corner case: *all* case labels have been removed as being
1022 : : out-of-range for INDEX_TYPE. Push one label and let the
1023 : : CFG cleanups deal with this further. */
1024 : 0 : if (len == 0)
1025 : : {
1026 : 0 : tree label, elt;
1027 : :
1028 : 0 : label = CASE_LABEL (gimple_switch_default_label (stmt));
1029 : 0 : elt = build_case_label (build_int_cst (index_type, 0), NULL, label);
1030 : 0 : labels.quick_push (elt);
1031 : 0 : len = 1;
1032 : : }
1033 : :
1034 : 0 : for (i = 0; i < labels.length (); i++)
1035 : 0 : gimple_switch_set_label (stmt, i + 1, labels[i]);
1036 : 0 : for (i++ ; i < branch_num; i++)
1037 : 0 : gimple_switch_set_label (stmt, i, NULL_TREE);
1038 : 0 : gimple_switch_set_num_labels (stmt, len + 1);
1039 : :
1040 : : /* Cleanup any edges that are now dead. */
1041 : 0 : target_blocks = BITMAP_ALLOC (NULL);
1042 : 0 : for (i = 0; i < gimple_switch_num_labels (stmt); i++)
1043 : : {
1044 : 0 : tree elt = gimple_switch_label (stmt, i);
1045 : 0 : basic_block target = label_to_block (cfun, CASE_LABEL (elt));
1046 : 0 : bitmap_set_bit (target_blocks, target->index);
1047 : : }
1048 : 0 : for (ei = ei_start (gimple_bb (stmt)->succs); (e = ei_safe_edge (ei)); )
1049 : : {
1050 : 0 : if (! bitmap_bit_p (target_blocks, e->dest->index))
1051 : 0 : edges_to_remove.safe_push (std::make_pair (e->src->index,
1052 : 0 : e->dest->index));
1053 : : else
1054 : 0 : ei_next (&ei);
1055 : : }
1056 : 0 : BITMAP_FREE (target_blocks);
1057 : : }
1058 : 12998 : }
1059 : :
1060 : : /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
1061 : : the condition which we may be able to optimize better. */
1062 : :
1063 : : static bool
1064 : 103563 : simplify_gimple_switch (gswitch *stmt,
1065 : : vec<std::pair<int, int> > &edges_to_remove)
1066 : : {
1067 : : /* The optimization that we really care about is removing unnecessary
1068 : : casts. That will let us do much better in propagating the inferred
1069 : : constant at the switch target. */
1070 : 103563 : tree cond = gimple_switch_index (stmt);
1071 : 103563 : if (TREE_CODE (cond) == SSA_NAME)
1072 : : {
1073 : 103562 : gimple *def_stmt = SSA_NAME_DEF_STMT (cond);
1074 : 103562 : if (gimple_assign_cast_p (def_stmt))
1075 : : {
1076 : 13440 : tree def = gimple_assign_rhs1 (def_stmt);
1077 : 13440 : if (TREE_CODE (def) != SSA_NAME)
1078 : : return false;
1079 : :
1080 : : /* If we have an extension or sign-change that preserves the
1081 : : values we check against then we can copy the source value into
1082 : : the switch. */
1083 : 13440 : tree ti = TREE_TYPE (def);
1084 : 13440 : if (INTEGRAL_TYPE_P (ti)
1085 : 13440 : && TYPE_PRECISION (ti) <= TYPE_PRECISION (TREE_TYPE (cond)))
1086 : : {
1087 : 13196 : size_t n = gimple_switch_num_labels (stmt);
1088 : 13196 : tree min = NULL_TREE, max = NULL_TREE;
1089 : 13196 : if (n > 1)
1090 : : {
1091 : 13196 : min = CASE_LOW (gimple_switch_label (stmt, 1));
1092 : 13196 : if (CASE_HIGH (gimple_switch_label (stmt, n - 1)))
1093 : 1104 : max = CASE_HIGH (gimple_switch_label (stmt, n - 1));
1094 : : else
1095 : 12092 : max = CASE_LOW (gimple_switch_label (stmt, n - 1));
1096 : : }
1097 : 13196 : if ((!min || int_fits_type_p (min, ti))
1098 : 13192 : && (!max || int_fits_type_p (max, ti)))
1099 : : {
1100 : 12998 : gimple_switch_set_index (stmt, def);
1101 : 12998 : simplify_gimple_switch_label_vec (stmt, ti,
1102 : : edges_to_remove);
1103 : 12998 : update_stmt (stmt);
1104 : 12998 : return true;
1105 : : }
1106 : : }
1107 : : }
1108 : : }
1109 : :
1110 : : return false;
1111 : : }
1112 : :
1113 : : /* For pointers p2 and p1 return p2 - p1 if the
1114 : : difference is known and constant, otherwise return NULL. */
1115 : :
1116 : : static tree
1117 : 5048 : constant_pointer_difference (tree p1, tree p2)
1118 : : {
1119 : 5048 : int i, j;
1120 : : #define CPD_ITERATIONS 5
1121 : 5048 : tree exps[2][CPD_ITERATIONS];
1122 : 5048 : tree offs[2][CPD_ITERATIONS];
1123 : 5048 : int cnt[2];
1124 : :
1125 : 15144 : for (i = 0; i < 2; i++)
1126 : : {
1127 : 10096 : tree p = i ? p1 : p2;
1128 : 10096 : tree off = size_zero_node;
1129 : 10096 : gimple *stmt;
1130 : 10096 : enum tree_code code;
1131 : :
1132 : : /* For each of p1 and p2 we need to iterate at least
1133 : : twice, to handle ADDR_EXPR directly in p1/p2,
1134 : : SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc.
1135 : : on definition's stmt RHS. Iterate a few extra times. */
1136 : 10096 : j = 0;
1137 : 11863 : do
1138 : : {
1139 : 11863 : if (!POINTER_TYPE_P (TREE_TYPE (p)))
1140 : : break;
1141 : 11857 : if (TREE_CODE (p) == ADDR_EXPR)
1142 : : {
1143 : 8708 : tree q = TREE_OPERAND (p, 0);
1144 : 8708 : poly_int64 offset;
1145 : 8708 : tree base = get_addr_base_and_unit_offset (q, &offset);
1146 : 8708 : if (base)
1147 : : {
1148 : 7982 : q = base;
1149 : 7982 : if (maybe_ne (offset, 0))
1150 : 3323 : off = size_binop (PLUS_EXPR, off, size_int (offset));
1151 : : }
1152 : 8708 : if (TREE_CODE (q) == MEM_REF
1153 : 8708 : && TREE_CODE (TREE_OPERAND (q, 0)) == SSA_NAME)
1154 : : {
1155 : 213 : p = TREE_OPERAND (q, 0);
1156 : 213 : off = size_binop (PLUS_EXPR, off,
1157 : : wide_int_to_tree (sizetype,
1158 : : mem_ref_offset (q)));
1159 : : }
1160 : : else
1161 : : {
1162 : 8495 : exps[i][j] = q;
1163 : 8495 : offs[i][j++] = off;
1164 : 8495 : break;
1165 : : }
1166 : : }
1167 : 3362 : if (TREE_CODE (p) != SSA_NAME)
1168 : : break;
1169 : 3362 : exps[i][j] = p;
1170 : 3362 : offs[i][j++] = off;
1171 : 3362 : if (j == CPD_ITERATIONS)
1172 : : break;
1173 : 3362 : stmt = SSA_NAME_DEF_STMT (p);
1174 : 3362 : if (!is_gimple_assign (stmt) || gimple_assign_lhs (stmt) != p)
1175 : : break;
1176 : 2565 : code = gimple_assign_rhs_code (stmt);
1177 : 2565 : if (code == POINTER_PLUS_EXPR)
1178 : : {
1179 : 1323 : if (TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
1180 : : break;
1181 : 872 : off = size_binop (PLUS_EXPR, off, gimple_assign_rhs2 (stmt));
1182 : 872 : p = gimple_assign_rhs1 (stmt);
1183 : : }
1184 : 1242 : else if (code == ADDR_EXPR || CONVERT_EXPR_CODE_P (code))
1185 : 895 : p = gimple_assign_rhs1 (stmt);
1186 : : else
1187 : : break;
1188 : : }
1189 : : while (1);
1190 : 10096 : cnt[i] = j;
1191 : : }
1192 : :
1193 : 6948 : for (i = 0; i < cnt[0]; i++)
1194 : 9122 : for (j = 0; j < cnt[1]; j++)
1195 : 7222 : if (exps[0][i] == exps[1][j])
1196 : 4212 : return size_binop (MINUS_EXPR, offs[0][i], offs[1][j]);
1197 : :
1198 : : return NULL_TREE;
1199 : : }
1200 : :
1201 : : /* *GSI_P is a GIMPLE_CALL to a builtin function.
1202 : : Optimize
1203 : : memcpy (p, "abcd", 4);
1204 : : memset (p + 4, ' ', 3);
1205 : : into
1206 : : memcpy (p, "abcd ", 7);
1207 : : call if the latter can be stored by pieces during expansion.
1208 : :
1209 : : Optimize
1210 : : memchr ("abcd", a, 4) == 0;
1211 : : or
1212 : : memchr ("abcd", a, 4) != 0;
1213 : : to
1214 : : (a == 'a' || a == 'b' || a == 'c' || a == 'd') == 0
1215 : : or
1216 : : (a == 'a' || a == 'b' || a == 'c' || a == 'd') != 0
1217 : :
1218 : : Also canonicalize __atomic_fetch_op (p, x, y) op x
1219 : : to __atomic_op_fetch (p, x, y) or
1220 : : __atomic_op_fetch (p, x, y) iop x
1221 : : to __atomic_fetch_op (p, x, y) when possible (also __sync). */
1222 : :
1223 : : static bool
1224 : 5638694 : simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2)
1225 : : {
1226 : 5638694 : gimple *stmt1, *stmt2 = gsi_stmt (*gsi_p);
1227 : 5638694 : enum built_in_function other_atomic = END_BUILTINS;
1228 : 5638694 : enum tree_code atomic_op = ERROR_MARK;
1229 : 11274903 : tree vuse = gimple_vuse (stmt2);
1230 : 5638694 : if (vuse == NULL)
1231 : : return false;
1232 : 4698428 : stmt1 = SSA_NAME_DEF_STMT (vuse);
1233 : :
1234 : 4698428 : tree res;
1235 : :
1236 : 4698428 : switch (DECL_FUNCTION_CODE (callee2))
1237 : : {
1238 : 8578 : case BUILT_IN_MEMCHR:
1239 : 8578 : if (gimple_call_num_args (stmt2) == 3
1240 : 8578 : && (res = gimple_call_lhs (stmt2)) != nullptr
1241 : 8578 : && use_in_zero_equality (res) != nullptr
1242 : : && CHAR_BIT == 8
1243 : 8578 : && BITS_PER_UNIT == 8)
1244 : : {
1245 : 1053 : tree ptr = gimple_call_arg (stmt2, 0);
1246 : 1053 : if (TREE_CODE (ptr) != ADDR_EXPR
1247 : 1053 : || TREE_CODE (TREE_OPERAND (ptr, 0)) != STRING_CST)
1248 : : break;
1249 : 145 : unsigned HOST_WIDE_INT slen
1250 : 145 : = TREE_STRING_LENGTH (TREE_OPERAND (ptr, 0));
1251 : : /* It must be a non-empty string constant. */
1252 : 145 : if (slen < 2)
1253 : : break;
1254 : : /* For -Os, only simplify strings with a single character. */
1255 : 141 : if (!optimize_bb_for_speed_p (gimple_bb (stmt2))
1256 : 141 : && slen > 2)
1257 : : break;
1258 : 125 : tree size = gimple_call_arg (stmt2, 2);
1259 : : /* Size must be a constant which is <= UNITS_PER_WORD and
1260 : : <= the string length. */
1261 : 125 : if (TREE_CODE (size) != INTEGER_CST)
1262 : : break;
1263 : :
1264 : 125 : if (!tree_fits_uhwi_p (size))
1265 : : break;
1266 : :
1267 : 125 : unsigned HOST_WIDE_INT sz = tree_to_uhwi (size);
1268 : 126 : if (sz == 0 || sz > UNITS_PER_WORD || sz >= slen)
1269 : : break;
1270 : :
1271 : 73 : tree ch = gimple_call_arg (stmt2, 1);
1272 : 73 : location_t loc = gimple_location (stmt2);
1273 : 73 : if (!useless_type_conversion_p (char_type_node,
1274 : 73 : TREE_TYPE (ch)))
1275 : 73 : ch = fold_convert_loc (loc, char_type_node, ch);
1276 : 73 : const char *p = TREE_STRING_POINTER (TREE_OPERAND (ptr, 0));
1277 : 73 : unsigned int isize = sz;
1278 : 73 : tree *op = XALLOCAVEC (tree, isize);
1279 : 315 : for (unsigned int i = 0; i < isize; i++)
1280 : : {
1281 : 242 : op[i] = build_int_cst (char_type_node, p[i]);
1282 : 242 : op[i] = fold_build2_loc (loc, EQ_EXPR, boolean_type_node,
1283 : : op[i], ch);
1284 : : }
1285 : 242 : for (unsigned int i = isize - 1; i >= 1; i--)
1286 : 169 : op[i - 1] = fold_convert_loc (loc, boolean_type_node,
1287 : : fold_build2_loc (loc,
1288 : : BIT_IOR_EXPR,
1289 : : boolean_type_node,
1290 : 169 : op[i - 1],
1291 : 169 : op[i]));
1292 : 73 : res = fold_convert_loc (loc, TREE_TYPE (res), op[0]);
1293 : 73 : gimplify_and_update_call_from_tree (gsi_p, res);
1294 : 73 : return true;
1295 : : }
1296 : : break;
1297 : :
1298 : 103866 : case BUILT_IN_MEMSET:
1299 : 103866 : if (gimple_call_num_args (stmt2) != 3
1300 : 103866 : || gimple_call_lhs (stmt2)
1301 : : || CHAR_BIT != 8
1302 : 207732 : || BITS_PER_UNIT != 8)
1303 : : break;
1304 : : else
1305 : : {
1306 : 97160 : tree callee1;
1307 : 97160 : tree ptr1, src1, str1, off1, len1, lhs1;
1308 : 97160 : tree ptr2 = gimple_call_arg (stmt2, 0);
1309 : 97160 : tree val2 = gimple_call_arg (stmt2, 1);
1310 : 97160 : tree len2 = gimple_call_arg (stmt2, 2);
1311 : 97160 : tree diff, vdef, new_str_cst;
1312 : 97160 : gimple *use_stmt;
1313 : 97160 : unsigned int ptr1_align;
1314 : 97160 : unsigned HOST_WIDE_INT src_len;
1315 : 97160 : char *src_buf;
1316 : 97160 : use_operand_p use_p;
1317 : :
1318 : 97160 : if (!tree_fits_shwi_p (val2)
1319 : 93386 : || !tree_fits_uhwi_p (len2)
1320 : 155903 : || compare_tree_int (len2, 1024) == 1)
1321 : : break;
1322 : 53973 : if (is_gimple_call (stmt1))
1323 : : {
1324 : : /* If first stmt is a call, it needs to be memcpy
1325 : : or mempcpy, with string literal as second argument and
1326 : : constant length. */
1327 : 28292 : callee1 = gimple_call_fndecl (stmt1);
1328 : 28292 : if (callee1 == NULL_TREE
1329 : 28185 : || !fndecl_built_in_p (callee1, BUILT_IN_NORMAL)
1330 : 53216 : || gimple_call_num_args (stmt1) != 3)
1331 : : break;
1332 : 23682 : if (DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMCPY
1333 : 23682 : && DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMPCPY)
1334 : : break;
1335 : 10521 : ptr1 = gimple_call_arg (stmt1, 0);
1336 : 10521 : src1 = gimple_call_arg (stmt1, 1);
1337 : 10521 : len1 = gimple_call_arg (stmt1, 2);
1338 : 10521 : lhs1 = gimple_call_lhs (stmt1);
1339 : 10521 : if (!tree_fits_uhwi_p (len1))
1340 : : break;
1341 : 10437 : str1 = string_constant (src1, &off1, NULL, NULL);
1342 : 10437 : if (str1 == NULL_TREE)
1343 : : break;
1344 : 4706 : if (!tree_fits_uhwi_p (off1)
1345 : 4706 : || compare_tree_int (off1, TREE_STRING_LENGTH (str1) - 1) > 0
1346 : 4706 : || compare_tree_int (len1, TREE_STRING_LENGTH (str1)
1347 : 4706 : - tree_to_uhwi (off1)) > 0
1348 : 4706 : || TREE_CODE (TREE_TYPE (str1)) != ARRAY_TYPE
1349 : 14118 : || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1)))
1350 : 4706 : != TYPE_MODE (char_type_node))
1351 : : break;
1352 : : }
1353 : 25681 : else if (gimple_assign_single_p (stmt1))
1354 : : {
1355 : : /* Otherwise look for length 1 memcpy optimized into
1356 : : assignment. */
1357 : 15916 : ptr1 = gimple_assign_lhs (stmt1);
1358 : 15916 : src1 = gimple_assign_rhs1 (stmt1);
1359 : 15916 : if (TREE_CODE (ptr1) != MEM_REF
1360 : 2691 : || TYPE_MODE (TREE_TYPE (ptr1)) != TYPE_MODE (char_type_node)
1361 : 16654 : || !tree_fits_shwi_p (src1))
1362 : : break;
1363 : 335 : ptr1 = build_fold_addr_expr (ptr1);
1364 : 335 : STRIP_USELESS_TYPE_CONVERSION (ptr1);
1365 : 335 : callee1 = NULL_TREE;
1366 : 335 : len1 = size_one_node;
1367 : 335 : lhs1 = NULL_TREE;
1368 : 335 : off1 = size_zero_node;
1369 : 335 : str1 = NULL_TREE;
1370 : : }
1371 : : else
1372 : : break;
1373 : :
1374 : 5041 : diff = constant_pointer_difference (ptr1, ptr2);
1375 : 5041 : if (diff == NULL && lhs1 != NULL)
1376 : : {
1377 : 7 : diff = constant_pointer_difference (lhs1, ptr2);
1378 : 7 : if (DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
1379 : 7 : && diff != NULL)
1380 : 7 : diff = size_binop (PLUS_EXPR, diff,
1381 : : fold_convert (sizetype, len1));
1382 : : }
1383 : : /* If the difference between the second and first destination pointer
1384 : : is not constant, or is bigger than memcpy length, bail out. */
1385 : 5041 : if (diff == NULL
1386 : 4212 : || !tree_fits_uhwi_p (diff)
1387 : 4212 : || tree_int_cst_lt (len1, diff)
1388 : 9015 : || compare_tree_int (diff, 1024) == 1)
1389 : : break;
1390 : :
1391 : : /* Use maximum of difference plus memset length and memcpy length
1392 : : as the new memcpy length, if it is too big, bail out. */
1393 : 3974 : src_len = tree_to_uhwi (diff);
1394 : 3974 : src_len += tree_to_uhwi (len2);
1395 : 3974 : if (src_len < tree_to_uhwi (len1))
1396 : : src_len = tree_to_uhwi (len1);
1397 : 3974 : if (src_len > 1024)
1398 : : break;
1399 : :
1400 : : /* If mempcpy value is used elsewhere, bail out, as mempcpy
1401 : : with bigger length will return different result. */
1402 : 3974 : if (lhs1 != NULL_TREE
1403 : 187 : && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
1404 : 3981 : && (TREE_CODE (lhs1) != SSA_NAME
1405 : 7 : || !single_imm_use (lhs1, &use_p, &use_stmt)
1406 : 7 : || use_stmt != stmt2))
1407 : : break;
1408 : :
1409 : : /* If anything reads memory in between memcpy and memset
1410 : : call, the modified memcpy call might change it. */
1411 : 3974 : vdef = gimple_vdef (stmt1);
1412 : 3974 : if (vdef != NULL
1413 : 3974 : && (!single_imm_use (vdef, &use_p, &use_stmt)
1414 : 3245 : || use_stmt != stmt2))
1415 : : break;
1416 : :
1417 : 3245 : ptr1_align = get_pointer_alignment (ptr1);
1418 : : /* Construct the new source string literal. */
1419 : 3245 : src_buf = XALLOCAVEC (char, src_len + 1);
1420 : 3245 : if (callee1)
1421 : 3123 : memcpy (src_buf,
1422 : 3123 : TREE_STRING_POINTER (str1) + tree_to_uhwi (off1),
1423 : : tree_to_uhwi (len1));
1424 : : else
1425 : 122 : src_buf[0] = tree_to_shwi (src1);
1426 : 3245 : memset (src_buf + tree_to_uhwi (diff),
1427 : 3245 : tree_to_shwi (val2), tree_to_uhwi (len2));
1428 : 3245 : src_buf[src_len] = '\0';
1429 : : /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
1430 : : handle embedded '\0's. */
1431 : 3245 : if (strlen (src_buf) != src_len)
1432 : : break;
1433 : 3172 : rtl_profile_for_bb (gimple_bb (stmt2));
1434 : : /* If the new memcpy wouldn't be emitted by storing the literal
1435 : : by pieces, this optimization might enlarge .rodata too much,
1436 : : as commonly used string literals couldn't be shared any
1437 : : longer. */
1438 : 3172 : if (!can_store_by_pieces (src_len,
1439 : : builtin_strncpy_read_str,
1440 : : src_buf, ptr1_align, false))
1441 : : break;
1442 : :
1443 : 2412 : new_str_cst = build_string_literal (src_len, src_buf);
1444 : 2412 : if (callee1)
1445 : : {
1446 : : /* If STMT1 is a mem{,p}cpy call, adjust it and remove
1447 : : memset call. */
1448 : 2307 : if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
1449 : 7 : gimple_call_set_lhs (stmt1, NULL_TREE);
1450 : 2307 : gimple_call_set_arg (stmt1, 1, new_str_cst);
1451 : 2307 : gimple_call_set_arg (stmt1, 2,
1452 : 2307 : build_int_cst (TREE_TYPE (len1), src_len));
1453 : 2307 : update_stmt (stmt1);
1454 : 2307 : unlink_stmt_vdef (stmt2);
1455 : 2307 : gsi_replace (gsi_p, gimple_build_nop (), false);
1456 : 2307 : fwprop_invalidate_lattice (gimple_get_lhs (stmt2));
1457 : 2307 : release_defs (stmt2);
1458 : 2307 : if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
1459 : : {
1460 : 7 : fwprop_invalidate_lattice (lhs1);
1461 : 7 : release_ssa_name (lhs1);
1462 : : }
1463 : 2412 : return true;
1464 : : }
1465 : : else
1466 : : {
1467 : : /* Otherwise, if STMT1 is length 1 memcpy optimized into
1468 : : assignment, remove STMT1 and change memset call into
1469 : : memcpy call. */
1470 : 105 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt1);
1471 : :
1472 : 105 : if (!is_gimple_val (ptr1))
1473 : 12 : ptr1 = force_gimple_operand_gsi (gsi_p, ptr1, true, NULL_TREE,
1474 : : true, GSI_SAME_STMT);
1475 : 105 : tree fndecl = builtin_decl_explicit (BUILT_IN_MEMCPY);
1476 : 105 : gimple_call_set_fndecl (stmt2, fndecl);
1477 : 105 : gimple_call_set_fntype (as_a <gcall *> (stmt2),
1478 : 105 : TREE_TYPE (fndecl));
1479 : 105 : gimple_call_set_arg (stmt2, 0, ptr1);
1480 : 105 : gimple_call_set_arg (stmt2, 1, new_str_cst);
1481 : 105 : gimple_call_set_arg (stmt2, 2,
1482 : 105 : build_int_cst (TREE_TYPE (len2), src_len));
1483 : 105 : unlink_stmt_vdef (stmt1);
1484 : 105 : gsi_remove (&gsi, true);
1485 : 105 : fwprop_invalidate_lattice (gimple_get_lhs (stmt1));
1486 : 105 : release_defs (stmt1);
1487 : 105 : update_stmt (stmt2);
1488 : 105 : return false;
1489 : : }
1490 : : }
1491 : : break;
1492 : :
1493 : : #define CASE_ATOMIC(NAME, OTHER, OP) \
1494 : : case BUILT_IN_##NAME##_1: \
1495 : : case BUILT_IN_##NAME##_2: \
1496 : : case BUILT_IN_##NAME##_4: \
1497 : : case BUILT_IN_##NAME##_8: \
1498 : : case BUILT_IN_##NAME##_16: \
1499 : : atomic_op = OP; \
1500 : : other_atomic \
1501 : : = (enum built_in_function) (BUILT_IN_##OTHER##_1 \
1502 : : + (DECL_FUNCTION_CODE (callee2) \
1503 : : - BUILT_IN_##NAME##_1)); \
1504 : : goto handle_atomic_fetch_op;
1505 : :
1506 : 51055 : CASE_ATOMIC (ATOMIC_FETCH_ADD, ATOMIC_ADD_FETCH, PLUS_EXPR)
1507 : 8515 : CASE_ATOMIC (ATOMIC_FETCH_SUB, ATOMIC_SUB_FETCH, MINUS_EXPR)
1508 : 2856 : CASE_ATOMIC (ATOMIC_FETCH_AND, ATOMIC_AND_FETCH, BIT_AND_EXPR)
1509 : 2875 : CASE_ATOMIC (ATOMIC_FETCH_XOR, ATOMIC_XOR_FETCH, BIT_XOR_EXPR)
1510 : 3885 : CASE_ATOMIC (ATOMIC_FETCH_OR, ATOMIC_OR_FETCH, BIT_IOR_EXPR)
1511 : :
1512 : 2355 : CASE_ATOMIC (SYNC_FETCH_AND_ADD, SYNC_ADD_AND_FETCH, PLUS_EXPR)
1513 : 1996 : CASE_ATOMIC (SYNC_FETCH_AND_SUB, SYNC_SUB_AND_FETCH, MINUS_EXPR)
1514 : 1868 : CASE_ATOMIC (SYNC_FETCH_AND_AND, SYNC_AND_AND_FETCH, BIT_AND_EXPR)
1515 : 2136 : CASE_ATOMIC (SYNC_FETCH_AND_XOR, SYNC_XOR_AND_FETCH, BIT_XOR_EXPR)
1516 : 1979 : CASE_ATOMIC (SYNC_FETCH_AND_OR, SYNC_OR_AND_FETCH, BIT_IOR_EXPR)
1517 : :
1518 : 13899 : CASE_ATOMIC (ATOMIC_ADD_FETCH, ATOMIC_FETCH_ADD, MINUS_EXPR)
1519 : 9025 : CASE_ATOMIC (ATOMIC_SUB_FETCH, ATOMIC_FETCH_SUB, PLUS_EXPR)
1520 : 2366 : CASE_ATOMIC (ATOMIC_XOR_FETCH, ATOMIC_FETCH_XOR, BIT_XOR_EXPR)
1521 : :
1522 : 821 : CASE_ATOMIC (SYNC_ADD_AND_FETCH, SYNC_FETCH_AND_ADD, MINUS_EXPR)
1523 : 732 : CASE_ATOMIC (SYNC_SUB_AND_FETCH, SYNC_FETCH_AND_SUB, PLUS_EXPR)
1524 : 800 : CASE_ATOMIC (SYNC_XOR_AND_FETCH, SYNC_FETCH_AND_XOR, BIT_XOR_EXPR)
1525 : :
1526 : : #undef CASE_ATOMIC
1527 : :
1528 : 107163 : handle_atomic_fetch_op:
1529 : 107163 : if (gimple_call_num_args (stmt2) >= 2 && gimple_call_lhs (stmt2))
1530 : : {
1531 : 60460 : tree lhs2 = gimple_call_lhs (stmt2), lhsc = lhs2;
1532 : 60460 : tree arg = gimple_call_arg (stmt2, 1);
1533 : 60460 : gimple *use_stmt, *cast_stmt = NULL;
1534 : 60460 : use_operand_p use_p;
1535 : 60460 : tree ndecl = builtin_decl_explicit (other_atomic);
1536 : :
1537 : 60460 : if (ndecl == NULL_TREE || !single_imm_use (lhs2, &use_p, &use_stmt))
1538 : : break;
1539 : :
1540 : 57678 : if (gimple_assign_cast_p (use_stmt))
1541 : : {
1542 : 30223 : cast_stmt = use_stmt;
1543 : 30223 : lhsc = gimple_assign_lhs (cast_stmt);
1544 : 30223 : if (lhsc == NULL_TREE
1545 : 30223 : || !INTEGRAL_TYPE_P (TREE_TYPE (lhsc))
1546 : 29608 : || (TYPE_PRECISION (TREE_TYPE (lhsc))
1547 : 29608 : != TYPE_PRECISION (TREE_TYPE (lhs2)))
1548 : 58305 : || !single_imm_use (lhsc, &use_p, &use_stmt))
1549 : : {
1550 : 2759 : use_stmt = cast_stmt;
1551 : 2759 : cast_stmt = NULL;
1552 : 2759 : lhsc = lhs2;
1553 : : }
1554 : : }
1555 : :
1556 : 57678 : bool ok = false;
1557 : 57678 : tree oarg = NULL_TREE;
1558 : 57678 : enum tree_code ccode = ERROR_MARK;
1559 : 57678 : tree crhs1 = NULL_TREE, crhs2 = NULL_TREE;
1560 : 57678 : if (is_gimple_assign (use_stmt)
1561 : 57678 : && gimple_assign_rhs_code (use_stmt) == atomic_op)
1562 : : {
1563 : 1416 : if (gimple_assign_rhs1 (use_stmt) == lhsc)
1564 : 1025 : oarg = gimple_assign_rhs2 (use_stmt);
1565 : 391 : else if (atomic_op != MINUS_EXPR)
1566 : : oarg = gimple_assign_rhs1 (use_stmt);
1567 : : }
1568 : 56262 : else if (atomic_op == MINUS_EXPR
1569 : 12932 : && is_gimple_assign (use_stmt)
1570 : 3614 : && gimple_assign_rhs_code (use_stmt) == PLUS_EXPR
1571 : 195 : && TREE_CODE (arg) == INTEGER_CST
1572 : 56457 : && (TREE_CODE (gimple_assign_rhs2 (use_stmt))
1573 : : == INTEGER_CST))
1574 : : {
1575 : 179 : tree a = fold_convert (TREE_TYPE (lhs2), arg);
1576 : 179 : tree o = fold_convert (TREE_TYPE (lhs2),
1577 : : gimple_assign_rhs2 (use_stmt));
1578 : 179 : if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
1579 : : ok = true;
1580 : : }
1581 : 56083 : else if (atomic_op == BIT_AND_EXPR || atomic_op == BIT_IOR_EXPR)
1582 : : ;
1583 : 50897 : else if (gimple_code (use_stmt) == GIMPLE_COND)
1584 : : {
1585 : 19518 : ccode = gimple_cond_code (use_stmt);
1586 : 19518 : crhs1 = gimple_cond_lhs (use_stmt);
1587 : 19518 : crhs2 = gimple_cond_rhs (use_stmt);
1588 : : }
1589 : 31379 : else if (is_gimple_assign (use_stmt))
1590 : : {
1591 : 9478 : if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
1592 : : {
1593 : 3939 : ccode = gimple_assign_rhs_code (use_stmt);
1594 : 3939 : crhs1 = gimple_assign_rhs1 (use_stmt);
1595 : 3939 : crhs2 = gimple_assign_rhs2 (use_stmt);
1596 : : }
1597 : 5539 : else if (gimple_assign_rhs_code (use_stmt) == COND_EXPR)
1598 : : {
1599 : 0 : tree cond = gimple_assign_rhs1 (use_stmt);
1600 : 0 : if (COMPARISON_CLASS_P (cond))
1601 : : {
1602 : 0 : ccode = TREE_CODE (cond);
1603 : 0 : crhs1 = TREE_OPERAND (cond, 0);
1604 : 0 : crhs2 = TREE_OPERAND (cond, 1);
1605 : : }
1606 : : }
1607 : : }
1608 : 24482 : if (ccode == EQ_EXPR || ccode == NE_EXPR)
1609 : : {
1610 : : /* Deal with x - y == 0 or x ^ y == 0
1611 : : being optimized into x == y and x + cst == 0
1612 : : into x == -cst. */
1613 : 22013 : tree o = NULL_TREE;
1614 : 22013 : if (crhs1 == lhsc)
1615 : : o = crhs2;
1616 : 133 : else if (crhs2 == lhsc)
1617 : 133 : o = crhs1;
1618 : 22013 : if (o && atomic_op != PLUS_EXPR)
1619 : : oarg = o;
1620 : 10086 : else if (o
1621 : 10086 : && TREE_CODE (o) == INTEGER_CST
1622 : 10086 : && TREE_CODE (arg) == INTEGER_CST)
1623 : : {
1624 : 9398 : tree a = fold_convert (TREE_TYPE (lhs2), arg);
1625 : 9398 : o = fold_convert (TREE_TYPE (lhs2), o);
1626 : 9398 : if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
1627 : 57678 : ok = true;
1628 : : }
1629 : : }
1630 : 57678 : if (oarg && !ok)
1631 : : {
1632 : 13343 : if (operand_equal_p (arg, oarg, 0))
1633 : : ok = true;
1634 : 12189 : else if (TREE_CODE (arg) == SSA_NAME
1635 : 2371 : && TREE_CODE (oarg) == SSA_NAME)
1636 : : {
1637 : 937 : tree oarg2 = oarg;
1638 : 937 : if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (oarg)))
1639 : : {
1640 : 104 : gimple *g = SSA_NAME_DEF_STMT (oarg);
1641 : 104 : oarg2 = gimple_assign_rhs1 (g);
1642 : 104 : if (TREE_CODE (oarg2) != SSA_NAME
1643 : 104 : || !INTEGRAL_TYPE_P (TREE_TYPE (oarg2))
1644 : 208 : || (TYPE_PRECISION (TREE_TYPE (oarg2))
1645 : 104 : != TYPE_PRECISION (TREE_TYPE (oarg))))
1646 : : oarg2 = oarg;
1647 : : }
1648 : 937 : if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg)))
1649 : : {
1650 : 856 : gimple *g = SSA_NAME_DEF_STMT (arg);
1651 : 856 : tree rhs1 = gimple_assign_rhs1 (g);
1652 : : /* Handle e.g.
1653 : : x.0_1 = (long unsigned int) x_4(D);
1654 : : _2 = __atomic_fetch_add_8 (&vlong, x.0_1, 0);
1655 : : _3 = (long int) _2;
1656 : : _7 = x_4(D) + _3; */
1657 : 856 : if (rhs1 == oarg || rhs1 == oarg2)
1658 : : ok = true;
1659 : : /* Handle e.g.
1660 : : x.18_1 = (short unsigned int) x_5(D);
1661 : : _2 = (int) x.18_1;
1662 : : _3 = __atomic_fetch_xor_2 (&vshort, _2, 0);
1663 : : _4 = (short int) _3;
1664 : : _8 = x_5(D) ^ _4;
1665 : : This happens only for char/short. */
1666 : 472 : else if (TREE_CODE (rhs1) == SSA_NAME
1667 : 472 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1668 : 944 : && (TYPE_PRECISION (TREE_TYPE (rhs1))
1669 : 472 : == TYPE_PRECISION (TREE_TYPE (lhs2))))
1670 : : {
1671 : 472 : g = SSA_NAME_DEF_STMT (rhs1);
1672 : 472 : if (gimple_assign_cast_p (g)
1673 : 472 : && (gimple_assign_rhs1 (g) == oarg
1674 : 104 : || gimple_assign_rhs1 (g) == oarg2))
1675 : : ok = true;
1676 : : }
1677 : : }
1678 : 937 : if (!ok && arg == oarg2)
1679 : : /* Handle e.g.
1680 : : _1 = __sync_fetch_and_add_4 (&v, x_5(D));
1681 : : _2 = (int) _1;
1682 : : x.0_3 = (int) x_5(D);
1683 : : _7 = _2 + x.0_3; */
1684 : : ok = true;
1685 : : }
1686 : : }
1687 : :
1688 : 56524 : if (ok)
1689 : : {
1690 : 2249 : tree new_lhs = make_ssa_name (TREE_TYPE (lhs2));
1691 : 2249 : gimple_call_set_lhs (stmt2, new_lhs);
1692 : 2249 : gimple_call_set_fndecl (stmt2, ndecl);
1693 : 2249 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1694 : 2249 : if (ccode == ERROR_MARK)
1695 : 1992 : gimple_assign_set_rhs_with_ops (&gsi, cast_stmt
1696 : : ? NOP_EXPR : SSA_NAME,
1697 : : new_lhs);
1698 : : else
1699 : : {
1700 : 1030 : crhs1 = new_lhs;
1701 : 1030 : crhs2 = build_zero_cst (TREE_TYPE (lhs2));
1702 : 1030 : if (gimple_code (use_stmt) == GIMPLE_COND)
1703 : : {
1704 : 677 : gcond *cond_stmt = as_a <gcond *> (use_stmt);
1705 : 677 : gimple_cond_set_lhs (cond_stmt, crhs1);
1706 : 677 : gimple_cond_set_rhs (cond_stmt, crhs2);
1707 : : }
1708 : 353 : else if (gimple_assign_rhs_class (use_stmt)
1709 : : == GIMPLE_BINARY_RHS)
1710 : : {
1711 : 353 : gimple_assign_set_rhs1 (use_stmt, crhs1);
1712 : 353 : gimple_assign_set_rhs2 (use_stmt, crhs2);
1713 : : }
1714 : : else
1715 : : {
1716 : 0 : gcc_checking_assert (gimple_assign_rhs_code (use_stmt)
1717 : : == COND_EXPR);
1718 : 0 : tree cond = build2 (ccode, boolean_type_node,
1719 : : crhs1, crhs2);
1720 : 0 : gimple_assign_set_rhs1 (use_stmt, cond);
1721 : : }
1722 : : }
1723 : 2249 : update_stmt (use_stmt);
1724 : 2249 : if (atomic_op != BIT_AND_EXPR
1725 : 2249 : && atomic_op != BIT_IOR_EXPR
1726 : 2249 : && !stmt_ends_bb_p (stmt2))
1727 : : {
1728 : : /* For the benefit of debug stmts, emit stmt(s) to set
1729 : : lhs2 to the value it had from the new builtin.
1730 : : E.g. if it was previously:
1731 : : lhs2 = __atomic_fetch_add_8 (ptr, arg, 0);
1732 : : emit:
1733 : : new_lhs = __atomic_add_fetch_8 (ptr, arg, 0);
1734 : : lhs2 = new_lhs - arg;
1735 : : We also keep cast_stmt if any in the IL for
1736 : : the same reasons.
1737 : : These stmts will be DCEd later and proper debug info
1738 : : will be emitted.
1739 : : This is only possible for reversible operations
1740 : : (+/-/^) and without -fnon-call-exceptions. */
1741 : 1908 : gsi = gsi_for_stmt (stmt2);
1742 : 1908 : tree type = TREE_TYPE (lhs2);
1743 : 1908 : if (TREE_CODE (arg) == INTEGER_CST)
1744 : 1326 : arg = fold_convert (type, arg);
1745 : 582 : else if (!useless_type_conversion_p (type, TREE_TYPE (arg)))
1746 : : {
1747 : 288 : tree narg = make_ssa_name (type);
1748 : 288 : gimple *g = gimple_build_assign (narg, NOP_EXPR, arg);
1749 : 288 : gsi_insert_after (&gsi, g, GSI_NEW_STMT);
1750 : 288 : arg = narg;
1751 : : }
1752 : 1908 : enum tree_code rcode;
1753 : 1908 : switch (atomic_op)
1754 : : {
1755 : : case PLUS_EXPR: rcode = MINUS_EXPR; break;
1756 : 740 : case MINUS_EXPR: rcode = PLUS_EXPR; break;
1757 : 492 : case BIT_XOR_EXPR: rcode = atomic_op; break;
1758 : 0 : default: gcc_unreachable ();
1759 : : }
1760 : 1908 : gimple *g = gimple_build_assign (lhs2, rcode, new_lhs, arg);
1761 : 1908 : gsi_insert_after (&gsi, g, GSI_NEW_STMT);
1762 : 1908 : update_stmt (stmt2);
1763 : : }
1764 : : else
1765 : : {
1766 : : /* For e.g.
1767 : : lhs2 = __atomic_fetch_or_8 (ptr, arg, 0);
1768 : : after we change it to
1769 : : new_lhs = __atomic_or_fetch_8 (ptr, arg, 0);
1770 : : there is no way to find out the lhs2 value (i.e.
1771 : : what the atomic memory contained before the operation),
1772 : : values of some bits are lost. We have checked earlier
1773 : : that we don't have any non-debug users except for what
1774 : : we are already changing, so we need to reset the
1775 : : debug stmts and remove the cast_stmt if any. */
1776 : 341 : imm_use_iterator iter;
1777 : 676 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs2)
1778 : 335 : if (use_stmt != cast_stmt)
1779 : : {
1780 : 168 : gcc_assert (is_gimple_debug (use_stmt));
1781 : 168 : gimple_debug_bind_reset_value (use_stmt);
1782 : 168 : update_stmt (use_stmt);
1783 : 341 : }
1784 : 341 : if (cast_stmt)
1785 : : {
1786 : 167 : gsi = gsi_for_stmt (cast_stmt);
1787 : 167 : gsi_remove (&gsi, true);
1788 : : }
1789 : 341 : update_stmt (stmt2);
1790 : 341 : release_ssa_name (lhs2);
1791 : : }
1792 : : }
1793 : : }
1794 : : break;
1795 : :
1796 : : default:
1797 : : break;
1798 : : }
1799 : : return false;
1800 : : }
1801 : :
1802 : : /* Given a ssa_name in NAME see if it was defined by an assignment and
1803 : : set CODE to be the code and ARG1 to the first operand on the rhs and ARG2
1804 : : to the second operand on the rhs. */
1805 : :
1806 : : static inline void
1807 : 16335541 : defcodefor_name (tree name, enum tree_code *code, tree *arg1, tree *arg2)
1808 : : {
1809 : 16335541 : gimple *def;
1810 : 16335541 : enum tree_code code1;
1811 : 16335541 : tree arg11;
1812 : 16335541 : tree arg21;
1813 : 16335541 : tree arg31;
1814 : 16335541 : enum gimple_rhs_class grhs_class;
1815 : :
1816 : 16335541 : code1 = TREE_CODE (name);
1817 : 16335541 : arg11 = name;
1818 : 16335541 : arg21 = NULL_TREE;
1819 : 16335541 : arg31 = NULL_TREE;
1820 : 16335541 : grhs_class = get_gimple_rhs_class (code1);
1821 : :
1822 : 16335541 : if (code1 == SSA_NAME)
1823 : : {
1824 : 11009957 : def = SSA_NAME_DEF_STMT (name);
1825 : :
1826 : 11009957 : if (def && is_gimple_assign (def)
1827 : 17861232 : && can_propagate_from (def))
1828 : : {
1829 : 4780816 : code1 = gimple_assign_rhs_code (def);
1830 : 4780816 : arg11 = gimple_assign_rhs1 (def);
1831 : 4780816 : arg21 = gimple_assign_rhs2 (def);
1832 : 4780816 : arg31 = gimple_assign_rhs3 (def);
1833 : : }
1834 : : }
1835 : 5325584 : else if (grhs_class != GIMPLE_SINGLE_RHS)
1836 : 0 : code1 = ERROR_MARK;
1837 : :
1838 : 16335541 : *code = code1;
1839 : 16335541 : *arg1 = arg11;
1840 : 16335541 : if (arg2)
1841 : 16316587 : *arg2 = arg21;
1842 : 16335541 : if (arg31)
1843 : 2280 : *code = ERROR_MARK;
1844 : 16335541 : }
1845 : :
1846 : :
1847 : : /* Recognize rotation patterns. Return true if a transformation
1848 : : applied, otherwise return false.
1849 : :
1850 : : We are looking for X with unsigned type T with bitsize B, OP being
1851 : : +, | or ^, some type T2 wider than T. For:
1852 : : (X << CNT1) OP (X >> CNT2) iff CNT1 + CNT2 == B
1853 : : ((T) ((T2) X << CNT1)) OP ((T) ((T2) X >> CNT2)) iff CNT1 + CNT2 == B
1854 : :
1855 : : transform these into:
1856 : : X r<< CNT1
1857 : :
1858 : : Or for:
1859 : : (X << Y) OP (X >> (B - Y))
1860 : : (X << (int) Y) OP (X >> (int) (B - Y))
1861 : : ((T) ((T2) X << Y)) OP ((T) ((T2) X >> (B - Y)))
1862 : : ((T) ((T2) X << (int) Y)) OP ((T) ((T2) X >> (int) (B - Y)))
1863 : : (X << Y) | (X >> ((-Y) & (B - 1)))
1864 : : (X << (int) Y) | (X >> (int) ((-Y) & (B - 1)))
1865 : : ((T) ((T2) X << Y)) | ((T) ((T2) X >> ((-Y) & (B - 1))))
1866 : : ((T) ((T2) X << (int) Y)) | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
1867 : :
1868 : : transform these into (last 2 only if ranger can prove Y < B
1869 : : or Y = N * B):
1870 : : X r<< Y
1871 : : or
1872 : : X r<< (& & (B - 1))
1873 : : The latter for the forms with T2 wider than T if ranger can't prove Y < B.
1874 : :
1875 : : Or for:
1876 : : (X << (Y & (B - 1))) | (X >> ((-Y) & (B - 1)))
1877 : : (X << (int) (Y & (B - 1))) | (X >> (int) ((-Y) & (B - 1)))
1878 : : ((T) ((T2) X << (Y & (B - 1)))) | ((T) ((T2) X >> ((-Y) & (B - 1))))
1879 : : ((T) ((T2) X << (int) (Y & (B - 1)))) \
1880 : : | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
1881 : :
1882 : : transform these into:
1883 : : X r<< (Y & (B - 1))
1884 : :
1885 : : Note, in the patterns with T2 type, the type of OP operands
1886 : : might be even a signed type, but should have precision B.
1887 : : Expressions with & (B - 1) should be recognized only if B is
1888 : : a power of 2. */
1889 : :
1890 : : static bool
1891 : 9725880 : simplify_rotate (gimple_stmt_iterator *gsi)
1892 : : {
1893 : 9725880 : gimple *stmt = gsi_stmt (*gsi);
1894 : 9725880 : tree arg[2], rtype, rotcnt = NULL_TREE;
1895 : 9725880 : tree def_arg1[2], def_arg2[2];
1896 : 9725880 : enum tree_code def_code[2];
1897 : 9725880 : tree lhs;
1898 : 9725880 : int i;
1899 : 9725880 : bool swapped_p = false;
1900 : 9725880 : gimple *g;
1901 : 9725880 : gimple *def_arg_stmt[2] = { NULL, NULL };
1902 : 9725880 : int wider_prec = 0;
1903 : 9725880 : bool add_masking = false;
1904 : :
1905 : 9725880 : arg[0] = gimple_assign_rhs1 (stmt);
1906 : 9725880 : arg[1] = gimple_assign_rhs2 (stmt);
1907 : 9725880 : rtype = TREE_TYPE (arg[0]);
1908 : :
1909 : : /* Only create rotates in complete modes. Other cases are not
1910 : : expanded properly. */
1911 : 9725880 : if (!INTEGRAL_TYPE_P (rtype)
1912 : 9725880 : || !type_has_mode_precision_p (rtype))
1913 : 1608283 : return false;
1914 : :
1915 : 24352791 : for (i = 0; i < 2; i++)
1916 : : {
1917 : 16235194 : defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
1918 : 16235194 : if (TREE_CODE (arg[i]) == SSA_NAME)
1919 : 10909610 : def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
1920 : : }
1921 : :
1922 : : /* Look through narrowing (or same precision) conversions. */
1923 : 7206576 : if (CONVERT_EXPR_CODE_P (def_code[0])
1924 : 911021 : && CONVERT_EXPR_CODE_P (def_code[1])
1925 : 136066 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[0]))
1926 : 113716 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[1]))
1927 : 107706 : && TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
1928 : 107706 : == TYPE_PRECISION (TREE_TYPE (def_arg1[1]))
1929 : 59089 : && TYPE_PRECISION (TREE_TYPE (def_arg1[0])) >= TYPE_PRECISION (rtype)
1930 : 38830 : && has_single_use (arg[0])
1931 : 8148712 : && has_single_use (arg[1]))
1932 : : {
1933 : 26933 : wider_prec = TYPE_PRECISION (TREE_TYPE (def_arg1[0]));
1934 : 80799 : for (i = 0; i < 2; i++)
1935 : : {
1936 : 53866 : arg[i] = def_arg1[i];
1937 : 53866 : defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
1938 : 53866 : if (TREE_CODE (arg[i]) == SSA_NAME)
1939 : 53866 : def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
1940 : : }
1941 : : }
1942 : : else
1943 : : {
1944 : : /* Handle signed rotate; the RSHIFT_EXPR has to be done
1945 : : in unsigned type but LSHIFT_EXPR could be signed. */
1946 : 8090664 : i = (def_code[0] == LSHIFT_EXPR || def_code[0] == RSHIFT_EXPR);
1947 : 7189681 : if (CONVERT_EXPR_CODE_P (def_code[i])
1948 : 900983 : && (def_code[1 - i] == LSHIFT_EXPR || def_code[1 - i] == RSHIFT_EXPR)
1949 : 28385 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[i]))
1950 : 27237 : && TYPE_PRECISION (rtype) == TYPE_PRECISION (TREE_TYPE (def_arg1[i]))
1951 : 8094118 : && has_single_use (arg[i]))
1952 : : {
1953 : 2048 : arg[i] = def_arg1[i];
1954 : 2048 : defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
1955 : 2048 : if (TREE_CODE (arg[i]) == SSA_NAME)
1956 : 2048 : def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
1957 : : }
1958 : : }
1959 : :
1960 : : /* One operand has to be LSHIFT_EXPR and one RSHIFT_EXPR. */
1961 : 8330451 : for (i = 0; i < 2; i++)
1962 : 8300845 : if (def_code[i] != LSHIFT_EXPR && def_code[i] != RSHIFT_EXPR)
1963 : : return false;
1964 : 254893 : else if (!has_single_use (arg[i]))
1965 : : return false;
1966 : 29606 : if (def_code[0] == def_code[1])
1967 : : return false;
1968 : :
1969 : : /* If we've looked through narrowing conversions before, look through
1970 : : widening conversions from unsigned type with the same precision
1971 : : as rtype here. */
1972 : 23009 : if (TYPE_PRECISION (TREE_TYPE (def_arg1[0])) != TYPE_PRECISION (rtype))
1973 : 21597 : for (i = 0; i < 2; i++)
1974 : : {
1975 : 14400 : tree tem;
1976 : 14400 : enum tree_code code;
1977 : 14400 : defcodefor_name (def_arg1[i], &code, &tem, NULL);
1978 : 6 : if (!CONVERT_EXPR_CODE_P (code)
1979 : 14394 : || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
1980 : 28794 : || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
1981 : 6 : return false;
1982 : 14394 : def_arg1[i] = tem;
1983 : : }
1984 : : /* Both shifts have to use the same first operand. */
1985 : 23003 : if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
1986 : 36393 : || !types_compatible_p (TREE_TYPE (def_arg1[0]),
1987 : 13390 : TREE_TYPE (def_arg1[1])))
1988 : : {
1989 : 9613 : if ((TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
1990 : 9613 : != TYPE_PRECISION (TREE_TYPE (def_arg1[1])))
1991 : 9613 : || (TYPE_UNSIGNED (TREE_TYPE (def_arg1[0]))
1992 : 9613 : == TYPE_UNSIGNED (TREE_TYPE (def_arg1[1]))))
1993 : 9591 : return false;
1994 : :
1995 : : /* Handle signed rotate; the RSHIFT_EXPR has to be done
1996 : : in unsigned type but LSHIFT_EXPR could be signed. */
1997 : 543 : i = def_code[0] != RSHIFT_EXPR;
1998 : 543 : if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[i])))
1999 : : return false;
2000 : :
2001 : 494 : tree tem;
2002 : 494 : enum tree_code code;
2003 : 494 : defcodefor_name (def_arg1[i], &code, &tem, NULL);
2004 : 271 : if (!CONVERT_EXPR_CODE_P (code)
2005 : 223 : || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
2006 : 717 : || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
2007 : : return false;
2008 : 214 : def_arg1[i] = tem;
2009 : 214 : if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
2010 : 236 : || !types_compatible_p (TREE_TYPE (def_arg1[0]),
2011 : 22 : TREE_TYPE (def_arg1[1])))
2012 : 192 : return false;
2013 : : }
2014 : 13390 : else if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[0])))
2015 : : return false;
2016 : :
2017 : : /* CNT1 + CNT2 == B case above. */
2018 : 11695 : if (tree_fits_uhwi_p (def_arg2[0])
2019 : 1238 : && tree_fits_uhwi_p (def_arg2[1])
2020 : 11695 : && tree_to_uhwi (def_arg2[0])
2021 : 1238 : + tree_to_uhwi (def_arg2[1]) == TYPE_PRECISION (rtype))
2022 : : rotcnt = def_arg2[0];
2023 : 10897 : else if (TREE_CODE (def_arg2[0]) != SSA_NAME
2024 : 10457 : || TREE_CODE (def_arg2[1]) != SSA_NAME)
2025 : : return false;
2026 : : else
2027 : : {
2028 : 10457 : tree cdef_arg1[2], cdef_arg2[2], def_arg2_alt[2];
2029 : 10457 : enum tree_code cdef_code[2];
2030 : 10457 : gimple *def_arg_alt_stmt[2] = { NULL, NULL };
2031 : 10457 : int check_range = 0;
2032 : 10457 : gimple *check_range_stmt = NULL;
2033 : : /* Look through conversion of the shift count argument.
2034 : : The C/C++ FE cast any shift count argument to integer_type_node.
2035 : : The only problem might be if the shift count type maximum value
2036 : : is equal or smaller than number of bits in rtype. */
2037 : 31371 : for (i = 0; i < 2; i++)
2038 : : {
2039 : 20914 : def_arg2_alt[i] = def_arg2[i];
2040 : 20914 : defcodefor_name (def_arg2[i], &cdef_code[i],
2041 : : &cdef_arg1[i], &cdef_arg2[i]);
2042 : 16349 : if (CONVERT_EXPR_CODE_P (cdef_code[i])
2043 : 4565 : && INTEGRAL_TYPE_P (TREE_TYPE (cdef_arg1[i]))
2044 : 4565 : && TYPE_PRECISION (TREE_TYPE (cdef_arg1[i]))
2045 : 9130 : > floor_log2 (TYPE_PRECISION (rtype))
2046 : 25479 : && type_has_mode_precision_p (TREE_TYPE (cdef_arg1[i])))
2047 : : {
2048 : 4565 : def_arg2_alt[i] = cdef_arg1[i];
2049 : 4565 : if (TREE_CODE (def_arg2[i]) == SSA_NAME)
2050 : 4565 : def_arg_alt_stmt[i] = SSA_NAME_DEF_STMT (def_arg2[i]);
2051 : 4565 : defcodefor_name (def_arg2_alt[i], &cdef_code[i],
2052 : : &cdef_arg1[i], &cdef_arg2[i]);
2053 : : }
2054 : : else
2055 : 16349 : def_arg_alt_stmt[i] = def_arg_stmt[i];
2056 : : }
2057 : 28567 : for (i = 0; i < 2; i++)
2058 : : /* Check for one shift count being Y and the other B - Y,
2059 : : with optional casts. */
2060 : 20569 : if (cdef_code[i] == MINUS_EXPR
2061 : 877 : && tree_fits_shwi_p (cdef_arg1[i])
2062 : 877 : && tree_to_shwi (cdef_arg1[i]) == TYPE_PRECISION (rtype)
2063 : 21388 : && TREE_CODE (cdef_arg2[i]) == SSA_NAME)
2064 : : {
2065 : 819 : tree tem;
2066 : 819 : enum tree_code code;
2067 : :
2068 : 819 : if (cdef_arg2[i] == def_arg2[1 - i]
2069 : 472 : || cdef_arg2[i] == def_arg2_alt[1 - i])
2070 : : {
2071 : 347 : rotcnt = cdef_arg2[i];
2072 : 347 : check_range = -1;
2073 : 347 : if (cdef_arg2[i] == def_arg2[1 - i])
2074 : 347 : check_range_stmt = def_arg_stmt[1 - i];
2075 : : else
2076 : 0 : check_range_stmt = def_arg_alt_stmt[1 - i];
2077 : 803 : break;
2078 : : }
2079 : 472 : defcodefor_name (cdef_arg2[i], &code, &tem, NULL);
2080 : 8 : if (CONVERT_EXPR_CODE_P (code)
2081 : 464 : && INTEGRAL_TYPE_P (TREE_TYPE (tem))
2082 : 464 : && TYPE_PRECISION (TREE_TYPE (tem))
2083 : 928 : > floor_log2 (TYPE_PRECISION (rtype))
2084 : 464 : && type_has_mode_precision_p (TREE_TYPE (tem))
2085 : 936 : && (tem == def_arg2[1 - i]
2086 : 296 : || tem == def_arg2_alt[1 - i]))
2087 : : {
2088 : 456 : rotcnt = tem;
2089 : 456 : check_range = -1;
2090 : 456 : if (tem == def_arg2[1 - i])
2091 : 168 : check_range_stmt = def_arg_stmt[1 - i];
2092 : : else
2093 : 288 : check_range_stmt = def_arg_alt_stmt[1 - i];
2094 : : break;
2095 : : }
2096 : : }
2097 : : /* The above sequence isn't safe for Y being 0,
2098 : : because then one of the shifts triggers undefined behavior.
2099 : : This alternative is safe even for rotation count of 0.
2100 : : One shift count is Y and the other (-Y) & (B - 1).
2101 : : Or one shift count is Y & (B - 1) and the other (-Y) & (B - 1). */
2102 : 19750 : else if (cdef_code[i] == BIT_AND_EXPR
2103 : 31873 : && pow2p_hwi (TYPE_PRECISION (rtype))
2104 : 13763 : && tree_fits_shwi_p (cdef_arg2[i])
2105 : 27526 : && tree_to_shwi (cdef_arg2[i])
2106 : 13763 : == TYPE_PRECISION (rtype) - 1
2107 : 13682 : && TREE_CODE (cdef_arg1[i]) == SSA_NAME
2108 : 33432 : && gimple_assign_rhs_code (stmt) == BIT_IOR_EXPR)
2109 : : {
2110 : 2526 : tree tem;
2111 : 2526 : enum tree_code code;
2112 : :
2113 : 2526 : defcodefor_name (cdef_arg1[i], &code, &tem, NULL);
2114 : 2328 : if (CONVERT_EXPR_CODE_P (code)
2115 : 198 : && INTEGRAL_TYPE_P (TREE_TYPE (tem))
2116 : 198 : && TYPE_PRECISION (TREE_TYPE (tem))
2117 : 396 : > floor_log2 (TYPE_PRECISION (rtype))
2118 : 2724 : && type_has_mode_precision_p (TREE_TYPE (tem)))
2119 : 198 : defcodefor_name (tem, &code, &tem, NULL);
2120 : :
2121 : 2526 : if (code == NEGATE_EXPR)
2122 : : {
2123 : 1670 : if (tem == def_arg2[1 - i] || tem == def_arg2_alt[1 - i])
2124 : : {
2125 : 999 : rotcnt = tem;
2126 : 999 : check_range = 1;
2127 : 999 : if (tem == def_arg2[1 - i])
2128 : 991 : check_range_stmt = def_arg_stmt[1 - i];
2129 : : else
2130 : 8 : check_range_stmt = def_arg_alt_stmt[1 - i];
2131 : 1656 : break;
2132 : : }
2133 : 671 : tree tem2;
2134 : 671 : defcodefor_name (tem, &code, &tem2, NULL);
2135 : 225 : if (CONVERT_EXPR_CODE_P (code)
2136 : 446 : && INTEGRAL_TYPE_P (TREE_TYPE (tem2))
2137 : 446 : && TYPE_PRECISION (TREE_TYPE (tem2))
2138 : 892 : > floor_log2 (TYPE_PRECISION (rtype))
2139 : 1117 : && type_has_mode_precision_p (TREE_TYPE (tem2)))
2140 : : {
2141 : 446 : if (tem2 == def_arg2[1 - i]
2142 : 446 : || tem2 == def_arg2_alt[1 - i])
2143 : : {
2144 : 228 : rotcnt = tem2;
2145 : 228 : check_range = 1;
2146 : 228 : if (tem2 == def_arg2[1 - i])
2147 : 0 : check_range_stmt = def_arg_stmt[1 - i];
2148 : : else
2149 : 228 : check_range_stmt = def_arg_alt_stmt[1 - i];
2150 : : break;
2151 : : }
2152 : : }
2153 : : else
2154 : 225 : tem2 = NULL_TREE;
2155 : :
2156 : 443 : if (cdef_code[1 - i] == BIT_AND_EXPR
2157 : 430 : && tree_fits_shwi_p (cdef_arg2[1 - i])
2158 : 860 : && tree_to_shwi (cdef_arg2[1 - i])
2159 : 430 : == TYPE_PRECISION (rtype) - 1
2160 : 873 : && TREE_CODE (cdef_arg1[1 - i]) == SSA_NAME)
2161 : : {
2162 : 430 : if (tem == cdef_arg1[1 - i]
2163 : 205 : || tem2 == cdef_arg1[1 - i])
2164 : : {
2165 : : rotcnt = def_arg2[1 - i];
2166 : 429 : break;
2167 : : }
2168 : 193 : tree tem3;
2169 : 193 : defcodefor_name (cdef_arg1[1 - i], &code, &tem3, NULL);
2170 : 0 : if (CONVERT_EXPR_CODE_P (code)
2171 : 193 : && INTEGRAL_TYPE_P (TREE_TYPE (tem3))
2172 : 193 : && TYPE_PRECISION (TREE_TYPE (tem3))
2173 : 386 : > floor_log2 (TYPE_PRECISION (rtype))
2174 : 386 : && type_has_mode_precision_p (TREE_TYPE (tem3)))
2175 : : {
2176 : 193 : if (tem == tem3 || tem2 == tem3)
2177 : : {
2178 : : rotcnt = def_arg2[1 - i];
2179 : : break;
2180 : : }
2181 : : }
2182 : : }
2183 : : }
2184 : : }
2185 : 2459 : if (check_range && wider_prec > TYPE_PRECISION (rtype))
2186 : : {
2187 : 1676 : if (TREE_CODE (rotcnt) != SSA_NAME)
2188 : 718 : return false;
2189 : 1676 : int_range_max r;
2190 : 1676 : range_query *q = get_range_query (cfun);
2191 : 1676 : if (q == get_global_range_query ())
2192 : 1520 : q = enable_ranger (cfun);
2193 : 1676 : if (!q->range_of_expr (r, rotcnt, check_range_stmt))
2194 : : {
2195 : 0 : if (check_range > 0)
2196 : : return false;
2197 : 0 : r.set_varying (TREE_TYPE (rotcnt));
2198 : : }
2199 : 1676 : int prec = TYPE_PRECISION (TREE_TYPE (rotcnt));
2200 : 1676 : signop sign = TYPE_SIGN (TREE_TYPE (rotcnt));
2201 : 1676 : wide_int min = wide_int::from (TYPE_PRECISION (rtype), prec, sign);
2202 : 1676 : wide_int max = wide_int::from (wider_prec - 1, prec, sign);
2203 : 1676 : if (check_range < 0)
2204 : 614 : max = min;
2205 : 1676 : int_range<1> r2 (TREE_TYPE (rotcnt), min, max);
2206 : 1676 : r.intersect (r2);
2207 : 1676 : if (!r.undefined_p ())
2208 : : {
2209 : 1324 : if (check_range > 0)
2210 : : {
2211 : 734 : int_range_max r3;
2212 : 2296 : for (int i = TYPE_PRECISION (rtype) + 1; i < wider_prec;
2213 : 1562 : i += TYPE_PRECISION (rtype))
2214 : : {
2215 : 1562 : int j = i + TYPE_PRECISION (rtype) - 2;
2216 : 1562 : min = wide_int::from (i, prec, sign);
2217 : 1562 : max = wide_int::from (MIN (j, wider_prec - 1),
2218 : 1562 : prec, sign);
2219 : 1562 : int_range<1> r4 (TREE_TYPE (rotcnt), min, max);
2220 : 1562 : r3.union_ (r4);
2221 : 1562 : }
2222 : 734 : r.intersect (r3);
2223 : 734 : if (!r.undefined_p ())
2224 : 718 : return false;
2225 : 734 : }
2226 : : add_masking = true;
2227 : : }
2228 : 1676 : }
2229 : 9739 : if (rotcnt == NULL_TREE)
2230 : : return false;
2231 : 1741 : swapped_p = i != 1;
2232 : : }
2233 : :
2234 : 2539 : if (!useless_type_conversion_p (TREE_TYPE (def_arg2[0]),
2235 : 2539 : TREE_TYPE (rotcnt)))
2236 : : {
2237 : 496 : g = gimple_build_assign (make_ssa_name (TREE_TYPE (def_arg2[0])),
2238 : : NOP_EXPR, rotcnt);
2239 : 496 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
2240 : 496 : rotcnt = gimple_assign_lhs (g);
2241 : : }
2242 : 2539 : if (add_masking)
2243 : : {
2244 : 606 : g = gimple_build_assign (make_ssa_name (TREE_TYPE (rotcnt)),
2245 : : BIT_AND_EXPR, rotcnt,
2246 : 606 : build_int_cst (TREE_TYPE (rotcnt),
2247 : 606 : TYPE_PRECISION (rtype) - 1));
2248 : 606 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
2249 : 606 : rotcnt = gimple_assign_lhs (g);
2250 : : }
2251 : 2539 : lhs = gimple_assign_lhs (stmt);
2252 : 2539 : if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0])))
2253 : 1014 : lhs = make_ssa_name (TREE_TYPE (def_arg1[0]));
2254 : 2539 : g = gimple_build_assign (lhs,
2255 : 2539 : ((def_code[0] == LSHIFT_EXPR) ^ swapped_p)
2256 : : ? LROTATE_EXPR : RROTATE_EXPR, def_arg1[0], rotcnt);
2257 : 2539 : if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0])))
2258 : : {
2259 : 1014 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
2260 : 1014 : g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, lhs);
2261 : : }
2262 : 2539 : gsi_replace (gsi, g, false);
2263 : 2539 : return true;
2264 : : }
2265 : :
2266 : :
2267 : : /* Check whether an array contains a valid ctz table. */
2268 : : static bool
2269 : 5 : check_ctz_array (tree ctor, unsigned HOST_WIDE_INT mulc,
2270 : : HOST_WIDE_INT &zero_val, unsigned shift, unsigned bits)
2271 : : {
2272 : 5 : tree elt, idx;
2273 : 5 : unsigned HOST_WIDE_INT i, mask, raw_idx = 0;
2274 : 5 : unsigned matched = 0;
2275 : :
2276 : 5 : mask = ((HOST_WIDE_INT_1U << (bits - shift)) - 1) << shift;
2277 : :
2278 : 5 : zero_val = 0;
2279 : :
2280 : 254 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, elt)
2281 : : {
2282 : 254 : if (!tree_fits_shwi_p (idx))
2283 : : return false;
2284 : 254 : if (!tree_fits_shwi_p (elt) && TREE_CODE (elt) != RAW_DATA_CST)
2285 : : return false;
2286 : :
2287 : 254 : unsigned HOST_WIDE_INT index = tree_to_shwi (idx);
2288 : 254 : HOST_WIDE_INT val;
2289 : :
2290 : 254 : if (TREE_CODE (elt) == INTEGER_CST)
2291 : 190 : val = tree_to_shwi (elt);
2292 : : else
2293 : : {
2294 : 64 : if (raw_idx == (unsigned) RAW_DATA_LENGTH (elt))
2295 : : {
2296 : 0 : raw_idx = 0;
2297 : 0 : continue;
2298 : : }
2299 : 64 : if (TYPE_UNSIGNED (TREE_TYPE (elt)))
2300 : 0 : val = RAW_DATA_UCHAR_ELT (elt, raw_idx);
2301 : : else
2302 : 64 : val = RAW_DATA_SCHAR_ELT (elt, raw_idx);
2303 : 64 : index += raw_idx;
2304 : 64 : raw_idx++;
2305 : 64 : i--;
2306 : : }
2307 : :
2308 : 254 : if (index > bits * 2)
2309 : : return false;
2310 : :
2311 : 254 : if (index == 0)
2312 : : {
2313 : 5 : zero_val = val;
2314 : 5 : matched++;
2315 : : }
2316 : :
2317 : 254 : if (val >= 0 && val < bits && (((mulc << val) & mask) >> shift) == index)
2318 : 192 : matched++;
2319 : :
2320 : 254 : if (matched > bits)
2321 : : return true;
2322 : : }
2323 : :
2324 : : return false;
2325 : : }
2326 : :
2327 : : /* Check whether a string contains a valid ctz table. */
2328 : : static bool
2329 : 3 : check_ctz_string (tree string, unsigned HOST_WIDE_INT mulc,
2330 : : HOST_WIDE_INT &zero_val, unsigned shift, unsigned bits)
2331 : : {
2332 : 3 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (string);
2333 : 3 : unsigned HOST_WIDE_INT mask;
2334 : 3 : unsigned matched = 0;
2335 : 3 : const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (string);
2336 : :
2337 : 3 : if (len < bits || len > bits * 2)
2338 : : return false;
2339 : :
2340 : 3 : mask = ((HOST_WIDE_INT_1U << (bits - shift)) - 1) << shift;
2341 : :
2342 : 3 : zero_val = p[0];
2343 : :
2344 : 131 : for (unsigned i = 0; i < len; i++)
2345 : 128 : if (p[i] < bits && (((mulc << p[i]) & mask) >> shift) == i)
2346 : 128 : matched++;
2347 : :
2348 : 3 : return matched == bits;
2349 : : }
2350 : :
2351 : : /* Recognize count trailing zeroes idiom.
2352 : : The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
2353 : : constant which when multiplied by a power of 2 creates a unique value
2354 : : in the top 5 or 6 bits. This is then indexed into a table which maps it
2355 : : to the number of trailing zeroes. Array[0] is returned so the caller can
2356 : : emit an appropriate sequence depending on whether ctz (0) is defined on
2357 : : the target. */
2358 : : static bool
2359 : 21 : optimize_count_trailing_zeroes (tree array_ref, tree x, tree mulc,
2360 : : tree tshift, HOST_WIDE_INT &zero_val)
2361 : : {
2362 : 21 : tree type = TREE_TYPE (array_ref);
2363 : 21 : tree array = TREE_OPERAND (array_ref, 0);
2364 : :
2365 : 21 : gcc_assert (TREE_CODE (mulc) == INTEGER_CST);
2366 : 21 : gcc_assert (TREE_CODE (tshift) == INTEGER_CST);
2367 : :
2368 : 21 : tree input_type = TREE_TYPE (x);
2369 : 21 : unsigned input_bits = tree_to_shwi (TYPE_SIZE (input_type));
2370 : :
2371 : : /* Check the array element type is not wider than 32 bits and the input is
2372 : : an unsigned 32-bit or 64-bit type. */
2373 : 21 : if (TYPE_PRECISION (type) > 32 || !TYPE_UNSIGNED (input_type))
2374 : : return false;
2375 : 17 : if (input_bits != 32 && input_bits != 64)
2376 : : return false;
2377 : :
2378 : 17 : if (!direct_internal_fn_supported_p (IFN_CTZ, input_type, OPTIMIZE_FOR_BOTH))
2379 : : return false;
2380 : :
2381 : : /* Check the lower bound of the array is zero. */
2382 : 17 : tree low = array_ref_low_bound (array_ref);
2383 : 17 : if (!low || !integer_zerop (low))
2384 : 0 : return false;
2385 : :
2386 : 17 : unsigned shiftval = tree_to_shwi (tshift);
2387 : :
2388 : : /* Check the shift extracts the top 5..7 bits. */
2389 : 17 : if (shiftval < input_bits - 7 || shiftval > input_bits - 5)
2390 : : return false;
2391 : :
2392 : 16 : tree ctor = ctor_for_folding (array);
2393 : 16 : if (!ctor)
2394 : : return false;
2395 : :
2396 : 16 : unsigned HOST_WIDE_INT val = tree_to_uhwi (mulc);
2397 : :
2398 : 16 : if (TREE_CODE (ctor) == CONSTRUCTOR)
2399 : 5 : return check_ctz_array (ctor, val, zero_val, shiftval, input_bits);
2400 : :
2401 : 11 : if (TREE_CODE (ctor) == STRING_CST
2402 : 11 : && TYPE_PRECISION (type) == CHAR_TYPE_SIZE)
2403 : 3 : return check_ctz_string (ctor, val, zero_val, shiftval, input_bits);
2404 : :
2405 : : return false;
2406 : : }
2407 : :
2408 : : /* Match.pd function to match the ctz expression. */
2409 : : extern bool gimple_ctz_table_index (tree, tree *, tree (*)(tree));
2410 : :
2411 : : static bool
2412 : 1910905 : simplify_count_trailing_zeroes (gimple_stmt_iterator *gsi)
2413 : : {
2414 : 1910905 : gimple *stmt = gsi_stmt (*gsi);
2415 : 1910905 : tree array_ref = gimple_assign_rhs1 (stmt);
2416 : 1910905 : tree res_ops[3];
2417 : 1910905 : HOST_WIDE_INT zero_val;
2418 : :
2419 : 1910905 : gcc_checking_assert (TREE_CODE (array_ref) == ARRAY_REF);
2420 : :
2421 : 1910905 : if (!gimple_ctz_table_index (TREE_OPERAND (array_ref, 1), &res_ops[0], NULL))
2422 : : return false;
2423 : :
2424 : 21 : if (optimize_count_trailing_zeroes (array_ref, res_ops[0],
2425 : : res_ops[1], res_ops[2], zero_val))
2426 : : {
2427 : 8 : tree type = TREE_TYPE (res_ops[0]);
2428 : 8 : HOST_WIDE_INT ctz_val = 0;
2429 : 8 : HOST_WIDE_INT type_size = tree_to_shwi (TYPE_SIZE (type));
2430 : 8 : bool zero_ok
2431 : 8 : = CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type), ctz_val) == 2;
2432 : 8 : int nargs = 2;
2433 : :
2434 : : /* If the input value can't be zero, don't special case ctz (0). */
2435 : 8 : if (tree_expr_nonzero_p (res_ops[0]))
2436 : : {
2437 : 0 : zero_ok = true;
2438 : 0 : zero_val = 0;
2439 : 0 : ctz_val = 0;
2440 : 0 : nargs = 1;
2441 : : }
2442 : :
2443 : : /* Skip if there is no value defined at zero, or if we can't easily
2444 : : return the correct value for zero. */
2445 : 8 : if (!zero_ok)
2446 : : return false;
2447 : 8 : if (zero_val != ctz_val && !(zero_val == 0 && ctz_val == type_size))
2448 : : return false;
2449 : :
2450 : 8 : gimple_seq seq = NULL;
2451 : 8 : gimple *g;
2452 : 8 : gcall *call
2453 : 16 : = gimple_build_call_internal (IFN_CTZ, nargs, res_ops[0],
2454 : : nargs == 1 ? NULL_TREE
2455 : 8 : : build_int_cst (integer_type_node,
2456 : : ctz_val));
2457 : 8 : gimple_set_location (call, gimple_location (stmt));
2458 : 8 : gimple_set_lhs (call, make_ssa_name (integer_type_node));
2459 : 8 : gimple_seq_add_stmt (&seq, call);
2460 : :
2461 : 8 : tree prev_lhs = gimple_call_lhs (call);
2462 : :
2463 : : /* Emit ctz (x) & 31 if ctz (0) is 32 but we need to return 0. */
2464 : 8 : if (zero_val == 0 && ctz_val == type_size)
2465 : : {
2466 : 6 : g = gimple_build_assign (make_ssa_name (integer_type_node),
2467 : : BIT_AND_EXPR, prev_lhs,
2468 : 6 : build_int_cst (integer_type_node,
2469 : 6 : type_size - 1));
2470 : 6 : gimple_set_location (g, gimple_location (stmt));
2471 : 6 : gimple_seq_add_stmt (&seq, g);
2472 : 6 : prev_lhs = gimple_assign_lhs (g);
2473 : : }
2474 : :
2475 : 8 : g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, prev_lhs);
2476 : 8 : gimple_seq_add_stmt (&seq, g);
2477 : 8 : gsi_replace_with_seq (gsi, seq, true);
2478 : 8 : return true;
2479 : : }
2480 : :
2481 : : return false;
2482 : : }
2483 : :
2484 : :
2485 : : /* Determine whether applying the 2 permutations (mask1 then mask2)
2486 : : gives back one of the input. */
2487 : :
2488 : : static int
2489 : 18 : is_combined_permutation_identity (tree mask1, tree mask2)
2490 : : {
2491 : 18 : tree mask;
2492 : 18 : unsigned HOST_WIDE_INT nelts, i, j;
2493 : 18 : bool maybe_identity1 = true;
2494 : 18 : bool maybe_identity2 = true;
2495 : :
2496 : 18 : gcc_checking_assert (TREE_CODE (mask1) == VECTOR_CST
2497 : : && TREE_CODE (mask2) == VECTOR_CST);
2498 : :
2499 : : /* For VLA masks, check for the following pattern:
2500 : : v1 = VEC_PERM_EXPR (v0, ..., mask1)
2501 : : v2 = VEC_PERM_EXPR (v1, ..., mask2)
2502 : : -->
2503 : : v2 = v0
2504 : : if mask1 == mask2 == {nelts - 1, nelts - 2, ...}. */
2505 : :
2506 : 18 : if (operand_equal_p (mask1, mask2, 0)
2507 : 18 : && !VECTOR_CST_NELTS (mask1).is_constant ())
2508 : : {
2509 : : vec_perm_builder builder;
2510 : : if (tree_to_vec_perm_builder (&builder, mask1))
2511 : : {
2512 : : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask1));
2513 : : vec_perm_indices sel (builder, 1, nelts);
2514 : : if (sel.series_p (0, 1, nelts - 1, -1))
2515 : : return 1;
2516 : : }
2517 : : }
2518 : :
2519 : 18 : mask = fold_ternary (VEC_PERM_EXPR, TREE_TYPE (mask1), mask1, mask1, mask2);
2520 : 18 : if (mask == NULL_TREE || TREE_CODE (mask) != VECTOR_CST)
2521 : : return 0;
2522 : :
2523 : 18 : if (!VECTOR_CST_NELTS (mask).is_constant (&nelts))
2524 : : return 0;
2525 : 36 : for (i = 0; i < nelts; i++)
2526 : : {
2527 : 36 : tree val = VECTOR_CST_ELT (mask, i);
2528 : 36 : gcc_assert (TREE_CODE (val) == INTEGER_CST);
2529 : 36 : j = TREE_INT_CST_LOW (val) & (2 * nelts - 1);
2530 : 36 : if (j == i)
2531 : : maybe_identity2 = false;
2532 : 27 : else if (j == i + nelts)
2533 : : maybe_identity1 = false;
2534 : : else
2535 : : return 0;
2536 : : }
2537 : 0 : return maybe_identity1 ? 1 : maybe_identity2 ? 2 : 0;
2538 : : }
2539 : :
2540 : : /* Combine a shuffle with its arguments. Returns 1 if there were any
2541 : : changes made, 2 if cfg-cleanup needs to run. Else it returns 0. */
2542 : :
2543 : : static int
2544 : 167567 : simplify_permutation (gimple_stmt_iterator *gsi)
2545 : : {
2546 : 167567 : gimple *stmt = gsi_stmt (*gsi);
2547 : 167567 : gimple *def_stmt = NULL;
2548 : 167567 : tree op0, op1, op2, op3, arg0, arg1;
2549 : 167567 : enum tree_code code, code2 = ERROR_MARK;
2550 : 167567 : bool single_use_op0 = false;
2551 : :
2552 : 167567 : gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
2553 : :
2554 : 167567 : op0 = gimple_assign_rhs1 (stmt);
2555 : 167567 : op1 = gimple_assign_rhs2 (stmt);
2556 : 167567 : op2 = gimple_assign_rhs3 (stmt);
2557 : :
2558 : 167567 : if (TREE_CODE (op2) != VECTOR_CST)
2559 : : return 0;
2560 : :
2561 : 164849 : if (TREE_CODE (op0) == VECTOR_CST)
2562 : : {
2563 : : code = VECTOR_CST;
2564 : : arg0 = op0;
2565 : : }
2566 : 163195 : else if (TREE_CODE (op0) == SSA_NAME)
2567 : : {
2568 : 163195 : def_stmt = get_prop_source_stmt (op0, false, &single_use_op0);
2569 : 163195 : if (!def_stmt)
2570 : : return 0;
2571 : 155362 : code = gimple_assign_rhs_code (def_stmt);
2572 : 155362 : if (code == VIEW_CONVERT_EXPR)
2573 : : {
2574 : 1271 : tree rhs = gimple_assign_rhs1 (def_stmt);
2575 : 1271 : tree name = TREE_OPERAND (rhs, 0);
2576 : 1271 : if (TREE_CODE (name) != SSA_NAME)
2577 : : return 0;
2578 : 1271 : if (!has_single_use (name))
2579 : 206 : single_use_op0 = false;
2580 : : /* Here we update the def_stmt through this VIEW_CONVERT_EXPR,
2581 : : but still keep the code to indicate it comes from
2582 : : VIEW_CONVERT_EXPR. */
2583 : 1271 : def_stmt = SSA_NAME_DEF_STMT (name);
2584 : 1271 : if (!def_stmt || !is_gimple_assign (def_stmt))
2585 : : return 0;
2586 : 487 : if (gimple_assign_rhs_code (def_stmt) != CONSTRUCTOR)
2587 : : return 0;
2588 : : }
2589 : 154190 : if (!can_propagate_from (def_stmt))
2590 : : return 0;
2591 : 17993 : arg0 = gimple_assign_rhs1 (def_stmt);
2592 : : }
2593 : : else
2594 : : return 0;
2595 : :
2596 : : /* Two consecutive shuffles. */
2597 : 17993 : if (code == VEC_PERM_EXPR)
2598 : : {
2599 : 5908 : tree orig;
2600 : 5908 : int ident;
2601 : :
2602 : 5908 : if (op0 != op1)
2603 : : return 0;
2604 : 18 : op3 = gimple_assign_rhs3 (def_stmt);
2605 : 18 : if (TREE_CODE (op3) != VECTOR_CST)
2606 : : return 0;
2607 : 18 : ident = is_combined_permutation_identity (op3, op2);
2608 : 18 : if (!ident)
2609 : : return 0;
2610 : 0 : orig = (ident == 1) ? gimple_assign_rhs1 (def_stmt)
2611 : 0 : : gimple_assign_rhs2 (def_stmt);
2612 : 0 : gimple_assign_set_rhs1 (stmt, unshare_expr (orig));
2613 : 0 : gimple_assign_set_rhs_code (stmt, TREE_CODE (orig));
2614 : 0 : gimple_set_num_ops (stmt, 2);
2615 : 0 : update_stmt (stmt);
2616 : 0 : return remove_prop_source_from_use (op0) ? 2 : 1;
2617 : : }
2618 : 13739 : else if (code == CONSTRUCTOR
2619 : 13739 : || code == VECTOR_CST
2620 : : || code == VIEW_CONVERT_EXPR)
2621 : : {
2622 : 2348 : if (op0 != op1)
2623 : : {
2624 : 2206 : if (TREE_CODE (op0) == SSA_NAME && !single_use_op0)
2625 : : return 0;
2626 : :
2627 : 1919 : if (TREE_CODE (op1) == VECTOR_CST)
2628 : : arg1 = op1;
2629 : 1633 : else if (TREE_CODE (op1) == SSA_NAME)
2630 : : {
2631 : 1633 : gimple *def_stmt2 = get_prop_source_stmt (op1, true, NULL);
2632 : 1633 : if (!def_stmt2)
2633 : : return 0;
2634 : 159 : code2 = gimple_assign_rhs_code (def_stmt2);
2635 : 159 : if (code2 == VIEW_CONVERT_EXPR)
2636 : : {
2637 : 0 : tree rhs = gimple_assign_rhs1 (def_stmt2);
2638 : 0 : tree name = TREE_OPERAND (rhs, 0);
2639 : 0 : if (TREE_CODE (name) != SSA_NAME)
2640 : : return 0;
2641 : 0 : if (!has_single_use (name))
2642 : : return 0;
2643 : 0 : def_stmt2 = SSA_NAME_DEF_STMT (name);
2644 : 0 : if (!def_stmt2 || !is_gimple_assign (def_stmt2))
2645 : : return 0;
2646 : 0 : if (gimple_assign_rhs_code (def_stmt2) != CONSTRUCTOR)
2647 : : return 0;
2648 : : }
2649 : 159 : else if (code2 != CONSTRUCTOR && code2 != VECTOR_CST)
2650 : : return 0;
2651 : 49 : if (!can_propagate_from (def_stmt2))
2652 : : return 0;
2653 : 49 : arg1 = gimple_assign_rhs1 (def_stmt2);
2654 : : }
2655 : : else
2656 : : return 0;
2657 : : }
2658 : : else
2659 : : {
2660 : : /* Already used twice in this statement. */
2661 : 142 : if (TREE_CODE (op0) == SSA_NAME && num_imm_uses (op0) > 2)
2662 : : return 0;
2663 : : arg1 = arg0;
2664 : : }
2665 : :
2666 : : /* If there are any VIEW_CONVERT_EXPRs found when finding permutation
2667 : : operands source, check whether it's valid to transform and prepare
2668 : : the required new operands. */
2669 : 398 : if (code == VIEW_CONVERT_EXPR || code2 == VIEW_CONVERT_EXPR)
2670 : : {
2671 : : /* Figure out the target vector type to which operands should be
2672 : : converted. If both are CONSTRUCTOR, the types should be the
2673 : : same, otherwise, use the one of CONSTRUCTOR. */
2674 : 18 : tree tgt_type = NULL_TREE;
2675 : 18 : if (code == VIEW_CONVERT_EXPR)
2676 : : {
2677 : 18 : gcc_assert (gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR);
2678 : 18 : code = CONSTRUCTOR;
2679 : 18 : tgt_type = TREE_TYPE (arg0);
2680 : : }
2681 : 18 : if (code2 == VIEW_CONVERT_EXPR)
2682 : : {
2683 : 0 : tree arg1_type = TREE_TYPE (arg1);
2684 : 0 : if (tgt_type == NULL_TREE)
2685 : : tgt_type = arg1_type;
2686 : 0 : else if (tgt_type != arg1_type)
2687 : 17 : return 0;
2688 : : }
2689 : :
2690 : 18 : if (!VECTOR_TYPE_P (tgt_type))
2691 : : return 0;
2692 : 18 : tree op2_type = TREE_TYPE (op2);
2693 : :
2694 : : /* Figure out the shrunk factor. */
2695 : 18 : poly_uint64 tgt_units = TYPE_VECTOR_SUBPARTS (tgt_type);
2696 : 18 : poly_uint64 op2_units = TYPE_VECTOR_SUBPARTS (op2_type);
2697 : 18 : if (maybe_gt (tgt_units, op2_units))
2698 : : return 0;
2699 : 18 : unsigned int factor;
2700 : 35 : if (!constant_multiple_p (op2_units, tgt_units, &factor))
2701 : : return 0;
2702 : :
2703 : : /* Build the new permutation control vector as target vector. */
2704 : 18 : vec_perm_builder builder;
2705 : 18 : if (!tree_to_vec_perm_builder (&builder, op2))
2706 : : return 0;
2707 : 18 : vec_perm_indices indices (builder, 2, op2_units);
2708 : 18 : vec_perm_indices new_indices;
2709 : 18 : if (new_indices.new_shrunk_vector (indices, factor))
2710 : : {
2711 : 1 : tree mask_type = tgt_type;
2712 : 1 : if (!VECTOR_INTEGER_TYPE_P (mask_type))
2713 : : {
2714 : 0 : tree elem_type = TREE_TYPE (mask_type);
2715 : 0 : unsigned elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type));
2716 : 0 : tree int_type = build_nonstandard_integer_type (elem_size, 0);
2717 : 0 : mask_type = build_vector_type (int_type, tgt_units);
2718 : : }
2719 : 1 : op2 = vec_perm_indices_to_tree (mask_type, new_indices);
2720 : : }
2721 : : else
2722 : 17 : return 0;
2723 : :
2724 : : /* Convert the VECTOR_CST to the appropriate vector type. */
2725 : 1 : if (tgt_type != TREE_TYPE (arg0))
2726 : 0 : arg0 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg0);
2727 : 1 : else if (tgt_type != TREE_TYPE (arg1))
2728 : 0 : arg1 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg1);
2729 : 35 : }
2730 : :
2731 : : /* VIEW_CONVERT_EXPR should be updated to CONSTRUCTOR before. */
2732 : 381 : gcc_assert (code == CONSTRUCTOR || code == VECTOR_CST);
2733 : :
2734 : : /* Shuffle of a constructor. */
2735 : 381 : bool ret = false;
2736 : 381 : tree res_type
2737 : 381 : = build_vector_type (TREE_TYPE (TREE_TYPE (arg0)),
2738 : 381 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (op2)));
2739 : 381 : tree opt = fold_ternary (VEC_PERM_EXPR, res_type, arg0, arg1, op2);
2740 : 381 : if (!opt
2741 : 142 : || (TREE_CODE (opt) != CONSTRUCTOR && TREE_CODE (opt) != VECTOR_CST))
2742 : : return 0;
2743 : : /* Found VIEW_CONVERT_EXPR before, need one explicit conversion. */
2744 : 142 : if (res_type != TREE_TYPE (op0))
2745 : : {
2746 : 1 : tree name = make_ssa_name (TREE_TYPE (opt));
2747 : 1 : gimple *ass_stmt = gimple_build_assign (name, opt);
2748 : 1 : gsi_insert_before (gsi, ass_stmt, GSI_SAME_STMT);
2749 : 1 : opt = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op0), name);
2750 : : }
2751 : 142 : gimple_assign_set_rhs_from_tree (gsi, opt);
2752 : 142 : update_stmt (gsi_stmt (*gsi));
2753 : 142 : if (TREE_CODE (op0) == SSA_NAME)
2754 : 1 : ret = remove_prop_source_from_use (op0);
2755 : 142 : if (op0 != op1 && TREE_CODE (op1) == SSA_NAME)
2756 : 0 : ret |= remove_prop_source_from_use (op1);
2757 : 142 : return ret ? 2 : 1;
2758 : : }
2759 : :
2760 : : return 0;
2761 : : }
2762 : :
2763 : : /* Get the BIT_FIELD_REF definition of VAL, if any, looking through
2764 : : conversions with code CONV_CODE or update it if still ERROR_MARK.
2765 : : Return NULL_TREE if no such matching def was found. */
2766 : :
2767 : : static tree
2768 : 407932 : get_bit_field_ref_def (tree val, enum tree_code &conv_code)
2769 : : {
2770 : 407932 : if (TREE_CODE (val) != SSA_NAME)
2771 : : return NULL_TREE ;
2772 : 380213 : gimple *def_stmt = get_prop_source_stmt (val, false, NULL);
2773 : 380213 : if (!def_stmt)
2774 : : return NULL_TREE;
2775 : 303202 : enum tree_code code = gimple_assign_rhs_code (def_stmt);
2776 : 303202 : if (code == FLOAT_EXPR
2777 : 303202 : || code == FIX_TRUNC_EXPR
2778 : : || CONVERT_EXPR_CODE_P (code))
2779 : : {
2780 : 197299 : tree op1 = gimple_assign_rhs1 (def_stmt);
2781 : 197299 : if (conv_code == ERROR_MARK)
2782 : 97516 : conv_code = code;
2783 : 99783 : else if (conv_code != code)
2784 : : return NULL_TREE;
2785 : 197273 : if (TREE_CODE (op1) != SSA_NAME)
2786 : : return NULL_TREE;
2787 : 92687 : def_stmt = SSA_NAME_DEF_STMT (op1);
2788 : 92687 : if (! is_gimple_assign (def_stmt))
2789 : : return NULL_TREE;
2790 : 55988 : code = gimple_assign_rhs_code (def_stmt);
2791 : : }
2792 : 161891 : if (code != BIT_FIELD_REF)
2793 : : return NULL_TREE;
2794 : 21726 : return gimple_assign_rhs1 (def_stmt);
2795 : : }
2796 : :
2797 : : /* Recognize a VEC_PERM_EXPR. Returns true if there were any changes. */
2798 : :
2799 : : static bool
2800 : 172152 : simplify_vector_constructor (gimple_stmt_iterator *gsi)
2801 : : {
2802 : 172152 : gimple *stmt = gsi_stmt (*gsi);
2803 : 172152 : tree op, orig[2], type, elem_type;
2804 : 172152 : unsigned elem_size, i;
2805 : 172152 : unsigned HOST_WIDE_INT nelts;
2806 : 172152 : unsigned HOST_WIDE_INT refnelts;
2807 : 172152 : enum tree_code conv_code;
2808 : 172152 : constructor_elt *elt;
2809 : :
2810 : 172152 : op = gimple_assign_rhs1 (stmt);
2811 : 172152 : type = TREE_TYPE (op);
2812 : 172152 : gcc_checking_assert (TREE_CODE (op) == CONSTRUCTOR
2813 : : && TREE_CODE (type) == VECTOR_TYPE);
2814 : :
2815 : 172152 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
2816 : : return false;
2817 : 172152 : elem_type = TREE_TYPE (type);
2818 : 172152 : elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type));
2819 : :
2820 : 172152 : orig[0] = NULL;
2821 : 172152 : orig[1] = NULL;
2822 : 172152 : conv_code = ERROR_MARK;
2823 : 172152 : bool maybe_ident = true;
2824 : 172152 : bool maybe_blend[2] = { true, true };
2825 : 172152 : tree one_constant = NULL_TREE;
2826 : 172152 : tree one_nonconstant = NULL_TREE;
2827 : 172152 : auto_vec<tree> constants;
2828 : 172152 : constants.safe_grow_cleared (nelts, true);
2829 : 172152 : auto_vec<std::pair<unsigned, unsigned>, 64> elts;
2830 : 460400 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (op), i, elt)
2831 : : {
2832 : 407932 : tree ref, op1;
2833 : 407932 : unsigned int elem;
2834 : :
2835 : 407932 : if (i >= nelts)
2836 : 172152 : return false;
2837 : :
2838 : : /* Look for elements extracted and possibly converted from
2839 : : another vector. */
2840 : 407932 : op1 = get_bit_field_ref_def (elt->value, conv_code);
2841 : 407932 : if (op1
2842 : 21726 : && TREE_CODE ((ref = TREE_OPERAND (op1, 0))) == SSA_NAME
2843 : 4098 : && VECTOR_TYPE_P (TREE_TYPE (ref))
2844 : 4030 : && useless_type_conversion_p (TREE_TYPE (op1),
2845 : 4030 : TREE_TYPE (TREE_TYPE (ref)))
2846 : 3654 : && constant_multiple_p (bit_field_offset (op1),
2847 : 411586 : bit_field_size (op1), &elem)
2848 : 411586 : && TYPE_VECTOR_SUBPARTS (TREE_TYPE (ref)).is_constant (&refnelts))
2849 : : {
2850 : : unsigned int j;
2851 : 3802 : for (j = 0; j < 2; ++j)
2852 : : {
2853 : 3791 : if (!orig[j])
2854 : : {
2855 : 2043 : if (j == 0
2856 : 2140 : || useless_type_conversion_p (TREE_TYPE (orig[0]),
2857 : 97 : TREE_TYPE (ref)))
2858 : : break;
2859 : : }
2860 : 1748 : else if (ref == orig[j])
2861 : : break;
2862 : : }
2863 : : /* Found a suitable vector element. */
2864 : 3654 : if (j < 2)
2865 : : {
2866 : 3643 : orig[j] = ref;
2867 : 3643 : if (elem != i || j != 0)
2868 : 1490 : maybe_ident = false;
2869 : 3643 : if (elem != i)
2870 : 1421 : maybe_blend[j] = false;
2871 : 3643 : elts.safe_push (std::make_pair (j, elem));
2872 : 3643 : continue;
2873 : : }
2874 : : /* Else fallthru. */
2875 : : }
2876 : : /* Handle elements not extracted from a vector.
2877 : : 1. constants by permuting with constant vector
2878 : : 2. a unique non-constant element by permuting with a splat vector */
2879 : 404289 : if (orig[1]
2880 : 245584 : && orig[1] != error_mark_node)
2881 : : return false;
2882 : 404277 : orig[1] = error_mark_node;
2883 : 404277 : if (CONSTANT_CLASS_P (elt->value))
2884 : : {
2885 : 27715 : if (one_nonconstant)
2886 : : return false;
2887 : 19053 : if (!one_constant)
2888 : 9989 : one_constant = elt->value;
2889 : 19053 : constants[i] = elt->value;
2890 : : }
2891 : : else
2892 : : {
2893 : 376562 : if (one_constant)
2894 : : return false;
2895 : 366827 : if (!one_nonconstant)
2896 : : one_nonconstant = elt->value;
2897 : 218111 : else if (!operand_equal_p (one_nonconstant, elt->value, 0))
2898 : : return false;
2899 : : }
2900 : 284605 : elts.safe_push (std::make_pair (1, i));
2901 : 284605 : maybe_ident = false;
2902 : : }
2903 : 52468 : if (i < nelts)
2904 : : return false;
2905 : :
2906 : 39088 : if (! orig[0]
2907 : 39088 : || ! VECTOR_TYPE_P (TREE_TYPE (orig[0])))
2908 : : return false;
2909 : 1554 : refnelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (orig[0])).to_constant ();
2910 : : /* We currently do not handle larger destination vectors. */
2911 : 1554 : if (refnelts < nelts)
2912 : : return false;
2913 : :
2914 : 1453 : if (maybe_ident)
2915 : : {
2916 : 372 : tree conv_src_type
2917 : : = (nelts != refnelts
2918 : 742 : ? (conv_code != ERROR_MARK
2919 : 2 : ? build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])), nelts)
2920 : : : type)
2921 : 370 : : TREE_TYPE (orig[0]));
2922 : 372 : if (conv_code != ERROR_MARK
2923 : 372 : && !supportable_convert_operation (conv_code, type, conv_src_type,
2924 : : &conv_code))
2925 : : {
2926 : : /* Only few targets implement direct conversion patterns so try
2927 : : some simple special cases via VEC_[UN]PACK[_FLOAT]_LO_EXPR. */
2928 : 4 : optab optab;
2929 : 4 : insn_code icode;
2930 : 4 : tree halfvectype, dblvectype;
2931 : 4 : enum tree_code unpack_op;
2932 : :
2933 : 4 : if (!BYTES_BIG_ENDIAN)
2934 : 4 : unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
2935 : 4 : ? VEC_UNPACK_FLOAT_LO_EXPR
2936 : : : VEC_UNPACK_LO_EXPR);
2937 : : else
2938 : : unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
2939 : : ? VEC_UNPACK_FLOAT_HI_EXPR
2940 : : : VEC_UNPACK_HI_EXPR);
2941 : :
2942 : : /* Conversions between DFP and FP have no special tree code
2943 : : but we cannot handle those since all relevant vector conversion
2944 : : optabs only have a single mode. */
2945 : 2 : if (CONVERT_EXPR_CODE_P (conv_code)
2946 : 2 : && FLOAT_TYPE_P (TREE_TYPE (type))
2947 : 8 : && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (type))
2948 : 2 : != DECIMAL_FLOAT_TYPE_P (TREE_TYPE (conv_src_type))))
2949 : : return false;
2950 : :
2951 : 2 : if (CONVERT_EXPR_CODE_P (conv_code)
2952 : 1 : && (2 * TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
2953 : 1 : == TYPE_PRECISION (TREE_TYPE (type)))
2954 : 0 : && mode_for_vector (as_a <scalar_mode>
2955 : 0 : (TYPE_MODE (TREE_TYPE (TREE_TYPE (orig[0])))),
2956 : 0 : nelts * 2).exists ()
2957 : 0 : && (dblvectype
2958 : 0 : = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
2959 : 0 : nelts * 2))
2960 : : /* Only use it for vector modes or for vector booleans
2961 : : represented as scalar bitmasks. See PR95528. */
2962 : 0 : && (VECTOR_MODE_P (TYPE_MODE (dblvectype))
2963 : 0 : || VECTOR_BOOLEAN_TYPE_P (dblvectype))
2964 : 0 : && (optab = optab_for_tree_code (unpack_op,
2965 : : dblvectype,
2966 : : optab_default))
2967 : 0 : && ((icode = optab_handler (optab, TYPE_MODE (dblvectype)))
2968 : : != CODE_FOR_nothing)
2969 : 3 : && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
2970 : : {
2971 : 0 : gimple_seq stmts = NULL;
2972 : 0 : tree dbl;
2973 : 0 : if (refnelts == nelts)
2974 : : {
2975 : : /* ??? Paradoxical subregs don't exist, so insert into
2976 : : the lower half of a wider zero vector. */
2977 : 0 : dbl = gimple_build (&stmts, BIT_INSERT_EXPR, dblvectype,
2978 : : build_zero_cst (dblvectype), orig[0],
2979 : : bitsize_zero_node);
2980 : : }
2981 : 0 : else if (refnelts == 2 * nelts)
2982 : : dbl = orig[0];
2983 : : else
2984 : 0 : dbl = gimple_build (&stmts, BIT_FIELD_REF, dblvectype,
2985 : 0 : orig[0], TYPE_SIZE (dblvectype),
2986 : : bitsize_zero_node);
2987 : 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
2988 : 0 : gimple_assign_set_rhs_with_ops (gsi, unpack_op, dbl);
2989 : : }
2990 : 2 : else if (CONVERT_EXPR_CODE_P (conv_code)
2991 : 1 : && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
2992 : 1 : == 2 * TYPE_PRECISION (TREE_TYPE (type)))
2993 : 1 : && mode_for_vector (as_a <scalar_mode>
2994 : 1 : (TYPE_MODE
2995 : : (TREE_TYPE (TREE_TYPE (orig[0])))),
2996 : 2 : nelts / 2).exists ()
2997 : 1 : && (halfvectype
2998 : 1 : = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
2999 : 1 : nelts / 2))
3000 : : /* Only use it for vector modes or for vector booleans
3001 : : represented as scalar bitmasks. See PR95528. */
3002 : 1 : && (VECTOR_MODE_P (TYPE_MODE (halfvectype))
3003 : 0 : || VECTOR_BOOLEAN_TYPE_P (halfvectype))
3004 : 1 : && (optab = optab_for_tree_code (VEC_PACK_TRUNC_EXPR,
3005 : : halfvectype,
3006 : : optab_default))
3007 : 1 : && ((icode = optab_handler (optab, TYPE_MODE (halfvectype)))
3008 : : != CODE_FOR_nothing)
3009 : 4 : && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
3010 : : {
3011 : 0 : gimple_seq stmts = NULL;
3012 : 0 : tree low = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
3013 : 0 : orig[0], TYPE_SIZE (halfvectype),
3014 : : bitsize_zero_node);
3015 : 0 : tree hig = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
3016 : 0 : orig[0], TYPE_SIZE (halfvectype),
3017 : 0 : TYPE_SIZE (halfvectype));
3018 : 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3019 : 0 : gimple_assign_set_rhs_with_ops (gsi, VEC_PACK_TRUNC_EXPR,
3020 : : low, hig);
3021 : : }
3022 : : else
3023 : 3 : return false;
3024 : 0 : update_stmt (gsi_stmt (*gsi));
3025 : 0 : return true;
3026 : : }
3027 : 368 : if (nelts != refnelts)
3028 : : {
3029 : 2 : gassign *lowpart
3030 : 2 : = gimple_build_assign (make_ssa_name (conv_src_type),
3031 : : build3 (BIT_FIELD_REF, conv_src_type,
3032 : 2 : orig[0], TYPE_SIZE (conv_src_type),
3033 : : bitsize_zero_node));
3034 : 2 : gsi_insert_before (gsi, lowpart, GSI_SAME_STMT);
3035 : 2 : orig[0] = gimple_assign_lhs (lowpart);
3036 : : }
3037 : 368 : if (conv_code == ERROR_MARK)
3038 : : {
3039 : 351 : tree src_type = TREE_TYPE (orig[0]);
3040 : 351 : if (!useless_type_conversion_p (type, src_type))
3041 : : {
3042 : 0 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
3043 : : TYPE_VECTOR_SUBPARTS (src_type))
3044 : : && useless_type_conversion_p (TREE_TYPE (type),
3045 : : TREE_TYPE (src_type)));
3046 : 0 : tree rhs = build1 (VIEW_CONVERT_EXPR, type, orig[0]);
3047 : 0 : orig[0] = make_ssa_name (type);
3048 : 0 : gassign *assign = gimple_build_assign (orig[0], rhs);
3049 : 0 : gsi_insert_before (gsi, assign, GSI_SAME_STMT);
3050 : : }
3051 : 351 : gimple_assign_set_rhs_from_tree (gsi, orig[0]);
3052 : : }
3053 : : else
3054 : 17 : gimple_assign_set_rhs_with_ops (gsi, conv_code, orig[0],
3055 : : NULL_TREE, NULL_TREE);
3056 : : }
3057 : : else
3058 : : {
3059 : : /* If we combine a vector with a non-vector avoid cases where
3060 : : we'll obviously end up with more GIMPLE stmts which is when
3061 : : we'll later not fold this to a single insert into the vector
3062 : : and we had a single extract originally. See PR92819. */
3063 : 1081 : if (nelts == 2
3064 : 924 : && refnelts > 2
3065 : 43 : && orig[1] == error_mark_node
3066 : 15 : && !maybe_blend[0])
3067 : 667 : return false;
3068 : 1068 : tree mask_type, perm_type, conv_src_type;
3069 : 1068 : perm_type = TREE_TYPE (orig[0]);
3070 : 2136 : conv_src_type = (nelts == refnelts
3071 : 1068 : ? perm_type
3072 : 48 : : build_vector_type (TREE_TYPE (perm_type), nelts));
3073 : 1068 : if (conv_code != ERROR_MARK
3074 : 1068 : && !supportable_convert_operation (conv_code, type, conv_src_type,
3075 : : &conv_code))
3076 : : return false;
3077 : :
3078 : : /* Now that we know the number of elements of the source build the
3079 : : permute vector.
3080 : : ??? When the second vector has constant values we can shuffle
3081 : : it and its source indexes to make the permutation supported.
3082 : : For now it mimics a blend. */
3083 : 516 : vec_perm_builder sel (refnelts, refnelts, 1);
3084 : 516 : bool all_same_p = true;
3085 : 4548 : for (i = 0; i < elts.length (); ++i)
3086 : : {
3087 : 1758 : sel.quick_push (elts[i].second + elts[i].first * refnelts);
3088 : 1758 : all_same_p &= known_eq (sel[i], sel[0]);
3089 : : }
3090 : : /* And fill the tail with "something". It's really don't care,
3091 : : and ideally we'd allow VEC_PERM to have a smaller destination
3092 : : vector. As a heuristic:
3093 : :
3094 : : (a) if what we have so far duplicates a single element, make the
3095 : : tail do the same
3096 : :
3097 : : (b) otherwise preserve a uniform orig[0]. This facilitates
3098 : : later pattern-matching of VEC_PERM_EXPR to a BIT_INSERT_EXPR. */
3099 : 792 : for (; i < refnelts; ++i)
3100 : 552 : sel.quick_push (all_same_p
3101 : 828 : ? sel[0]
3102 : 28 : : (elts[0].second == 0 && elts[0].first == 0
3103 : 260 : ? 0 : refnelts) + i);
3104 : 640 : vec_perm_indices indices (sel, orig[1] ? 2 : 1, refnelts);
3105 : 516 : machine_mode vmode = TYPE_MODE (perm_type);
3106 : 516 : if (!can_vec_perm_const_p (vmode, vmode, indices))
3107 : : return false;
3108 : 414 : mask_type
3109 : 414 : = build_vector_type (build_nonstandard_integer_type (elem_size, 1),
3110 : : refnelts);
3111 : 414 : if (GET_MODE_CLASS (TYPE_MODE (mask_type)) != MODE_VECTOR_INT
3112 : 1242 : || maybe_ne (GET_MODE_SIZE (TYPE_MODE (mask_type)),
3113 : 828 : GET_MODE_SIZE (TYPE_MODE (perm_type))))
3114 : 0 : return false;
3115 : 414 : tree op2 = vec_perm_indices_to_tree (mask_type, indices);
3116 : 414 : bool converted_orig1 = false;
3117 : 414 : gimple_seq stmts = NULL;
3118 : 414 : if (!orig[1])
3119 : 70 : orig[1] = orig[0];
3120 : 344 : else if (orig[1] == error_mark_node
3121 : 264 : && one_nonconstant)
3122 : : {
3123 : : /* ??? We can see if we can safely convert to the original
3124 : : element type. */
3125 : 190 : converted_orig1 = conv_code != ERROR_MARK;
3126 : 379 : orig[1] = gimple_build_vector_from_val (&stmts, UNKNOWN_LOCATION,
3127 : : converted_orig1
3128 : : ? type : perm_type,
3129 : : one_nonconstant);
3130 : : }
3131 : 154 : else if (orig[1] == error_mark_node)
3132 : : {
3133 : : /* ??? See if we can convert the vector to the original type. */
3134 : 74 : converted_orig1 = conv_code != ERROR_MARK;
3135 : 74 : unsigned n = converted_orig1 ? nelts : refnelts;
3136 : 60 : tree_vector_builder vec (converted_orig1
3137 : 74 : ? type : perm_type, n, 1);
3138 : 436 : for (unsigned i = 0; i < n; ++i)
3139 : 720 : if (i < nelts && constants[i])
3140 : 181 : vec.quick_push (constants[i]);
3141 : : else
3142 : : /* ??? Push a don't-care value. */
3143 : 181 : vec.quick_push (one_constant);
3144 : 74 : orig[1] = vec.build ();
3145 : 74 : }
3146 : 334 : tree blend_op2 = NULL_TREE;
3147 : 334 : if (converted_orig1)
3148 : : {
3149 : : /* Make sure we can do a blend in the target type. */
3150 : 15 : vec_perm_builder sel (nelts, nelts, 1);
3151 : 75 : for (i = 0; i < elts.length (); ++i)
3152 : 120 : sel.quick_push (elts[i].first
3153 : 60 : ? elts[i].second + nelts : i);
3154 : 15 : vec_perm_indices indices (sel, 2, nelts);
3155 : 15 : machine_mode vmode = TYPE_MODE (type);
3156 : 15 : if (!can_vec_perm_const_p (vmode, vmode, indices))
3157 : : return false;
3158 : 15 : mask_type
3159 : 15 : = build_vector_type (build_nonstandard_integer_type (elem_size, 1),
3160 : : nelts);
3161 : 15 : if (GET_MODE_CLASS (TYPE_MODE (mask_type)) != MODE_VECTOR_INT
3162 : 45 : || maybe_ne (GET_MODE_SIZE (TYPE_MODE (mask_type)),
3163 : 30 : GET_MODE_SIZE (TYPE_MODE (type))))
3164 : 0 : return false;
3165 : 15 : blend_op2 = vec_perm_indices_to_tree (mask_type, indices);
3166 : 15 : }
3167 : 414 : tree orig1_for_perm
3168 : 414 : = converted_orig1 ? build_zero_cst (perm_type) : orig[1];
3169 : 414 : tree res = gimple_build (&stmts, VEC_PERM_EXPR, perm_type,
3170 : : orig[0], orig1_for_perm, op2);
3171 : 414 : if (nelts != refnelts)
3172 : 36 : res = gimple_build (&stmts, BIT_FIELD_REF,
3173 : 36 : conv_code != ERROR_MARK ? conv_src_type : type,
3174 : 36 : res, TYPE_SIZE (type), bitsize_zero_node);
3175 : 414 : if (conv_code != ERROR_MARK)
3176 : 22 : res = gimple_build (&stmts, conv_code, type, res);
3177 : 392 : else if (!useless_type_conversion_p (type, TREE_TYPE (res)))
3178 : : {
3179 : 0 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
3180 : : TYPE_VECTOR_SUBPARTS (perm_type))
3181 : : && useless_type_conversion_p (TREE_TYPE (type),
3182 : : TREE_TYPE (perm_type)));
3183 : 0 : res = gimple_build (&stmts, VIEW_CONVERT_EXPR, type, res);
3184 : : }
3185 : : /* Blend in the actual constant. */
3186 : 414 : if (converted_orig1)
3187 : 15 : res = gimple_build (&stmts, VEC_PERM_EXPR, type,
3188 : : res, orig[1], blend_op2);
3189 : 414 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3190 : 414 : gimple_assign_set_rhs_with_ops (gsi, SSA_NAME, res);
3191 : 516 : }
3192 : 782 : update_stmt (gsi_stmt (*gsi));
3193 : 782 : return true;
3194 : 172152 : }
3195 : :
3196 : : /* Prepare a TARGET_MEM_REF ref so that it can be subsetted as
3197 : : lvalue. This splits out an address computation stmt before *GSI
3198 : : and returns a MEM_REF wrapping the address. */
3199 : :
3200 : : static tree
3201 : 1151 : prepare_target_mem_ref_lvalue (tree ref, gimple_stmt_iterator *gsi)
3202 : : {
3203 : 1151 : if (TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR)
3204 : 300 : mark_addressable (TREE_OPERAND (TREE_OPERAND (ref, 0), 0));
3205 : 1151 : tree ptrtype = build_pointer_type (TREE_TYPE (ref));
3206 : 1151 : tree tem = make_ssa_name (ptrtype);
3207 : 1151 : gimple *new_stmt
3208 : 1151 : = gimple_build_assign (tem, build1 (ADDR_EXPR, TREE_TYPE (tem),
3209 : : unshare_expr (ref)));
3210 : 1151 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
3211 : 2302 : ref = build2_loc (EXPR_LOCATION (ref),
3212 : 1151 : MEM_REF, TREE_TYPE (ref), tem,
3213 : 1151 : build_int_cst (TREE_TYPE (TREE_OPERAND (ref, 1)), 0));
3214 : 1151 : return ref;
3215 : : }
3216 : :
3217 : : /* Rewrite the vector load at *GSI to component-wise loads if the load
3218 : : is only used in BIT_FIELD_REF extractions with eventual intermediate
3219 : : widening. */
3220 : :
3221 : : static void
3222 : 266172 : optimize_vector_load (gimple_stmt_iterator *gsi)
3223 : : {
3224 : 266172 : gimple *stmt = gsi_stmt (*gsi);
3225 : 266172 : tree lhs = gimple_assign_lhs (stmt);
3226 : 266172 : tree rhs = gimple_assign_rhs1 (stmt);
3227 : :
3228 : : /* Gather BIT_FIELD_REFs to rewrite, looking through
3229 : : VEC_UNPACK_{LO,HI}_EXPR. */
3230 : 266172 : use_operand_p use_p;
3231 : 266172 : imm_use_iterator iter;
3232 : 266172 : bool rewrite = true;
3233 : 266172 : auto_vec<gimple *, 8> bf_stmts;
3234 : 266172 : auto_vec<tree, 8> worklist;
3235 : 266172 : worklist.quick_push (lhs);
3236 : 267722 : do
3237 : : {
3238 : 267722 : tree def = worklist.pop ();
3239 : 267722 : unsigned HOST_WIDE_INT def_eltsize
3240 : 267722 : = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (def))));
3241 : 341488 : FOR_EACH_IMM_USE_FAST (use_p, iter, def)
3242 : : {
3243 : 321721 : gimple *use_stmt = USE_STMT (use_p);
3244 : 321721 : if (is_gimple_debug (use_stmt))
3245 : 73766 : continue;
3246 : 321564 : if (!is_gimple_assign (use_stmt))
3247 : : {
3248 : : rewrite = false;
3249 : 247955 : break;
3250 : : }
3251 : 290552 : enum tree_code use_code = gimple_assign_rhs_code (use_stmt);
3252 : 290552 : tree use_rhs = gimple_assign_rhs1 (use_stmt);
3253 : 361068 : if (use_code == BIT_FIELD_REF
3254 : 70519 : && TREE_OPERAND (use_rhs, 0) == def
3255 : : /* If its on the VEC_UNPACK_{HI,LO}_EXPR
3256 : : def need to verify it is element aligned. */
3257 : 361071 : && (def == lhs
3258 : 119 : || (known_eq (bit_field_size (use_rhs), def_eltsize)
3259 : 119 : && constant_multiple_p (bit_field_offset (use_rhs),
3260 : : def_eltsize)
3261 : : /* We can simulate the VEC_UNPACK_{HI,LO}_EXPR
3262 : : via a NOP_EXPR only for integral types.
3263 : : ??? Support VEC_UNPACK_FLOAT_{HI,LO}_EXPR. */
3264 : 119 : && INTEGRAL_TYPE_P (TREE_TYPE (use_rhs)))))
3265 : : {
3266 : 70516 : bf_stmts.safe_push (use_stmt);
3267 : 70516 : continue;
3268 : : }
3269 : : /* Walk through one level of VEC_UNPACK_{LO,HI}_EXPR. */
3270 : 220036 : if (def == lhs
3271 : 218549 : && (use_code == VEC_UNPACK_HI_EXPR
3272 : 218549 : || use_code == VEC_UNPACK_LO_EXPR)
3273 : 3093 : && use_rhs == lhs)
3274 : : {
3275 : 3093 : worklist.safe_push (gimple_assign_lhs (use_stmt));
3276 : 3093 : continue;
3277 : : }
3278 : : rewrite = false;
3279 : : break;
3280 : : }
3281 : 247955 : if (!rewrite)
3282 : : break;
3283 : : }
3284 : 39534 : while (!worklist.is_empty ());
3285 : :
3286 : 266172 : if (!rewrite)
3287 : : {
3288 : 247955 : gsi_next (gsi);
3289 : 247955 : return;
3290 : : }
3291 : : /* We now have all ultimate uses of the load to rewrite in bf_stmts. */
3292 : :
3293 : : /* Prepare the original ref to be wrapped in adjusted BIT_FIELD_REFs.
3294 : : For TARGET_MEM_REFs we have to separate the LEA from the reference. */
3295 : 18217 : tree load_rhs = rhs;
3296 : 18217 : if (TREE_CODE (load_rhs) == TARGET_MEM_REF)
3297 : 1150 : load_rhs = prepare_target_mem_ref_lvalue (load_rhs, gsi);
3298 : :
3299 : : /* Rewrite the BIT_FIELD_REFs to be actual loads, re-emitting them at
3300 : : the place of the original load. */
3301 : 119897 : for (gimple *use_stmt : bf_stmts)
3302 : : {
3303 : 65246 : tree bfr = gimple_assign_rhs1 (use_stmt);
3304 : 65246 : tree new_rhs = unshare_expr (load_rhs);
3305 : 65246 : if (TREE_OPERAND (bfr, 0) != lhs)
3306 : : {
3307 : : /* When the BIT_FIELD_REF is on the promoted vector we have to
3308 : : adjust it and emit a conversion afterwards. */
3309 : 116 : gimple *def_stmt
3310 : 116 : = SSA_NAME_DEF_STMT (TREE_OPERAND (bfr, 0));
3311 : 116 : enum tree_code def_code
3312 : 116 : = gimple_assign_rhs_code (def_stmt);
3313 : :
3314 : : /* The adjusted BIT_FIELD_REF is of the promotion source
3315 : : vector size and at half of the offset... */
3316 : 116 : new_rhs = fold_build3 (BIT_FIELD_REF,
3317 : : TREE_TYPE (TREE_TYPE (lhs)),
3318 : : new_rhs,
3319 : : TYPE_SIZE (TREE_TYPE (TREE_TYPE (lhs))),
3320 : : size_binop (EXACT_DIV_EXPR,
3321 : : TREE_OPERAND (bfr, 2),
3322 : : bitsize_int (2)));
3323 : : /* ... and offsetted by half of the vector if VEC_UNPACK_HI_EXPR. */
3324 : 116 : if (def_code == (!BYTES_BIG_ENDIAN
3325 : : ? VEC_UNPACK_HI_EXPR : VEC_UNPACK_LO_EXPR))
3326 : 58 : TREE_OPERAND (new_rhs, 2)
3327 : 116 : = size_binop (PLUS_EXPR, TREE_OPERAND (new_rhs, 2),
3328 : : size_binop (EXACT_DIV_EXPR,
3329 : : TYPE_SIZE (TREE_TYPE (lhs)),
3330 : : bitsize_int (2)));
3331 : 116 : tree tem = make_ssa_name (TREE_TYPE (TREE_TYPE (lhs)));
3332 : 116 : gimple *new_stmt = gimple_build_assign (tem, new_rhs);
3333 : 116 : location_t loc = gimple_location (use_stmt);
3334 : 116 : gimple_set_location (new_stmt, loc);
3335 : 116 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
3336 : : /* Perform scalar promotion. */
3337 : 116 : new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
3338 : : NOP_EXPR, tem);
3339 : 116 : gimple_set_location (new_stmt, loc);
3340 : 116 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
3341 : : }
3342 : : else
3343 : : {
3344 : : /* When the BIT_FIELD_REF is on the original load result
3345 : : we can just wrap that. */
3346 : 65130 : tree new_rhs = fold_build3 (BIT_FIELD_REF, TREE_TYPE (bfr),
3347 : : unshare_expr (load_rhs),
3348 : : TREE_OPERAND (bfr, 1),
3349 : : TREE_OPERAND (bfr, 2));
3350 : 65130 : gimple *new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
3351 : : new_rhs);
3352 : 65130 : location_t loc = gimple_location (use_stmt);
3353 : 65130 : gimple_set_location (new_stmt, loc);
3354 : 65130 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
3355 : : }
3356 : 65246 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
3357 : 65246 : unlink_stmt_vdef (use_stmt);
3358 : 65246 : gsi_remove (&gsi2, true);
3359 : : }
3360 : :
3361 : : /* Finally get rid of the intermediate stmts. */
3362 : 18217 : gimple *use_stmt;
3363 : 18349 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
3364 : : {
3365 : 132 : if (is_gimple_debug (use_stmt))
3366 : : {
3367 : 88 : if (gimple_debug_bind_p (use_stmt))
3368 : : {
3369 : 88 : gimple_debug_bind_reset_value (use_stmt);
3370 : 88 : update_stmt (use_stmt);
3371 : : }
3372 : 88 : continue;
3373 : : }
3374 : 44 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
3375 : 44 : unlink_stmt_vdef (use_stmt);
3376 : 44 : release_defs (use_stmt);
3377 : 44 : gsi_remove (&gsi2, true);
3378 : 18217 : }
3379 : : /* And the original load. */
3380 : 18217 : release_defs (stmt);
3381 : 18217 : gsi_remove (gsi, true);
3382 : 266172 : }
3383 : :
3384 : :
3385 : : /* Primitive "lattice" function for gimple_simplify. */
3386 : :
3387 : : static tree
3388 : 1364178991 : fwprop_ssa_val (tree name)
3389 : : {
3390 : : /* First valueize NAME. */
3391 : 1364178991 : if (TREE_CODE (name) == SSA_NAME
3392 : 1364178991 : && SSA_NAME_VERSION (name) < lattice.length ())
3393 : : {
3394 : 1363544153 : tree val = lattice[SSA_NAME_VERSION (name)];
3395 : 1363544153 : if (val)
3396 : 1364178991 : name = val;
3397 : : }
3398 : : /* We continue matching along SSA use-def edges for SSA names
3399 : : that are not single-use. Currently there are no patterns
3400 : : that would cause any issues with that. */
3401 : 1364178991 : return name;
3402 : : }
3403 : :
3404 : : /* Search for opportunities to free half of the lanes in the following pattern:
3405 : :
3406 : : v_in = {e0, e1, e2, e3}
3407 : : v_1 = VEC_PERM <v_in, v_in, {0, 2, 0, 2}>
3408 : : // v_1 = {e0, e2, e0, e2}
3409 : : v_2 = VEC_PERM <v_in, v_in, {1, 3, 1, 3}>
3410 : : // v_2 = {e1, e3, e1, e3}
3411 : :
3412 : : v_x = v_1 + v_2
3413 : : // v_x = {e0+e1, e2+e3, e0+e1, e2+e3}
3414 : : v_y = v_1 - v_2
3415 : : // v_y = {e0-e1, e2-e3, e0-e1, e2-e3}
3416 : :
3417 : : v_out = VEC_PERM <v_x, v_y, {0, 1, 6, 7}>
3418 : : // v_out = {e0+e1, e2+e3, e0-e1, e2-e3}
3419 : :
3420 : : The last statement could be simplified to:
3421 : : v_out' = VEC_PERM <v_x, v_y, {0, 1, 4, 5}>
3422 : : // v_out' = {e0+e1, e2+e3, e0-e1, e2-e3}
3423 : :
3424 : : Characteristic properties:
3425 : : - v_1 and v_2 are created from the same input vector v_in and introduce the
3426 : : lane duplication (in the selection operand) that we can eliminate.
3427 : : - v_x and v_y are results from lane-preserving operations that use v_1 and
3428 : : v_2 as inputs.
3429 : : - v_out is created by selecting from duplicated lanes. */
3430 : :
3431 : : static bool
3432 : 167299 : recognise_vec_perm_simplify_seq (gassign *stmt, vec_perm_simplify_seq *seq)
3433 : : {
3434 : 167299 : unsigned HOST_WIDE_INT nelts;
3435 : :
3436 : 167299 : gcc_checking_assert (stmt);
3437 : 167299 : gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
3438 : 167299 : basic_block bb = gimple_bb (stmt);
3439 : :
3440 : : /* Decompose the final vec permute statement. */
3441 : 167299 : tree v_x = gimple_assign_rhs1 (stmt);
3442 : 167299 : tree v_y = gimple_assign_rhs2 (stmt);
3443 : 167299 : tree sel = gimple_assign_rhs3 (stmt);
3444 : :
3445 : 167299 : if (TREE_CODE (sel) != VECTOR_CST
3446 : 164581 : || !VECTOR_CST_NELTS (sel).is_constant (&nelts)
3447 : 164581 : || TREE_CODE (v_x) != SSA_NAME
3448 : 162933 : || TREE_CODE (v_y) != SSA_NAME
3449 : 160566 : || !has_single_use (v_x)
3450 : 269471 : || !has_single_use (v_y))
3451 : 66734 : return false;
3452 : :
3453 : : /* Don't analyse sequences with many lanes. */
3454 : 100565 : if (nelts > 4)
3455 : : return false;
3456 : :
3457 : : /* Lookup the definition of v_x and v_y. */
3458 : 98786 : gassign *v_x_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x));
3459 : 98786 : gassign *v_y_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_y));
3460 : 98418 : if (!v_x_stmt || gimple_bb (v_x_stmt) != bb
3461 : 197204 : || !v_y_stmt || gimple_bb (v_y_stmt) != bb)
3462 : : return false;
3463 : :
3464 : : /* Check the operations that define v_x and v_y. */
3465 : 98417 : if (TREE_CODE_CLASS (gimple_assign_rhs_code (v_x_stmt)) != tcc_binary
3466 : 99584 : || TREE_CODE_CLASS (gimple_assign_rhs_code (v_y_stmt)) != tcc_binary)
3467 : : return false;
3468 : :
3469 : 1167 : tree v_x_1 = gimple_assign_rhs1 (v_x_stmt);
3470 : 1167 : tree v_x_2 = gimple_assign_rhs2 (v_x_stmt);
3471 : 1167 : tree v_y_1 = gimple_assign_rhs1 (v_y_stmt);
3472 : 1167 : tree v_y_2 = gimple_assign_rhs2 (v_y_stmt);
3473 : :
3474 : 1167 : if (v_x_stmt == v_y_stmt
3475 : 1167 : || TREE_CODE (v_x_1) != SSA_NAME
3476 : 1164 : || TREE_CODE (v_x_2) != SSA_NAME
3477 : 1162 : || num_imm_uses (v_x_1) != 2
3478 : 2272 : || num_imm_uses (v_x_2) != 2)
3479 : : return false;
3480 : :
3481 : 1073 : if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
3482 : : {
3483 : : /* Allow operands of commutative operators to swap. */
3484 : 563 : if (commutative_tree_code (gimple_assign_rhs_code (v_x_stmt)))
3485 : : {
3486 : : /* Keep v_x_1 the first operand for non-commutative operators. */
3487 : 253 : v_x_1 = gimple_assign_rhs2 (v_x_stmt);
3488 : 253 : v_x_2 = gimple_assign_rhs1 (v_x_stmt);
3489 : 253 : if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
3490 : : return false;
3491 : : }
3492 : 310 : else if (commutative_tree_code (gimple_assign_rhs_code (v_y_stmt)))
3493 : : {
3494 : 310 : if (v_x_1 != v_y_2 || v_x_2 != v_y_1)
3495 : : return false;
3496 : : }
3497 : : else
3498 : : return false;
3499 : : }
3500 : 1073 : gassign *v_1_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_1));
3501 : 1073 : gassign *v_2_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_2));
3502 : 1009 : if (!v_1_stmt || gimple_bb (v_1_stmt) != bb
3503 : 2082 : || !v_2_stmt || gimple_bb (v_2_stmt) != bb)
3504 : : return false;
3505 : :
3506 : 1005 : if (gimple_assign_rhs_code (v_1_stmt) != VEC_PERM_EXPR
3507 : 1115 : || gimple_assign_rhs_code (v_2_stmt) != VEC_PERM_EXPR)
3508 : : return false;
3509 : :
3510 : : /* Decompose initial VEC_PERM_EXPRs. */
3511 : 106 : tree v_in = gimple_assign_rhs1 (v_1_stmt);
3512 : 106 : tree v_1_sel = gimple_assign_rhs3 (v_1_stmt);
3513 : 106 : tree v_2_sel = gimple_assign_rhs3 (v_2_stmt);
3514 : 106 : if (v_in != gimple_assign_rhs2 (v_1_stmt)
3515 : 101 : || v_in != gimple_assign_rhs1 (v_2_stmt)
3516 : 205 : || v_in != gimple_assign_rhs2 (v_2_stmt))
3517 : : return false;
3518 : :
3519 : 99 : unsigned HOST_WIDE_INT v_1_nelts, v_2_nelts;
3520 : 99 : if (TREE_CODE (v_1_sel) != VECTOR_CST
3521 : 99 : || !VECTOR_CST_NELTS (v_1_sel).is_constant (&v_1_nelts)
3522 : 99 : || TREE_CODE (v_2_sel) != VECTOR_CST
3523 : 198 : || !VECTOR_CST_NELTS (v_2_sel).is_constant (&v_2_nelts))
3524 : 0 : return false;
3525 : :
3526 : 99 : if (nelts != v_1_nelts || nelts != v_2_nelts)
3527 : : return false;
3528 : :
3529 : : /* Create the new selector. */
3530 : 99 : vec_perm_builder new_sel_perm (nelts, nelts, 1);
3531 : 99 : auto_vec<unsigned int> lanes (nelts);
3532 : 99 : lanes.quick_grow_cleared (nelts);
3533 : 495 : for (unsigned int i = 0; i < nelts; i++)
3534 : : {
3535 : : /* Extract the i-th value from the selector. */
3536 : 396 : unsigned int sel_cst = TREE_INT_CST_LOW (VECTOR_CST_ELT (sel, i));
3537 : 396 : unsigned int lane = sel_cst % nelts;
3538 : 396 : unsigned int offs = sel_cst / nelts;
3539 : :
3540 : : /* Check what's in the lane. */
3541 : 396 : unsigned int e_1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, lane));
3542 : 396 : unsigned int e_2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, lane));
3543 : :
3544 : : /* Reuse previous lane (if any). */
3545 : 396 : unsigned int l = 0;
3546 : 675 : for (; l < lane; l++)
3547 : : {
3548 : 477 : if ((TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, l)) == e_1)
3549 : 477 : && (TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, l)) == e_2))
3550 : : break;
3551 : : }
3552 : :
3553 : : /* Add to narrowed selector. */
3554 : 396 : new_sel_perm.quick_push (l + offs * nelts);
3555 : :
3556 : : /* Mark lane as used. */
3557 : 396 : lanes[l] = 1;
3558 : : }
3559 : :
3560 : : /* Count how many lanes are need. */
3561 : : unsigned int cnt = 0;
3562 : 495 : for (unsigned int i = 0; i < nelts; i++)
3563 : 396 : cnt += lanes[i];
3564 : :
3565 : : /* If more than (nelts/2) lanes are needed, skip the sequence. */
3566 : 99 : if (cnt > nelts / 2)
3567 : : return false;
3568 : :
3569 : : /* Check if the resulting permuation is cheap. */
3570 : 99 : vec_perm_indices new_indices (new_sel_perm, 2, nelts);
3571 : 99 : tree vectype = TREE_TYPE (gimple_assign_lhs (stmt));
3572 : 99 : machine_mode vmode = TYPE_MODE (vectype);
3573 : 99 : if (!can_vec_perm_const_p (vmode, vmode, new_indices, false))
3574 : : return false;
3575 : :
3576 : 99 : *seq = XNEW (struct _vec_perm_simplify_seq);
3577 : 99 : (*seq)->stmt = stmt;
3578 : 99 : (*seq)->v_1_stmt = v_1_stmt;
3579 : 99 : (*seq)->v_2_stmt = v_2_stmt;
3580 : 99 : (*seq)->v_x_stmt = v_x_stmt;
3581 : 99 : (*seq)->v_y_stmt = v_y_stmt;
3582 : 99 : (*seq)->nelts = nelts;
3583 : 99 : (*seq)->new_sel = vect_gen_perm_mask_checked (vectype, new_indices);
3584 : :
3585 : 99 : if (dump_file)
3586 : : {
3587 : 26 : fprintf (dump_file, "Found vec perm simplify sequence ending with:\n\t");
3588 : 26 : print_gimple_stmt (dump_file, stmt, 0);
3589 : :
3590 : 26 : if (dump_flags & TDF_DETAILS)
3591 : : {
3592 : 26 : fprintf (dump_file, "\tNarrowed vec_perm selector: ");
3593 : 26 : print_generic_expr (dump_file, (*seq)->new_sel);
3594 : 26 : fprintf (dump_file, "\n");
3595 : : }
3596 : : }
3597 : :
3598 : : return true;
3599 : 198 : }
3600 : :
3601 : : /* Reduce the lane consumption of a simplifiable vec perm sequence. */
3602 : :
3603 : : static void
3604 : 72 : narrow_vec_perm_simplify_seq (const vec_perm_simplify_seq &seq)
3605 : : {
3606 : 72 : gassign *stmt = seq->stmt;
3607 : 72 : if (dump_file && (dump_flags & TDF_DETAILS))
3608 : : {
3609 : 20 : fprintf (dump_file, "Updating VEC_PERM statment:\n");
3610 : 20 : fprintf (dump_file, "Old stmt: ");
3611 : 20 : print_gimple_stmt (dump_file, stmt, 0);
3612 : : }
3613 : :
3614 : : /* Update the last VEC_PERM statement. */
3615 : 72 : gimple_assign_set_rhs3 (stmt, seq->new_sel);
3616 : 72 : update_stmt (stmt);
3617 : :
3618 : 72 : if (dump_file && (dump_flags & TDF_DETAILS))
3619 : : {
3620 : 20 : fprintf (dump_file, "New stmt: ");
3621 : 20 : print_gimple_stmt (dump_file, stmt, 0);
3622 : : }
3623 : 72 : }
3624 : :
3625 : : /* Test if we can blend two simplifiable vec permute sequences.
3626 : : NEED_SWAP will be set, if sequences must be swapped for blending. */
3627 : :
3628 : : static bool
3629 : 46 : can_blend_vec_perm_simplify_seqs_p (vec_perm_simplify_seq seq1,
3630 : : vec_perm_simplify_seq seq2,
3631 : : bool *need_swap)
3632 : : {
3633 : 46 : unsigned int nelts = seq1->nelts;
3634 : 46 : basic_block bb = gimple_bb (seq1->stmt);
3635 : :
3636 : 46 : gcc_assert (gimple_bb (seq2->stmt) == bb);
3637 : :
3638 : : /* BBs and number of elements must be equal. */
3639 : 46 : if (gimple_bb (seq2->stmt) != bb || seq2->nelts != nelts)
3640 : : return false;
3641 : :
3642 : : /* We need vectors of the same type. */
3643 : 46 : if (TREE_TYPE (gimple_assign_lhs (seq1->stmt))
3644 : 46 : != TREE_TYPE (gimple_assign_lhs (seq2->stmt)))
3645 : : return false;
3646 : :
3647 : : /* We require isomorphic operators. */
3648 : 40 : if (((gimple_assign_rhs_code (seq1->v_x_stmt)
3649 : 40 : != gimple_assign_rhs_code (seq2->v_x_stmt))
3650 : 40 : || (gimple_assign_rhs_code (seq1->v_y_stmt)
3651 : 40 : != gimple_assign_rhs_code (seq2->v_y_stmt))))
3652 : : return false;
3653 : :
3654 : : /* We cannot have any dependencies between the sequences.
3655 : :
3656 : : For merging, we will reuse seq1->v_1_stmt and seq1->v_2_stmt.
3657 : : seq1's v_in is defined before these statements, but we need
3658 : : to check if seq2's v_in is defined before them as well.
3659 : :
3660 : : Further, we will reuse seq2->stmt. We need to ensure that
3661 : : seq1->v_x_stmt and seq1->v_y_stmt are before it.
3662 : :
3663 : : Note, that we don't need to check the BBs here, because all
3664 : : statements of both sequences have to be in the same BB.
3665 : : */
3666 : :
3667 : 40 : tree seq2_v_in = gimple_assign_rhs1 (seq2->v_1_stmt);
3668 : 40 : if (TREE_CODE (seq2_v_in) != SSA_NAME)
3669 : : return false;
3670 : :
3671 : 40 : gassign *seq2_v_in_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq2_v_in));
3672 : 40 : if (!seq2_v_in_stmt || gimple_bb (seq2_v_in_stmt) != bb
3673 : 40 : || (gimple_uid (seq2_v_in_stmt) > gimple_uid (seq1->v_1_stmt))
3674 : 36 : || (gimple_uid (seq1->v_x_stmt) > gimple_uid (seq2->stmt))
3675 : 36 : || (gimple_uid (seq1->v_y_stmt) > gimple_uid (seq2->stmt)))
3676 : : {
3677 : 4 : tree seq1_v_in = gimple_assign_rhs1 (seq1->v_1_stmt);
3678 : 4 : if (TREE_CODE (seq1_v_in) != SSA_NAME)
3679 : : return false;
3680 : :
3681 : 4 : gassign *seq1_v_in_stmt
3682 : 4 : = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq1_v_in));
3683 : : /* Let's try to see if we succeed when swapping the sequences. */
3684 : 4 : if (!seq1_v_in_stmt || gimple_bb (seq1_v_in_stmt)
3685 : 0 : || (gimple_uid (seq1_v_in_stmt) > gimple_uid (seq2->v_1_stmt))
3686 : 0 : || (gimple_uid (seq2->v_x_stmt) > gimple_uid (seq1->stmt))
3687 : 0 : || (gimple_uid (seq2->v_y_stmt) > gimple_uid (seq1->stmt)))
3688 : : return false;
3689 : 0 : *need_swap = true;
3690 : : }
3691 : : else
3692 : 36 : *need_swap = false;
3693 : :
3694 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
3695 : 10 : fprintf (dump_file, "Found vec perm simplify sequence pair.\n");
3696 : :
3697 : : return true;
3698 : : }
3699 : :
3700 : : /* Calculate the permutations for blending the two given vec permute
3701 : : sequences. This may fail if the resulting permutation is not
3702 : : supported. */
3703 : :
3704 : : static bool
3705 : 36 : calc_perm_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
3706 : : vec_perm_simplify_seq seq2,
3707 : : vec_perm_indices *seq2_stmt_indices,
3708 : : vec_perm_indices *seq1_v_1_stmt_indices,
3709 : : vec_perm_indices *seq1_v_2_stmt_indices)
3710 : : {
3711 : 36 : unsigned int i;
3712 : 36 : unsigned int nelts = seq1->nelts;
3713 : 36 : auto_vec<int> lane_assignment;
3714 : 36 : lane_assignment.create (nelts);
3715 : :
3716 : : /* Mark all lanes as free. */
3717 : 36 : lane_assignment.quick_grow_cleared (nelts);
3718 : :
3719 : : /* Allocate lanes for seq1. */
3720 : 180 : for (i = 0; i < nelts; i++)
3721 : : {
3722 : 144 : unsigned int l = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq1->new_sel, i));
3723 : 144 : l %= nelts;
3724 : 144 : lane_assignment[l] = 1;
3725 : : }
3726 : :
3727 : : /* Allocate lanes for seq2 and calculate selector for seq2->stmt. */
3728 : 36 : vec_perm_builder seq2_stmt_sel_perm (nelts, nelts, 1);
3729 : 180 : for (i = 0; i < nelts; i++)
3730 : : {
3731 : 144 : unsigned int sel = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, i));
3732 : 144 : unsigned int lane = sel % nelts;
3733 : 144 : unsigned int offs = sel / nelts;
3734 : 144 : unsigned int new_sel;
3735 : :
3736 : : /* Check if we already allocated the lane for seq2. */
3737 : 144 : unsigned int j = 0;
3738 : 255 : for (; j < i; j++)
3739 : : {
3740 : 183 : unsigned int sel_old;
3741 : 183 : sel_old = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, j));
3742 : 183 : unsigned int lane_old = sel_old % nelts;
3743 : 183 : if (lane == lane_old)
3744 : : {
3745 : 72 : new_sel = seq2_stmt_sel_perm[j].to_constant ();
3746 : 72 : new_sel = (new_sel % nelts) + offs * nelts;
3747 : 72 : break;
3748 : : }
3749 : : }
3750 : :
3751 : : /* If the lane is not allocated, we need to do that now. */
3752 : 144 : if (j == i)
3753 : : {
3754 : : unsigned int l_orig = lane;
3755 : 176 : while (lane_assignment[lane] != 0)
3756 : : {
3757 : 104 : lane = (lane + 1) % nelts;
3758 : :
3759 : : /* This should not happen if both sequences utilize no more than
3760 : : half of the lanes. Test anyway to guarantee termination. */
3761 : 104 : if (lane == l_orig)
3762 : 0 : return false;
3763 : : }
3764 : :
3765 : : /* Allocate lane. */
3766 : 72 : lane_assignment[lane] = 2;
3767 : 72 : new_sel = lane + offs * nelts;
3768 : : }
3769 : :
3770 : 144 : seq2_stmt_sel_perm.quick_push (new_sel);
3771 : : }
3772 : :
3773 : : /* Check if the resulting permuation is cheap. */
3774 : 36 : seq2_stmt_indices->new_vector (seq2_stmt_sel_perm, 2, nelts);
3775 : 36 : tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
3776 : 36 : machine_mode vmode = TYPE_MODE (vectype);
3777 : 36 : if (!can_vec_perm_const_p (vmode, vmode, *seq2_stmt_indices, false))
3778 : : return false;
3779 : :
3780 : : /* Calculate selectors for seq1->v_1_stmt and seq1->v_2_stmt. */
3781 : 36 : vec_perm_builder seq1_v_1_stmt_sel_perm (nelts, nelts, 1);
3782 : 36 : vec_perm_builder seq1_v_2_stmt_sel_perm (nelts, nelts, 1);
3783 : 180 : for (i = 0; i < nelts; i++)
3784 : : {
3785 : 144 : bool use_seq1 = lane_assignment[i] != 2;
3786 : 144 : unsigned int l1, l2;
3787 : :
3788 : 144 : if (use_seq1)
3789 : : {
3790 : : /* Just reuse the selector indices. */
3791 : 72 : tree s1 = gimple_assign_rhs3 (seq1->v_1_stmt);
3792 : 72 : tree s2 = gimple_assign_rhs3 (seq1->v_2_stmt);
3793 : 72 : l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, i));
3794 : 72 : l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, i));
3795 : : }
3796 : : else
3797 : : {
3798 : : /* We moved the lanes for seq2, so we need to adjust for that. */
3799 : 72 : tree s1 = gimple_assign_rhs3 (seq2->v_1_stmt);
3800 : 72 : tree s2 = gimple_assign_rhs3 (seq2->v_2_stmt);
3801 : :
3802 : 72 : unsigned int j = 0;
3803 : 128 : for (; j < i; j++)
3804 : : {
3805 : 128 : unsigned int sel_new;
3806 : 128 : sel_new = seq2_stmt_sel_perm[j].to_constant ();
3807 : 128 : sel_new %= nelts;
3808 : 128 : if (sel_new == i)
3809 : : break;
3810 : : }
3811 : :
3812 : : /* This should not happen. Test anyway to guarantee correctness. */
3813 : 72 : if (j == i)
3814 : : return false;
3815 : :
3816 : 72 : l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, j));
3817 : 72 : l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, j));
3818 : : }
3819 : :
3820 : 216 : seq1_v_1_stmt_sel_perm.quick_push (l1 + (use_seq1 ? 0 : nelts));
3821 : 216 : seq1_v_2_stmt_sel_perm.quick_push (l2 + (use_seq1 ? 0 : nelts));
3822 : : }
3823 : :
3824 : 36 : seq1_v_1_stmt_indices->new_vector (seq1_v_1_stmt_sel_perm, 2, nelts);
3825 : 36 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
3826 : 36 : vmode = TYPE_MODE (vectype);
3827 : 36 : if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_1_stmt_indices, false))
3828 : : return false;
3829 : :
3830 : 36 : seq1_v_2_stmt_indices->new_vector (seq1_v_2_stmt_sel_perm, 2, nelts);
3831 : 36 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
3832 : 36 : vmode = TYPE_MODE (vectype);
3833 : 36 : if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_2_stmt_indices, false))
3834 : : return false;
3835 : :
3836 : : return true;
3837 : 72 : }
3838 : :
3839 : : /* Blend the two given simplifiable vec permute sequences using the
3840 : : given permutations. */
3841 : :
3842 : : static void
3843 : 36 : blend_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
3844 : : vec_perm_simplify_seq seq2,
3845 : : const vec_perm_indices &seq2_stmt_indices,
3846 : : const vec_perm_indices &seq1_v_1_stmt_indices,
3847 : : const vec_perm_indices &seq1_v_2_stmt_indices)
3848 : : {
3849 : : /* We don't need to adjust seq1->stmt because its lanes consumption
3850 : : was already narrowed before entering this function. */
3851 : :
3852 : : /* Adjust seq2->stmt: copy RHS1/RHS2 from seq1->stmt and set new sel. */
3853 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
3854 : : {
3855 : 10 : fprintf (dump_file, "Updating VEC_PERM statment:\n");
3856 : 10 : fprintf (dump_file, "Old stmt: ");
3857 : 10 : print_gimple_stmt (dump_file, seq2->stmt, 0);
3858 : : }
3859 : :
3860 : 36 : gimple_assign_set_rhs1 (seq2->stmt, gimple_assign_rhs1 (seq1->stmt));
3861 : 72 : gimple_assign_set_rhs2 (seq2->stmt, gimple_assign_rhs2 (seq1->stmt));
3862 : 36 : tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
3863 : 36 : tree sel = vect_gen_perm_mask_checked (vectype, seq2_stmt_indices);
3864 : 36 : gimple_assign_set_rhs3 (seq2->stmt, sel);
3865 : 36 : update_stmt (seq2->stmt);
3866 : :
3867 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
3868 : : {
3869 : 10 : fprintf (dump_file, "New stmt: ");
3870 : 10 : print_gimple_stmt (dump_file, seq2->stmt, 0);
3871 : : }
3872 : :
3873 : : /* Adjust seq1->v_1_stmt: copy RHS2 from seq2->v_1_stmt and set new sel. */
3874 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
3875 : : {
3876 : 10 : fprintf (dump_file, "Updating VEC_PERM statment:\n");
3877 : 10 : fprintf (dump_file, "Old stmt: ");
3878 : 10 : print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
3879 : : }
3880 : :
3881 : 36 : gimple_assign_set_rhs2 (seq1->v_1_stmt, gimple_assign_rhs1 (seq2->v_1_stmt));
3882 : 36 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
3883 : 36 : sel = vect_gen_perm_mask_checked (vectype, seq1_v_1_stmt_indices);
3884 : 36 : gimple_assign_set_rhs3 (seq1->v_1_stmt, sel);
3885 : 36 : update_stmt (seq1->v_1_stmt);
3886 : :
3887 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
3888 : : {
3889 : 10 : fprintf (dump_file, "New stmt: ");
3890 : 10 : print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
3891 : : }
3892 : :
3893 : : /* Adjust seq1->v_2_stmt: copy RHS2 from seq2->v_2_stmt and set new sel. */
3894 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
3895 : : {
3896 : 10 : fprintf (dump_file, "Updating VEC_PERM statment:\n");
3897 : 10 : fprintf (dump_file, "Old stmt: ");
3898 : 10 : print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
3899 : : }
3900 : :
3901 : 36 : gimple_assign_set_rhs2 (seq1->v_2_stmt, gimple_assign_rhs1 (seq2->v_2_stmt));
3902 : 36 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
3903 : 36 : sel = vect_gen_perm_mask_checked (vectype, seq1_v_2_stmt_indices);
3904 : 36 : gimple_assign_set_rhs3 (seq1->v_2_stmt, sel);
3905 : 36 : update_stmt (seq1->v_2_stmt);
3906 : :
3907 : 36 : if (dump_file && (dump_flags & TDF_DETAILS))
3908 : : {
3909 : 10 : fprintf (dump_file, "New stmt: ");
3910 : 10 : print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
3911 : : }
3912 : :
3913 : : /* At this point, we have four unmodified seq2 stmts, which will be
3914 : : eliminated by DCE. */
3915 : :
3916 : 36 : if (dump_file)
3917 : 10 : fprintf (dump_file, "Vec perm simplify sequences have been blended.\n\n");
3918 : 36 : }
3919 : :
3920 : : /* Try to blend narrowed vec_perm_simplify_seqs pairwise.
3921 : : The provided list will be empty after this call. */
3922 : :
3923 : : static void
3924 : 282420115 : process_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l)
3925 : : {
3926 : 282420115 : unsigned int i, j;
3927 : 282420115 : vec_perm_simplify_seq seq1, seq2;
3928 : :
3929 : 282420115 : if (l->is_empty ())
3930 : 282420071 : return;
3931 : :
3932 : 44 : if (dump_file && (dump_flags & TDF_DETAILS))
3933 : 12 : fprintf (dump_file, "\nProcessing %u vec perm simplify sequences.\n",
3934 : : l->length ());
3935 : :
3936 : 107 : FOR_EACH_VEC_ELT (*l, i, seq1)
3937 : : {
3938 : 63 : if (i + 1 < l->length ())
3939 : : {
3940 : 50 : FOR_EACH_VEC_ELT_FROM (*l, j, seq2, i + 1)
3941 : : {
3942 : 46 : bool swap = false;
3943 : 46 : if (can_blend_vec_perm_simplify_seqs_p (seq1, seq2, &swap))
3944 : : {
3945 : 36 : vec_perm_indices seq2_stmt_indices;
3946 : 36 : vec_perm_indices seq1_v_1_stmt_indices;
3947 : 36 : vec_perm_indices seq1_v_2_stmt_indices;
3948 : 108 : if (calc_perm_vec_perm_simplify_seqs (swap ? seq2 : seq1,
3949 : : swap ? seq1 : seq2,
3950 : : &seq2_stmt_indices,
3951 : : &seq1_v_1_stmt_indices,
3952 : : &seq1_v_2_stmt_indices))
3953 : : {
3954 : : /* Narrow lane usage. */
3955 : 36 : narrow_vec_perm_simplify_seq (seq1);
3956 : 36 : narrow_vec_perm_simplify_seq (seq2);
3957 : :
3958 : : /* Blend sequences. */
3959 : 36 : blend_vec_perm_simplify_seqs (swap ? seq2 : seq1,
3960 : : swap ? seq1 : seq2,
3961 : : seq2_stmt_indices,
3962 : : seq1_v_1_stmt_indices,
3963 : : seq1_v_2_stmt_indices);
3964 : :
3965 : : /* We can use unordered_remove as we break the loop. */
3966 : 36 : l->unordered_remove (j);
3967 : 36 : XDELETE (seq2);
3968 : 36 : break;
3969 : : }
3970 : 36 : }
3971 : : }
3972 : : }
3973 : :
3974 : : /* We don't need to call l->remove for seq1. */
3975 : 63 : XDELETE (seq1);
3976 : : }
3977 : :
3978 : 44 : l->truncate (0);
3979 : : }
3980 : :
3981 : : static void
3982 : 99 : append_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l,
3983 : : const vec_perm_simplify_seq &seq)
3984 : : {
3985 : : /* If no space on list left, then process the list. */
3986 : 99 : if (!l->space (1))
3987 : 0 : process_vec_perm_simplify_seq_list (l);
3988 : :
3989 : 99 : l->quick_push (seq);
3990 : 99 : }
3991 : :
3992 : : /* Main entry point for the forward propagation and statement combine
3993 : : optimizer. */
3994 : :
3995 : : namespace {
3996 : :
3997 : : const pass_data pass_data_forwprop =
3998 : : {
3999 : : GIMPLE_PASS, /* type */
4000 : : "forwprop", /* name */
4001 : : OPTGROUP_NONE, /* optinfo_flags */
4002 : : TV_TREE_FORWPROP, /* tv_id */
4003 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
4004 : : 0, /* properties_provided */
4005 : : 0, /* properties_destroyed */
4006 : : 0, /* todo_flags_start */
4007 : : TODO_update_ssa, /* todo_flags_finish */
4008 : : };
4009 : :
4010 : : class pass_forwprop : public gimple_opt_pass
4011 : : {
4012 : : public:
4013 : 1132628 : pass_forwprop (gcc::context *ctxt)
4014 : 2265256 : : gimple_opt_pass (pass_data_forwprop, ctxt), last_p (false)
4015 : : {}
4016 : :
4017 : : /* opt_pass methods: */
4018 : 849471 : opt_pass * clone () final override { return new pass_forwprop (m_ctxt); }
4019 : 1132628 : void set_pass_param (unsigned int n, bool param) final override
4020 : : {
4021 : 1132628 : gcc_assert (n == 0);
4022 : 1132628 : last_p = param;
4023 : 1132628 : }
4024 : 5328748 : bool gate (function *) final override { return flag_tree_forwprop; }
4025 : : unsigned int execute (function *) final override;
4026 : :
4027 : : private:
4028 : : /* Determines whether the pass instance should set PROP_last_full_fold. */
4029 : : bool last_p;
4030 : : }; // class pass_forwprop
4031 : :
4032 : : unsigned int
4033 : 5326367 : pass_forwprop::execute (function *fun)
4034 : : {
4035 : 5326367 : unsigned int todoflags = 0;
4036 : :
4037 : 5326367 : cfg_changed = false;
4038 : 5326367 : if (last_p)
4039 : 1005874 : fun->curr_properties |= PROP_last_full_fold;
4040 : :
4041 : 5326367 : calculate_dominance_info (CDI_DOMINATORS);
4042 : :
4043 : : /* Combine stmts with the stmts defining their operands. Do that
4044 : : in an order that guarantees visiting SSA defs before SSA uses. */
4045 : 10652734 : lattice.create (num_ssa_names);
4046 : 10652734 : lattice.quick_grow_cleared (num_ssa_names);
4047 : 5326367 : int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (fun));
4048 : 5326367 : int postorder_num = pre_and_rev_post_order_compute_fn (fun, NULL,
4049 : : postorder, false);
4050 : 5326367 : int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
4051 : 47642638 : for (int i = 0; i < postorder_num; ++i)
4052 : : {
4053 : 42316271 : bb_to_rpo[postorder[i]] = i;
4054 : 42316271 : edge_iterator ei;
4055 : 42316271 : edge e;
4056 : 101918480 : FOR_EACH_EDGE (e, ei, BASIC_BLOCK_FOR_FN (fun, postorder[i])->succs)
4057 : 59602209 : e->flags &= ~EDGE_EXECUTABLE;
4058 : : }
4059 : 5326367 : single_succ_edge (BASIC_BLOCK_FOR_FN (fun, ENTRY_BLOCK))->flags
4060 : 5326367 : |= EDGE_EXECUTABLE;
4061 : 5326367 : auto_vec<gimple *, 4> to_fixup;
4062 : 5326367 : auto_vec<gimple *, 32> to_remove;
4063 : 5326367 : auto_vec<unsigned, 32> to_remove_defs;
4064 : 5326367 : auto_vec<std::pair<int, int>, 10> edges_to_remove;
4065 : 5326367 : auto_bitmap simple_dce_worklist;
4066 : 5326367 : auto_bitmap need_ab_cleanup;
4067 : 5326367 : to_purge = BITMAP_ALLOC (NULL);
4068 : 5326367 : auto_vec<vec_perm_simplify_seq, 8> vec_perm_simplify_seq_list;
4069 : 47642638 : for (int i = 0; i < postorder_num; ++i)
4070 : : {
4071 : 42316271 : gimple_stmt_iterator gsi;
4072 : 42316271 : basic_block bb = BASIC_BLOCK_FOR_FN (fun, postorder[i]);
4073 : 42316271 : edge_iterator ei;
4074 : 42316271 : edge e;
4075 : :
4076 : : /* Skip processing not executable blocks. We could improve
4077 : : single_use tracking by at least unlinking uses from unreachable
4078 : : blocks but since blocks with uses are not processed in a
4079 : : meaningful order this is probably not worth it. */
4080 : 42316271 : bool any = false;
4081 : 43376245 : FOR_EACH_EDGE (e, ei, bb->preds)
4082 : : {
4083 : 43364000 : if ((e->flags & EDGE_EXECUTABLE)
4084 : : /* We can handle backedges in natural loops correctly but
4085 : : for irreducible regions we have to take all backedges
4086 : : conservatively when we did not visit the source yet. */
4087 : 43364000 : || (bb_to_rpo[e->src->index] > i
4088 : 621367 : && !dominated_by_p (CDI_DOMINATORS, e->src, e->dest)))
4089 : : {
4090 : : any = true;
4091 : : break;
4092 : : }
4093 : : }
4094 : 42316271 : if (!any)
4095 : 12245 : continue;
4096 : :
4097 : : /* Record degenerate PHIs in the lattice. */
4098 : 57627080 : for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
4099 : 15323054 : gsi_next (&si))
4100 : : {
4101 : 15323054 : gphi *phi = si.phi ();
4102 : 15323054 : tree res = gimple_phi_result (phi);
4103 : 30646108 : if (virtual_operand_p (res))
4104 : 7131748 : continue;
4105 : :
4106 : 8191306 : tree first = NULL_TREE;
4107 : 8191306 : bool all_same = true;
4108 : 8191306 : edge_iterator ei;
4109 : 8191306 : edge e;
4110 : 16841254 : FOR_EACH_EDGE (e, ei, bb->preds)
4111 : : {
4112 : : /* Ignore not executable forward edges. */
4113 : 16621099 : if (!(e->flags & EDGE_EXECUTABLE))
4114 : : {
4115 : 3801877 : if (bb_to_rpo[e->src->index] < i)
4116 : 5533 : continue;
4117 : : /* Avoid equivalences from backedges - while we might
4118 : : be able to make irreducible regions reducible and
4119 : : thus turning a back into a forward edge we do not
4120 : : want to deal with the intermediate SSA issues that
4121 : : exposes. */
4122 : : all_same = false;
4123 : : }
4124 : 16615566 : tree use = PHI_ARG_DEF_FROM_EDGE (phi, e);
4125 : 16615566 : if (use == res)
4126 : : /* The PHI result can also appear on a backedge, if so
4127 : : we can ignore this case for the purpose of determining
4128 : : the singular value. */
4129 : : ;
4130 : 16601975 : else if (! first)
4131 : : first = use;
4132 : 8410669 : else if (! operand_equal_p (first, use, 0))
4133 : : {
4134 : : all_same = false;
4135 : : break;
4136 : : }
4137 : : }
4138 : 8191306 : if (all_same)
4139 : : {
4140 : 215454 : if (may_propagate_copy (res, first))
4141 : 214984 : to_remove_defs.safe_push (SSA_NAME_VERSION (res));
4142 : 215454 : fwprop_set_lattice_val (res, first);
4143 : : }
4144 : : }
4145 : :
4146 : : /* Apply forward propagation to all stmts in the basic-block.
4147 : : Note we update GSI within the loop as necessary. */
4148 : 42304026 : unsigned int uid = 1;
4149 : 387497435 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
4150 : : {
4151 : 302889383 : gimple *stmt = gsi_stmt (gsi);
4152 : 302889383 : tree lhs, rhs;
4153 : 302889383 : enum tree_code code;
4154 : :
4155 : 302889383 : gimple_set_uid (stmt, uid++);
4156 : :
4157 : 302889383 : if (!is_gimple_assign (stmt))
4158 : : {
4159 : 203408038 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
4160 : 203408038 : gsi_next (&gsi);
4161 : 203408038 : continue;
4162 : : }
4163 : :
4164 : 99481345 : lhs = gimple_assign_lhs (stmt);
4165 : 99481345 : rhs = gimple_assign_rhs1 (stmt);
4166 : 99481345 : code = gimple_assign_rhs_code (stmt);
4167 : :
4168 : 136189396 : if (TREE_CODE (lhs) != SSA_NAME
4169 : 99481345 : || has_zero_uses (lhs))
4170 : : {
4171 : 36708051 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
4172 : 36708051 : gsi_next (&gsi);
4173 : 36708051 : continue;
4174 : : }
4175 : :
4176 : : /* If this statement sets an SSA_NAME to an address,
4177 : : try to propagate the address into the uses of the SSA_NAME. */
4178 : 62773294 : if ((code == ADDR_EXPR
4179 : : /* Handle pointer conversions on invariant addresses
4180 : : as well, as this is valid gimple. */
4181 : 60603368 : || (CONVERT_EXPR_CODE_P (code)
4182 : 8589453 : && TREE_CODE (rhs) == ADDR_EXPR
4183 : 348129 : && POINTER_TYPE_P (TREE_TYPE (lhs))))
4184 : 62773518 : && TREE_CODE (TREE_OPERAND (rhs, 0)) != TARGET_MEM_REF)
4185 : : {
4186 : 2169727 : tree base = get_base_address (TREE_OPERAND (rhs, 0));
4187 : 2169727 : if ((!base
4188 : 2169727 : || !DECL_P (base)
4189 : 134631 : || decl_address_invariant_p (base))
4190 : 2169727 : && !stmt_references_abnormal_ssa_name (stmt)
4191 : 4339438 : && forward_propagate_addr_expr (lhs, rhs, true))
4192 : : {
4193 : 416212 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
4194 : 416212 : release_defs (stmt);
4195 : 416212 : gsi_remove (&gsi, true);
4196 : : }
4197 : : else
4198 : 1753515 : gsi_next (&gsi);
4199 : : }
4200 : 60603567 : else if (code == POINTER_PLUS_EXPR)
4201 : : {
4202 : 3318411 : tree off = gimple_assign_rhs2 (stmt);
4203 : 3318411 : if (TREE_CODE (off) == INTEGER_CST
4204 : 996397 : && can_propagate_from (stmt)
4205 : 996044 : && !simple_iv_increment_p (stmt)
4206 : : /* ??? Better adjust the interface to that function
4207 : : instead of building new trees here. */
4208 : 4059048 : && forward_propagate_addr_expr
4209 : 2221911 : (lhs,
4210 : : build1_loc (gimple_location (stmt),
4211 : 740637 : ADDR_EXPR, TREE_TYPE (rhs),
4212 : 740637 : fold_build2 (MEM_REF,
4213 : : TREE_TYPE (TREE_TYPE (rhs)),
4214 : : rhs,
4215 : : fold_convert (ptr_type_node,
4216 : : off))), true))
4217 : : {
4218 : 273406 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
4219 : 273406 : release_defs (stmt);
4220 : 273406 : gsi_remove (&gsi, true);
4221 : : }
4222 : 3045005 : else if (is_gimple_min_invariant (rhs))
4223 : : {
4224 : : /* Make sure to fold &a[0] + off_1 here. */
4225 : 391205 : fold_stmt_inplace (&gsi);
4226 : 391205 : update_stmt (stmt);
4227 : 391205 : if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
4228 : 391187 : gsi_next (&gsi);
4229 : : }
4230 : : else
4231 : 2653800 : gsi_next (&gsi);
4232 : : }
4233 : 57285156 : else if (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE
4234 : 210400 : && gimple_assign_load_p (stmt)
4235 : 133782 : && !gimple_has_volatile_ops (stmt)
4236 : 39713 : && TREE_CODE (rhs) != TARGET_MEM_REF
4237 : 39688 : && TREE_CODE (rhs) != BIT_FIELD_REF
4238 : 57324840 : && !stmt_can_throw_internal (fun, stmt))
4239 : : {
4240 : : /* Rewrite loads used only in real/imagpart extractions to
4241 : : component-wise loads. */
4242 : 39559 : use_operand_p use_p;
4243 : 39559 : imm_use_iterator iter;
4244 : 39559 : bool rewrite = true;
4245 : 44305 : FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
4246 : : {
4247 : 42227 : gimple *use_stmt = USE_STMT (use_p);
4248 : 42227 : if (is_gimple_debug (use_stmt))
4249 : 684 : continue;
4250 : 41543 : if (!is_gimple_assign (use_stmt)
4251 : 27348 : || (gimple_assign_rhs_code (use_stmt) != REALPART_EXPR
4252 : 25302 : && gimple_assign_rhs_code (use_stmt) != IMAGPART_EXPR)
4253 : 45605 : || TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) != lhs)
4254 : : {
4255 : : rewrite = false;
4256 : : break;
4257 : : }
4258 : : }
4259 : 39559 : if (rewrite)
4260 : : {
4261 : 2078 : gimple *use_stmt;
4262 : 6550 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
4263 : : {
4264 : 4472 : if (is_gimple_debug (use_stmt))
4265 : : {
4266 : 449 : if (gimple_debug_bind_p (use_stmt))
4267 : : {
4268 : 449 : gimple_debug_bind_reset_value (use_stmt);
4269 : 449 : update_stmt (use_stmt);
4270 : : }
4271 : 449 : continue;
4272 : : }
4273 : :
4274 : 8046 : tree new_rhs = build1 (gimple_assign_rhs_code (use_stmt),
4275 : 4023 : TREE_TYPE (TREE_TYPE (rhs)),
4276 : : unshare_expr (rhs));
4277 : 4023 : gimple *new_stmt
4278 : 4023 : = gimple_build_assign (gimple_assign_lhs (use_stmt),
4279 : : new_rhs);
4280 : :
4281 : 4023 : location_t loc = gimple_location (use_stmt);
4282 : 4023 : gimple_set_location (new_stmt, loc);
4283 : 4023 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
4284 : 4023 : unlink_stmt_vdef (use_stmt);
4285 : 4023 : gsi_remove (&gsi2, true);
4286 : :
4287 : 4023 : gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
4288 : 2078 : }
4289 : :
4290 : 2078 : release_defs (stmt);
4291 : 2078 : gsi_remove (&gsi, true);
4292 : : }
4293 : : else
4294 : 37481 : gsi_next (&gsi);
4295 : : }
4296 : 57245597 : else if (TREE_CODE (TREE_TYPE (lhs)) == VECTOR_TYPE
4297 : 1560687 : && (TYPE_MODE (TREE_TYPE (lhs)) == BLKmode
4298 : : /* After vector lowering rewrite all loads, but
4299 : : initially do not since this conflicts with
4300 : : vector CONSTRUCTOR to shuffle optimization. */
4301 : 1540273 : || (fun->curr_properties & PROP_gimple_lvec))
4302 : 841717 : && gimple_assign_load_p (stmt)
4303 : 281310 : && !gimple_has_volatile_ops (stmt)
4304 : 266674 : && !stmt_can_throw_internal (fun, stmt)
4305 : 57512271 : && (!VAR_P (rhs) || !DECL_HARD_REGISTER (rhs)))
4306 : 266172 : optimize_vector_load (&gsi);
4307 : :
4308 : 56979425 : else if (code == COMPLEX_EXPR)
4309 : : {
4310 : : /* Rewrite stores of a single-use complex build expression
4311 : : to component-wise stores. */
4312 : 36140 : use_operand_p use_p;
4313 : 36140 : gimple *use_stmt, *def1, *def2;
4314 : 36140 : tree rhs2;
4315 : 36140 : if (single_imm_use (lhs, &use_p, &use_stmt)
4316 : 34010 : && gimple_store_p (use_stmt)
4317 : 40910 : && !gimple_has_volatile_ops (use_stmt)
4318 : 2539 : && is_gimple_assign (use_stmt)
4319 : 2535 : && (TREE_CODE (TREE_TYPE (gimple_assign_lhs (use_stmt)))
4320 : : == COMPLEX_TYPE)
4321 : 38670 : && (TREE_CODE (gimple_assign_lhs (use_stmt))
4322 : : != TARGET_MEM_REF))
4323 : : {
4324 : 2530 : tree use_lhs = gimple_assign_lhs (use_stmt);
4325 : 2530 : if (auto_var_p (use_lhs))
4326 : 600 : DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
4327 : 5060 : tree new_lhs = build1 (REALPART_EXPR,
4328 : 2530 : TREE_TYPE (TREE_TYPE (use_lhs)),
4329 : : unshare_expr (use_lhs));
4330 : 2530 : gimple *new_stmt = gimple_build_assign (new_lhs, rhs);
4331 : 2530 : location_t loc = gimple_location (use_stmt);
4332 : 2530 : gimple_set_location (new_stmt, loc);
4333 : 5060 : gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
4334 : 2530 : gimple_set_vdef (new_stmt, make_ssa_name (gimple_vop (fun)));
4335 : 5060 : SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
4336 : 5060 : gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
4337 : 2530 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
4338 : 2530 : gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
4339 : :
4340 : 5060 : new_lhs = build1 (IMAGPART_EXPR,
4341 : 2530 : TREE_TYPE (TREE_TYPE (use_lhs)),
4342 : : unshare_expr (use_lhs));
4343 : 2530 : gimple_assign_set_lhs (use_stmt, new_lhs);
4344 : 2530 : gimple_assign_set_rhs1 (use_stmt, gimple_assign_rhs2 (stmt));
4345 : 2530 : update_stmt (use_stmt);
4346 : :
4347 : 2530 : release_defs (stmt);
4348 : 2530 : gsi_remove (&gsi, true);
4349 : : }
4350 : : /* Rewrite a component-wise load of a complex to a complex
4351 : : load if the components are not used separately. */
4352 : 33610 : else if (TREE_CODE (rhs) == SSA_NAME
4353 : 33170 : && has_single_use (rhs)
4354 : 29834 : && ((rhs2 = gimple_assign_rhs2 (stmt)), true)
4355 : 29834 : && TREE_CODE (rhs2) == SSA_NAME
4356 : 28164 : && has_single_use (rhs2)
4357 : 27744 : && (def1 = SSA_NAME_DEF_STMT (rhs),
4358 : 27744 : gimple_assign_load_p (def1))
4359 : 1104 : && (def2 = SSA_NAME_DEF_STMT (rhs2),
4360 : 1104 : gimple_assign_load_p (def2))
4361 : 1624 : && (gimple_vuse (def1) == gimple_vuse (def2))
4362 : 809 : && !gimple_has_volatile_ops (def1)
4363 : 809 : && !gimple_has_volatile_ops (def2)
4364 : 809 : && !stmt_can_throw_internal (fun, def1)
4365 : 809 : && !stmt_can_throw_internal (fun, def2)
4366 : 809 : && gimple_assign_rhs_code (def1) == REALPART_EXPR
4367 : 554 : && gimple_assign_rhs_code (def2) == IMAGPART_EXPR
4368 : 34164 : && operand_equal_p (TREE_OPERAND (gimple_assign_rhs1
4369 : : (def1), 0),
4370 : 554 : TREE_OPERAND (gimple_assign_rhs1
4371 : : (def2), 0)))
4372 : : {
4373 : 554 : tree cl = TREE_OPERAND (gimple_assign_rhs1 (def1), 0);
4374 : 554 : gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (cl));
4375 : 554 : gcc_assert (gsi_stmt (gsi) == stmt);
4376 : 1108 : gimple_set_vuse (stmt, gimple_vuse (def1));
4377 : 554 : gimple_set_modified (stmt, true);
4378 : 554 : gimple_stmt_iterator gsi2 = gsi_for_stmt (def1);
4379 : 554 : gsi_remove (&gsi, false);
4380 : 554 : gsi_insert_after (&gsi2, stmt, GSI_SAME_STMT);
4381 : : }
4382 : : else
4383 : 33056 : gsi_next (&gsi);
4384 : : }
4385 : 56943285 : else if (code == CONSTRUCTOR
4386 : 161586 : && VECTOR_TYPE_P (TREE_TYPE (rhs))
4387 : 161586 : && TYPE_MODE (TREE_TYPE (rhs)) == BLKmode
4388 : 2732 : && CONSTRUCTOR_NELTS (rhs) > 0
4389 : 56946017 : && (!VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
4390 : 492 : || (TYPE_MODE (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
4391 : : != BLKmode)))
4392 : : {
4393 : : /* Rewrite stores of a single-use vector constructors
4394 : : to component-wise stores if the mode isn't supported. */
4395 : 2731 : use_operand_p use_p;
4396 : 2731 : gimple *use_stmt;
4397 : 2731 : if (single_imm_use (lhs, &use_p, &use_stmt)
4398 : 2346 : && gimple_store_p (use_stmt)
4399 : 2880 : && !gimple_has_volatile_ops (use_stmt)
4400 : 1434 : && !stmt_can_throw_internal (fun, use_stmt)
4401 : 4159 : && is_gimple_assign (use_stmt))
4402 : : {
4403 : 1428 : tree elt_t = TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value);
4404 : 1428 : unsigned HOST_WIDE_INT elt_w
4405 : 1428 : = tree_to_uhwi (TYPE_SIZE (elt_t));
4406 : 1428 : unsigned HOST_WIDE_INT n
4407 : 1428 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs)));
4408 : 1428 : tree use_lhs = gimple_assign_lhs (use_stmt);
4409 : 1428 : if (auto_var_p (use_lhs))
4410 : 533 : DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
4411 : 895 : else if (TREE_CODE (use_lhs) == TARGET_MEM_REF)
4412 : : {
4413 : 1 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
4414 : 1 : use_lhs = prepare_target_mem_ref_lvalue (use_lhs, &gsi2);
4415 : : }
4416 : 32635 : for (unsigned HOST_WIDE_INT bi = 0; bi < n; bi += elt_w)
4417 : : {
4418 : 31207 : unsigned HOST_WIDE_INT ci = bi / elt_w;
4419 : 31207 : tree new_rhs;
4420 : 31207 : if (ci < CONSTRUCTOR_NELTS (rhs))
4421 : 30589 : new_rhs = CONSTRUCTOR_ELT (rhs, ci)->value;
4422 : : else
4423 : 618 : new_rhs = build_zero_cst (elt_t);
4424 : 31207 : tree new_lhs = build3 (BIT_FIELD_REF,
4425 : : elt_t,
4426 : : unshare_expr (use_lhs),
4427 : 31207 : bitsize_int (elt_w),
4428 : 31207 : bitsize_int (bi));
4429 : 31207 : gimple *new_stmt = gimple_build_assign (new_lhs, new_rhs);
4430 : 31207 : location_t loc = gimple_location (use_stmt);
4431 : 31207 : gimple_set_location (new_stmt, loc);
4432 : 62414 : gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
4433 : 31207 : gimple_set_vdef (new_stmt,
4434 : : make_ssa_name (gimple_vop (fun)));
4435 : 62414 : SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
4436 : 62414 : gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
4437 : 31207 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
4438 : 31207 : gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
4439 : : }
4440 : 1428 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
4441 : 1428 : unlink_stmt_vdef (use_stmt);
4442 : 1428 : release_defs (use_stmt);
4443 : 1428 : gsi_remove (&gsi2, true);
4444 : 1428 : release_defs (stmt);
4445 : 1428 : gsi_remove (&gsi, true);
4446 : : }
4447 : : else
4448 : 1303 : gsi_next (&gsi);
4449 : : }
4450 : 56940554 : else if (code == VEC_PERM_EXPR)
4451 : : {
4452 : : /* Find vectorized sequences, where we can reduce the lane
4453 : : utilization. The narrowing will be donw later and only
4454 : : if we find a pair of sequences that can be blended. */
4455 : 167299 : gassign *assign = dyn_cast <gassign *> (stmt);
4456 : 167299 : vec_perm_simplify_seq seq;
4457 : 167299 : if (recognise_vec_perm_simplify_seq (assign, &seq))
4458 : 99 : append_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list,
4459 : : seq);
4460 : :
4461 : 167299 : gsi_next (&gsi);
4462 : : }
4463 : : else
4464 : 56773255 : gsi_next (&gsi);
4465 : : }
4466 : :
4467 : 42304026 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
4468 : :
4469 : : /* Combine stmts with the stmts defining their operands.
4470 : : Note we update GSI within the loop as necessary. */
4471 : 387178578 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4472 : : {
4473 : 302570526 : gimple *stmt = gsi_stmt (gsi);
4474 : :
4475 : : /* Mark stmt as potentially needing revisiting. */
4476 : 302570526 : gimple_set_plf (stmt, GF_PLF_1, false);
4477 : :
4478 : 302570526 : bool can_make_abnormal_goto = (is_gimple_call (stmt)
4479 : 302570526 : && stmt_can_make_abnormal_goto (stmt));
4480 : :
4481 : : /* Substitute from our lattice. We need to do so only once. */
4482 : 302570526 : bool substituted_p = false;
4483 : 302570526 : use_operand_p usep;
4484 : 302570526 : ssa_op_iter iter;
4485 : 450906636 : FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
4486 : : {
4487 : 148336110 : tree use = USE_FROM_PTR (usep);
4488 : 148336110 : tree val = fwprop_ssa_val (use);
4489 : 148336110 : if (val && val != use)
4490 : : {
4491 : 1748963 : if (!is_gimple_debug (stmt))
4492 : 1466176 : bitmap_set_bit (simple_dce_worklist, SSA_NAME_VERSION (use));
4493 : 1748963 : if (may_propagate_copy (use, val))
4494 : : {
4495 : 1745850 : propagate_value (usep, val);
4496 : 1745850 : substituted_p = true;
4497 : : }
4498 : : }
4499 : : }
4500 : 302570526 : if (substituted_p
4501 : 1692404 : && is_gimple_assign (stmt)
4502 : 303601175 : && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
4503 : 21631 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
4504 : 302570526 : if (substituted_p
4505 : 302570526 : && can_make_abnormal_goto
4506 : 302570526 : && !stmt_can_make_abnormal_goto (stmt))
4507 : 3 : bitmap_set_bit (need_ab_cleanup, bb->index);
4508 : :
4509 : 304542047 : bool changed;
4510 : 609084094 : do
4511 : : {
4512 : 304542047 : gimple *orig_stmt = stmt = gsi_stmt (gsi);
4513 : 304542047 : bool was_noreturn = (is_gimple_call (stmt)
4514 : 304542047 : && gimple_call_noreturn_p (stmt));
4515 : 304542047 : changed = false;
4516 : :
4517 : 304542047 : auto_vec<tree, 8> uses;
4518 : 455502585 : FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
4519 : 150960538 : if (uses.space (1))
4520 : 150580020 : uses.quick_push (USE_FROM_PTR (usep));
4521 : :
4522 : 304542047 : if (fold_stmt (&gsi, fwprop_ssa_val, simple_dce_worklist))
4523 : : {
4524 : 4314246 : changed = true;
4525 : 4314246 : stmt = gsi_stmt (gsi);
4526 : : /* Cleanup the CFG if we simplified a condition to
4527 : : true or false. */
4528 : 4314246 : if (gcond *cond = dyn_cast <gcond *> (stmt))
4529 : 2557061 : if (gimple_cond_true_p (cond)
4530 : 2557061 : || gimple_cond_false_p (cond))
4531 : 20561 : cfg_changed = true;
4532 : : /* Queue old uses for simple DCE if not debug statement. */
4533 : 4314246 : if (!is_gimple_debug (stmt))
4534 : 18421661 : for (tree use : uses)
4535 : 5509346 : if (TREE_CODE (use) == SSA_NAME
4536 : 5509346 : && !SSA_NAME_IS_DEFAULT_DEF (use))
4537 : 4979582 : bitmap_set_bit (simple_dce_worklist,
4538 : 4979582 : SSA_NAME_VERSION (use));
4539 : : }
4540 : :
4541 : 304531906 : if (changed || substituted_p)
4542 : : {
4543 : 5594451 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
4544 : 71 : bitmap_set_bit (to_purge, bb->index);
4545 : 5594451 : if (!was_noreturn
4546 : 5594451 : && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
4547 : 12 : to_fixup.safe_push (stmt);
4548 : 5594451 : update_stmt (stmt);
4549 : 5594451 : substituted_p = false;
4550 : : }
4551 : :
4552 : 304542047 : switch (gimple_code (stmt))
4553 : : {
4554 : 100632831 : case GIMPLE_ASSIGN:
4555 : 100632831 : {
4556 : 100632831 : tree rhs1 = gimple_assign_rhs1 (stmt);
4557 : 100632831 : enum tree_code code = gimple_assign_rhs_code (stmt);
4558 : :
4559 : 100632831 : if (TREE_CODE_CLASS (code) == tcc_comparison)
4560 : : {
4561 : 2293067 : int did_something;
4562 : 2293067 : did_something = forward_propagate_into_comparison (&gsi);
4563 : 2293067 : if (maybe_clean_or_replace_eh_stmt (stmt, gsi_stmt (gsi)))
4564 : 6 : bitmap_set_bit (to_purge, bb->index);
4565 : 2293067 : if (did_something == 2)
4566 : 0 : cfg_changed = true;
4567 : 2293067 : changed = did_something != 0;
4568 : : }
4569 : 98339764 : else if ((code == PLUS_EXPR
4570 : 98339764 : || code == BIT_IOR_EXPR
4571 : 88740770 : || code == BIT_XOR_EXPR)
4572 : 98466650 : && simplify_rotate (&gsi))
4573 : : changed = true;
4574 : 98337225 : else if (code == VEC_PERM_EXPR)
4575 : : {
4576 : 167567 : int did_something = simplify_permutation (&gsi);
4577 : 167567 : if (did_something == 2)
4578 : 0 : cfg_changed = true;
4579 : 167567 : changed = did_something != 0;
4580 : : }
4581 : 98169658 : else if (code == CONSTRUCTOR
4582 : 98169658 : && TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
4583 : 172152 : changed = simplify_vector_constructor (&gsi);
4584 : 97997506 : else if (code == ARRAY_REF)
4585 : 1910905 : changed = simplify_count_trailing_zeroes (&gsi);
4586 : : break;
4587 : : }
4588 : :
4589 : 103563 : case GIMPLE_SWITCH:
4590 : 103563 : changed = simplify_gimple_switch (as_a <gswitch *> (stmt),
4591 : : edges_to_remove);
4592 : 103563 : break;
4593 : :
4594 : 17042286 : case GIMPLE_COND:
4595 : 17042286 : {
4596 : 17042286 : int did_something = forward_propagate_into_gimple_cond
4597 : 17042286 : (as_a <gcond *> (stmt));
4598 : 17042286 : if (did_something == 2)
4599 : 2102 : cfg_changed = true;
4600 : 17042286 : changed = did_something != 0;
4601 : 17042286 : break;
4602 : : }
4603 : :
4604 : 21802107 : case GIMPLE_CALL:
4605 : 21802107 : {
4606 : 21802107 : tree callee = gimple_call_fndecl (stmt);
4607 : 21802107 : if (callee != NULL_TREE
4608 : 21802107 : && fndecl_built_in_p (callee, BUILT_IN_NORMAL))
4609 : 5638694 : changed = simplify_builtin_call (&gsi, callee);
4610 : : break;
4611 : : }
4612 : :
4613 : 304542047 : default:;
4614 : : }
4615 : :
4616 : 304542047 : if (changed)
4617 : : {
4618 : : /* If the stmt changed then re-visit it and the statements
4619 : : inserted before it. */
4620 : 6224537 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
4621 : 3935517 : if (gimple_plf (gsi_stmt (gsi), GF_PLF_1))
4622 : : break;
4623 : 1971521 : if (gsi_end_p (gsi))
4624 : 325024 : gsi = gsi_start_bb (bb);
4625 : : else
4626 : 1809009 : gsi_next (&gsi);
4627 : : }
4628 : 304542047 : }
4629 : : while (changed);
4630 : :
4631 : : /* Stmt no longer needs to be revisited. */
4632 : 302570526 : stmt = gsi_stmt (gsi);
4633 : 302570526 : gcc_checking_assert (!gimple_plf (stmt, GF_PLF_1));
4634 : 302570526 : gimple_set_plf (stmt, GF_PLF_1, true);
4635 : :
4636 : : /* Fill up the lattice. */
4637 : 302570526 : if (gimple_assign_single_p (stmt))
4638 : : {
4639 : 66674637 : tree lhs = gimple_assign_lhs (stmt);
4640 : 66674637 : tree rhs = gimple_assign_rhs1 (stmt);
4641 : 66674637 : if (TREE_CODE (lhs) == SSA_NAME)
4642 : : {
4643 : 30547377 : tree val = lhs;
4644 : 30547377 : if (TREE_CODE (rhs) == SSA_NAME)
4645 : 746811 : val = fwprop_ssa_val (rhs);
4646 : 29800566 : else if (is_gimple_min_invariant (rhs))
4647 : 410600 : val = rhs;
4648 : : /* If we can propagate the lattice-value mark the
4649 : : stmt for removal. */
4650 : 30547377 : if (val != lhs
4651 : 30547377 : && may_propagate_copy (lhs, val))
4652 : 1154113 : to_remove_defs.safe_push (SSA_NAME_VERSION (lhs));
4653 : 30547377 : fwprop_set_lattice_val (lhs, val);
4654 : : }
4655 : : }
4656 : 235895889 : else if (gimple_nop_p (stmt))
4657 : 81052 : to_remove.safe_push (stmt);
4658 : : }
4659 : :
4660 : : /* Substitute in destination PHI arguments. */
4661 : 101896482 : FOR_EACH_EDGE (e, ei, bb->succs)
4662 : 59592456 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
4663 : 98733937 : !gsi_end_p (gsi); gsi_next (&gsi))
4664 : : {
4665 : 39141481 : gphi *phi = gsi.phi ();
4666 : 39141481 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
4667 : 39141481 : tree arg = USE_FROM_PTR (use_p);
4668 : 64764904 : if (TREE_CODE (arg) != SSA_NAME
4669 : 39141481 : || virtual_operand_p (arg))
4670 : 25623423 : continue;
4671 : 13518058 : tree val = fwprop_ssa_val (arg);
4672 : 13518058 : if (val != arg
4673 : 13518058 : && may_propagate_copy (arg, val, !(e->flags & EDGE_ABNORMAL)))
4674 : 245648 : propagate_value (use_p, val);
4675 : : }
4676 : :
4677 : : /* Mark outgoing exectuable edges. */
4678 : 42304026 : if (edge e = find_taken_edge (bb, NULL))
4679 : : {
4680 : 18311754 : e->flags |= EDGE_EXECUTABLE;
4681 : 42324588 : if (EDGE_COUNT (bb->succs) > 1)
4682 : 20562 : cfg_changed = true;
4683 : : }
4684 : : else
4685 : : {
4686 : 65252411 : FOR_EACH_EDGE (e, ei, bb->succs)
4687 : 41260139 : e->flags |= EDGE_EXECUTABLE;
4688 : : }
4689 : : }
4690 : 5326367 : free (postorder);
4691 : 5326367 : free (bb_to_rpo);
4692 : 5326367 : lattice.release ();
4693 : :
4694 : : /* First remove chains of stmts where we check no uses remain. */
4695 : 5326367 : simple_dce_from_worklist (simple_dce_worklist, to_purge);
4696 : :
4697 : 5658634 : auto remove = [](gimple *stmt)
4698 : : {
4699 : 332267 : if (dump_file && (dump_flags & TDF_DETAILS))
4700 : : {
4701 : 1 : fprintf (dump_file, "Removing dead stmt ");
4702 : 1 : print_gimple_stmt (dump_file, stmt, 0);
4703 : 1 : fprintf (dump_file, "\n");
4704 : : }
4705 : 332267 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
4706 : 332267 : if (gimple_code (stmt) == GIMPLE_PHI)
4707 : 102529 : remove_phi_node (&gsi, true);
4708 : : else
4709 : : {
4710 : 229738 : unlink_stmt_vdef (stmt);
4711 : 229738 : gsi_remove (&gsi, true);
4712 : 229738 : release_defs (stmt);
4713 : : }
4714 : 332267 : };
4715 : :
4716 : : /* Then remove stmts we know we can remove even though we did not
4717 : : substitute in dead code regions, so uses can remain. Do so in reverse
4718 : : order to make debug stmt creation possible. */
4719 : 12021831 : while (!to_remove_defs.is_empty())
4720 : : {
4721 : 1369097 : tree def = ssa_name (to_remove_defs.pop ());
4722 : : /* For example remove_prop_source_from_use can remove stmts queued
4723 : : for removal. Deal with this gracefully. */
4724 : 1369097 : if (!def)
4725 : 1117882 : continue;
4726 : 251215 : gimple *stmt = SSA_NAME_DEF_STMT (def);
4727 : 251215 : remove (stmt);
4728 : : }
4729 : :
4730 : : /* Wipe other queued stmts that do not have SSA defs. */
4731 : 5407419 : while (!to_remove.is_empty())
4732 : : {
4733 : 81052 : gimple *stmt = to_remove.pop ();
4734 : 81052 : remove (stmt);
4735 : : }
4736 : :
4737 : : /* Fixup stmts that became noreturn calls. This may require splitting
4738 : : blocks and thus isn't possible during the walk. Do this
4739 : : in reverse order so we don't inadvertedly remove a stmt we want to
4740 : : fixup by visiting a dominating now noreturn call first. */
4741 : 5326379 : while (!to_fixup.is_empty ())
4742 : : {
4743 : 12 : gimple *stmt = to_fixup.pop ();
4744 : 12 : if (dump_file && dump_flags & TDF_DETAILS)
4745 : : {
4746 : 0 : fprintf (dump_file, "Fixing up noreturn call ");
4747 : 0 : print_gimple_stmt (dump_file, stmt, 0);
4748 : 0 : fprintf (dump_file, "\n");
4749 : : }
4750 : 12 : cfg_changed |= fixup_noreturn_call (stmt);
4751 : : }
4752 : :
4753 : 5326367 : cfg_changed |= gimple_purge_all_dead_eh_edges (to_purge);
4754 : 5326367 : cfg_changed |= gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
4755 : 5326367 : BITMAP_FREE (to_purge);
4756 : :
4757 : : /* Remove edges queued from switch stmt simplification. */
4758 : 15979101 : for (auto ep : edges_to_remove)
4759 : : {
4760 : 0 : basic_block src = BASIC_BLOCK_FOR_FN (fun, ep.first);
4761 : 0 : basic_block dest = BASIC_BLOCK_FOR_FN (fun, ep.second);
4762 : 0 : edge e;
4763 : 0 : if (src && dest && (e = find_edge (src, dest)))
4764 : : {
4765 : 0 : free_dominance_info (CDI_DOMINATORS);
4766 : 0 : remove_edge (e);
4767 : 0 : cfg_changed = true;
4768 : : }
4769 : : }
4770 : :
4771 : 10651214 : if (get_range_query (fun) != get_global_range_query ())
4772 : 1520 : disable_ranger (fun);
4773 : :
4774 : 5326367 : if (cfg_changed)
4775 : 9509 : todoflags |= TODO_cleanup_cfg;
4776 : :
4777 : 5326367 : return todoflags;
4778 : 5326367 : }
4779 : :
4780 : : } // anon namespace
4781 : :
4782 : : gimple_opt_pass *
4783 : 283157 : make_pass_forwprop (gcc::context *ctxt)
4784 : : {
4785 : 283157 : return new pass_forwprop (ctxt);
4786 : : }
|