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