d: Merge upstream dmd 56589f0f4, druntime 651389b5, phobos 1516ecad9.
[official-gcc.git] / gcc / tree-ssa-forwprop.cc
blob69567ab3275939e084a7ae2e6e73d72b824b9762
1 /* Forward propagation of expressions for single use variables.
2 Copyright (C) 2004-2022 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
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 "builtins.h"
46 #include "tree-cfgcleanup.h"
47 #include "cfganal.h"
48 #include "optabs-tree.h"
49 #include "tree-vector-builder.h"
50 #include "vec-perm-indices.h"
51 #include "internal-fn.h"
52 #include "cgraph.h"
53 #include "tree-ssa.h"
55 /* This pass propagates the RHS of assignment statements into use
56 sites of the LHS of the assignment. It's basically a specialized
57 form of tree combination. It is hoped all of this can disappear
58 when we have a generalized tree combiner.
60 One class of common cases we handle is forward propagating a single use
61 variable into a COND_EXPR.
63 bb0:
64 x = a COND b;
65 if (x) goto ... else goto ...
67 Will be transformed into:
69 bb0:
70 if (a COND b) goto ... else goto ...
72 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
74 Or (assuming c1 and c2 are constants):
76 bb0:
77 x = a + c1;
78 if (x EQ/NEQ c2) goto ... else goto ...
80 Will be transformed into:
82 bb0:
83 if (a EQ/NEQ (c2 - c1)) goto ... else goto ...
85 Similarly for x = a - c1.
89 bb0:
90 x = !a
91 if (x) goto ... else goto ...
93 Will be transformed into:
95 bb0:
96 if (a == 0) goto ... else goto ...
98 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
99 For these cases, we propagate A into all, possibly more than one,
100 COND_EXPRs that use X.
104 bb0:
105 x = (typecast) a
106 if (x) goto ... else goto ...
108 Will be transformed into:
110 bb0:
111 if (a != 0) goto ... else goto ...
113 (Assuming a is an integral type and x is a boolean or x is an
114 integral and a is a boolean.)
116 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
117 For these cases, we propagate A into all, possibly more than one,
118 COND_EXPRs that use X.
120 In addition to eliminating the variable and the statement which assigns
121 a value to the variable, we may be able to later thread the jump without
122 adding insane complexity in the dominator optimizer.
124 Also note these transformations can cascade. We handle this by having
125 a worklist of COND_EXPR statements to examine. As we make a change to
126 a statement, we put it back on the worklist to examine on the next
127 iteration of the main loop.
129 A second class of propagation opportunities arises for ADDR_EXPR
130 nodes.
132 ptr = &x->y->z;
133 res = *ptr;
135 Will get turned into
137 res = x->y->z;
140 ptr = (type1*)&type2var;
141 res = *ptr
143 Will get turned into (if type1 and type2 are the same size
144 and neither have volatile on them):
145 res = VIEW_CONVERT_EXPR<type1>(type2var)
149 ptr = &x[0];
150 ptr2 = ptr + <constant>;
152 Will get turned into
154 ptr2 = &x[constant/elementsize];
158 ptr = &x[0];
159 offset = index * element_size;
160 offset_p = (pointer) offset;
161 ptr2 = ptr + offset_p
163 Will get turned into:
165 ptr2 = &x[index];
168 ssa = (int) decl
169 res = ssa & 1
171 Provided that decl has known alignment >= 2, will get turned into
173 res = 0
175 We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to
176 allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent
177 {NOT_EXPR,NEG_EXPR}.
179 This will (of course) be extended as other needs arise. */
181 static bool forward_propagate_addr_expr (tree, tree, bool);
183 /* Set to true if we delete dead edges during the optimization. */
184 static bool cfg_changed;
186 static tree rhs_to_tree (tree type, gimple *stmt);
188 static bitmap to_purge;
190 /* Const-and-copy lattice. */
191 static vec<tree> lattice;
193 /* Set the lattice entry for NAME to VAL. */
194 static void
195 fwprop_set_lattice_val (tree name, tree val)
197 if (TREE_CODE (name) == SSA_NAME)
199 if (SSA_NAME_VERSION (name) >= lattice.length ())
201 lattice.reserve (num_ssa_names - lattice.length ());
202 lattice.quick_grow_cleared (num_ssa_names);
204 lattice[SSA_NAME_VERSION (name)] = val;
208 /* Invalidate the lattice entry for NAME, done when releasing SSA names. */
209 static void
210 fwprop_invalidate_lattice (tree name)
212 if (name
213 && TREE_CODE (name) == SSA_NAME
214 && SSA_NAME_VERSION (name) < lattice.length ())
215 lattice[SSA_NAME_VERSION (name)] = NULL_TREE;
219 /* Get the statement we can propagate from into NAME skipping
220 trivial copies. Returns the statement which defines the
221 propagation source or NULL_TREE if there is no such one.
222 If SINGLE_USE_ONLY is set considers only sources which have
223 a single use chain up to NAME. If SINGLE_USE_P is non-null,
224 it is set to whether the chain to NAME is a single use chain
225 or not. SINGLE_USE_P is not written to if SINGLE_USE_ONLY is set. */
227 static gimple *
228 get_prop_source_stmt (tree name, bool single_use_only, bool *single_use_p)
230 bool single_use = true;
232 do {
233 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
235 if (!has_single_use (name))
237 single_use = false;
238 if (single_use_only)
239 return NULL;
242 /* If name is defined by a PHI node or is the default def, bail out. */
243 if (!is_gimple_assign (def_stmt))
244 return NULL;
246 /* If def_stmt is a simple copy, continue looking. */
247 if (gimple_assign_rhs_code (def_stmt) == SSA_NAME)
248 name = gimple_assign_rhs1 (def_stmt);
249 else
251 if (!single_use_only && single_use_p)
252 *single_use_p = single_use;
254 return def_stmt;
256 } while (1);
259 /* Checks if the destination ssa name in DEF_STMT can be used as
260 propagation source. Returns true if so, otherwise false. */
262 static bool
263 can_propagate_from (gimple *def_stmt)
265 gcc_assert (is_gimple_assign (def_stmt));
267 /* If the rhs has side-effects we cannot propagate from it. */
268 if (gimple_has_volatile_ops (def_stmt))
269 return false;
271 /* If the rhs is a load we cannot propagate from it. */
272 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_reference
273 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_declaration)
274 return false;
276 /* Constants can be always propagated. */
277 if (gimple_assign_single_p (def_stmt)
278 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
279 return true;
281 /* We cannot propagate ssa names that occur in abnormal phi nodes. */
282 if (stmt_references_abnormal_ssa_name (def_stmt))
283 return false;
285 /* If the definition is a conversion of a pointer to a function type,
286 then we cannot apply optimizations as some targets require
287 function pointers to be canonicalized and in this case this
288 optimization could eliminate a necessary canonicalization. */
289 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
291 tree rhs = gimple_assign_rhs1 (def_stmt);
292 if (POINTER_TYPE_P (TREE_TYPE (rhs))
293 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs))) == FUNCTION_TYPE)
294 return false;
297 return true;
300 /* Remove a chain of dead statements starting at the definition of
301 NAME. The chain is linked via the first operand of the defining statements.
302 If NAME was replaced in its only use then this function can be used
303 to clean up dead stmts. The function handles already released SSA
304 names gracefully.
305 Returns true if cleanup-cfg has to run. */
307 static bool
308 remove_prop_source_from_use (tree name)
310 gimple_stmt_iterator gsi;
311 gimple *stmt;
312 bool cfg_changed = false;
314 do {
315 basic_block bb;
317 if (SSA_NAME_IN_FREE_LIST (name)
318 || SSA_NAME_IS_DEFAULT_DEF (name)
319 || !has_zero_uses (name))
320 return cfg_changed;
322 stmt = SSA_NAME_DEF_STMT (name);
323 if (gimple_code (stmt) == GIMPLE_PHI
324 || gimple_has_side_effects (stmt))
325 return cfg_changed;
327 bb = gimple_bb (stmt);
328 gsi = gsi_for_stmt (stmt);
329 unlink_stmt_vdef (stmt);
330 if (gsi_remove (&gsi, true))
331 bitmap_set_bit (to_purge, bb->index);
332 fwprop_invalidate_lattice (gimple_get_lhs (stmt));
333 release_defs (stmt);
335 name = is_gimple_assign (stmt) ? gimple_assign_rhs1 (stmt) : NULL_TREE;
336 } while (name && TREE_CODE (name) == SSA_NAME);
338 return cfg_changed;
341 /* Return the rhs of a gassign *STMT in a form of a single tree,
342 converted to type TYPE.
344 This should disappear, but is needed so we can combine expressions and use
345 the fold() interfaces. Long term, we need to develop folding and combine
346 routines that deal with gimple exclusively . */
348 static tree
349 rhs_to_tree (tree type, gimple *stmt)
351 location_t loc = gimple_location (stmt);
352 enum tree_code code = gimple_assign_rhs_code (stmt);
353 switch (get_gimple_rhs_class (code))
355 case GIMPLE_TERNARY_RHS:
356 return fold_build3_loc (loc, code, type, gimple_assign_rhs1 (stmt),
357 gimple_assign_rhs2 (stmt),
358 gimple_assign_rhs3 (stmt));
359 case GIMPLE_BINARY_RHS:
360 return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (stmt),
361 gimple_assign_rhs2 (stmt));
362 case GIMPLE_UNARY_RHS:
363 return build1 (code, type, gimple_assign_rhs1 (stmt));
364 case GIMPLE_SINGLE_RHS:
365 return gimple_assign_rhs1 (stmt);
366 default:
367 gcc_unreachable ();
371 /* Combine OP0 CODE OP1 in the context of a COND_EXPR. Returns
372 the folded result in a form suitable for COND_EXPR_COND or
373 NULL_TREE, if there is no suitable simplified form. If
374 INVARIANT_ONLY is true only gimple_min_invariant results are
375 considered simplified. */
377 static tree
378 combine_cond_expr_cond (gimple *stmt, enum tree_code code, tree type,
379 tree op0, tree op1, bool invariant_only)
381 tree t;
383 gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
385 fold_defer_overflow_warnings ();
386 t = fold_binary_loc (gimple_location (stmt), code, type, op0, op1);
387 if (!t)
389 fold_undefer_overflow_warnings (false, NULL, 0);
390 return NULL_TREE;
393 /* Require that we got a boolean type out if we put one in. */
394 gcc_assert (TREE_CODE (TREE_TYPE (t)) == TREE_CODE (type));
396 /* Canonicalize the combined condition for use in a COND_EXPR. */
397 t = canonicalize_cond_expr_cond (t);
399 /* Bail out if we required an invariant but didn't get one. */
400 if (!t || (invariant_only && !is_gimple_min_invariant (t)))
402 fold_undefer_overflow_warnings (false, NULL, 0);
403 return NULL_TREE;
406 bool nowarn = warning_suppressed_p (stmt, OPT_Wstrict_overflow);
407 fold_undefer_overflow_warnings (!nowarn, stmt, 0);
409 return t;
412 /* Combine the comparison OP0 CODE OP1 at LOC with the defining statements
413 of its operand. Return a new comparison tree or NULL_TREE if there
414 were no simplifying combines. */
416 static tree
417 forward_propagate_into_comparison_1 (gimple *stmt,
418 enum tree_code code, tree type,
419 tree op0, tree op1)
421 tree tmp = NULL_TREE;
422 tree rhs0 = NULL_TREE, rhs1 = NULL_TREE;
423 bool single_use0_p = false, single_use1_p = false;
425 /* For comparisons use the first operand, that is likely to
426 simplify comparisons against constants. */
427 if (TREE_CODE (op0) == SSA_NAME)
429 gimple *def_stmt = get_prop_source_stmt (op0, false, &single_use0_p);
430 if (def_stmt && can_propagate_from (def_stmt))
432 enum tree_code def_code = gimple_assign_rhs_code (def_stmt);
433 bool invariant_only_p = !single_use0_p;
435 rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt);
437 /* Always combine comparisons or conversions from booleans. */
438 if (TREE_CODE (op1) == INTEGER_CST
439 && ((CONVERT_EXPR_CODE_P (def_code)
440 && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs0, 0)))
441 == BOOLEAN_TYPE)
442 || TREE_CODE_CLASS (def_code) == tcc_comparison))
443 invariant_only_p = false;
445 tmp = combine_cond_expr_cond (stmt, code, type,
446 rhs0, op1, invariant_only_p);
447 if (tmp)
448 return tmp;
452 /* If that wasn't successful, try the second operand. */
453 if (TREE_CODE (op1) == SSA_NAME)
455 gimple *def_stmt = get_prop_source_stmt (op1, false, &single_use1_p);
456 if (def_stmt && can_propagate_from (def_stmt))
458 rhs1 = rhs_to_tree (TREE_TYPE (op0), def_stmt);
459 tmp = combine_cond_expr_cond (stmt, code, type,
460 op0, rhs1, !single_use1_p);
461 if (tmp)
462 return tmp;
466 /* If that wasn't successful either, try both operands. */
467 if (rhs0 != NULL_TREE
468 && rhs1 != NULL_TREE)
469 tmp = combine_cond_expr_cond (stmt, code, type,
470 rhs0, rhs1,
471 !(single_use0_p && single_use1_p));
473 return tmp;
476 /* Propagate from the ssa name definition statements of the assignment
477 from a comparison at *GSI into the conditional if that simplifies it.
478 Returns 1 if the stmt was modified and 2 if the CFG needs cleanup,
479 otherwise returns 0. */
481 static int
482 forward_propagate_into_comparison (gimple_stmt_iterator *gsi)
484 gimple *stmt = gsi_stmt (*gsi);
485 tree tmp;
486 bool cfg_changed = false;
487 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
488 tree rhs1 = gimple_assign_rhs1 (stmt);
489 tree rhs2 = gimple_assign_rhs2 (stmt);
491 /* Combine the comparison with defining statements. */
492 tmp = forward_propagate_into_comparison_1 (stmt,
493 gimple_assign_rhs_code (stmt),
494 type, rhs1, rhs2);
495 if (tmp && useless_type_conversion_p (type, TREE_TYPE (tmp)))
497 gimple_assign_set_rhs_from_tree (gsi, tmp);
498 fold_stmt (gsi);
499 update_stmt (gsi_stmt (*gsi));
501 if (TREE_CODE (rhs1) == SSA_NAME)
502 cfg_changed |= remove_prop_source_from_use (rhs1);
503 if (TREE_CODE (rhs2) == SSA_NAME)
504 cfg_changed |= remove_prop_source_from_use (rhs2);
505 return cfg_changed ? 2 : 1;
508 return 0;
511 /* Propagate from the ssa name definition statements of COND_EXPR
512 in GIMPLE_COND statement STMT into the conditional if that simplifies it.
513 Returns zero if no statement was changed, one if there were
514 changes and two if cfg_cleanup needs to run. */
516 static int
517 forward_propagate_into_gimple_cond (gcond *stmt)
519 tree tmp;
520 enum tree_code code = gimple_cond_code (stmt);
521 bool cfg_changed = false;
522 tree rhs1 = gimple_cond_lhs (stmt);
523 tree rhs2 = gimple_cond_rhs (stmt);
525 /* We can do tree combining on SSA_NAME and comparison expressions. */
526 if (TREE_CODE_CLASS (gimple_cond_code (stmt)) != tcc_comparison)
527 return 0;
529 tmp = forward_propagate_into_comparison_1 (stmt, code,
530 boolean_type_node,
531 rhs1, rhs2);
532 if (tmp
533 && is_gimple_condexpr_for_cond (tmp))
535 if (dump_file)
537 fprintf (dump_file, " Replaced '");
538 print_gimple_expr (dump_file, stmt, 0);
539 fprintf (dump_file, "' with '");
540 print_generic_expr (dump_file, tmp);
541 fprintf (dump_file, "'\n");
544 gimple_cond_set_condition_from_tree (stmt, unshare_expr (tmp));
545 update_stmt (stmt);
547 if (TREE_CODE (rhs1) == SSA_NAME)
548 cfg_changed |= remove_prop_source_from_use (rhs1);
549 if (TREE_CODE (rhs2) == SSA_NAME)
550 cfg_changed |= remove_prop_source_from_use (rhs2);
551 return (cfg_changed || is_gimple_min_invariant (tmp)) ? 2 : 1;
554 /* Canonicalize _Bool == 0 and _Bool != 1 to _Bool != 0 by swapping edges. */
555 if ((TREE_CODE (TREE_TYPE (rhs1)) == BOOLEAN_TYPE
556 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
557 && TYPE_PRECISION (TREE_TYPE (rhs1)) == 1))
558 && ((code == EQ_EXPR
559 && integer_zerop (rhs2))
560 || (code == NE_EXPR
561 && integer_onep (rhs2))))
563 basic_block bb = gimple_bb (stmt);
564 gimple_cond_set_code (stmt, NE_EXPR);
565 gimple_cond_set_rhs (stmt, build_zero_cst (TREE_TYPE (rhs1)));
566 EDGE_SUCC (bb, 0)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
567 EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
568 return 1;
571 return 0;
574 /* We've just substituted an ADDR_EXPR into stmt. Update all the
575 relevant data structures to match. */
577 static void
578 tidy_after_forward_propagate_addr (gimple *stmt)
580 /* We may have turned a trapping insn into a non-trapping insn. */
581 if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
582 bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
584 if (TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
585 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
588 /* NAME is a SSA_NAME representing DEF_RHS which is of the form
589 ADDR_EXPR <whatever>.
591 Try to forward propagate the ADDR_EXPR into the use USE_STMT.
592 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
593 node or for recovery of array indexing from pointer arithmetic.
595 Return true if the propagation was successful (the propagation can
596 be not totally successful, yet things may have been changed). */
598 static bool
599 forward_propagate_addr_expr_1 (tree name, tree def_rhs,
600 gimple_stmt_iterator *use_stmt_gsi,
601 bool single_use_p)
603 tree lhs, rhs, rhs2, array_ref;
604 gimple *use_stmt = gsi_stmt (*use_stmt_gsi);
605 enum tree_code rhs_code;
606 bool res = true;
608 gcc_assert (TREE_CODE (def_rhs) == ADDR_EXPR);
610 lhs = gimple_assign_lhs (use_stmt);
611 rhs_code = gimple_assign_rhs_code (use_stmt);
612 rhs = gimple_assign_rhs1 (use_stmt);
614 /* Do not perform copy-propagation but recurse through copy chains. */
615 if (TREE_CODE (lhs) == SSA_NAME
616 && rhs_code == SSA_NAME)
617 return forward_propagate_addr_expr (lhs, def_rhs, single_use_p);
619 /* The use statement could be a conversion. Recurse to the uses of the
620 lhs as copyprop does not copy through pointer to integer to pointer
621 conversions and FRE does not catch all cases either.
622 Treat the case of a single-use name and
623 a conversion to def_rhs type separate, though. */
624 if (TREE_CODE (lhs) == SSA_NAME
625 && CONVERT_EXPR_CODE_P (rhs_code))
627 /* If there is a point in a conversion chain where the types match
628 so we can remove a conversion re-materialize the address here
629 and stop. */
630 if (single_use_p
631 && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs)))
633 gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs));
634 gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs));
635 return true;
638 /* Else recurse if the conversion preserves the address value. */
639 if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
640 || POINTER_TYPE_P (TREE_TYPE (lhs)))
641 && (TYPE_PRECISION (TREE_TYPE (lhs))
642 >= TYPE_PRECISION (TREE_TYPE (def_rhs))))
643 return forward_propagate_addr_expr (lhs, def_rhs, single_use_p);
645 return false;
648 /* If this isn't a conversion chain from this on we only can propagate
649 into compatible pointer contexts. */
650 if (!types_compatible_p (TREE_TYPE (name), TREE_TYPE (def_rhs)))
651 return false;
653 /* Propagate through constant pointer adjustments. */
654 if (TREE_CODE (lhs) == SSA_NAME
655 && rhs_code == POINTER_PLUS_EXPR
656 && rhs == name
657 && TREE_CODE (gimple_assign_rhs2 (use_stmt)) == INTEGER_CST)
659 tree new_def_rhs;
660 /* As we come here with non-invariant addresses in def_rhs we need
661 to make sure we can build a valid constant offsetted address
662 for further propagation. Simply rely on fold building that
663 and check after the fact. */
664 new_def_rhs = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (rhs)),
665 def_rhs,
666 fold_convert (ptr_type_node,
667 gimple_assign_rhs2 (use_stmt)));
668 if (TREE_CODE (new_def_rhs) == MEM_REF
669 && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs, 0)))
670 return false;
671 new_def_rhs = build1 (ADDR_EXPR, TREE_TYPE (rhs), new_def_rhs);
673 /* Recurse. If we could propagate into all uses of lhs do not
674 bother to replace into the current use but just pretend we did. */
675 if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p))
676 return true;
678 if (useless_type_conversion_p (TREE_TYPE (lhs),
679 TREE_TYPE (new_def_rhs)))
680 gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs),
681 new_def_rhs);
682 else if (is_gimple_min_invariant (new_def_rhs))
683 gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR, new_def_rhs);
684 else
685 return false;
686 gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt);
687 update_stmt (use_stmt);
688 return true;
691 /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
692 ADDR_EXPR will not appear on the LHS. */
693 tree *lhsp = gimple_assign_lhs_ptr (use_stmt);
694 while (handled_component_p (*lhsp))
695 lhsp = &TREE_OPERAND (*lhsp, 0);
696 lhs = *lhsp;
698 /* Now see if the LHS node is a MEM_REF using NAME. If so,
699 propagate the ADDR_EXPR into the use of NAME and fold the result. */
700 if (TREE_CODE (lhs) == MEM_REF
701 && TREE_OPERAND (lhs, 0) == name)
703 tree def_rhs_base;
704 poly_int64 def_rhs_offset;
705 /* If the address is invariant we can always fold it. */
706 if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
707 &def_rhs_offset)))
709 poly_offset_int off = mem_ref_offset (lhs);
710 tree new_ptr;
711 off += def_rhs_offset;
712 if (TREE_CODE (def_rhs_base) == MEM_REF)
714 off += mem_ref_offset (def_rhs_base);
715 new_ptr = TREE_OPERAND (def_rhs_base, 0);
717 else
718 new_ptr = build_fold_addr_expr (def_rhs_base);
719 TREE_OPERAND (lhs, 0) = new_ptr;
720 TREE_OPERAND (lhs, 1)
721 = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs, 1)), off);
722 tidy_after_forward_propagate_addr (use_stmt);
723 /* Continue propagating into the RHS if this was not the only use. */
724 if (single_use_p)
725 return true;
727 /* If the LHS is a plain dereference and the value type is the same as
728 that of the pointed-to type of the address we can put the
729 dereferenced address on the LHS preserving the original alias-type. */
730 else if (integer_zerop (TREE_OPERAND (lhs, 1))
731 && ((gimple_assign_lhs (use_stmt) == lhs
732 && useless_type_conversion_p
733 (TREE_TYPE (TREE_OPERAND (def_rhs, 0)),
734 TREE_TYPE (gimple_assign_rhs1 (use_stmt))))
735 || types_compatible_p (TREE_TYPE (lhs),
736 TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
737 /* Don't forward anything into clobber stmts if it would result
738 in the lhs no longer being a MEM_REF. */
739 && (!gimple_clobber_p (use_stmt)
740 || TREE_CODE (TREE_OPERAND (def_rhs, 0)) == MEM_REF))
742 tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
743 tree new_offset, new_base, saved, new_lhs;
744 while (handled_component_p (*def_rhs_basep))
745 def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
746 saved = *def_rhs_basep;
747 if (TREE_CODE (*def_rhs_basep) == MEM_REF)
749 new_base = TREE_OPERAND (*def_rhs_basep, 0);
750 new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (lhs, 1)),
751 TREE_OPERAND (*def_rhs_basep, 1));
753 else
755 new_base = build_fold_addr_expr (*def_rhs_basep);
756 new_offset = TREE_OPERAND (lhs, 1);
758 *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep),
759 new_base, new_offset);
760 TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (lhs);
761 TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (lhs);
762 TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (lhs);
763 new_lhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
764 *lhsp = new_lhs;
765 TREE_THIS_VOLATILE (new_lhs) = TREE_THIS_VOLATILE (lhs);
766 TREE_SIDE_EFFECTS (new_lhs) = TREE_SIDE_EFFECTS (lhs);
767 *def_rhs_basep = saved;
768 tidy_after_forward_propagate_addr (use_stmt);
769 /* Continue propagating into the RHS if this was not the
770 only use. */
771 if (single_use_p)
772 return true;
774 else
775 /* We can have a struct assignment dereferencing our name twice.
776 Note that we didn't propagate into the lhs to not falsely
777 claim we did when propagating into the rhs. */
778 res = false;
781 /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
782 nodes from the RHS. */
783 tree *rhsp = gimple_assign_rhs1_ptr (use_stmt);
784 if (TREE_CODE (*rhsp) == ADDR_EXPR)
785 rhsp = &TREE_OPERAND (*rhsp, 0);
786 while (handled_component_p (*rhsp))
787 rhsp = &TREE_OPERAND (*rhsp, 0);
788 rhs = *rhsp;
790 /* Now see if the RHS node is a MEM_REF using NAME. If so,
791 propagate the ADDR_EXPR into the use of NAME and fold the result. */
792 if (TREE_CODE (rhs) == MEM_REF
793 && TREE_OPERAND (rhs, 0) == name)
795 tree def_rhs_base;
796 poly_int64 def_rhs_offset;
797 if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
798 &def_rhs_offset)))
800 poly_offset_int off = mem_ref_offset (rhs);
801 tree new_ptr;
802 off += def_rhs_offset;
803 if (TREE_CODE (def_rhs_base) == MEM_REF)
805 off += mem_ref_offset (def_rhs_base);
806 new_ptr = TREE_OPERAND (def_rhs_base, 0);
808 else
809 new_ptr = build_fold_addr_expr (def_rhs_base);
810 TREE_OPERAND (rhs, 0) = new_ptr;
811 TREE_OPERAND (rhs, 1)
812 = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs, 1)), off);
813 fold_stmt_inplace (use_stmt_gsi);
814 tidy_after_forward_propagate_addr (use_stmt);
815 return res;
817 /* If the RHS is a plain dereference and the value type is the same as
818 that of the pointed-to type of the address we can put the
819 dereferenced address on the RHS preserving the original alias-type. */
820 else if (integer_zerop (TREE_OPERAND (rhs, 1))
821 && ((gimple_assign_rhs1 (use_stmt) == rhs
822 && useless_type_conversion_p
823 (TREE_TYPE (gimple_assign_lhs (use_stmt)),
824 TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
825 || types_compatible_p (TREE_TYPE (rhs),
826 TREE_TYPE (TREE_OPERAND (def_rhs, 0)))))
828 tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
829 tree new_offset, new_base, saved, new_rhs;
830 while (handled_component_p (*def_rhs_basep))
831 def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
832 saved = *def_rhs_basep;
833 if (TREE_CODE (*def_rhs_basep) == MEM_REF)
835 new_base = TREE_OPERAND (*def_rhs_basep, 0);
836 new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (rhs, 1)),
837 TREE_OPERAND (*def_rhs_basep, 1));
839 else
841 new_base = build_fold_addr_expr (*def_rhs_basep);
842 new_offset = TREE_OPERAND (rhs, 1);
844 *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep),
845 new_base, new_offset);
846 TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (rhs);
847 TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (rhs);
848 TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (rhs);
849 new_rhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
850 *rhsp = new_rhs;
851 TREE_THIS_VOLATILE (new_rhs) = TREE_THIS_VOLATILE (rhs);
852 TREE_SIDE_EFFECTS (new_rhs) = TREE_SIDE_EFFECTS (rhs);
853 *def_rhs_basep = saved;
854 fold_stmt_inplace (use_stmt_gsi);
855 tidy_after_forward_propagate_addr (use_stmt);
856 return res;
860 /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there
861 is nothing to do. */
862 if (gimple_assign_rhs_code (use_stmt) != POINTER_PLUS_EXPR
863 || gimple_assign_rhs1 (use_stmt) != name)
864 return false;
866 /* The remaining cases are all for turning pointer arithmetic into
867 array indexing. They only apply when we have the address of
868 element zero in an array. If that is not the case then there
869 is nothing to do. */
870 array_ref = TREE_OPERAND (def_rhs, 0);
871 if ((TREE_CODE (array_ref) != ARRAY_REF
872 || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
873 || TREE_CODE (TREE_OPERAND (array_ref, 1)) != INTEGER_CST)
874 && TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE)
875 return false;
877 rhs2 = gimple_assign_rhs2 (use_stmt);
878 /* Optimize &x[C1] p+ C2 to &x p+ C3 with C3 = C1 * element_size + C2. */
879 if (TREE_CODE (rhs2) == INTEGER_CST)
881 tree new_rhs = build1_loc (gimple_location (use_stmt),
882 ADDR_EXPR, TREE_TYPE (def_rhs),
883 fold_build2 (MEM_REF,
884 TREE_TYPE (TREE_TYPE (def_rhs)),
885 unshare_expr (def_rhs),
886 fold_convert (ptr_type_node,
887 rhs2)));
888 gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs);
889 use_stmt = gsi_stmt (*use_stmt_gsi);
890 update_stmt (use_stmt);
891 tidy_after_forward_propagate_addr (use_stmt);
892 return true;
895 return false;
898 /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
900 Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
901 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
902 node or for recovery of array indexing from pointer arithmetic.
904 PARENT_SINGLE_USE_P tells if, when in a recursive invocation, NAME was
905 the single use in the previous invocation. Pass true when calling
906 this as toplevel.
908 Returns true, if all uses have been propagated into. */
910 static bool
911 forward_propagate_addr_expr (tree name, tree rhs, bool parent_single_use_p)
913 imm_use_iterator iter;
914 gimple *use_stmt;
915 bool all = true;
916 bool single_use_p = parent_single_use_p && has_single_use (name);
918 FOR_EACH_IMM_USE_STMT (use_stmt, iter, name)
920 bool result;
921 tree use_rhs;
923 /* If the use is not in a simple assignment statement, then
924 there is nothing we can do. */
925 if (!is_gimple_assign (use_stmt))
927 if (!is_gimple_debug (use_stmt))
928 all = false;
929 continue;
932 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
933 result = forward_propagate_addr_expr_1 (name, rhs, &gsi,
934 single_use_p);
935 /* If the use has moved to a different statement adjust
936 the update machinery for the old statement too. */
937 if (use_stmt != gsi_stmt (gsi))
939 update_stmt (use_stmt);
940 use_stmt = gsi_stmt (gsi);
942 update_stmt (use_stmt);
943 all &= result;
945 /* Remove intermediate now unused copy and conversion chains. */
946 use_rhs = gimple_assign_rhs1 (use_stmt);
947 if (result
948 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
949 && TREE_CODE (use_rhs) == SSA_NAME
950 && has_zero_uses (gimple_assign_lhs (use_stmt)))
952 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
953 fwprop_invalidate_lattice (gimple_get_lhs (use_stmt));
954 release_defs (use_stmt);
955 gsi_remove (&gsi, true);
959 return all && has_zero_uses (name);
963 /* Helper function for simplify_gimple_switch. Remove case labels that
964 have values outside the range of the new type. */
966 static void
967 simplify_gimple_switch_label_vec (gswitch *stmt, tree index_type)
969 unsigned int branch_num = gimple_switch_num_labels (stmt);
970 auto_vec<tree> labels (branch_num);
971 unsigned int i, len;
973 /* Collect the existing case labels in a VEC, and preprocess it as if
974 we are gimplifying a GENERIC SWITCH_EXPR. */
975 for (i = 1; i < branch_num; i++)
976 labels.quick_push (gimple_switch_label (stmt, i));
977 preprocess_case_label_vec_for_gimple (labels, index_type, NULL);
979 /* If any labels were removed, replace the existing case labels
980 in the GIMPLE_SWITCH statement with the correct ones.
981 Note that the type updates were done in-place on the case labels,
982 so we only have to replace the case labels in the GIMPLE_SWITCH
983 if the number of labels changed. */
984 len = labels.length ();
985 if (len < branch_num - 1)
987 bitmap target_blocks;
988 edge_iterator ei;
989 edge e;
991 /* Corner case: *all* case labels have been removed as being
992 out-of-range for INDEX_TYPE. Push one label and let the
993 CFG cleanups deal with this further. */
994 if (len == 0)
996 tree label, elt;
998 label = CASE_LABEL (gimple_switch_default_label (stmt));
999 elt = build_case_label (build_int_cst (index_type, 0), NULL, label);
1000 labels.quick_push (elt);
1001 len = 1;
1004 for (i = 0; i < labels.length (); i++)
1005 gimple_switch_set_label (stmt, i + 1, labels[i]);
1006 for (i++ ; i < branch_num; i++)
1007 gimple_switch_set_label (stmt, i, NULL_TREE);
1008 gimple_switch_set_num_labels (stmt, len + 1);
1010 /* Cleanup any edges that are now dead. */
1011 target_blocks = BITMAP_ALLOC (NULL);
1012 for (i = 0; i < gimple_switch_num_labels (stmt); i++)
1014 tree elt = gimple_switch_label (stmt, i);
1015 basic_block target = label_to_block (cfun, CASE_LABEL (elt));
1016 bitmap_set_bit (target_blocks, target->index);
1018 for (ei = ei_start (gimple_bb (stmt)->succs); (e = ei_safe_edge (ei)); )
1020 if (! bitmap_bit_p (target_blocks, e->dest->index))
1022 remove_edge (e);
1023 cfg_changed = true;
1024 free_dominance_info (CDI_DOMINATORS);
1026 else
1027 ei_next (&ei);
1029 BITMAP_FREE (target_blocks);
1033 /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
1034 the condition which we may be able to optimize better. */
1036 static bool
1037 simplify_gimple_switch (gswitch *stmt)
1039 /* The optimization that we really care about is removing unnecessary
1040 casts. That will let us do much better in propagating the inferred
1041 constant at the switch target. */
1042 tree cond = gimple_switch_index (stmt);
1043 if (TREE_CODE (cond) == SSA_NAME)
1045 gimple *def_stmt = SSA_NAME_DEF_STMT (cond);
1046 if (gimple_assign_cast_p (def_stmt))
1048 tree def = gimple_assign_rhs1 (def_stmt);
1049 if (TREE_CODE (def) != SSA_NAME)
1050 return false;
1052 /* If we have an extension or sign-change that preserves the
1053 values we check against then we can copy the source value into
1054 the switch. */
1055 tree ti = TREE_TYPE (def);
1056 if (INTEGRAL_TYPE_P (ti)
1057 && TYPE_PRECISION (ti) <= TYPE_PRECISION (TREE_TYPE (cond)))
1059 size_t n = gimple_switch_num_labels (stmt);
1060 tree min = NULL_TREE, max = NULL_TREE;
1061 if (n > 1)
1063 min = CASE_LOW (gimple_switch_label (stmt, 1));
1064 if (CASE_HIGH (gimple_switch_label (stmt, n - 1)))
1065 max = CASE_HIGH (gimple_switch_label (stmt, n - 1));
1066 else
1067 max = CASE_LOW (gimple_switch_label (stmt, n - 1));
1069 if ((!min || int_fits_type_p (min, ti))
1070 && (!max || int_fits_type_p (max, ti)))
1072 gimple_switch_set_index (stmt, def);
1073 simplify_gimple_switch_label_vec (stmt, ti);
1074 update_stmt (stmt);
1075 return true;
1081 return false;
1084 /* For pointers p2 and p1 return p2 - p1 if the
1085 difference is known and constant, otherwise return NULL. */
1087 static tree
1088 constant_pointer_difference (tree p1, tree p2)
1090 int i, j;
1091 #define CPD_ITERATIONS 5
1092 tree exps[2][CPD_ITERATIONS];
1093 tree offs[2][CPD_ITERATIONS];
1094 int cnt[2];
1096 for (i = 0; i < 2; i++)
1098 tree p = i ? p1 : p2;
1099 tree off = size_zero_node;
1100 gimple *stmt;
1101 enum tree_code code;
1103 /* For each of p1 and p2 we need to iterate at least
1104 twice, to handle ADDR_EXPR directly in p1/p2,
1105 SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc.
1106 on definition's stmt RHS. Iterate a few extra times. */
1107 j = 0;
1110 if (!POINTER_TYPE_P (TREE_TYPE (p)))
1111 break;
1112 if (TREE_CODE (p) == ADDR_EXPR)
1114 tree q = TREE_OPERAND (p, 0);
1115 poly_int64 offset;
1116 tree base = get_addr_base_and_unit_offset (q, &offset);
1117 if (base)
1119 q = base;
1120 if (maybe_ne (offset, 0))
1121 off = size_binop (PLUS_EXPR, off, size_int (offset));
1123 if (TREE_CODE (q) == MEM_REF
1124 && TREE_CODE (TREE_OPERAND (q, 0)) == SSA_NAME)
1126 p = TREE_OPERAND (q, 0);
1127 off = size_binop (PLUS_EXPR, off,
1128 wide_int_to_tree (sizetype,
1129 mem_ref_offset (q)));
1131 else
1133 exps[i][j] = q;
1134 offs[i][j++] = off;
1135 break;
1138 if (TREE_CODE (p) != SSA_NAME)
1139 break;
1140 exps[i][j] = p;
1141 offs[i][j++] = off;
1142 if (j == CPD_ITERATIONS)
1143 break;
1144 stmt = SSA_NAME_DEF_STMT (p);
1145 if (!is_gimple_assign (stmt) || gimple_assign_lhs (stmt) != p)
1146 break;
1147 code = gimple_assign_rhs_code (stmt);
1148 if (code == POINTER_PLUS_EXPR)
1150 if (TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
1151 break;
1152 off = size_binop (PLUS_EXPR, off, gimple_assign_rhs2 (stmt));
1153 p = gimple_assign_rhs1 (stmt);
1155 else if (code == ADDR_EXPR || CONVERT_EXPR_CODE_P (code))
1156 p = gimple_assign_rhs1 (stmt);
1157 else
1158 break;
1160 while (1);
1161 cnt[i] = j;
1164 for (i = 0; i < cnt[0]; i++)
1165 for (j = 0; j < cnt[1]; j++)
1166 if (exps[0][i] == exps[1][j])
1167 return size_binop (MINUS_EXPR, offs[0][i], offs[1][j]);
1169 return NULL_TREE;
1172 /* *GSI_P is a GIMPLE_CALL to a builtin function.
1173 Optimize
1174 memcpy (p, "abcd", 4);
1175 memset (p + 4, ' ', 3);
1176 into
1177 memcpy (p, "abcd ", 7);
1178 call if the latter can be stored by pieces during expansion.
1180 Also canonicalize __atomic_fetch_op (p, x, y) op x
1181 to __atomic_op_fetch (p, x, y) or
1182 __atomic_op_fetch (p, x, y) iop x
1183 to __atomic_fetch_op (p, x, y) when possible (also __sync). */
1185 static bool
1186 simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2)
1188 gimple *stmt1, *stmt2 = gsi_stmt (*gsi_p);
1189 enum built_in_function other_atomic = END_BUILTINS;
1190 enum tree_code atomic_op = ERROR_MARK;
1191 tree vuse = gimple_vuse (stmt2);
1192 if (vuse == NULL)
1193 return false;
1194 stmt1 = SSA_NAME_DEF_STMT (vuse);
1196 switch (DECL_FUNCTION_CODE (callee2))
1198 case BUILT_IN_MEMSET:
1199 if (gimple_call_num_args (stmt2) != 3
1200 || gimple_call_lhs (stmt2)
1201 || CHAR_BIT != 8
1202 || BITS_PER_UNIT != 8)
1203 break;
1204 else
1206 tree callee1;
1207 tree ptr1, src1, str1, off1, len1, lhs1;
1208 tree ptr2 = gimple_call_arg (stmt2, 0);
1209 tree val2 = gimple_call_arg (stmt2, 1);
1210 tree len2 = gimple_call_arg (stmt2, 2);
1211 tree diff, vdef, new_str_cst;
1212 gimple *use_stmt;
1213 unsigned int ptr1_align;
1214 unsigned HOST_WIDE_INT src_len;
1215 char *src_buf;
1216 use_operand_p use_p;
1218 if (!tree_fits_shwi_p (val2)
1219 || !tree_fits_uhwi_p (len2)
1220 || compare_tree_int (len2, 1024) == 1)
1221 break;
1222 if (is_gimple_call (stmt1))
1224 /* If first stmt is a call, it needs to be memcpy
1225 or mempcpy, with string literal as second argument and
1226 constant length. */
1227 callee1 = gimple_call_fndecl (stmt1);
1228 if (callee1 == NULL_TREE
1229 || !fndecl_built_in_p (callee1, BUILT_IN_NORMAL)
1230 || gimple_call_num_args (stmt1) != 3)
1231 break;
1232 if (DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMCPY
1233 && DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMPCPY)
1234 break;
1235 ptr1 = gimple_call_arg (stmt1, 0);
1236 src1 = gimple_call_arg (stmt1, 1);
1237 len1 = gimple_call_arg (stmt1, 2);
1238 lhs1 = gimple_call_lhs (stmt1);
1239 if (!tree_fits_uhwi_p (len1))
1240 break;
1241 str1 = string_constant (src1, &off1, NULL, NULL);
1242 if (str1 == NULL_TREE)
1243 break;
1244 if (!tree_fits_uhwi_p (off1)
1245 || compare_tree_int (off1, TREE_STRING_LENGTH (str1) - 1) > 0
1246 || compare_tree_int (len1, TREE_STRING_LENGTH (str1)
1247 - tree_to_uhwi (off1)) > 0
1248 || TREE_CODE (TREE_TYPE (str1)) != ARRAY_TYPE
1249 || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1)))
1250 != TYPE_MODE (char_type_node))
1251 break;
1253 else if (gimple_assign_single_p (stmt1))
1255 /* Otherwise look for length 1 memcpy optimized into
1256 assignment. */
1257 ptr1 = gimple_assign_lhs (stmt1);
1258 src1 = gimple_assign_rhs1 (stmt1);
1259 if (TREE_CODE (ptr1) != MEM_REF
1260 || TYPE_MODE (TREE_TYPE (ptr1)) != TYPE_MODE (char_type_node)
1261 || !tree_fits_shwi_p (src1))
1262 break;
1263 ptr1 = build_fold_addr_expr (ptr1);
1264 STRIP_USELESS_TYPE_CONVERSION (ptr1);
1265 callee1 = NULL_TREE;
1266 len1 = size_one_node;
1267 lhs1 = NULL_TREE;
1268 off1 = size_zero_node;
1269 str1 = NULL_TREE;
1271 else
1272 break;
1274 diff = constant_pointer_difference (ptr1, ptr2);
1275 if (diff == NULL && lhs1 != NULL)
1277 diff = constant_pointer_difference (lhs1, ptr2);
1278 if (DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
1279 && diff != NULL)
1280 diff = size_binop (PLUS_EXPR, diff,
1281 fold_convert (sizetype, len1));
1283 /* If the difference between the second and first destination pointer
1284 is not constant, or is bigger than memcpy length, bail out. */
1285 if (diff == NULL
1286 || !tree_fits_uhwi_p (diff)
1287 || tree_int_cst_lt (len1, diff)
1288 || compare_tree_int (diff, 1024) == 1)
1289 break;
1291 /* Use maximum of difference plus memset length and memcpy length
1292 as the new memcpy length, if it is too big, bail out. */
1293 src_len = tree_to_uhwi (diff);
1294 src_len += tree_to_uhwi (len2);
1295 if (src_len < tree_to_uhwi (len1))
1296 src_len = tree_to_uhwi (len1);
1297 if (src_len > 1024)
1298 break;
1300 /* If mempcpy value is used elsewhere, bail out, as mempcpy
1301 with bigger length will return different result. */
1302 if (lhs1 != NULL_TREE
1303 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
1304 && (TREE_CODE (lhs1) != SSA_NAME
1305 || !single_imm_use (lhs1, &use_p, &use_stmt)
1306 || use_stmt != stmt2))
1307 break;
1309 /* If anything reads memory in between memcpy and memset
1310 call, the modified memcpy call might change it. */
1311 vdef = gimple_vdef (stmt1);
1312 if (vdef != NULL
1313 && (!single_imm_use (vdef, &use_p, &use_stmt)
1314 || use_stmt != stmt2))
1315 break;
1317 ptr1_align = get_pointer_alignment (ptr1);
1318 /* Construct the new source string literal. */
1319 src_buf = XALLOCAVEC (char, src_len + 1);
1320 if (callee1)
1321 memcpy (src_buf,
1322 TREE_STRING_POINTER (str1) + tree_to_uhwi (off1),
1323 tree_to_uhwi (len1));
1324 else
1325 src_buf[0] = tree_to_shwi (src1);
1326 memset (src_buf + tree_to_uhwi (diff),
1327 tree_to_shwi (val2), tree_to_uhwi (len2));
1328 src_buf[src_len] = '\0';
1329 /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
1330 handle embedded '\0's. */
1331 if (strlen (src_buf) != src_len)
1332 break;
1333 rtl_profile_for_bb (gimple_bb (stmt2));
1334 /* If the new memcpy wouldn't be emitted by storing the literal
1335 by pieces, this optimization might enlarge .rodata too much,
1336 as commonly used string literals couldn't be shared any
1337 longer. */
1338 if (!can_store_by_pieces (src_len,
1339 builtin_strncpy_read_str,
1340 src_buf, ptr1_align, false))
1341 break;
1343 new_str_cst = build_string_literal (src_len, src_buf);
1344 if (callee1)
1346 /* If STMT1 is a mem{,p}cpy call, adjust it and remove
1347 memset call. */
1348 if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
1349 gimple_call_set_lhs (stmt1, NULL_TREE);
1350 gimple_call_set_arg (stmt1, 1, new_str_cst);
1351 gimple_call_set_arg (stmt1, 2,
1352 build_int_cst (TREE_TYPE (len1), src_len));
1353 update_stmt (stmt1);
1354 unlink_stmt_vdef (stmt2);
1355 gsi_replace (gsi_p, gimple_build_nop (), false);
1356 fwprop_invalidate_lattice (gimple_get_lhs (stmt2));
1357 release_defs (stmt2);
1358 if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
1360 fwprop_invalidate_lattice (lhs1);
1361 release_ssa_name (lhs1);
1363 return true;
1365 else
1367 /* Otherwise, if STMT1 is length 1 memcpy optimized into
1368 assignment, remove STMT1 and change memset call into
1369 memcpy call. */
1370 gimple_stmt_iterator gsi = gsi_for_stmt (stmt1);
1372 if (!is_gimple_val (ptr1))
1373 ptr1 = force_gimple_operand_gsi (gsi_p, ptr1, true, NULL_TREE,
1374 true, GSI_SAME_STMT);
1375 tree fndecl = builtin_decl_explicit (BUILT_IN_MEMCPY);
1376 gimple_call_set_fndecl (stmt2, fndecl);
1377 gimple_call_set_fntype (as_a <gcall *> (stmt2),
1378 TREE_TYPE (fndecl));
1379 gimple_call_set_arg (stmt2, 0, ptr1);
1380 gimple_call_set_arg (stmt2, 1, new_str_cst);
1381 gimple_call_set_arg (stmt2, 2,
1382 build_int_cst (TREE_TYPE (len2), src_len));
1383 unlink_stmt_vdef (stmt1);
1384 gsi_remove (&gsi, true);
1385 fwprop_invalidate_lattice (gimple_get_lhs (stmt1));
1386 release_defs (stmt1);
1387 update_stmt (stmt2);
1388 return false;
1391 break;
1393 #define CASE_ATOMIC(NAME, OTHER, OP) \
1394 case BUILT_IN_##NAME##_1: \
1395 case BUILT_IN_##NAME##_2: \
1396 case BUILT_IN_##NAME##_4: \
1397 case BUILT_IN_##NAME##_8: \
1398 case BUILT_IN_##NAME##_16: \
1399 atomic_op = OP; \
1400 other_atomic \
1401 = (enum built_in_function) (BUILT_IN_##OTHER##_1 \
1402 + (DECL_FUNCTION_CODE (callee2) \
1403 - BUILT_IN_##NAME##_1)); \
1404 goto handle_atomic_fetch_op;
1406 CASE_ATOMIC (ATOMIC_FETCH_ADD, ATOMIC_ADD_FETCH, PLUS_EXPR)
1407 CASE_ATOMIC (ATOMIC_FETCH_SUB, ATOMIC_SUB_FETCH, MINUS_EXPR)
1408 CASE_ATOMIC (ATOMIC_FETCH_AND, ATOMIC_AND_FETCH, BIT_AND_EXPR)
1409 CASE_ATOMIC (ATOMIC_FETCH_XOR, ATOMIC_XOR_FETCH, BIT_XOR_EXPR)
1410 CASE_ATOMIC (ATOMIC_FETCH_OR, ATOMIC_OR_FETCH, BIT_IOR_EXPR)
1412 CASE_ATOMIC (SYNC_FETCH_AND_ADD, SYNC_ADD_AND_FETCH, PLUS_EXPR)
1413 CASE_ATOMIC (SYNC_FETCH_AND_SUB, SYNC_SUB_AND_FETCH, MINUS_EXPR)
1414 CASE_ATOMIC (SYNC_FETCH_AND_AND, SYNC_AND_AND_FETCH, BIT_AND_EXPR)
1415 CASE_ATOMIC (SYNC_FETCH_AND_XOR, SYNC_XOR_AND_FETCH, BIT_XOR_EXPR)
1416 CASE_ATOMIC (SYNC_FETCH_AND_OR, SYNC_OR_AND_FETCH, BIT_IOR_EXPR)
1418 CASE_ATOMIC (ATOMIC_ADD_FETCH, ATOMIC_FETCH_ADD, MINUS_EXPR)
1419 CASE_ATOMIC (ATOMIC_SUB_FETCH, ATOMIC_FETCH_SUB, PLUS_EXPR)
1420 CASE_ATOMIC (ATOMIC_XOR_FETCH, ATOMIC_FETCH_XOR, BIT_XOR_EXPR)
1422 CASE_ATOMIC (SYNC_ADD_AND_FETCH, SYNC_FETCH_AND_ADD, MINUS_EXPR)
1423 CASE_ATOMIC (SYNC_SUB_AND_FETCH, SYNC_FETCH_AND_SUB, PLUS_EXPR)
1424 CASE_ATOMIC (SYNC_XOR_AND_FETCH, SYNC_FETCH_AND_XOR, BIT_XOR_EXPR)
1426 #undef CASE_ATOMIC
1428 handle_atomic_fetch_op:
1429 if (gimple_call_num_args (stmt2) >= 2 && gimple_call_lhs (stmt2))
1431 tree lhs2 = gimple_call_lhs (stmt2), lhsc = lhs2;
1432 tree arg = gimple_call_arg (stmt2, 1);
1433 gimple *use_stmt, *cast_stmt = NULL;
1434 use_operand_p use_p;
1435 tree ndecl = builtin_decl_explicit (other_atomic);
1437 if (ndecl == NULL_TREE || !single_imm_use (lhs2, &use_p, &use_stmt))
1438 break;
1440 if (gimple_assign_cast_p (use_stmt))
1442 cast_stmt = use_stmt;
1443 lhsc = gimple_assign_lhs (cast_stmt);
1444 if (lhsc == NULL_TREE
1445 || !INTEGRAL_TYPE_P (TREE_TYPE (lhsc))
1446 || (TYPE_PRECISION (TREE_TYPE (lhsc))
1447 != TYPE_PRECISION (TREE_TYPE (lhs2)))
1448 || !single_imm_use (lhsc, &use_p, &use_stmt))
1450 use_stmt = cast_stmt;
1451 cast_stmt = NULL;
1452 lhsc = lhs2;
1456 bool ok = false;
1457 tree oarg = NULL_TREE;
1458 enum tree_code ccode = ERROR_MARK;
1459 tree crhs1 = NULL_TREE, crhs2 = NULL_TREE;
1460 if (is_gimple_assign (use_stmt)
1461 && gimple_assign_rhs_code (use_stmt) == atomic_op)
1463 if (gimple_assign_rhs1 (use_stmt) == lhsc)
1464 oarg = gimple_assign_rhs2 (use_stmt);
1465 else if (atomic_op != MINUS_EXPR)
1466 oarg = gimple_assign_rhs1 (use_stmt);
1468 else if (atomic_op == MINUS_EXPR
1469 && is_gimple_assign (use_stmt)
1470 && gimple_assign_rhs_code (use_stmt) == PLUS_EXPR
1471 && TREE_CODE (arg) == INTEGER_CST
1472 && (TREE_CODE (gimple_assign_rhs2 (use_stmt))
1473 == INTEGER_CST))
1475 tree a = fold_convert (TREE_TYPE (lhs2), arg);
1476 tree o = fold_convert (TREE_TYPE (lhs2),
1477 gimple_assign_rhs2 (use_stmt));
1478 if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
1479 ok = true;
1481 else if (atomic_op == BIT_AND_EXPR || atomic_op == BIT_IOR_EXPR)
1483 else if (gimple_code (use_stmt) == GIMPLE_COND)
1485 ccode = gimple_cond_code (use_stmt);
1486 crhs1 = gimple_cond_lhs (use_stmt);
1487 crhs2 = gimple_cond_rhs (use_stmt);
1489 else if (is_gimple_assign (use_stmt))
1491 if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
1493 ccode = gimple_assign_rhs_code (use_stmt);
1494 crhs1 = gimple_assign_rhs1 (use_stmt);
1495 crhs2 = gimple_assign_rhs2 (use_stmt);
1497 else if (gimple_assign_rhs_code (use_stmt) == COND_EXPR)
1499 tree cond = gimple_assign_rhs1 (use_stmt);
1500 if (COMPARISON_CLASS_P (cond))
1502 ccode = TREE_CODE (cond);
1503 crhs1 = TREE_OPERAND (cond, 0);
1504 crhs2 = TREE_OPERAND (cond, 1);
1508 if (ccode == EQ_EXPR || ccode == NE_EXPR)
1510 /* Deal with x - y == 0 or x ^ y == 0
1511 being optimized into x == y and x + cst == 0
1512 into x == -cst. */
1513 tree o = NULL_TREE;
1514 if (crhs1 == lhsc)
1515 o = crhs2;
1516 else if (crhs2 == lhsc)
1517 o = crhs1;
1518 if (o && atomic_op != PLUS_EXPR)
1519 oarg = o;
1520 else if (o
1521 && TREE_CODE (o) == INTEGER_CST
1522 && TREE_CODE (arg) == INTEGER_CST)
1524 tree a = fold_convert (TREE_TYPE (lhs2), arg);
1525 o = fold_convert (TREE_TYPE (lhs2), o);
1526 if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
1527 ok = true;
1530 if (oarg && !ok)
1532 if (operand_equal_p (arg, oarg, 0))
1533 ok = true;
1534 else if (TREE_CODE (arg) == SSA_NAME
1535 && TREE_CODE (oarg) == SSA_NAME)
1537 tree oarg2 = oarg;
1538 if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (oarg)))
1540 gimple *g = SSA_NAME_DEF_STMT (oarg);
1541 oarg2 = gimple_assign_rhs1 (g);
1542 if (TREE_CODE (oarg2) != SSA_NAME
1543 || !INTEGRAL_TYPE_P (TREE_TYPE (oarg2))
1544 || (TYPE_PRECISION (TREE_TYPE (oarg2))
1545 != TYPE_PRECISION (TREE_TYPE (oarg))))
1546 oarg2 = oarg;
1548 if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg)))
1550 gimple *g = SSA_NAME_DEF_STMT (arg);
1551 tree rhs1 = gimple_assign_rhs1 (g);
1552 /* Handle e.g.
1553 x.0_1 = (long unsigned int) x_4(D);
1554 _2 = __atomic_fetch_add_8 (&vlong, x.0_1, 0);
1555 _3 = (long int) _2;
1556 _7 = x_4(D) + _3; */
1557 if (rhs1 == oarg || rhs1 == oarg2)
1558 ok = true;
1559 /* Handle e.g.
1560 x.18_1 = (short unsigned int) x_5(D);
1561 _2 = (int) x.18_1;
1562 _3 = __atomic_fetch_xor_2 (&vshort, _2, 0);
1563 _4 = (short int) _3;
1564 _8 = x_5(D) ^ _4;
1565 This happens only for char/short. */
1566 else if (TREE_CODE (rhs1) == SSA_NAME
1567 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1568 && (TYPE_PRECISION (TREE_TYPE (rhs1))
1569 == TYPE_PRECISION (TREE_TYPE (lhs2))))
1571 g = SSA_NAME_DEF_STMT (rhs1);
1572 if (gimple_assign_cast_p (g)
1573 && (gimple_assign_rhs1 (g) == oarg
1574 || gimple_assign_rhs1 (g) == oarg2))
1575 ok = true;
1578 if (!ok && arg == oarg2)
1579 /* Handle e.g.
1580 _1 = __sync_fetch_and_add_4 (&v, x_5(D));
1581 _2 = (int) _1;
1582 x.0_3 = (int) x_5(D);
1583 _7 = _2 + x.0_3; */
1584 ok = true;
1588 if (ok)
1590 tree new_lhs = make_ssa_name (TREE_TYPE (lhs2));
1591 gimple_call_set_lhs (stmt2, new_lhs);
1592 gimple_call_set_fndecl (stmt2, ndecl);
1593 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1594 if (ccode == ERROR_MARK)
1595 gimple_assign_set_rhs_with_ops (&gsi, cast_stmt
1596 ? NOP_EXPR : SSA_NAME,
1597 new_lhs);
1598 else
1600 crhs1 = new_lhs;
1601 crhs2 = build_zero_cst (TREE_TYPE (lhs2));
1602 if (gimple_code (use_stmt) == GIMPLE_COND)
1604 gcond *cond_stmt = as_a <gcond *> (use_stmt);
1605 gimple_cond_set_lhs (cond_stmt, crhs1);
1606 gimple_cond_set_rhs (cond_stmt, crhs2);
1608 else if (gimple_assign_rhs_class (use_stmt)
1609 == GIMPLE_BINARY_RHS)
1611 gimple_assign_set_rhs1 (use_stmt, crhs1);
1612 gimple_assign_set_rhs2 (use_stmt, crhs2);
1614 else
1616 gcc_checking_assert (gimple_assign_rhs_code (use_stmt)
1617 == COND_EXPR);
1618 tree cond = build2 (ccode, boolean_type_node,
1619 crhs1, crhs2);
1620 gimple_assign_set_rhs1 (use_stmt, cond);
1623 update_stmt (use_stmt);
1624 if (atomic_op != BIT_AND_EXPR
1625 && atomic_op != BIT_IOR_EXPR
1626 && !stmt_ends_bb_p (stmt2))
1628 /* For the benefit of debug stmts, emit stmt(s) to set
1629 lhs2 to the value it had from the new builtin.
1630 E.g. if it was previously:
1631 lhs2 = __atomic_fetch_add_8 (ptr, arg, 0);
1632 emit:
1633 new_lhs = __atomic_add_fetch_8 (ptr, arg, 0);
1634 lhs2 = new_lhs - arg;
1635 We also keep cast_stmt if any in the IL for
1636 the same reasons.
1637 These stmts will be DCEd later and proper debug info
1638 will be emitted.
1639 This is only possible for reversible operations
1640 (+/-/^) and without -fnon-call-exceptions. */
1641 gsi = gsi_for_stmt (stmt2);
1642 tree type = TREE_TYPE (lhs2);
1643 if (TREE_CODE (arg) == INTEGER_CST)
1644 arg = fold_convert (type, arg);
1645 else if (!useless_type_conversion_p (type, TREE_TYPE (arg)))
1647 tree narg = make_ssa_name (type);
1648 gimple *g = gimple_build_assign (narg, NOP_EXPR, arg);
1649 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
1650 arg = narg;
1652 enum tree_code rcode;
1653 switch (atomic_op)
1655 case PLUS_EXPR: rcode = MINUS_EXPR; break;
1656 case MINUS_EXPR: rcode = PLUS_EXPR; break;
1657 case BIT_XOR_EXPR: rcode = atomic_op; break;
1658 default: gcc_unreachable ();
1660 gimple *g = gimple_build_assign (lhs2, rcode, new_lhs, arg);
1661 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
1662 update_stmt (stmt2);
1664 else
1666 /* For e.g.
1667 lhs2 = __atomic_fetch_or_8 (ptr, arg, 0);
1668 after we change it to
1669 new_lhs = __atomic_or_fetch_8 (ptr, arg, 0);
1670 there is no way to find out the lhs2 value (i.e.
1671 what the atomic memory contained before the operation),
1672 values of some bits are lost. We have checked earlier
1673 that we don't have any non-debug users except for what
1674 we are already changing, so we need to reset the
1675 debug stmts and remove the cast_stmt if any. */
1676 imm_use_iterator iter;
1677 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs2)
1678 if (use_stmt != cast_stmt)
1680 gcc_assert (is_gimple_debug (use_stmt));
1681 gimple_debug_bind_reset_value (use_stmt);
1682 update_stmt (use_stmt);
1684 if (cast_stmt)
1686 gsi = gsi_for_stmt (cast_stmt);
1687 gsi_remove (&gsi, true);
1689 update_stmt (stmt2);
1690 release_ssa_name (lhs2);
1694 break;
1696 default:
1697 break;
1699 return false;
1702 /* Given a ssa_name in NAME see if it was defined by an assignment and
1703 set CODE to be the code and ARG1 to the first operand on the rhs and ARG2
1704 to the second operand on the rhs. */
1706 static inline void
1707 defcodefor_name (tree name, enum tree_code *code, tree *arg1, tree *arg2)
1709 gimple *def;
1710 enum tree_code code1;
1711 tree arg11;
1712 tree arg21;
1713 tree arg31;
1714 enum gimple_rhs_class grhs_class;
1716 code1 = TREE_CODE (name);
1717 arg11 = name;
1718 arg21 = NULL_TREE;
1719 arg31 = NULL_TREE;
1720 grhs_class = get_gimple_rhs_class (code1);
1722 if (code1 == SSA_NAME)
1724 def = SSA_NAME_DEF_STMT (name);
1726 if (def && is_gimple_assign (def)
1727 && can_propagate_from (def))
1729 code1 = gimple_assign_rhs_code (def);
1730 arg11 = gimple_assign_rhs1 (def);
1731 arg21 = gimple_assign_rhs2 (def);
1732 arg31 = gimple_assign_rhs3 (def);
1735 else if (grhs_class != GIMPLE_SINGLE_RHS)
1736 code1 = ERROR_MARK;
1738 *code = code1;
1739 *arg1 = arg11;
1740 if (arg2)
1741 *arg2 = arg21;
1742 if (arg31)
1743 *code = ERROR_MARK;
1747 /* Recognize rotation patterns. Return true if a transformation
1748 applied, otherwise return false.
1750 We are looking for X with unsigned type T with bitsize B, OP being
1751 +, | or ^, some type T2 wider than T. For:
1752 (X << CNT1) OP (X >> CNT2) iff CNT1 + CNT2 == B
1753 ((T) ((T2) X << CNT1)) OP ((T) ((T2) X >> CNT2)) iff CNT1 + CNT2 == B
1755 transform these into:
1756 X r<< CNT1
1758 Or for:
1759 (X << Y) OP (X >> (B - Y))
1760 (X << (int) Y) OP (X >> (int) (B - Y))
1761 ((T) ((T2) X << Y)) OP ((T) ((T2) X >> (B - Y)))
1762 ((T) ((T2) X << (int) Y)) OP ((T) ((T2) X >> (int) (B - Y)))
1763 (X << Y) | (X >> ((-Y) & (B - 1)))
1764 (X << (int) Y) | (X >> (int) ((-Y) & (B - 1)))
1765 ((T) ((T2) X << Y)) | ((T) ((T2) X >> ((-Y) & (B - 1))))
1766 ((T) ((T2) X << (int) Y)) | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
1768 transform these into:
1769 X r<< Y
1771 Or for:
1772 (X << (Y & (B - 1))) | (X >> ((-Y) & (B - 1)))
1773 (X << (int) (Y & (B - 1))) | (X >> (int) ((-Y) & (B - 1)))
1774 ((T) ((T2) X << (Y & (B - 1)))) | ((T) ((T2) X >> ((-Y) & (B - 1))))
1775 ((T) ((T2) X << (int) (Y & (B - 1)))) \
1776 | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
1778 transform these into:
1779 X r<< (Y & (B - 1))
1781 Note, in the patterns with T2 type, the type of OP operands
1782 might be even a signed type, but should have precision B.
1783 Expressions with & (B - 1) should be recognized only if B is
1784 a power of 2. */
1786 static bool
1787 simplify_rotate (gimple_stmt_iterator *gsi)
1789 gimple *stmt = gsi_stmt (*gsi);
1790 tree arg[2], rtype, rotcnt = NULL_TREE;
1791 tree def_arg1[2], def_arg2[2];
1792 enum tree_code def_code[2];
1793 tree lhs;
1794 int i;
1795 bool swapped_p = false;
1796 gimple *g;
1798 arg[0] = gimple_assign_rhs1 (stmt);
1799 arg[1] = gimple_assign_rhs2 (stmt);
1800 rtype = TREE_TYPE (arg[0]);
1802 /* Only create rotates in complete modes. Other cases are not
1803 expanded properly. */
1804 if (!INTEGRAL_TYPE_P (rtype)
1805 || !type_has_mode_precision_p (rtype))
1806 return false;
1808 for (i = 0; i < 2; i++)
1809 defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
1811 /* Look through narrowing (or same precision) conversions. */
1812 if (CONVERT_EXPR_CODE_P (def_code[0])
1813 && CONVERT_EXPR_CODE_P (def_code[1])
1814 && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[0]))
1815 && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[1]))
1816 && TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
1817 == TYPE_PRECISION (TREE_TYPE (def_arg1[1]))
1818 && TYPE_PRECISION (TREE_TYPE (def_arg1[0])) >= TYPE_PRECISION (rtype)
1819 && has_single_use (arg[0])
1820 && has_single_use (arg[1]))
1822 for (i = 0; i < 2; i++)
1824 arg[i] = def_arg1[i];
1825 defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
1828 else
1830 /* Handle signed rotate; the RSHIFT_EXPR has to be done
1831 in unsigned type but LSHIFT_EXPR could be signed. */
1832 i = (def_code[0] == LSHIFT_EXPR || def_code[0] == RSHIFT_EXPR);
1833 if (CONVERT_EXPR_CODE_P (def_code[i])
1834 && (def_code[1 - i] == LSHIFT_EXPR || def_code[1 - i] == RSHIFT_EXPR)
1835 && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[i]))
1836 && TYPE_PRECISION (rtype) == TYPE_PRECISION (TREE_TYPE (def_arg1[i]))
1837 && has_single_use (arg[i]))
1839 arg[i] = def_arg1[i];
1840 defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
1844 /* One operand has to be LSHIFT_EXPR and one RSHIFT_EXPR. */
1845 for (i = 0; i < 2; i++)
1846 if (def_code[i] != LSHIFT_EXPR && def_code[i] != RSHIFT_EXPR)
1847 return false;
1848 else if (!has_single_use (arg[i]))
1849 return false;
1850 if (def_code[0] == def_code[1])
1851 return false;
1853 /* If we've looked through narrowing conversions before, look through
1854 widening conversions from unsigned type with the same precision
1855 as rtype here. */
1856 if (TYPE_PRECISION (TREE_TYPE (def_arg1[0])) != TYPE_PRECISION (rtype))
1857 for (i = 0; i < 2; i++)
1859 tree tem;
1860 enum tree_code code;
1861 defcodefor_name (def_arg1[i], &code, &tem, NULL);
1862 if (!CONVERT_EXPR_CODE_P (code)
1863 || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
1864 || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
1865 return false;
1866 def_arg1[i] = tem;
1868 /* Both shifts have to use the same first operand. */
1869 if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
1870 || !types_compatible_p (TREE_TYPE (def_arg1[0]),
1871 TREE_TYPE (def_arg1[1])))
1873 if ((TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
1874 != TYPE_PRECISION (TREE_TYPE (def_arg1[1])))
1875 || (TYPE_UNSIGNED (TREE_TYPE (def_arg1[0]))
1876 == TYPE_UNSIGNED (TREE_TYPE (def_arg1[1]))))
1877 return false;
1879 /* Handle signed rotate; the RSHIFT_EXPR has to be done
1880 in unsigned type but LSHIFT_EXPR could be signed. */
1881 i = def_code[0] != RSHIFT_EXPR;
1882 if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[i])))
1883 return false;
1885 tree tem;
1886 enum tree_code code;
1887 defcodefor_name (def_arg1[i], &code, &tem, NULL);
1888 if (!CONVERT_EXPR_CODE_P (code)
1889 || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
1890 || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
1891 return false;
1892 def_arg1[i] = tem;
1893 if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
1894 || !types_compatible_p (TREE_TYPE (def_arg1[0]),
1895 TREE_TYPE (def_arg1[1])))
1896 return false;
1898 else if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[0])))
1899 return false;
1901 /* CNT1 + CNT2 == B case above. */
1902 if (tree_fits_uhwi_p (def_arg2[0])
1903 && tree_fits_uhwi_p (def_arg2[1])
1904 && tree_to_uhwi (def_arg2[0])
1905 + tree_to_uhwi (def_arg2[1]) == TYPE_PRECISION (rtype))
1906 rotcnt = def_arg2[0];
1907 else if (TREE_CODE (def_arg2[0]) != SSA_NAME
1908 || TREE_CODE (def_arg2[1]) != SSA_NAME)
1909 return false;
1910 else
1912 tree cdef_arg1[2], cdef_arg2[2], def_arg2_alt[2];
1913 enum tree_code cdef_code[2];
1914 /* Look through conversion of the shift count argument.
1915 The C/C++ FE cast any shift count argument to integer_type_node.
1916 The only problem might be if the shift count type maximum value
1917 is equal or smaller than number of bits in rtype. */
1918 for (i = 0; i < 2; i++)
1920 def_arg2_alt[i] = def_arg2[i];
1921 defcodefor_name (def_arg2[i], &cdef_code[i],
1922 &cdef_arg1[i], &cdef_arg2[i]);
1923 if (CONVERT_EXPR_CODE_P (cdef_code[i])
1924 && INTEGRAL_TYPE_P (TREE_TYPE (cdef_arg1[i]))
1925 && TYPE_PRECISION (TREE_TYPE (cdef_arg1[i]))
1926 > floor_log2 (TYPE_PRECISION (rtype))
1927 && type_has_mode_precision_p (TREE_TYPE (cdef_arg1[i])))
1929 def_arg2_alt[i] = cdef_arg1[i];
1930 defcodefor_name (def_arg2_alt[i], &cdef_code[i],
1931 &cdef_arg1[i], &cdef_arg2[i]);
1934 for (i = 0; i < 2; i++)
1935 /* Check for one shift count being Y and the other B - Y,
1936 with optional casts. */
1937 if (cdef_code[i] == MINUS_EXPR
1938 && tree_fits_shwi_p (cdef_arg1[i])
1939 && tree_to_shwi (cdef_arg1[i]) == TYPE_PRECISION (rtype)
1940 && TREE_CODE (cdef_arg2[i]) == SSA_NAME)
1942 tree tem;
1943 enum tree_code code;
1945 if (cdef_arg2[i] == def_arg2[1 - i]
1946 || cdef_arg2[i] == def_arg2_alt[1 - i])
1948 rotcnt = cdef_arg2[i];
1949 break;
1951 defcodefor_name (cdef_arg2[i], &code, &tem, NULL);
1952 if (CONVERT_EXPR_CODE_P (code)
1953 && INTEGRAL_TYPE_P (TREE_TYPE (tem))
1954 && TYPE_PRECISION (TREE_TYPE (tem))
1955 > floor_log2 (TYPE_PRECISION (rtype))
1956 && type_has_mode_precision_p (TREE_TYPE (tem))
1957 && (tem == def_arg2[1 - i]
1958 || tem == def_arg2_alt[1 - i]))
1960 rotcnt = tem;
1961 break;
1964 /* The above sequence isn't safe for Y being 0,
1965 because then one of the shifts triggers undefined behavior.
1966 This alternative is safe even for rotation count of 0.
1967 One shift count is Y and the other (-Y) & (B - 1).
1968 Or one shift count is Y & (B - 1) and the other (-Y) & (B - 1). */
1969 else if (cdef_code[i] == BIT_AND_EXPR
1970 && pow2p_hwi (TYPE_PRECISION (rtype))
1971 && tree_fits_shwi_p (cdef_arg2[i])
1972 && tree_to_shwi (cdef_arg2[i])
1973 == TYPE_PRECISION (rtype) - 1
1974 && TREE_CODE (cdef_arg1[i]) == SSA_NAME
1975 && gimple_assign_rhs_code (stmt) == BIT_IOR_EXPR)
1977 tree tem;
1978 enum tree_code code;
1980 defcodefor_name (cdef_arg1[i], &code, &tem, NULL);
1981 if (CONVERT_EXPR_CODE_P (code)
1982 && INTEGRAL_TYPE_P (TREE_TYPE (tem))
1983 && TYPE_PRECISION (TREE_TYPE (tem))
1984 > floor_log2 (TYPE_PRECISION (rtype))
1985 && type_has_mode_precision_p (TREE_TYPE (tem)))
1986 defcodefor_name (tem, &code, &tem, NULL);
1988 if (code == NEGATE_EXPR)
1990 if (tem == def_arg2[1 - i] || tem == def_arg2_alt[1 - i])
1992 rotcnt = tem;
1993 break;
1995 tree tem2;
1996 defcodefor_name (tem, &code, &tem2, NULL);
1997 if (CONVERT_EXPR_CODE_P (code)
1998 && INTEGRAL_TYPE_P (TREE_TYPE (tem2))
1999 && TYPE_PRECISION (TREE_TYPE (tem2))
2000 > floor_log2 (TYPE_PRECISION (rtype))
2001 && type_has_mode_precision_p (TREE_TYPE (tem2)))
2003 if (tem2 == def_arg2[1 - i]
2004 || tem2 == def_arg2_alt[1 - i])
2006 rotcnt = tem2;
2007 break;
2010 else
2011 tem2 = NULL_TREE;
2013 if (cdef_code[1 - i] == BIT_AND_EXPR
2014 && tree_fits_shwi_p (cdef_arg2[1 - i])
2015 && tree_to_shwi (cdef_arg2[1 - i])
2016 == TYPE_PRECISION (rtype) - 1
2017 && TREE_CODE (cdef_arg1[1 - i]) == SSA_NAME)
2019 if (tem == cdef_arg1[1 - i]
2020 || tem2 == cdef_arg1[1 - i])
2022 rotcnt = def_arg2[1 - i];
2023 break;
2025 tree tem3;
2026 defcodefor_name (cdef_arg1[1 - i], &code, &tem3, NULL);
2027 if (CONVERT_EXPR_CODE_P (code)
2028 && INTEGRAL_TYPE_P (TREE_TYPE (tem3))
2029 && TYPE_PRECISION (TREE_TYPE (tem3))
2030 > floor_log2 (TYPE_PRECISION (rtype))
2031 && type_has_mode_precision_p (TREE_TYPE (tem3)))
2033 if (tem == tem3 || tem2 == tem3)
2035 rotcnt = def_arg2[1 - i];
2036 break;
2042 if (rotcnt == NULL_TREE)
2043 return false;
2044 swapped_p = i != 1;
2047 if (!useless_type_conversion_p (TREE_TYPE (def_arg2[0]),
2048 TREE_TYPE (rotcnt)))
2050 g = gimple_build_assign (make_ssa_name (TREE_TYPE (def_arg2[0])),
2051 NOP_EXPR, rotcnt);
2052 gsi_insert_before (gsi, g, GSI_SAME_STMT);
2053 rotcnt = gimple_assign_lhs (g);
2055 lhs = gimple_assign_lhs (stmt);
2056 if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0])))
2057 lhs = make_ssa_name (TREE_TYPE (def_arg1[0]));
2058 g = gimple_build_assign (lhs,
2059 ((def_code[0] == LSHIFT_EXPR) ^ swapped_p)
2060 ? LROTATE_EXPR : RROTATE_EXPR, def_arg1[0], rotcnt);
2061 if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0])))
2063 gsi_insert_before (gsi, g, GSI_SAME_STMT);
2064 g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, lhs);
2066 gsi_replace (gsi, g, false);
2067 return true;
2071 /* Check whether an array contains a valid ctz table. */
2072 static bool
2073 check_ctz_array (tree ctor, unsigned HOST_WIDE_INT mulc,
2074 HOST_WIDE_INT &zero_val, unsigned shift, unsigned bits)
2076 tree elt, idx;
2077 unsigned HOST_WIDE_INT i, mask;
2078 unsigned matched = 0;
2080 mask = ((HOST_WIDE_INT_1U << (bits - shift)) - 1) << shift;
2082 zero_val = 0;
2084 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, elt)
2086 if (TREE_CODE (idx) != INTEGER_CST || TREE_CODE (elt) != INTEGER_CST)
2087 return false;
2088 if (i > bits * 2)
2089 return false;
2091 unsigned HOST_WIDE_INT index = tree_to_shwi (idx);
2092 HOST_WIDE_INT val = tree_to_shwi (elt);
2094 if (index == 0)
2096 zero_val = val;
2097 matched++;
2100 if (val >= 0 && val < bits && (((mulc << val) & mask) >> shift) == index)
2101 matched++;
2103 if (matched > bits)
2104 return true;
2107 return false;
2110 /* Check whether a string contains a valid ctz table. */
2111 static bool
2112 check_ctz_string (tree string, unsigned HOST_WIDE_INT mulc,
2113 HOST_WIDE_INT &zero_val, unsigned shift, unsigned bits)
2115 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (string);
2116 unsigned HOST_WIDE_INT mask;
2117 unsigned matched = 0;
2118 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (string);
2120 if (len < bits || len > bits * 2)
2121 return false;
2123 mask = ((HOST_WIDE_INT_1U << (bits - shift)) - 1) << shift;
2125 zero_val = p[0];
2127 for (unsigned i = 0; i < len; i++)
2128 if (p[i] < bits && (((mulc << p[i]) & mask) >> shift) == i)
2129 matched++;
2131 return matched == bits;
2134 /* Recognize count trailing zeroes idiom.
2135 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
2136 constant which when multiplied by a power of 2 creates a unique value
2137 in the top 5 or 6 bits. This is then indexed into a table which maps it
2138 to the number of trailing zeroes. Array[0] is returned so the caller can
2139 emit an appropriate sequence depending on whether ctz (0) is defined on
2140 the target. */
2141 static bool
2142 optimize_count_trailing_zeroes (tree array_ref, tree x, tree mulc,
2143 tree tshift, HOST_WIDE_INT &zero_val)
2145 tree type = TREE_TYPE (array_ref);
2146 tree array = TREE_OPERAND (array_ref, 0);
2148 gcc_assert (TREE_CODE (mulc) == INTEGER_CST);
2149 gcc_assert (TREE_CODE (tshift) == INTEGER_CST);
2151 tree input_type = TREE_TYPE (x);
2152 unsigned input_bits = tree_to_shwi (TYPE_SIZE (input_type));
2154 /* Check the array element type is not wider than 32 bits and the input is
2155 an unsigned 32-bit or 64-bit type. */
2156 if (TYPE_PRECISION (type) > 32 || !TYPE_UNSIGNED (input_type))
2157 return false;
2158 if (input_bits != 32 && input_bits != 64)
2159 return false;
2161 if (!direct_internal_fn_supported_p (IFN_CTZ, input_type, OPTIMIZE_FOR_BOTH))
2162 return false;
2164 /* Check the lower bound of the array is zero. */
2165 tree low = array_ref_low_bound (array_ref);
2166 if (!low || !integer_zerop (low))
2167 return false;
2169 unsigned shiftval = tree_to_shwi (tshift);
2171 /* Check the shift extracts the top 5..7 bits. */
2172 if (shiftval < input_bits - 7 || shiftval > input_bits - 5)
2173 return false;
2175 tree ctor = ctor_for_folding (array);
2176 if (!ctor)
2177 return false;
2179 unsigned HOST_WIDE_INT val = tree_to_uhwi (mulc);
2181 if (TREE_CODE (ctor) == CONSTRUCTOR)
2182 return check_ctz_array (ctor, val, zero_val, shiftval, input_bits);
2184 if (TREE_CODE (ctor) == STRING_CST
2185 && TYPE_PRECISION (type) == CHAR_TYPE_SIZE)
2186 return check_ctz_string (ctor, val, zero_val, shiftval, input_bits);
2188 return false;
2191 /* Match.pd function to match the ctz expression. */
2192 extern bool gimple_ctz_table_index (tree, tree *, tree (*)(tree));
2194 static bool
2195 simplify_count_trailing_zeroes (gimple_stmt_iterator *gsi)
2197 gimple *stmt = gsi_stmt (*gsi);
2198 tree array_ref = gimple_assign_rhs1 (stmt);
2199 tree res_ops[3];
2200 HOST_WIDE_INT zero_val;
2202 gcc_checking_assert (TREE_CODE (array_ref) == ARRAY_REF);
2204 if (!gimple_ctz_table_index (TREE_OPERAND (array_ref, 1), &res_ops[0], NULL))
2205 return false;
2207 if (optimize_count_trailing_zeroes (array_ref, res_ops[0],
2208 res_ops[1], res_ops[2], zero_val))
2210 tree type = TREE_TYPE (res_ops[0]);
2211 HOST_WIDE_INT ctz_val = 0;
2212 HOST_WIDE_INT type_size = tree_to_shwi (TYPE_SIZE (type));
2213 bool zero_ok
2214 = CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type), ctz_val) == 2;
2216 /* If the input value can't be zero, don't special case ctz (0). */
2217 if (tree_expr_nonzero_p (res_ops[0]))
2219 zero_ok = true;
2220 zero_val = 0;
2221 ctz_val = 0;
2224 /* Skip if there is no value defined at zero, or if we can't easily
2225 return the correct value for zero. */
2226 if (!zero_ok)
2227 return false;
2228 if (zero_val != ctz_val && !(zero_val == 0 && ctz_val == type_size))
2229 return false;
2231 gimple_seq seq = NULL;
2232 gimple *g;
2233 gcall *call = gimple_build_call_internal (IFN_CTZ, 1, res_ops[0]);
2234 gimple_set_location (call, gimple_location (stmt));
2235 gimple_set_lhs (call, make_ssa_name (integer_type_node));
2236 gimple_seq_add_stmt (&seq, call);
2238 tree prev_lhs = gimple_call_lhs (call);
2240 /* Emit ctz (x) & 31 if ctz (0) is 32 but we need to return 0. */
2241 if (zero_val == 0 && ctz_val == type_size)
2243 g = gimple_build_assign (make_ssa_name (integer_type_node),
2244 BIT_AND_EXPR, prev_lhs,
2245 build_int_cst (integer_type_node,
2246 type_size - 1));
2247 gimple_set_location (g, gimple_location (stmt));
2248 gimple_seq_add_stmt (&seq, g);
2249 prev_lhs = gimple_assign_lhs (g);
2252 g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, prev_lhs);
2253 gimple_seq_add_stmt (&seq, g);
2254 gsi_replace_with_seq (gsi, seq, true);
2255 return true;
2258 return false;
2262 /* Combine an element access with a shuffle. Returns true if there were
2263 any changes made, else it returns false. */
2265 static bool
2266 simplify_bitfield_ref (gimple_stmt_iterator *gsi)
2268 gimple *stmt = gsi_stmt (*gsi);
2269 gimple *def_stmt;
2270 tree op, op0, op1;
2271 tree elem_type, type;
2272 tree p, m, tem;
2273 unsigned HOST_WIDE_INT nelts, idx;
2274 poly_uint64 size, elem_size;
2275 enum tree_code code;
2277 op = gimple_assign_rhs1 (stmt);
2278 gcc_checking_assert (TREE_CODE (op) == BIT_FIELD_REF);
2280 op0 = TREE_OPERAND (op, 0);
2281 if (TREE_CODE (op0) != SSA_NAME
2282 || TREE_CODE (TREE_TYPE (op0)) != VECTOR_TYPE)
2283 return false;
2285 def_stmt = get_prop_source_stmt (op0, false, NULL);
2286 if (!def_stmt || !can_propagate_from (def_stmt))
2287 return false;
2289 op1 = TREE_OPERAND (op, 1);
2290 code = gimple_assign_rhs_code (def_stmt);
2291 elem_type = TREE_TYPE (TREE_TYPE (op0));
2292 type = TREE_TYPE (op);
2293 /* Also handle vector type.
2294 .i.e.
2295 _7 = VEC_PERM_EXPR <_1, _1, { 2, 3, 2, 3 }>;
2296 _11 = BIT_FIELD_REF <_7, 64, 0>;
2300 _11 = BIT_FIELD_REF <_1, 64, 64>. */
2302 size = tree_to_poly_uint64 (TYPE_SIZE (type));
2303 if (maybe_ne (bit_field_size (op), size))
2304 return false;
2306 elem_size = tree_to_poly_uint64 (TYPE_SIZE (elem_type));
2307 if (code != VEC_PERM_EXPR
2308 || !constant_multiple_p (bit_field_offset (op), elem_size, &idx))
2309 return false;
2311 m = gimple_assign_rhs3 (def_stmt);
2312 if (TREE_CODE (m) != VECTOR_CST
2313 || !VECTOR_CST_NELTS (m).is_constant (&nelts))
2314 return false;
2316 /* One element. */
2317 if (known_eq (size, elem_size))
2318 idx = TREE_INT_CST_LOW (VECTOR_CST_ELT (m, idx)) % (2 * nelts);
2319 else
2321 unsigned HOST_WIDE_INT nelts_op;
2322 if (!constant_multiple_p (size, elem_size, &nelts_op)
2323 || !pow2p_hwi (nelts_op))
2324 return false;
2325 /* Clamp vec_perm_expr index. */
2326 unsigned start = TREE_INT_CST_LOW (vector_cst_elt (m, idx)) % (2 * nelts);
2327 unsigned end = TREE_INT_CST_LOW (vector_cst_elt (m, idx + nelts_op - 1))
2328 % (2 * nelts);
2329 /* Be in the same vector. */
2330 if ((start < nelts) != (end < nelts))
2331 return false;
2332 for (unsigned HOST_WIDE_INT i = 1; i != nelts_op; i++)
2334 /* Continuous area. */
2335 if (TREE_INT_CST_LOW (vector_cst_elt (m, idx + i)) % (2 * nelts) - 1
2336 != TREE_INT_CST_LOW (vector_cst_elt (m, idx + i - 1))
2337 % (2 * nelts))
2338 return false;
2340 /* Alignment not worse than before. */
2341 if (start % nelts_op)
2342 return false;
2343 idx = start;
2346 if (idx < nelts)
2347 p = gimple_assign_rhs1 (def_stmt);
2348 else
2350 p = gimple_assign_rhs2 (def_stmt);
2351 idx -= nelts;
2354 tem = build3 (BIT_FIELD_REF, TREE_TYPE (op),
2355 p, op1, bitsize_int (idx * elem_size));
2356 gimple_assign_set_rhs1 (stmt, tem);
2357 fold_stmt (gsi);
2358 update_stmt (gsi_stmt (*gsi));
2359 return true;
2362 /* Determine whether applying the 2 permutations (mask1 then mask2)
2363 gives back one of the input. */
2365 static int
2366 is_combined_permutation_identity (tree mask1, tree mask2)
2368 tree mask;
2369 unsigned HOST_WIDE_INT nelts, i, j;
2370 bool maybe_identity1 = true;
2371 bool maybe_identity2 = true;
2373 gcc_checking_assert (TREE_CODE (mask1) == VECTOR_CST
2374 && TREE_CODE (mask2) == VECTOR_CST);
2375 mask = fold_ternary (VEC_PERM_EXPR, TREE_TYPE (mask1), mask1, mask1, mask2);
2376 if (mask == NULL_TREE || TREE_CODE (mask) != VECTOR_CST)
2377 return 0;
2379 if (!VECTOR_CST_NELTS (mask).is_constant (&nelts))
2380 return 0;
2381 for (i = 0; i < nelts; i++)
2383 tree val = VECTOR_CST_ELT (mask, i);
2384 gcc_assert (TREE_CODE (val) == INTEGER_CST);
2385 j = TREE_INT_CST_LOW (val) & (2 * nelts - 1);
2386 if (j == i)
2387 maybe_identity2 = false;
2388 else if (j == i + nelts)
2389 maybe_identity1 = false;
2390 else
2391 return 0;
2393 return maybe_identity1 ? 1 : maybe_identity2 ? 2 : 0;
2396 /* Combine a shuffle with its arguments. Returns 1 if there were any
2397 changes made, 2 if cfg-cleanup needs to run. Else it returns 0. */
2399 static int
2400 simplify_permutation (gimple_stmt_iterator *gsi)
2402 gimple *stmt = gsi_stmt (*gsi);
2403 gimple *def_stmt = NULL;
2404 tree op0, op1, op2, op3, arg0, arg1;
2405 enum tree_code code, code2 = ERROR_MARK;
2406 bool single_use_op0 = false;
2408 gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
2410 op0 = gimple_assign_rhs1 (stmt);
2411 op1 = gimple_assign_rhs2 (stmt);
2412 op2 = gimple_assign_rhs3 (stmt);
2414 if (TREE_CODE (op2) != VECTOR_CST)
2415 return 0;
2417 if (TREE_CODE (op0) == VECTOR_CST)
2419 code = VECTOR_CST;
2420 arg0 = op0;
2422 else if (TREE_CODE (op0) == SSA_NAME)
2424 def_stmt = get_prop_source_stmt (op0, false, &single_use_op0);
2425 if (!def_stmt)
2426 return 0;
2427 code = gimple_assign_rhs_code (def_stmt);
2428 if (code == VIEW_CONVERT_EXPR)
2430 tree rhs = gimple_assign_rhs1 (def_stmt);
2431 tree name = TREE_OPERAND (rhs, 0);
2432 if (TREE_CODE (name) != SSA_NAME)
2433 return 0;
2434 if (!has_single_use (name))
2435 single_use_op0 = false;
2436 /* Here we update the def_stmt through this VIEW_CONVERT_EXPR,
2437 but still keep the code to indicate it comes from
2438 VIEW_CONVERT_EXPR. */
2439 def_stmt = SSA_NAME_DEF_STMT (name);
2440 if (!def_stmt || !is_gimple_assign (def_stmt))
2441 return 0;
2442 if (gimple_assign_rhs_code (def_stmt) != CONSTRUCTOR)
2443 return 0;
2445 if (!can_propagate_from (def_stmt))
2446 return 0;
2447 arg0 = gimple_assign_rhs1 (def_stmt);
2449 else
2450 return 0;
2452 /* Two consecutive shuffles. */
2453 if (code == VEC_PERM_EXPR)
2455 tree orig;
2456 int ident;
2458 if (op0 != op1)
2459 return 0;
2460 op3 = gimple_assign_rhs3 (def_stmt);
2461 if (TREE_CODE (op3) != VECTOR_CST)
2462 return 0;
2463 ident = is_combined_permutation_identity (op3, op2);
2464 if (!ident)
2465 return 0;
2466 orig = (ident == 1) ? gimple_assign_rhs1 (def_stmt)
2467 : gimple_assign_rhs2 (def_stmt);
2468 gimple_assign_set_rhs1 (stmt, unshare_expr (orig));
2469 gimple_assign_set_rhs_code (stmt, TREE_CODE (orig));
2470 gimple_set_num_ops (stmt, 2);
2471 update_stmt (stmt);
2472 return remove_prop_source_from_use (op0) ? 2 : 1;
2474 else if (code == CONSTRUCTOR
2475 || code == VECTOR_CST
2476 || code == VIEW_CONVERT_EXPR)
2478 if (op0 != op1)
2480 if (TREE_CODE (op0) == SSA_NAME && !single_use_op0)
2481 return 0;
2483 if (TREE_CODE (op1) == VECTOR_CST)
2484 arg1 = op1;
2485 else if (TREE_CODE (op1) == SSA_NAME)
2487 gimple *def_stmt2 = get_prop_source_stmt (op1, true, NULL);
2488 if (!def_stmt2)
2489 return 0;
2490 code2 = gimple_assign_rhs_code (def_stmt2);
2491 if (code2 == VIEW_CONVERT_EXPR)
2493 tree rhs = gimple_assign_rhs1 (def_stmt2);
2494 tree name = TREE_OPERAND (rhs, 0);
2495 if (TREE_CODE (name) != SSA_NAME)
2496 return 0;
2497 if (!has_single_use (name))
2498 return 0;
2499 def_stmt2 = SSA_NAME_DEF_STMT (name);
2500 if (!def_stmt2 || !is_gimple_assign (def_stmt2))
2501 return 0;
2502 if (gimple_assign_rhs_code (def_stmt2) != CONSTRUCTOR)
2503 return 0;
2505 else if (code2 != CONSTRUCTOR && code2 != VECTOR_CST)
2506 return 0;
2507 if (!can_propagate_from (def_stmt2))
2508 return 0;
2509 arg1 = gimple_assign_rhs1 (def_stmt2);
2511 else
2512 return 0;
2514 else
2516 /* Already used twice in this statement. */
2517 if (TREE_CODE (op0) == SSA_NAME && num_imm_uses (op0) > 2)
2518 return 0;
2519 arg1 = arg0;
2522 /* If there are any VIEW_CONVERT_EXPRs found when finding permutation
2523 operands source, check whether it's valid to transform and prepare
2524 the required new operands. */
2525 if (code == VIEW_CONVERT_EXPR || code2 == VIEW_CONVERT_EXPR)
2527 /* Figure out the target vector type to which operands should be
2528 converted. If both are CONSTRUCTOR, the types should be the
2529 same, otherwise, use the one of CONSTRUCTOR. */
2530 tree tgt_type = NULL_TREE;
2531 if (code == VIEW_CONVERT_EXPR)
2533 gcc_assert (gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR);
2534 code = CONSTRUCTOR;
2535 tgt_type = TREE_TYPE (arg0);
2537 if (code2 == VIEW_CONVERT_EXPR)
2539 tree arg1_type = TREE_TYPE (arg1);
2540 if (tgt_type == NULL_TREE)
2541 tgt_type = arg1_type;
2542 else if (tgt_type != arg1_type)
2543 return 0;
2546 if (!VECTOR_TYPE_P (tgt_type))
2547 return 0;
2548 tree op2_type = TREE_TYPE (op2);
2550 /* Figure out the shrunk factor. */
2551 poly_uint64 tgt_units = TYPE_VECTOR_SUBPARTS (tgt_type);
2552 poly_uint64 op2_units = TYPE_VECTOR_SUBPARTS (op2_type);
2553 if (maybe_gt (tgt_units, op2_units))
2554 return 0;
2555 unsigned int factor;
2556 if (!constant_multiple_p (op2_units, tgt_units, &factor))
2557 return 0;
2559 /* Build the new permutation control vector as target vector. */
2560 vec_perm_builder builder;
2561 if (!tree_to_vec_perm_builder (&builder, op2))
2562 return 0;
2563 vec_perm_indices indices (builder, 2, op2_units);
2564 vec_perm_indices new_indices;
2565 if (new_indices.new_shrunk_vector (indices, factor))
2567 tree mask_type = tgt_type;
2568 if (!VECTOR_INTEGER_TYPE_P (mask_type))
2570 tree elem_type = TREE_TYPE (mask_type);
2571 unsigned elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type));
2572 tree int_type = build_nonstandard_integer_type (elem_size, 0);
2573 mask_type = build_vector_type (int_type, tgt_units);
2575 op2 = vec_perm_indices_to_tree (mask_type, new_indices);
2577 else
2578 return 0;
2580 /* Convert the VECTOR_CST to the appropriate vector type. */
2581 if (tgt_type != TREE_TYPE (arg0))
2582 arg0 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg0);
2583 else if (tgt_type != TREE_TYPE (arg1))
2584 arg1 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg1);
2587 /* VIEW_CONVERT_EXPR should be updated to CONSTRUCTOR before. */
2588 gcc_assert (code == CONSTRUCTOR || code == VECTOR_CST);
2590 /* Shuffle of a constructor. */
2591 bool ret = false;
2592 tree res_type = TREE_TYPE (arg0);
2593 tree opt = fold_ternary (VEC_PERM_EXPR, res_type, arg0, arg1, op2);
2594 if (!opt
2595 || (TREE_CODE (opt) != CONSTRUCTOR && TREE_CODE (opt) != VECTOR_CST))
2596 return 0;
2597 /* Found VIEW_CONVERT_EXPR before, need one explicit conversion. */
2598 if (res_type != TREE_TYPE (op0))
2600 tree name = make_ssa_name (TREE_TYPE (opt));
2601 gimple *ass_stmt = gimple_build_assign (name, opt);
2602 gsi_insert_before (gsi, ass_stmt, GSI_SAME_STMT);
2603 opt = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op0), name);
2605 gimple_assign_set_rhs_from_tree (gsi, opt);
2606 update_stmt (gsi_stmt (*gsi));
2607 if (TREE_CODE (op0) == SSA_NAME)
2608 ret = remove_prop_source_from_use (op0);
2609 if (op0 != op1 && TREE_CODE (op1) == SSA_NAME)
2610 ret |= remove_prop_source_from_use (op1);
2611 return ret ? 2 : 1;
2614 return 0;
2617 /* Get the BIT_FIELD_REF definition of VAL, if any, looking through
2618 conversions with code CONV_CODE or update it if still ERROR_MARK.
2619 Return NULL_TREE if no such matching def was found. */
2621 static tree
2622 get_bit_field_ref_def (tree val, enum tree_code &conv_code)
2624 if (TREE_CODE (val) != SSA_NAME)
2625 return NULL_TREE ;
2626 gimple *def_stmt = get_prop_source_stmt (val, false, NULL);
2627 if (!def_stmt)
2628 return NULL_TREE;
2629 enum tree_code code = gimple_assign_rhs_code (def_stmt);
2630 if (code == FLOAT_EXPR
2631 || code == FIX_TRUNC_EXPR
2632 || CONVERT_EXPR_CODE_P (code))
2634 tree op1 = gimple_assign_rhs1 (def_stmt);
2635 if (conv_code == ERROR_MARK)
2636 conv_code = code;
2637 else if (conv_code != code)
2638 return NULL_TREE;
2639 if (TREE_CODE (op1) != SSA_NAME)
2640 return NULL_TREE;
2641 def_stmt = SSA_NAME_DEF_STMT (op1);
2642 if (! is_gimple_assign (def_stmt))
2643 return NULL_TREE;
2644 code = gimple_assign_rhs_code (def_stmt);
2646 if (code != BIT_FIELD_REF)
2647 return NULL_TREE;
2648 return gimple_assign_rhs1 (def_stmt);
2651 /* Recognize a VEC_PERM_EXPR. Returns true if there were any changes. */
2653 static bool
2654 simplify_vector_constructor (gimple_stmt_iterator *gsi)
2656 gimple *stmt = gsi_stmt (*gsi);
2657 tree op, orig[2], type, elem_type;
2658 unsigned elem_size, i;
2659 unsigned HOST_WIDE_INT nelts;
2660 unsigned HOST_WIDE_INT refnelts;
2661 enum tree_code conv_code;
2662 constructor_elt *elt;
2664 op = gimple_assign_rhs1 (stmt);
2665 type = TREE_TYPE (op);
2666 gcc_checking_assert (TREE_CODE (op) == CONSTRUCTOR
2667 && TREE_CODE (type) == VECTOR_TYPE);
2669 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
2670 return false;
2671 elem_type = TREE_TYPE (type);
2672 elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type));
2674 orig[0] = NULL;
2675 orig[1] = NULL;
2676 conv_code = ERROR_MARK;
2677 bool maybe_ident = true;
2678 bool maybe_blend[2] = { true, true };
2679 tree one_constant = NULL_TREE;
2680 tree one_nonconstant = NULL_TREE;
2681 auto_vec<tree> constants;
2682 constants.safe_grow_cleared (nelts, true);
2683 auto_vec<std::pair<unsigned, unsigned>, 64> elts;
2684 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (op), i, elt)
2686 tree ref, op1;
2687 unsigned int elem;
2689 if (i >= nelts)
2690 return false;
2692 /* Look for elements extracted and possibly converted from
2693 another vector. */
2694 op1 = get_bit_field_ref_def (elt->value, conv_code);
2695 if (op1
2696 && TREE_CODE ((ref = TREE_OPERAND (op1, 0))) == SSA_NAME
2697 && VECTOR_TYPE_P (TREE_TYPE (ref))
2698 && useless_type_conversion_p (TREE_TYPE (op1),
2699 TREE_TYPE (TREE_TYPE (ref)))
2700 && constant_multiple_p (bit_field_offset (op1),
2701 bit_field_size (op1), &elem)
2702 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (ref)).is_constant (&refnelts))
2704 unsigned int j;
2705 for (j = 0; j < 2; ++j)
2707 if (!orig[j])
2709 if (j == 0
2710 || useless_type_conversion_p (TREE_TYPE (orig[0]),
2711 TREE_TYPE (ref)))
2712 break;
2714 else if (ref == orig[j])
2715 break;
2717 /* Found a suitable vector element. */
2718 if (j < 2)
2720 orig[j] = ref;
2721 if (elem != i || j != 0)
2722 maybe_ident = false;
2723 if (elem != i)
2724 maybe_blend[j] = false;
2725 elts.safe_push (std::make_pair (j, elem));
2726 continue;
2728 /* Else fallthru. */
2730 /* Handle elements not extracted from a vector.
2731 1. constants by permuting with constant vector
2732 2. a unique non-constant element by permuting with a splat vector */
2733 if (orig[1]
2734 && orig[1] != error_mark_node)
2735 return false;
2736 orig[1] = error_mark_node;
2737 if (CONSTANT_CLASS_P (elt->value))
2739 if (one_nonconstant)
2740 return false;
2741 if (!one_constant)
2742 one_constant = elt->value;
2743 constants[i] = elt->value;
2745 else
2747 if (one_constant)
2748 return false;
2749 if (!one_nonconstant)
2750 one_nonconstant = elt->value;
2751 else if (!operand_equal_p (one_nonconstant, elt->value, 0))
2752 return false;
2754 elts.safe_push (std::make_pair (1, i));
2755 maybe_ident = false;
2757 if (i < nelts)
2758 return false;
2760 if (! orig[0]
2761 || ! VECTOR_TYPE_P (TREE_TYPE (orig[0])))
2762 return false;
2763 refnelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (orig[0])).to_constant ();
2764 /* We currently do not handle larger destination vectors. */
2765 if (refnelts < nelts)
2766 return false;
2768 if (maybe_ident)
2770 tree conv_src_type
2771 = (nelts != refnelts
2772 ? (conv_code != ERROR_MARK
2773 ? build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])), nelts)
2774 : type)
2775 : TREE_TYPE (orig[0]));
2776 if (conv_code != ERROR_MARK
2777 && !supportable_convert_operation (conv_code, type, conv_src_type,
2778 &conv_code))
2780 /* Only few targets implement direct conversion patterns so try
2781 some simple special cases via VEC_[UN]PACK[_FLOAT]_LO_EXPR. */
2782 optab optab;
2783 tree halfvectype, dblvectype;
2784 enum tree_code unpack_op;
2786 if (!BYTES_BIG_ENDIAN)
2787 unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
2788 ? VEC_UNPACK_FLOAT_LO_EXPR
2789 : VEC_UNPACK_LO_EXPR);
2790 else
2791 unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
2792 ? VEC_UNPACK_FLOAT_HI_EXPR
2793 : VEC_UNPACK_HI_EXPR);
2795 /* Conversions between DFP and FP have no special tree code
2796 but we cannot handle those since all relevant vector conversion
2797 optabs only have a single mode. */
2798 if (CONVERT_EXPR_CODE_P (conv_code)
2799 && FLOAT_TYPE_P (TREE_TYPE (type))
2800 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (type))
2801 != DECIMAL_FLOAT_TYPE_P (TREE_TYPE (conv_src_type))))
2802 return false;
2804 if (CONVERT_EXPR_CODE_P (conv_code)
2805 && (2 * TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
2806 == TYPE_PRECISION (TREE_TYPE (type)))
2807 && mode_for_vector (as_a <scalar_mode>
2808 (TYPE_MODE (TREE_TYPE (TREE_TYPE (orig[0])))),
2809 nelts * 2).exists ()
2810 && (dblvectype
2811 = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
2812 nelts * 2))
2813 /* Only use it for vector modes or for vector booleans
2814 represented as scalar bitmasks. See PR95528. */
2815 && (VECTOR_MODE_P (TYPE_MODE (dblvectype))
2816 || VECTOR_BOOLEAN_TYPE_P (dblvectype))
2817 && (optab = optab_for_tree_code (unpack_op,
2818 dblvectype,
2819 optab_default))
2820 && (optab_handler (optab, TYPE_MODE (dblvectype))
2821 != CODE_FOR_nothing))
2823 gimple_seq stmts = NULL;
2824 tree dbl;
2825 if (refnelts == nelts)
2827 /* ??? Paradoxical subregs don't exist, so insert into
2828 the lower half of a wider zero vector. */
2829 dbl = gimple_build (&stmts, BIT_INSERT_EXPR, dblvectype,
2830 build_zero_cst (dblvectype), orig[0],
2831 bitsize_zero_node);
2833 else if (refnelts == 2 * nelts)
2834 dbl = orig[0];
2835 else
2836 dbl = gimple_build (&stmts, BIT_FIELD_REF, dblvectype,
2837 orig[0], TYPE_SIZE (dblvectype),
2838 bitsize_zero_node);
2839 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
2840 gimple_assign_set_rhs_with_ops (gsi, unpack_op, dbl);
2842 else if (CONVERT_EXPR_CODE_P (conv_code)
2843 && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
2844 == 2 * TYPE_PRECISION (TREE_TYPE (type)))
2845 && mode_for_vector (as_a <scalar_mode>
2846 (TYPE_MODE
2847 (TREE_TYPE (TREE_TYPE (orig[0])))),
2848 nelts / 2).exists ()
2849 && (halfvectype
2850 = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
2851 nelts / 2))
2852 /* Only use it for vector modes or for vector booleans
2853 represented as scalar bitmasks. See PR95528. */
2854 && (VECTOR_MODE_P (TYPE_MODE (halfvectype))
2855 || VECTOR_BOOLEAN_TYPE_P (halfvectype))
2856 && (optab = optab_for_tree_code (VEC_PACK_TRUNC_EXPR,
2857 halfvectype,
2858 optab_default))
2859 && (optab_handler (optab, TYPE_MODE (halfvectype))
2860 != CODE_FOR_nothing))
2862 gimple_seq stmts = NULL;
2863 tree low = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
2864 orig[0], TYPE_SIZE (halfvectype),
2865 bitsize_zero_node);
2866 tree hig = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
2867 orig[0], TYPE_SIZE (halfvectype),
2868 TYPE_SIZE (halfvectype));
2869 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
2870 gimple_assign_set_rhs_with_ops (gsi, VEC_PACK_TRUNC_EXPR,
2871 low, hig);
2873 else
2874 return false;
2875 update_stmt (gsi_stmt (*gsi));
2876 return true;
2878 if (nelts != refnelts)
2880 gassign *lowpart
2881 = gimple_build_assign (make_ssa_name (conv_src_type),
2882 build3 (BIT_FIELD_REF, conv_src_type,
2883 orig[0], TYPE_SIZE (conv_src_type),
2884 bitsize_zero_node));
2885 gsi_insert_before (gsi, lowpart, GSI_SAME_STMT);
2886 orig[0] = gimple_assign_lhs (lowpart);
2888 if (conv_code == ERROR_MARK)
2890 tree src_type = TREE_TYPE (orig[0]);
2891 if (!useless_type_conversion_p (type, src_type))
2893 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
2894 TYPE_VECTOR_SUBPARTS (src_type))
2895 && useless_type_conversion_p (TREE_TYPE (type),
2896 TREE_TYPE (src_type)));
2897 tree rhs = build1 (VIEW_CONVERT_EXPR, type, orig[0]);
2898 orig[0] = make_ssa_name (type);
2899 gassign *assign = gimple_build_assign (orig[0], rhs);
2900 gsi_insert_before (gsi, assign, GSI_SAME_STMT);
2902 gimple_assign_set_rhs_from_tree (gsi, orig[0]);
2904 else
2905 gimple_assign_set_rhs_with_ops (gsi, conv_code, orig[0],
2906 NULL_TREE, NULL_TREE);
2908 else
2910 /* If we combine a vector with a non-vector avoid cases where
2911 we'll obviously end up with more GIMPLE stmts which is when
2912 we'll later not fold this to a single insert into the vector
2913 and we had a single extract originally. See PR92819. */
2914 if (nelts == 2
2915 && refnelts > 2
2916 && orig[1] == error_mark_node
2917 && !maybe_blend[0])
2918 return false;
2919 tree mask_type, perm_type, conv_src_type;
2920 perm_type = TREE_TYPE (orig[0]);
2921 conv_src_type = (nelts == refnelts
2922 ? perm_type
2923 : build_vector_type (TREE_TYPE (perm_type), nelts));
2924 if (conv_code != ERROR_MARK
2925 && !supportable_convert_operation (conv_code, type, conv_src_type,
2926 &conv_code))
2927 return false;
2929 /* Now that we know the number of elements of the source build the
2930 permute vector.
2931 ??? When the second vector has constant values we can shuffle
2932 it and its source indexes to make the permutation supported.
2933 For now it mimics a blend. */
2934 vec_perm_builder sel (refnelts, refnelts, 1);
2935 bool all_same_p = true;
2936 for (i = 0; i < elts.length (); ++i)
2938 sel.quick_push (elts[i].second + elts[i].first * refnelts);
2939 all_same_p &= known_eq (sel[i], sel[0]);
2941 /* And fill the tail with "something". It's really don't care,
2942 and ideally we'd allow VEC_PERM to have a smaller destination
2943 vector. As a heuristic:
2945 (a) if what we have so far duplicates a single element, make the
2946 tail do the same
2948 (b) otherwise preserve a uniform orig[0]. This facilitates
2949 later pattern-matching of VEC_PERM_EXPR to a BIT_INSERT_EXPR. */
2950 for (; i < refnelts; ++i)
2951 sel.quick_push (all_same_p
2952 ? sel[0]
2953 : (elts[0].second == 0 && elts[0].first == 0
2954 ? 0 : refnelts) + i);
2955 vec_perm_indices indices (sel, orig[1] ? 2 : 1, refnelts);
2956 machine_mode vmode = TYPE_MODE (perm_type);
2957 if (!can_vec_perm_const_p (vmode, vmode, indices))
2958 return false;
2959 mask_type
2960 = build_vector_type (build_nonstandard_integer_type (elem_size, 1),
2961 refnelts);
2962 if (GET_MODE_CLASS (TYPE_MODE (mask_type)) != MODE_VECTOR_INT
2963 || maybe_ne (GET_MODE_SIZE (TYPE_MODE (mask_type)),
2964 GET_MODE_SIZE (TYPE_MODE (perm_type))))
2965 return false;
2966 tree op2 = vec_perm_indices_to_tree (mask_type, indices);
2967 bool converted_orig1 = false;
2968 gimple_seq stmts = NULL;
2969 if (!orig[1])
2970 orig[1] = orig[0];
2971 else if (orig[1] == error_mark_node
2972 && one_nonconstant)
2974 /* ??? We can see if we can safely convert to the original
2975 element type. */
2976 converted_orig1 = conv_code != ERROR_MARK;
2977 orig[1] = gimple_build_vector_from_val (&stmts, UNKNOWN_LOCATION,
2978 converted_orig1
2979 ? type : perm_type,
2980 one_nonconstant);
2982 else if (orig[1] == error_mark_node)
2984 /* ??? See if we can convert the vector to the original type. */
2985 converted_orig1 = conv_code != ERROR_MARK;
2986 unsigned n = converted_orig1 ? nelts : refnelts;
2987 tree_vector_builder vec (converted_orig1
2988 ? type : perm_type, n, 1);
2989 for (unsigned i = 0; i < n; ++i)
2990 if (i < nelts && constants[i])
2991 vec.quick_push (constants[i]);
2992 else
2993 /* ??? Push a don't-care value. */
2994 vec.quick_push (one_constant);
2995 orig[1] = vec.build ();
2997 tree blend_op2 = NULL_TREE;
2998 if (converted_orig1)
3000 /* Make sure we can do a blend in the target type. */
3001 vec_perm_builder sel (nelts, nelts, 1);
3002 for (i = 0; i < elts.length (); ++i)
3003 sel.quick_push (elts[i].first
3004 ? elts[i].second + nelts : i);
3005 vec_perm_indices indices (sel, 2, nelts);
3006 machine_mode vmode = TYPE_MODE (type);
3007 if (!can_vec_perm_const_p (vmode, vmode, indices))
3008 return false;
3009 mask_type
3010 = build_vector_type (build_nonstandard_integer_type (elem_size, 1),
3011 nelts);
3012 if (GET_MODE_CLASS (TYPE_MODE (mask_type)) != MODE_VECTOR_INT
3013 || maybe_ne (GET_MODE_SIZE (TYPE_MODE (mask_type)),
3014 GET_MODE_SIZE (TYPE_MODE (type))))
3015 return false;
3016 blend_op2 = vec_perm_indices_to_tree (mask_type, indices);
3018 tree orig1_for_perm
3019 = converted_orig1 ? build_zero_cst (perm_type) : orig[1];
3020 tree res = gimple_build (&stmts, VEC_PERM_EXPR, perm_type,
3021 orig[0], orig1_for_perm, op2);
3022 if (nelts != refnelts)
3023 res = gimple_build (&stmts, BIT_FIELD_REF,
3024 conv_code != ERROR_MARK ? conv_src_type : type,
3025 res, TYPE_SIZE (type), bitsize_zero_node);
3026 if (conv_code != ERROR_MARK)
3027 res = gimple_build (&stmts, conv_code, type, res);
3028 else if (!useless_type_conversion_p (type, TREE_TYPE (res)))
3030 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
3031 TYPE_VECTOR_SUBPARTS (perm_type))
3032 && useless_type_conversion_p (TREE_TYPE (type),
3033 TREE_TYPE (perm_type)));
3034 res = gimple_build (&stmts, VIEW_CONVERT_EXPR, type, res);
3036 /* Blend in the actual constant. */
3037 if (converted_orig1)
3038 res = gimple_build (&stmts, VEC_PERM_EXPR, type,
3039 res, orig[1], blend_op2);
3040 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3041 gimple_assign_set_rhs_with_ops (gsi, SSA_NAME, res);
3043 update_stmt (gsi_stmt (*gsi));
3044 return true;
3048 /* Rewrite the vector load at *GSI to component-wise loads if the load
3049 is only used in BIT_FIELD_REF extractions with eventual intermediate
3050 widening. */
3052 static void
3053 optimize_vector_load (gimple_stmt_iterator *gsi)
3055 gimple *stmt = gsi_stmt (*gsi);
3056 tree lhs = gimple_assign_lhs (stmt);
3057 tree rhs = gimple_assign_rhs1 (stmt);
3059 /* Gather BIT_FIELD_REFs to rewrite, looking through
3060 VEC_UNPACK_{LO,HI}_EXPR. */
3061 use_operand_p use_p;
3062 imm_use_iterator iter;
3063 bool rewrite = true;
3064 auto_vec<gimple *, 8> bf_stmts;
3065 auto_vec<tree, 8> worklist;
3066 worklist.quick_push (lhs);
3069 tree def = worklist.pop ();
3070 unsigned HOST_WIDE_INT def_eltsize
3071 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (def))));
3072 FOR_EACH_IMM_USE_FAST (use_p, iter, def)
3074 gimple *use_stmt = USE_STMT (use_p);
3075 if (is_gimple_debug (use_stmt))
3076 continue;
3077 if (!is_gimple_assign (use_stmt))
3079 rewrite = false;
3080 break;
3082 enum tree_code use_code = gimple_assign_rhs_code (use_stmt);
3083 tree use_rhs = gimple_assign_rhs1 (use_stmt);
3084 if (use_code == BIT_FIELD_REF
3085 && TREE_OPERAND (use_rhs, 0) == def
3086 /* If its on the VEC_UNPACK_{HI,LO}_EXPR
3087 def need to verify it is element aligned. */
3088 && (def == lhs
3089 || (known_eq (bit_field_size (use_rhs), def_eltsize)
3090 && constant_multiple_p (bit_field_offset (use_rhs),
3091 def_eltsize))))
3093 bf_stmts.safe_push (use_stmt);
3094 continue;
3096 /* Walk through one level of VEC_UNPACK_{LO,HI}_EXPR. */
3097 if (def == lhs
3098 && (use_code == VEC_UNPACK_HI_EXPR
3099 || use_code == VEC_UNPACK_LO_EXPR)
3100 && use_rhs == lhs)
3102 worklist.safe_push (gimple_assign_lhs (use_stmt));
3103 continue;
3105 rewrite = false;
3106 break;
3108 if (!rewrite)
3109 break;
3111 while (!worklist.is_empty ());
3113 if (!rewrite)
3115 gsi_next (gsi);
3116 return;
3118 /* We now have all ultimate uses of the load to rewrite in bf_stmts. */
3120 /* Prepare the original ref to be wrapped in adjusted BIT_FIELD_REFs.
3121 For TARGET_MEM_REFs we have to separate the LEA from the reference. */
3122 tree load_rhs = rhs;
3123 if (TREE_CODE (load_rhs) == TARGET_MEM_REF)
3125 if (TREE_CODE (TREE_OPERAND (load_rhs, 0)) == ADDR_EXPR)
3126 mark_addressable (TREE_OPERAND (TREE_OPERAND (load_rhs, 0), 0));
3127 tree tem = make_ssa_name (TREE_TYPE (TREE_OPERAND (load_rhs, 0)));
3128 gimple *new_stmt
3129 = gimple_build_assign (tem, build1 (ADDR_EXPR, TREE_TYPE (tem),
3130 unshare_expr (load_rhs)));
3131 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
3132 load_rhs = build2_loc (EXPR_LOCATION (load_rhs),
3133 MEM_REF, TREE_TYPE (load_rhs), tem,
3134 build_int_cst
3135 (TREE_TYPE (TREE_OPERAND (load_rhs, 1)), 0));
3138 /* Rewrite the BIT_FIELD_REFs to be actual loads, re-emitting them at
3139 the place of the original load. */
3140 for (gimple *use_stmt : bf_stmts)
3142 tree bfr = gimple_assign_rhs1 (use_stmt);
3143 tree new_rhs = unshare_expr (load_rhs);
3144 if (TREE_OPERAND (bfr, 0) != lhs)
3146 /* When the BIT_FIELD_REF is on the promoted vector we have to
3147 adjust it and emit a conversion afterwards. */
3148 gimple *def_stmt
3149 = SSA_NAME_DEF_STMT (TREE_OPERAND (bfr, 0));
3150 enum tree_code def_code
3151 = gimple_assign_rhs_code (def_stmt);
3153 /* The adjusted BIT_FIELD_REF is of the promotion source
3154 vector size and at half of the offset... */
3155 new_rhs = fold_build3 (BIT_FIELD_REF,
3156 TREE_TYPE (TREE_TYPE (lhs)),
3157 new_rhs,
3158 TYPE_SIZE (TREE_TYPE (TREE_TYPE (lhs))),
3159 size_binop (EXACT_DIV_EXPR,
3160 TREE_OPERAND (bfr, 2),
3161 bitsize_int (2)));
3162 /* ... and offsetted by half of the vector if VEC_UNPACK_HI_EXPR. */
3163 if (def_code == (!BYTES_BIG_ENDIAN
3164 ? VEC_UNPACK_HI_EXPR : VEC_UNPACK_LO_EXPR))
3165 TREE_OPERAND (new_rhs, 2)
3166 = size_binop (PLUS_EXPR, TREE_OPERAND (new_rhs, 2),
3167 size_binop (EXACT_DIV_EXPR,
3168 TYPE_SIZE (TREE_TYPE (lhs)),
3169 bitsize_int (2)));
3170 tree tem = make_ssa_name (TREE_TYPE (TREE_TYPE (lhs)));
3171 gimple *new_stmt = gimple_build_assign (tem, new_rhs);
3172 location_t loc = gimple_location (use_stmt);
3173 gimple_set_location (new_stmt, loc);
3174 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
3175 /* Perform scalar promotion. */
3176 new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
3177 NOP_EXPR, tem);
3178 gimple_set_location (new_stmt, loc);
3179 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
3181 else
3183 /* When the BIT_FIELD_REF is on the original load result
3184 we can just wrap that. */
3185 tree new_rhs = fold_build3 (BIT_FIELD_REF, TREE_TYPE (bfr),
3186 unshare_expr (load_rhs),
3187 TREE_OPERAND (bfr, 1),
3188 TREE_OPERAND (bfr, 2));
3189 gimple *new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
3190 new_rhs);
3191 location_t loc = gimple_location (use_stmt);
3192 gimple_set_location (new_stmt, loc);
3193 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
3195 gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
3196 unlink_stmt_vdef (use_stmt);
3197 gsi_remove (&gsi2, true);
3200 /* Finally get rid of the intermediate stmts. */
3201 gimple *use_stmt;
3202 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
3204 if (is_gimple_debug (use_stmt))
3206 if (gimple_debug_bind_p (use_stmt))
3208 gimple_debug_bind_reset_value (use_stmt);
3209 update_stmt (use_stmt);
3211 continue;
3213 gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
3214 unlink_stmt_vdef (use_stmt);
3215 release_defs (use_stmt);
3216 gsi_remove (&gsi2, true);
3218 /* And the original load. */
3219 release_defs (stmt);
3220 gsi_remove (gsi, true);
3224 /* Primitive "lattice" function for gimple_simplify. */
3226 static tree
3227 fwprop_ssa_val (tree name)
3229 /* First valueize NAME. */
3230 if (TREE_CODE (name) == SSA_NAME
3231 && SSA_NAME_VERSION (name) < lattice.length ())
3233 tree val = lattice[SSA_NAME_VERSION (name)];
3234 if (val)
3235 name = val;
3237 /* We continue matching along SSA use-def edges for SSA names
3238 that are not single-use. Currently there are no patterns
3239 that would cause any issues with that. */
3240 return name;
3243 /* Main entry point for the forward propagation and statement combine
3244 optimizer. */
3246 namespace {
3248 const pass_data pass_data_forwprop =
3250 GIMPLE_PASS, /* type */
3251 "forwprop", /* name */
3252 OPTGROUP_NONE, /* optinfo_flags */
3253 TV_TREE_FORWPROP, /* tv_id */
3254 ( PROP_cfg | PROP_ssa ), /* properties_required */
3255 0, /* properties_provided */
3256 0, /* properties_destroyed */
3257 0, /* todo_flags_start */
3258 TODO_update_ssa, /* todo_flags_finish */
3261 class pass_forwprop : public gimple_opt_pass
3263 public:
3264 pass_forwprop (gcc::context *ctxt)
3265 : gimple_opt_pass (pass_data_forwprop, ctxt)
3268 /* opt_pass methods: */
3269 opt_pass * clone () final override { return new pass_forwprop (m_ctxt); }
3270 bool gate (function *) final override { return flag_tree_forwprop; }
3271 unsigned int execute (function *) final override;
3273 }; // class pass_forwprop
3275 unsigned int
3276 pass_forwprop::execute (function *fun)
3278 unsigned int todoflags = 0;
3280 cfg_changed = false;
3282 /* Combine stmts with the stmts defining their operands. Do that
3283 in an order that guarantees visiting SSA defs before SSA uses. */
3284 lattice.create (num_ssa_names);
3285 lattice.quick_grow_cleared (num_ssa_names);
3286 int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (fun));
3287 int postorder_num = pre_and_rev_post_order_compute_fn (cfun, NULL,
3288 postorder, false);
3289 auto_vec<gimple *, 4> to_fixup;
3290 auto_vec<gimple *, 32> to_remove;
3291 to_purge = BITMAP_ALLOC (NULL);
3292 for (int i = 0; i < postorder_num; ++i)
3294 gimple_stmt_iterator gsi;
3295 basic_block bb = BASIC_BLOCK_FOR_FN (fun, postorder[i]);
3297 /* Record degenerate PHIs in the lattice. */
3298 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
3299 gsi_next (&si))
3301 gphi *phi = si.phi ();
3302 tree res = gimple_phi_result (phi);
3303 if (virtual_operand_p (res))
3304 continue;
3306 use_operand_p use_p;
3307 ssa_op_iter it;
3308 tree first = NULL_TREE;
3309 bool all_same = true;
3310 FOR_EACH_PHI_ARG (use_p, phi, it, SSA_OP_USE)
3312 tree use = USE_FROM_PTR (use_p);
3313 if (! first)
3314 first = use;
3315 else if (! operand_equal_p (first, use, 0))
3317 all_same = false;
3318 break;
3321 if (all_same)
3323 if (may_propagate_copy (res, first))
3324 to_remove.safe_push (phi);
3325 fwprop_set_lattice_val (res, first);
3329 /* Apply forward propagation to all stmts in the basic-block.
3330 Note we update GSI within the loop as necessary. */
3331 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
3333 gimple *stmt = gsi_stmt (gsi);
3334 tree lhs, rhs;
3335 enum tree_code code;
3337 if (!is_gimple_assign (stmt))
3339 gsi_next (&gsi);
3340 continue;
3343 lhs = gimple_assign_lhs (stmt);
3344 rhs = gimple_assign_rhs1 (stmt);
3345 code = gimple_assign_rhs_code (stmt);
3346 if (TREE_CODE (lhs) != SSA_NAME
3347 || has_zero_uses (lhs))
3349 gsi_next (&gsi);
3350 continue;
3353 /* If this statement sets an SSA_NAME to an address,
3354 try to propagate the address into the uses of the SSA_NAME. */
3355 if ((code == ADDR_EXPR
3356 /* Handle pointer conversions on invariant addresses
3357 as well, as this is valid gimple. */
3358 || (CONVERT_EXPR_CODE_P (code)
3359 && TREE_CODE (rhs) == ADDR_EXPR
3360 && POINTER_TYPE_P (TREE_TYPE (lhs))))
3361 && TREE_CODE (TREE_OPERAND (rhs, 0)) != TARGET_MEM_REF)
3363 tree base = get_base_address (TREE_OPERAND (rhs, 0));
3364 if ((!base
3365 || !DECL_P (base)
3366 || decl_address_invariant_p (base))
3367 && !stmt_references_abnormal_ssa_name (stmt)
3368 && forward_propagate_addr_expr (lhs, rhs, true))
3370 fwprop_invalidate_lattice (gimple_get_lhs (stmt));
3371 release_defs (stmt);
3372 gsi_remove (&gsi, true);
3374 else
3375 gsi_next (&gsi);
3377 else if (code == POINTER_PLUS_EXPR)
3379 tree off = gimple_assign_rhs2 (stmt);
3380 if (TREE_CODE (off) == INTEGER_CST
3381 && can_propagate_from (stmt)
3382 && !simple_iv_increment_p (stmt)
3383 /* ??? Better adjust the interface to that function
3384 instead of building new trees here. */
3385 && forward_propagate_addr_expr
3386 (lhs,
3387 build1_loc (gimple_location (stmt),
3388 ADDR_EXPR, TREE_TYPE (rhs),
3389 fold_build2 (MEM_REF,
3390 TREE_TYPE (TREE_TYPE (rhs)),
3391 rhs,
3392 fold_convert (ptr_type_node,
3393 off))), true))
3395 fwprop_invalidate_lattice (gimple_get_lhs (stmt));
3396 release_defs (stmt);
3397 gsi_remove (&gsi, true);
3399 else if (is_gimple_min_invariant (rhs))
3401 /* Make sure to fold &a[0] + off_1 here. */
3402 fold_stmt_inplace (&gsi);
3403 update_stmt (stmt);
3404 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
3405 gsi_next (&gsi);
3407 else
3408 gsi_next (&gsi);
3410 else if (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE
3411 && gimple_assign_load_p (stmt)
3412 && !gimple_has_volatile_ops (stmt)
3413 && (TREE_CODE (gimple_assign_rhs1 (stmt))
3414 != TARGET_MEM_REF)
3415 && !stmt_can_throw_internal (cfun, stmt))
3417 /* Rewrite loads used only in real/imagpart extractions to
3418 component-wise loads. */
3419 use_operand_p use_p;
3420 imm_use_iterator iter;
3421 bool rewrite = true;
3422 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
3424 gimple *use_stmt = USE_STMT (use_p);
3425 if (is_gimple_debug (use_stmt))
3426 continue;
3427 if (!is_gimple_assign (use_stmt)
3428 || (gimple_assign_rhs_code (use_stmt) != REALPART_EXPR
3429 && gimple_assign_rhs_code (use_stmt) != IMAGPART_EXPR)
3430 || TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) != lhs)
3432 rewrite = false;
3433 break;
3436 if (rewrite)
3438 gimple *use_stmt;
3439 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
3441 if (is_gimple_debug (use_stmt))
3443 if (gimple_debug_bind_p (use_stmt))
3445 gimple_debug_bind_reset_value (use_stmt);
3446 update_stmt (use_stmt);
3448 continue;
3451 tree new_rhs = build1 (gimple_assign_rhs_code (use_stmt),
3452 TREE_TYPE (TREE_TYPE (rhs)),
3453 unshare_expr (rhs));
3454 gimple *new_stmt
3455 = gimple_build_assign (gimple_assign_lhs (use_stmt),
3456 new_rhs);
3458 location_t loc = gimple_location (use_stmt);
3459 gimple_set_location (new_stmt, loc);
3460 gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
3461 unlink_stmt_vdef (use_stmt);
3462 gsi_remove (&gsi2, true);
3464 gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
3467 release_defs (stmt);
3468 gsi_remove (&gsi, true);
3470 else
3471 gsi_next (&gsi);
3473 else if (TREE_CODE (TREE_TYPE (lhs)) == VECTOR_TYPE
3474 && (TYPE_MODE (TREE_TYPE (lhs)) == BLKmode
3475 /* After vector lowering rewrite all loads, but
3476 initially do not since this conflicts with
3477 vector CONSTRUCTOR to shuffle optimization. */
3478 || (fun->curr_properties & PROP_gimple_lvec))
3479 && gimple_assign_load_p (stmt)
3480 && !gimple_has_volatile_ops (stmt)
3481 && !stmt_can_throw_internal (cfun, stmt)
3482 && (!VAR_P (rhs) || !DECL_HARD_REGISTER (rhs)))
3483 optimize_vector_load (&gsi);
3485 else if (code == COMPLEX_EXPR)
3487 /* Rewrite stores of a single-use complex build expression
3488 to component-wise stores. */
3489 use_operand_p use_p;
3490 gimple *use_stmt;
3491 if (single_imm_use (lhs, &use_p, &use_stmt)
3492 && gimple_store_p (use_stmt)
3493 && !gimple_has_volatile_ops (use_stmt)
3494 && is_gimple_assign (use_stmt)
3495 && (TREE_CODE (gimple_assign_lhs (use_stmt))
3496 != TARGET_MEM_REF))
3498 tree use_lhs = gimple_assign_lhs (use_stmt);
3499 if (auto_var_p (use_lhs))
3500 DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
3501 tree new_lhs = build1 (REALPART_EXPR,
3502 TREE_TYPE (TREE_TYPE (use_lhs)),
3503 unshare_expr (use_lhs));
3504 gimple *new_stmt = gimple_build_assign (new_lhs, rhs);
3505 location_t loc = gimple_location (use_stmt);
3506 gimple_set_location (new_stmt, loc);
3507 gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
3508 gimple_set_vdef (new_stmt, make_ssa_name (gimple_vop (cfun)));
3509 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
3510 gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
3511 gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
3512 gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
3514 new_lhs = build1 (IMAGPART_EXPR,
3515 TREE_TYPE (TREE_TYPE (use_lhs)),
3516 unshare_expr (use_lhs));
3517 gimple_assign_set_lhs (use_stmt, new_lhs);
3518 gimple_assign_set_rhs1 (use_stmt, gimple_assign_rhs2 (stmt));
3519 update_stmt (use_stmt);
3521 release_defs (stmt);
3522 gsi_remove (&gsi, true);
3524 else
3525 gsi_next (&gsi);
3527 else if (code == CONSTRUCTOR
3528 && VECTOR_TYPE_P (TREE_TYPE (rhs))
3529 && TYPE_MODE (TREE_TYPE (rhs)) == BLKmode
3530 && CONSTRUCTOR_NELTS (rhs) > 0
3531 && (!VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
3532 || (TYPE_MODE (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
3533 != BLKmode)))
3535 /* Rewrite stores of a single-use vector constructors
3536 to component-wise stores if the mode isn't supported. */
3537 use_operand_p use_p;
3538 gimple *use_stmt;
3539 if (single_imm_use (lhs, &use_p, &use_stmt)
3540 && gimple_store_p (use_stmt)
3541 && !gimple_has_volatile_ops (use_stmt)
3542 && !stmt_can_throw_internal (cfun, use_stmt)
3543 && is_gimple_assign (use_stmt)
3544 && (TREE_CODE (gimple_assign_lhs (use_stmt))
3545 != TARGET_MEM_REF))
3547 tree elt_t = TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value);
3548 unsigned HOST_WIDE_INT elt_w
3549 = tree_to_uhwi (TYPE_SIZE (elt_t));
3550 unsigned HOST_WIDE_INT n
3551 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs)));
3552 tree use_lhs = gimple_assign_lhs (use_stmt);
3553 if (auto_var_p (use_lhs))
3554 DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
3555 for (unsigned HOST_WIDE_INT bi = 0; bi < n; bi += elt_w)
3557 unsigned HOST_WIDE_INT ci = bi / elt_w;
3558 tree new_rhs;
3559 if (ci < CONSTRUCTOR_NELTS (rhs))
3560 new_rhs = CONSTRUCTOR_ELT (rhs, ci)->value;
3561 else
3562 new_rhs = build_zero_cst (elt_t);
3563 tree new_lhs = build3 (BIT_FIELD_REF,
3564 elt_t,
3565 unshare_expr (use_lhs),
3566 bitsize_int (elt_w),
3567 bitsize_int (bi));
3568 gimple *new_stmt = gimple_build_assign (new_lhs, new_rhs);
3569 location_t loc = gimple_location (use_stmt);
3570 gimple_set_location (new_stmt, loc);
3571 gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
3572 gimple_set_vdef (new_stmt,
3573 make_ssa_name (gimple_vop (cfun)));
3574 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
3575 gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
3576 gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
3577 gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
3579 gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
3580 unlink_stmt_vdef (use_stmt);
3581 release_defs (use_stmt);
3582 gsi_remove (&gsi2, true);
3583 release_defs (stmt);
3584 gsi_remove (&gsi, true);
3586 else
3587 gsi_next (&gsi);
3589 else
3590 gsi_next (&gsi);
3593 /* Combine stmts with the stmts defining their operands.
3594 Note we update GSI within the loop as necessary. */
3595 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3597 gimple *stmt = gsi_stmt (gsi);
3599 /* Mark stmt as potentially needing revisiting. */
3600 gimple_set_plf (stmt, GF_PLF_1, false);
3602 /* Substitute from our lattice. We need to do so only once. */
3603 bool substituted_p = false;
3604 use_operand_p usep;
3605 ssa_op_iter iter;
3606 FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
3608 tree use = USE_FROM_PTR (usep);
3609 tree val = fwprop_ssa_val (use);
3610 if (val && val != use && may_propagate_copy (use, val))
3612 propagate_value (usep, val);
3613 substituted_p = true;
3616 if (substituted_p
3617 && is_gimple_assign (stmt)
3618 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
3619 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
3621 bool changed;
3624 gimple *orig_stmt = stmt = gsi_stmt (gsi);
3625 bool was_noreturn = (is_gimple_call (stmt)
3626 && gimple_call_noreturn_p (stmt));
3627 changed = false;
3629 if (fold_stmt (&gsi, fwprop_ssa_val))
3631 changed = true;
3632 stmt = gsi_stmt (gsi);
3633 /* Cleanup the CFG if we simplified a condition to
3634 true or false. */
3635 if (gcond *cond = dyn_cast <gcond *> (stmt))
3636 if (gimple_cond_true_p (cond)
3637 || gimple_cond_false_p (cond))
3638 cfg_changed = true;
3641 if (changed || substituted_p)
3643 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
3644 bitmap_set_bit (to_purge, bb->index);
3645 if (!was_noreturn
3646 && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
3647 to_fixup.safe_push (stmt);
3648 update_stmt (stmt);
3649 substituted_p = false;
3652 switch (gimple_code (stmt))
3654 case GIMPLE_ASSIGN:
3656 tree rhs1 = gimple_assign_rhs1 (stmt);
3657 enum tree_code code = gimple_assign_rhs_code (stmt);
3659 if (TREE_CODE_CLASS (code) == tcc_comparison)
3661 int did_something;
3662 did_something = forward_propagate_into_comparison (&gsi);
3663 if (maybe_clean_or_replace_eh_stmt (stmt, gsi_stmt (gsi)))
3664 bitmap_set_bit (to_purge, bb->index);
3665 if (did_something == 2)
3666 cfg_changed = true;
3667 changed = did_something != 0;
3669 else if ((code == PLUS_EXPR
3670 || code == BIT_IOR_EXPR
3671 || code == BIT_XOR_EXPR)
3672 && simplify_rotate (&gsi))
3673 changed = true;
3674 else if (code == VEC_PERM_EXPR)
3676 int did_something = simplify_permutation (&gsi);
3677 if (did_something == 2)
3678 cfg_changed = true;
3679 changed = did_something != 0;
3681 else if (code == BIT_FIELD_REF)
3682 changed = simplify_bitfield_ref (&gsi);
3683 else if (code == CONSTRUCTOR
3684 && TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
3685 changed = simplify_vector_constructor (&gsi);
3686 else if (code == ARRAY_REF)
3687 changed = simplify_count_trailing_zeroes (&gsi);
3688 break;
3691 case GIMPLE_SWITCH:
3692 changed = simplify_gimple_switch (as_a <gswitch *> (stmt));
3693 break;
3695 case GIMPLE_COND:
3697 int did_something = forward_propagate_into_gimple_cond
3698 (as_a <gcond *> (stmt));
3699 if (did_something == 2)
3700 cfg_changed = true;
3701 changed = did_something != 0;
3702 break;
3705 case GIMPLE_CALL:
3707 tree callee = gimple_call_fndecl (stmt);
3708 if (callee != NULL_TREE
3709 && fndecl_built_in_p (callee, BUILT_IN_NORMAL))
3710 changed = simplify_builtin_call (&gsi, callee);
3711 break;
3714 default:;
3717 if (changed)
3719 /* If the stmt changed then re-visit it and the statements
3720 inserted before it. */
3721 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
3722 if (gimple_plf (gsi_stmt (gsi), GF_PLF_1))
3723 break;
3724 if (gsi_end_p (gsi))
3725 gsi = gsi_start_bb (bb);
3726 else
3727 gsi_next (&gsi);
3730 while (changed);
3732 /* Stmt no longer needs to be revisited. */
3733 stmt = gsi_stmt (gsi);
3734 gcc_checking_assert (!gimple_plf (stmt, GF_PLF_1));
3735 gimple_set_plf (stmt, GF_PLF_1, true);
3737 /* Fill up the lattice. */
3738 if (gimple_assign_single_p (stmt))
3740 tree lhs = gimple_assign_lhs (stmt);
3741 tree rhs = gimple_assign_rhs1 (stmt);
3742 if (TREE_CODE (lhs) == SSA_NAME)
3744 tree val = lhs;
3745 if (TREE_CODE (rhs) == SSA_NAME)
3746 val = fwprop_ssa_val (rhs);
3747 else if (is_gimple_min_invariant (rhs))
3748 val = rhs;
3749 /* If we can propagate the lattice-value mark the
3750 stmt for removal. */
3751 if (val != lhs
3752 && may_propagate_copy (lhs, val))
3753 to_remove.safe_push (stmt);
3754 fwprop_set_lattice_val (lhs, val);
3757 else if (gimple_nop_p (stmt))
3758 to_remove.safe_push (stmt);
3761 /* Substitute in destination PHI arguments. */
3762 edge_iterator ei;
3763 edge e;
3764 FOR_EACH_EDGE (e, ei, bb->succs)
3765 for (gphi_iterator gsi = gsi_start_phis (e->dest);
3766 !gsi_end_p (gsi); gsi_next (&gsi))
3768 gphi *phi = gsi.phi ();
3769 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
3770 tree arg = USE_FROM_PTR (use_p);
3771 if (TREE_CODE (arg) != SSA_NAME
3772 || virtual_operand_p (arg))
3773 continue;
3774 tree val = fwprop_ssa_val (arg);
3775 if (val != arg
3776 && may_propagate_copy (arg, val))
3777 propagate_value (use_p, val);
3780 free (postorder);
3781 lattice.release ();
3783 /* Remove stmts in reverse order to make debug stmt creation possible. */
3784 while (!to_remove.is_empty())
3786 gimple *stmt = to_remove.pop ();
3787 if (dump_file && (dump_flags & TDF_DETAILS))
3789 fprintf (dump_file, "Removing dead stmt ");
3790 print_gimple_stmt (dump_file, stmt, 0);
3791 fprintf (dump_file, "\n");
3793 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
3794 if (gimple_code (stmt) == GIMPLE_PHI)
3795 remove_phi_node (&gsi, true);
3796 else
3798 unlink_stmt_vdef (stmt);
3799 gsi_remove (&gsi, true);
3800 release_defs (stmt);
3804 /* Fixup stmts that became noreturn calls. This may require splitting
3805 blocks and thus isn't possible during the walk. Do this
3806 in reverse order so we don't inadvertedly remove a stmt we want to
3807 fixup by visiting a dominating now noreturn call first. */
3808 while (!to_fixup.is_empty ())
3810 gimple *stmt = to_fixup.pop ();
3811 if (dump_file && dump_flags & TDF_DETAILS)
3813 fprintf (dump_file, "Fixing up noreturn call ");
3814 print_gimple_stmt (dump_file, stmt, 0);
3815 fprintf (dump_file, "\n");
3817 cfg_changed |= fixup_noreturn_call (stmt);
3820 cfg_changed |= gimple_purge_all_dead_eh_edges (to_purge);
3821 BITMAP_FREE (to_purge);
3823 if (cfg_changed)
3824 todoflags |= TODO_cleanup_cfg;
3826 return todoflags;
3829 } // anon namespace
3831 gimple_opt_pass *
3832 make_pass_forwprop (gcc::context *ctxt)
3834 return new pass_forwprop (ctxt);