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