1 /* Forward propagation of expressions for single use variables.
2 Copyright (C) 2004-2021 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)
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/>. */
22 #include "coretypes.h"
28 #include "tree-pass.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-fold.h"
38 #include "gimple-iterator.h"
39 #include "gimplify-me.h"
43 #include "tree-ssa-propagate.h"
44 #include "tree-ssa-dom.h"
46 #include "tree-cfgcleanup.h"
48 #include "optabs-tree.h"
49 #include "tree-vector-builder.h"
50 #include "vec-perm-indices.h"
51 #include "internal-fn.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.
65 if (x) goto ... else goto ...
67 Will be transformed into:
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):
78 if (x EQ/NEQ c2) goto ... else goto ...
80 Will be transformed into:
83 if (a EQ/NEQ (c2 - c1)) goto ... else goto ...
85 Similarly for x = a - c1.
91 if (x) goto ... else goto ...
93 Will be transformed into:
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.
106 if (x) goto ... else goto ...
108 Will be transformed into:
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
140 ptr = (type1*)&type2var;
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)
150 ptr2 = ptr + <constant>;
154 ptr2 = &x[constant/elementsize];
159 offset = index * element_size;
160 offset_p = (pointer) offset;
161 ptr2 = ptr + offset_p
163 Will get turned into:
171 Provided that decl has known alignment >= 2, will get turned into
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
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. */
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. */
210 fwprop_invalidate_lattice (tree 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. */
228 get_prop_source_stmt (tree name
, bool single_use_only
, bool *single_use_p
)
230 bool single_use
= true;
233 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
235 if (!has_single_use (name
))
242 /* If name is defined by a PHI node or is the default def, bail out. */
243 if (!is_gimple_assign (def_stmt
))
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
);
251 if (!single_use_only
&& single_use_p
)
252 *single_use_p
= single_use
;
259 /* Checks if the destination ssa name in DEF_STMT can be used as
260 propagation source. Returns true if so, otherwise false. */
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
))
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
)
276 /* Constants can be always propagated. */
277 if (gimple_assign_single_p (def_stmt
)
278 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt
)))
281 /* We cannot propagate ssa names that occur in abnormal phi nodes. */
282 if (stmt_references_abnormal_ssa_name (def_stmt
))
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
)
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
305 Returns true if cleanup-cfg has to run. */
308 remove_prop_source_from_use (tree name
)
310 gimple_stmt_iterator gsi
;
312 bool cfg_changed
= false;
317 if (SSA_NAME_IN_FREE_LIST (name
)
318 || SSA_NAME_IS_DEFAULT_DEF (name
)
319 || !has_zero_uses (name
))
322 stmt
= SSA_NAME_DEF_STMT (name
);
323 if (gimple_code (stmt
) == GIMPLE_PHI
324 || gimple_has_side_effects (stmt
))
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
));
335 name
= is_gimple_assign (stmt
) ? gimple_assign_rhs1 (stmt
) : NULL_TREE
;
336 } while (name
&& TREE_CODE (name
) == SSA_NAME
);
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 . */
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
);
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. */
378 combine_cond_expr_cond (gimple
*stmt
, enum tree_code code
, tree type
,
379 tree op0
, tree op1
, bool invariant_only
)
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
);
389 fold_undefer_overflow_warnings (false, NULL
, 0);
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);
406 bool nowarn
= warning_suppressed_p (stmt
, OPT_Wstrict_overflow
);
407 fold_undefer_overflow_warnings (!nowarn
, stmt
, 0);
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. */
417 forward_propagate_into_comparison_1 (gimple
*stmt
,
418 enum tree_code code
, tree type
,
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)))
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
);
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
);
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
,
471 !(single_use0_p
&& single_use1_p
));
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. */
482 forward_propagate_into_comparison (gimple_stmt_iterator
*gsi
)
484 gimple
*stmt
= gsi_stmt (*gsi
);
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
),
495 if (tmp
&& useless_type_conversion_p (type
, TREE_TYPE (tmp
)))
497 gimple_assign_set_rhs_from_tree (gsi
, tmp
);
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;
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 This must be kept in sync with forward_propagate_into_cond. */
519 forward_propagate_into_gimple_cond (gcond
*stmt
)
522 enum tree_code code
= gimple_cond_code (stmt
);
523 bool cfg_changed
= false;
524 tree rhs1
= gimple_cond_lhs (stmt
);
525 tree rhs2
= gimple_cond_rhs (stmt
);
527 /* We can do tree combining on SSA_NAME and comparison expressions. */
528 if (TREE_CODE_CLASS (gimple_cond_code (stmt
)) != tcc_comparison
)
531 tmp
= forward_propagate_into_comparison_1 (stmt
, code
,
535 && is_gimple_condexpr_for_cond (tmp
))
539 fprintf (dump_file
, " Replaced '");
540 print_gimple_expr (dump_file
, stmt
, 0);
541 fprintf (dump_file
, "' with '");
542 print_generic_expr (dump_file
, tmp
);
543 fprintf (dump_file
, "'\n");
546 gimple_cond_set_condition_from_tree (stmt
, unshare_expr (tmp
));
549 if (TREE_CODE (rhs1
) == SSA_NAME
)
550 cfg_changed
|= remove_prop_source_from_use (rhs1
);
551 if (TREE_CODE (rhs2
) == SSA_NAME
)
552 cfg_changed
|= remove_prop_source_from_use (rhs2
);
553 return (cfg_changed
|| is_gimple_min_invariant (tmp
)) ? 2 : 1;
556 /* Canonicalize _Bool == 0 and _Bool != 1 to _Bool != 0 by swapping edges. */
557 if ((TREE_CODE (TREE_TYPE (rhs1
)) == BOOLEAN_TYPE
558 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
559 && TYPE_PRECISION (TREE_TYPE (rhs1
)) == 1))
561 && integer_zerop (rhs2
))
563 && integer_onep (rhs2
))))
565 basic_block bb
= gimple_bb (stmt
);
566 gimple_cond_set_code (stmt
, NE_EXPR
);
567 gimple_cond_set_rhs (stmt
, build_zero_cst (TREE_TYPE (rhs1
)));
568 EDGE_SUCC (bb
, 0)->flags
^= (EDGE_TRUE_VALUE
|EDGE_FALSE_VALUE
);
569 EDGE_SUCC (bb
, 1)->flags
^= (EDGE_TRUE_VALUE
|EDGE_FALSE_VALUE
);
577 /* Propagate from the ssa name definition statements of COND_EXPR
578 in the rhs of statement STMT into the conditional if that simplifies it.
579 Returns true zero if the stmt was changed. */
582 forward_propagate_into_cond (gimple_stmt_iterator
*gsi_p
)
584 gimple
*stmt
= gsi_stmt (*gsi_p
);
585 tree tmp
= NULL_TREE
;
586 tree cond
= gimple_assign_rhs1 (stmt
);
587 enum tree_code code
= gimple_assign_rhs_code (stmt
);
589 /* We can do tree combining on SSA_NAME and comparison expressions. */
590 if (COMPARISON_CLASS_P (cond
))
591 tmp
= forward_propagate_into_comparison_1 (stmt
, TREE_CODE (cond
),
593 TREE_OPERAND (cond
, 0),
594 TREE_OPERAND (cond
, 1));
595 else if (TREE_CODE (cond
) == SSA_NAME
)
597 enum tree_code def_code
;
599 gimple
*def_stmt
= get_prop_source_stmt (name
, true, NULL
);
600 if (!def_stmt
|| !can_propagate_from (def_stmt
))
603 def_code
= gimple_assign_rhs_code (def_stmt
);
604 if (TREE_CODE_CLASS (def_code
) == tcc_comparison
)
605 tmp
= fold_build2_loc (gimple_location (def_stmt
),
608 gimple_assign_rhs1 (def_stmt
),
609 gimple_assign_rhs2 (def_stmt
));
613 && is_gimple_condexpr (tmp
))
617 fprintf (dump_file
, " Replaced '");
618 print_generic_expr (dump_file
, cond
);
619 fprintf (dump_file
, "' with '");
620 print_generic_expr (dump_file
, tmp
);
621 fprintf (dump_file
, "'\n");
624 if ((code
== VEC_COND_EXPR
) ? integer_all_onesp (tmp
)
625 : integer_onep (tmp
))
626 gimple_assign_set_rhs_from_tree (gsi_p
, gimple_assign_rhs2 (stmt
));
627 else if (integer_zerop (tmp
))
628 gimple_assign_set_rhs_from_tree (gsi_p
, gimple_assign_rhs3 (stmt
));
630 gimple_assign_set_rhs1 (stmt
, unshare_expr (tmp
));
631 stmt
= gsi_stmt (*gsi_p
);
640 /* We've just substituted an ADDR_EXPR into stmt. Update all the
641 relevant data structures to match. */
644 tidy_after_forward_propagate_addr (gimple
*stmt
)
646 /* We may have turned a trapping insn into a non-trapping insn. */
647 if (maybe_clean_or_replace_eh_stmt (stmt
, stmt
))
648 bitmap_set_bit (to_purge
, gimple_bb (stmt
)->index
);
650 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) == ADDR_EXPR
)
651 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt
));
654 /* NAME is a SSA_NAME representing DEF_RHS which is of the form
655 ADDR_EXPR <whatever>.
657 Try to forward propagate the ADDR_EXPR into the use USE_STMT.
658 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
659 node or for recovery of array indexing from pointer arithmetic.
661 Return true if the propagation was successful (the propagation can
662 be not totally successful, yet things may have been changed). */
665 forward_propagate_addr_expr_1 (tree name
, tree def_rhs
,
666 gimple_stmt_iterator
*use_stmt_gsi
,
669 tree lhs
, rhs
, rhs2
, array_ref
;
670 gimple
*use_stmt
= gsi_stmt (*use_stmt_gsi
);
671 enum tree_code rhs_code
;
674 gcc_assert (TREE_CODE (def_rhs
) == ADDR_EXPR
);
676 lhs
= gimple_assign_lhs (use_stmt
);
677 rhs_code
= gimple_assign_rhs_code (use_stmt
);
678 rhs
= gimple_assign_rhs1 (use_stmt
);
680 /* Do not perform copy-propagation but recurse through copy chains. */
681 if (TREE_CODE (lhs
) == SSA_NAME
682 && rhs_code
== SSA_NAME
)
683 return forward_propagate_addr_expr (lhs
, def_rhs
, single_use_p
);
685 /* The use statement could be a conversion. Recurse to the uses of the
686 lhs as copyprop does not copy through pointer to integer to pointer
687 conversions and FRE does not catch all cases either.
688 Treat the case of a single-use name and
689 a conversion to def_rhs type separate, though. */
690 if (TREE_CODE (lhs
) == SSA_NAME
691 && CONVERT_EXPR_CODE_P (rhs_code
))
693 /* If there is a point in a conversion chain where the types match
694 so we can remove a conversion re-materialize the address here
697 && useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (def_rhs
)))
699 gimple_assign_set_rhs1 (use_stmt
, unshare_expr (def_rhs
));
700 gimple_assign_set_rhs_code (use_stmt
, TREE_CODE (def_rhs
));
704 /* Else recurse if the conversion preserves the address value. */
705 if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
706 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
707 && (TYPE_PRECISION (TREE_TYPE (lhs
))
708 >= TYPE_PRECISION (TREE_TYPE (def_rhs
))))
709 return forward_propagate_addr_expr (lhs
, def_rhs
, single_use_p
);
714 /* If this isn't a conversion chain from this on we only can propagate
715 into compatible pointer contexts. */
716 if (!types_compatible_p (TREE_TYPE (name
), TREE_TYPE (def_rhs
)))
719 /* Propagate through constant pointer adjustments. */
720 if (TREE_CODE (lhs
) == SSA_NAME
721 && rhs_code
== POINTER_PLUS_EXPR
723 && TREE_CODE (gimple_assign_rhs2 (use_stmt
)) == INTEGER_CST
)
726 /* As we come here with non-invariant addresses in def_rhs we need
727 to make sure we can build a valid constant offsetted address
728 for further propagation. Simply rely on fold building that
729 and check after the fact. */
730 new_def_rhs
= fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (rhs
)),
732 fold_convert (ptr_type_node
,
733 gimple_assign_rhs2 (use_stmt
)));
734 if (TREE_CODE (new_def_rhs
) == MEM_REF
735 && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs
, 0)))
737 new_def_rhs
= build1 (ADDR_EXPR
, TREE_TYPE (rhs
), new_def_rhs
);
739 /* Recurse. If we could propagate into all uses of lhs do not
740 bother to replace into the current use but just pretend we did. */
741 if (forward_propagate_addr_expr (lhs
, new_def_rhs
, single_use_p
))
744 if (useless_type_conversion_p (TREE_TYPE (lhs
),
745 TREE_TYPE (new_def_rhs
)))
746 gimple_assign_set_rhs_with_ops (use_stmt_gsi
, TREE_CODE (new_def_rhs
),
748 else if (is_gimple_min_invariant (new_def_rhs
))
749 gimple_assign_set_rhs_with_ops (use_stmt_gsi
, NOP_EXPR
, new_def_rhs
);
752 gcc_assert (gsi_stmt (*use_stmt_gsi
) == use_stmt
);
753 update_stmt (use_stmt
);
757 /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
758 ADDR_EXPR will not appear on the LHS. */
759 tree
*lhsp
= gimple_assign_lhs_ptr (use_stmt
);
760 while (handled_component_p (*lhsp
))
761 lhsp
= &TREE_OPERAND (*lhsp
, 0);
764 /* Now see if the LHS node is a MEM_REF using NAME. If so,
765 propagate the ADDR_EXPR into the use of NAME and fold the result. */
766 if (TREE_CODE (lhs
) == MEM_REF
767 && TREE_OPERAND (lhs
, 0) == name
)
770 poly_int64 def_rhs_offset
;
771 /* If the address is invariant we can always fold it. */
772 if ((def_rhs_base
= get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs
, 0),
775 poly_offset_int off
= mem_ref_offset (lhs
);
777 off
+= def_rhs_offset
;
778 if (TREE_CODE (def_rhs_base
) == MEM_REF
)
780 off
+= mem_ref_offset (def_rhs_base
);
781 new_ptr
= TREE_OPERAND (def_rhs_base
, 0);
784 new_ptr
= build_fold_addr_expr (def_rhs_base
);
785 TREE_OPERAND (lhs
, 0) = new_ptr
;
786 TREE_OPERAND (lhs
, 1)
787 = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs
, 1)), off
);
788 tidy_after_forward_propagate_addr (use_stmt
);
789 /* Continue propagating into the RHS if this was not the only use. */
793 /* If the LHS is a plain dereference and the value type is the same as
794 that of the pointed-to type of the address we can put the
795 dereferenced address on the LHS preserving the original alias-type. */
796 else if (integer_zerop (TREE_OPERAND (lhs
, 1))
797 && ((gimple_assign_lhs (use_stmt
) == lhs
798 && useless_type_conversion_p
799 (TREE_TYPE (TREE_OPERAND (def_rhs
, 0)),
800 TREE_TYPE (gimple_assign_rhs1 (use_stmt
))))
801 || types_compatible_p (TREE_TYPE (lhs
),
802 TREE_TYPE (TREE_OPERAND (def_rhs
, 0))))
803 /* Don't forward anything into clobber stmts if it would result
804 in the lhs no longer being a MEM_REF. */
805 && (!gimple_clobber_p (use_stmt
)
806 || TREE_CODE (TREE_OPERAND (def_rhs
, 0)) == MEM_REF
))
808 tree
*def_rhs_basep
= &TREE_OPERAND (def_rhs
, 0);
809 tree new_offset
, new_base
, saved
, new_lhs
;
810 while (handled_component_p (*def_rhs_basep
))
811 def_rhs_basep
= &TREE_OPERAND (*def_rhs_basep
, 0);
812 saved
= *def_rhs_basep
;
813 if (TREE_CODE (*def_rhs_basep
) == MEM_REF
)
815 new_base
= TREE_OPERAND (*def_rhs_basep
, 0);
816 new_offset
= fold_convert (TREE_TYPE (TREE_OPERAND (lhs
, 1)),
817 TREE_OPERAND (*def_rhs_basep
, 1));
821 new_base
= build_fold_addr_expr (*def_rhs_basep
);
822 new_offset
= TREE_OPERAND (lhs
, 1);
824 *def_rhs_basep
= build2 (MEM_REF
, TREE_TYPE (*def_rhs_basep
),
825 new_base
, new_offset
);
826 TREE_THIS_VOLATILE (*def_rhs_basep
) = TREE_THIS_VOLATILE (lhs
);
827 TREE_SIDE_EFFECTS (*def_rhs_basep
) = TREE_SIDE_EFFECTS (lhs
);
828 TREE_THIS_NOTRAP (*def_rhs_basep
) = TREE_THIS_NOTRAP (lhs
);
829 new_lhs
= unshare_expr (TREE_OPERAND (def_rhs
, 0));
831 TREE_THIS_VOLATILE (new_lhs
) = TREE_THIS_VOLATILE (lhs
);
832 TREE_SIDE_EFFECTS (new_lhs
) = TREE_SIDE_EFFECTS (lhs
);
833 *def_rhs_basep
= saved
;
834 tidy_after_forward_propagate_addr (use_stmt
);
835 /* Continue propagating into the RHS if this was not the
841 /* We can have a struct assignment dereferencing our name twice.
842 Note that we didn't propagate into the lhs to not falsely
843 claim we did when propagating into the rhs. */
847 /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
848 nodes from the RHS. */
849 tree
*rhsp
= gimple_assign_rhs1_ptr (use_stmt
);
850 if (TREE_CODE (*rhsp
) == ADDR_EXPR
)
851 rhsp
= &TREE_OPERAND (*rhsp
, 0);
852 while (handled_component_p (*rhsp
))
853 rhsp
= &TREE_OPERAND (*rhsp
, 0);
856 /* Now see if the RHS node is a MEM_REF using NAME. If so,
857 propagate the ADDR_EXPR into the use of NAME and fold the result. */
858 if (TREE_CODE (rhs
) == MEM_REF
859 && TREE_OPERAND (rhs
, 0) == name
)
862 poly_int64 def_rhs_offset
;
863 if ((def_rhs_base
= get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs
, 0),
866 poly_offset_int off
= mem_ref_offset (rhs
);
868 off
+= def_rhs_offset
;
869 if (TREE_CODE (def_rhs_base
) == MEM_REF
)
871 off
+= mem_ref_offset (def_rhs_base
);
872 new_ptr
= TREE_OPERAND (def_rhs_base
, 0);
875 new_ptr
= build_fold_addr_expr (def_rhs_base
);
876 TREE_OPERAND (rhs
, 0) = new_ptr
;
877 TREE_OPERAND (rhs
, 1)
878 = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs
, 1)), off
);
879 fold_stmt_inplace (use_stmt_gsi
);
880 tidy_after_forward_propagate_addr (use_stmt
);
883 /* If the RHS is a plain dereference and the value type is the same as
884 that of the pointed-to type of the address we can put the
885 dereferenced address on the RHS preserving the original alias-type. */
886 else if (integer_zerop (TREE_OPERAND (rhs
, 1))
887 && ((gimple_assign_rhs1 (use_stmt
) == rhs
888 && useless_type_conversion_p
889 (TREE_TYPE (gimple_assign_lhs (use_stmt
)),
890 TREE_TYPE (TREE_OPERAND (def_rhs
, 0))))
891 || types_compatible_p (TREE_TYPE (rhs
),
892 TREE_TYPE (TREE_OPERAND (def_rhs
, 0)))))
894 tree
*def_rhs_basep
= &TREE_OPERAND (def_rhs
, 0);
895 tree new_offset
, new_base
, saved
, new_rhs
;
896 while (handled_component_p (*def_rhs_basep
))
897 def_rhs_basep
= &TREE_OPERAND (*def_rhs_basep
, 0);
898 saved
= *def_rhs_basep
;
899 if (TREE_CODE (*def_rhs_basep
) == MEM_REF
)
901 new_base
= TREE_OPERAND (*def_rhs_basep
, 0);
902 new_offset
= fold_convert (TREE_TYPE (TREE_OPERAND (rhs
, 1)),
903 TREE_OPERAND (*def_rhs_basep
, 1));
907 new_base
= build_fold_addr_expr (*def_rhs_basep
);
908 new_offset
= TREE_OPERAND (rhs
, 1);
910 *def_rhs_basep
= build2 (MEM_REF
, TREE_TYPE (*def_rhs_basep
),
911 new_base
, new_offset
);
912 TREE_THIS_VOLATILE (*def_rhs_basep
) = TREE_THIS_VOLATILE (rhs
);
913 TREE_SIDE_EFFECTS (*def_rhs_basep
) = TREE_SIDE_EFFECTS (rhs
);
914 TREE_THIS_NOTRAP (*def_rhs_basep
) = TREE_THIS_NOTRAP (rhs
);
915 new_rhs
= unshare_expr (TREE_OPERAND (def_rhs
, 0));
917 TREE_THIS_VOLATILE (new_rhs
) = TREE_THIS_VOLATILE (rhs
);
918 TREE_SIDE_EFFECTS (new_rhs
) = TREE_SIDE_EFFECTS (rhs
);
919 *def_rhs_basep
= saved
;
920 fold_stmt_inplace (use_stmt_gsi
);
921 tidy_after_forward_propagate_addr (use_stmt
);
926 /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there
928 if (gimple_assign_rhs_code (use_stmt
) != POINTER_PLUS_EXPR
929 || gimple_assign_rhs1 (use_stmt
) != name
)
932 /* The remaining cases are all for turning pointer arithmetic into
933 array indexing. They only apply when we have the address of
934 element zero in an array. If that is not the case then there
936 array_ref
= TREE_OPERAND (def_rhs
, 0);
937 if ((TREE_CODE (array_ref
) != ARRAY_REF
938 || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref
, 0))) != ARRAY_TYPE
939 || TREE_CODE (TREE_OPERAND (array_ref
, 1)) != INTEGER_CST
)
940 && TREE_CODE (TREE_TYPE (array_ref
)) != ARRAY_TYPE
)
943 rhs2
= gimple_assign_rhs2 (use_stmt
);
944 /* Optimize &x[C1] p+ C2 to &x p+ C3 with C3 = C1 * element_size + C2. */
945 if (TREE_CODE (rhs2
) == INTEGER_CST
)
947 tree new_rhs
= build1_loc (gimple_location (use_stmt
),
948 ADDR_EXPR
, TREE_TYPE (def_rhs
),
949 fold_build2 (MEM_REF
,
950 TREE_TYPE (TREE_TYPE (def_rhs
)),
951 unshare_expr (def_rhs
),
952 fold_convert (ptr_type_node
,
954 gimple_assign_set_rhs_from_tree (use_stmt_gsi
, new_rhs
);
955 use_stmt
= gsi_stmt (*use_stmt_gsi
);
956 update_stmt (use_stmt
);
957 tidy_after_forward_propagate_addr (use_stmt
);
964 /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
966 Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
967 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
968 node or for recovery of array indexing from pointer arithmetic.
970 PARENT_SINGLE_USE_P tells if, when in a recursive invocation, NAME was
971 the single use in the previous invocation. Pass true when calling
974 Returns true, if all uses have been propagated into. */
977 forward_propagate_addr_expr (tree name
, tree rhs
, bool parent_single_use_p
)
979 imm_use_iterator iter
;
982 bool single_use_p
= parent_single_use_p
&& has_single_use (name
);
984 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, name
)
989 /* If the use is not in a simple assignment statement, then
990 there is nothing we can do. */
991 if (!is_gimple_assign (use_stmt
))
993 if (!is_gimple_debug (use_stmt
))
998 gimple_stmt_iterator gsi
= gsi_for_stmt (use_stmt
);
999 result
= forward_propagate_addr_expr_1 (name
, rhs
, &gsi
,
1001 /* If the use has moved to a different statement adjust
1002 the update machinery for the old statement too. */
1003 if (use_stmt
!= gsi_stmt (gsi
))
1005 update_stmt (use_stmt
);
1006 use_stmt
= gsi_stmt (gsi
);
1008 update_stmt (use_stmt
);
1011 /* Remove intermediate now unused copy and conversion chains. */
1012 use_rhs
= gimple_assign_rhs1 (use_stmt
);
1014 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
1015 && TREE_CODE (use_rhs
) == SSA_NAME
1016 && has_zero_uses (gimple_assign_lhs (use_stmt
)))
1018 gimple_stmt_iterator gsi
= gsi_for_stmt (use_stmt
);
1019 fwprop_invalidate_lattice (gimple_get_lhs (use_stmt
));
1020 release_defs (use_stmt
);
1021 gsi_remove (&gsi
, true);
1025 return all
&& has_zero_uses (name
);
1029 /* Helper function for simplify_gimple_switch. Remove case labels that
1030 have values outside the range of the new type. */
1033 simplify_gimple_switch_label_vec (gswitch
*stmt
, tree index_type
)
1035 unsigned int branch_num
= gimple_switch_num_labels (stmt
);
1036 auto_vec
<tree
> labels (branch_num
);
1037 unsigned int i
, len
;
1039 /* Collect the existing case labels in a VEC, and preprocess it as if
1040 we are gimplifying a GENERIC SWITCH_EXPR. */
1041 for (i
= 1; i
< branch_num
; i
++)
1042 labels
.quick_push (gimple_switch_label (stmt
, i
));
1043 preprocess_case_label_vec_for_gimple (labels
, index_type
, NULL
);
1045 /* If any labels were removed, replace the existing case labels
1046 in the GIMPLE_SWITCH statement with the correct ones.
1047 Note that the type updates were done in-place on the case labels,
1048 so we only have to replace the case labels in the GIMPLE_SWITCH
1049 if the number of labels changed. */
1050 len
= labels
.length ();
1051 if (len
< branch_num
- 1)
1053 bitmap target_blocks
;
1057 /* Corner case: *all* case labels have been removed as being
1058 out-of-range for INDEX_TYPE. Push one label and let the
1059 CFG cleanups deal with this further. */
1064 label
= CASE_LABEL (gimple_switch_default_label (stmt
));
1065 elt
= build_case_label (build_int_cst (index_type
, 0), NULL
, label
);
1066 labels
.quick_push (elt
);
1070 for (i
= 0; i
< labels
.length (); i
++)
1071 gimple_switch_set_label (stmt
, i
+ 1, labels
[i
]);
1072 for (i
++ ; i
< branch_num
; i
++)
1073 gimple_switch_set_label (stmt
, i
, NULL_TREE
);
1074 gimple_switch_set_num_labels (stmt
, len
+ 1);
1076 /* Cleanup any edges that are now dead. */
1077 target_blocks
= BITMAP_ALLOC (NULL
);
1078 for (i
= 0; i
< gimple_switch_num_labels (stmt
); i
++)
1080 tree elt
= gimple_switch_label (stmt
, i
);
1081 basic_block target
= label_to_block (cfun
, CASE_LABEL (elt
));
1082 bitmap_set_bit (target_blocks
, target
->index
);
1084 for (ei
= ei_start (gimple_bb (stmt
)->succs
); (e
= ei_safe_edge (ei
)); )
1086 if (! bitmap_bit_p (target_blocks
, e
->dest
->index
))
1090 free_dominance_info (CDI_DOMINATORS
);
1095 BITMAP_FREE (target_blocks
);
1099 /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
1100 the condition which we may be able to optimize better. */
1103 simplify_gimple_switch (gswitch
*stmt
)
1105 /* The optimization that we really care about is removing unnecessary
1106 casts. That will let us do much better in propagating the inferred
1107 constant at the switch target. */
1108 tree cond
= gimple_switch_index (stmt
);
1109 if (TREE_CODE (cond
) == SSA_NAME
)
1111 gimple
*def_stmt
= SSA_NAME_DEF_STMT (cond
);
1112 if (gimple_assign_cast_p (def_stmt
))
1114 tree def
= gimple_assign_rhs1 (def_stmt
);
1115 if (TREE_CODE (def
) != SSA_NAME
)
1118 /* If we have an extension or sign-change that preserves the
1119 values we check against then we can copy the source value into
1121 tree ti
= TREE_TYPE (def
);
1122 if (INTEGRAL_TYPE_P (ti
)
1123 && TYPE_PRECISION (ti
) <= TYPE_PRECISION (TREE_TYPE (cond
)))
1125 size_t n
= gimple_switch_num_labels (stmt
);
1126 tree min
= NULL_TREE
, max
= NULL_TREE
;
1129 min
= CASE_LOW (gimple_switch_label (stmt
, 1));
1130 if (CASE_HIGH (gimple_switch_label (stmt
, n
- 1)))
1131 max
= CASE_HIGH (gimple_switch_label (stmt
, n
- 1));
1133 max
= CASE_LOW (gimple_switch_label (stmt
, n
- 1));
1135 if ((!min
|| int_fits_type_p (min
, ti
))
1136 && (!max
|| int_fits_type_p (max
, ti
)))
1138 gimple_switch_set_index (stmt
, def
);
1139 simplify_gimple_switch_label_vec (stmt
, ti
);
1150 /* For pointers p2 and p1 return p2 - p1 if the
1151 difference is known and constant, otherwise return NULL. */
1154 constant_pointer_difference (tree p1
, tree p2
)
1157 #define CPD_ITERATIONS 5
1158 tree exps
[2][CPD_ITERATIONS
];
1159 tree offs
[2][CPD_ITERATIONS
];
1162 for (i
= 0; i
< 2; i
++)
1164 tree p
= i
? p1
: p2
;
1165 tree off
= size_zero_node
;
1167 enum tree_code code
;
1169 /* For each of p1 and p2 we need to iterate at least
1170 twice, to handle ADDR_EXPR directly in p1/p2,
1171 SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc.
1172 on definition's stmt RHS. Iterate a few extra times. */
1176 if (!POINTER_TYPE_P (TREE_TYPE (p
)))
1178 if (TREE_CODE (p
) == ADDR_EXPR
)
1180 tree q
= TREE_OPERAND (p
, 0);
1182 tree base
= get_addr_base_and_unit_offset (q
, &offset
);
1186 if (maybe_ne (offset
, 0))
1187 off
= size_binop (PLUS_EXPR
, off
, size_int (offset
));
1189 if (TREE_CODE (q
) == MEM_REF
1190 && TREE_CODE (TREE_OPERAND (q
, 0)) == SSA_NAME
)
1192 p
= TREE_OPERAND (q
, 0);
1193 off
= size_binop (PLUS_EXPR
, off
,
1194 wide_int_to_tree (sizetype
,
1195 mem_ref_offset (q
)));
1204 if (TREE_CODE (p
) != SSA_NAME
)
1208 if (j
== CPD_ITERATIONS
)
1210 stmt
= SSA_NAME_DEF_STMT (p
);
1211 if (!is_gimple_assign (stmt
) || gimple_assign_lhs (stmt
) != p
)
1213 code
= gimple_assign_rhs_code (stmt
);
1214 if (code
== POINTER_PLUS_EXPR
)
1216 if (TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
1218 off
= size_binop (PLUS_EXPR
, off
, gimple_assign_rhs2 (stmt
));
1219 p
= gimple_assign_rhs1 (stmt
);
1221 else if (code
== ADDR_EXPR
|| CONVERT_EXPR_CODE_P (code
))
1222 p
= gimple_assign_rhs1 (stmt
);
1230 for (i
= 0; i
< cnt
[0]; i
++)
1231 for (j
= 0; j
< cnt
[1]; j
++)
1232 if (exps
[0][i
] == exps
[1][j
])
1233 return size_binop (MINUS_EXPR
, offs
[0][i
], offs
[1][j
]);
1238 /* *GSI_P is a GIMPLE_CALL to a builtin function.
1240 memcpy (p, "abcd", 4);
1241 memset (p + 4, ' ', 3);
1243 memcpy (p, "abcd ", 7);
1244 call if the latter can be stored by pieces during expansion. */
1247 simplify_builtin_call (gimple_stmt_iterator
*gsi_p
, tree callee2
)
1249 gimple
*stmt1
, *stmt2
= gsi_stmt (*gsi_p
);
1250 tree vuse
= gimple_vuse (stmt2
);
1253 stmt1
= SSA_NAME_DEF_STMT (vuse
);
1255 switch (DECL_FUNCTION_CODE (callee2
))
1257 case BUILT_IN_MEMSET
:
1258 if (gimple_call_num_args (stmt2
) != 3
1259 || gimple_call_lhs (stmt2
)
1261 || BITS_PER_UNIT
!= 8)
1266 tree ptr1
, src1
, str1
, off1
, len1
, lhs1
;
1267 tree ptr2
= gimple_call_arg (stmt2
, 0);
1268 tree val2
= gimple_call_arg (stmt2
, 1);
1269 tree len2
= gimple_call_arg (stmt2
, 2);
1270 tree diff
, vdef
, new_str_cst
;
1272 unsigned int ptr1_align
;
1273 unsigned HOST_WIDE_INT src_len
;
1275 use_operand_p use_p
;
1277 if (!tree_fits_shwi_p (val2
)
1278 || !tree_fits_uhwi_p (len2
)
1279 || compare_tree_int (len2
, 1024) == 1)
1281 if (is_gimple_call (stmt1
))
1283 /* If first stmt is a call, it needs to be memcpy
1284 or mempcpy, with string literal as second argument and
1286 callee1
= gimple_call_fndecl (stmt1
);
1287 if (callee1
== NULL_TREE
1288 || !fndecl_built_in_p (callee1
, BUILT_IN_NORMAL
)
1289 || gimple_call_num_args (stmt1
) != 3)
1291 if (DECL_FUNCTION_CODE (callee1
) != BUILT_IN_MEMCPY
1292 && DECL_FUNCTION_CODE (callee1
) != BUILT_IN_MEMPCPY
)
1294 ptr1
= gimple_call_arg (stmt1
, 0);
1295 src1
= gimple_call_arg (stmt1
, 1);
1296 len1
= gimple_call_arg (stmt1
, 2);
1297 lhs1
= gimple_call_lhs (stmt1
);
1298 if (!tree_fits_uhwi_p (len1
))
1300 str1
= string_constant (src1
, &off1
, NULL
, NULL
);
1301 if (str1
== NULL_TREE
)
1303 if (!tree_fits_uhwi_p (off1
)
1304 || compare_tree_int (off1
, TREE_STRING_LENGTH (str1
) - 1) > 0
1305 || compare_tree_int (len1
, TREE_STRING_LENGTH (str1
)
1306 - tree_to_uhwi (off1
)) > 0
1307 || TREE_CODE (TREE_TYPE (str1
)) != ARRAY_TYPE
1308 || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1
)))
1309 != TYPE_MODE (char_type_node
))
1312 else if (gimple_assign_single_p (stmt1
))
1314 /* Otherwise look for length 1 memcpy optimized into
1316 ptr1
= gimple_assign_lhs (stmt1
);
1317 src1
= gimple_assign_rhs1 (stmt1
);
1318 if (TREE_CODE (ptr1
) != MEM_REF
1319 || TYPE_MODE (TREE_TYPE (ptr1
)) != TYPE_MODE (char_type_node
)
1320 || !tree_fits_shwi_p (src1
))
1322 ptr1
= build_fold_addr_expr (ptr1
);
1323 STRIP_USELESS_TYPE_CONVERSION (ptr1
);
1324 callee1
= NULL_TREE
;
1325 len1
= size_one_node
;
1327 off1
= size_zero_node
;
1333 diff
= constant_pointer_difference (ptr1
, ptr2
);
1334 if (diff
== NULL
&& lhs1
!= NULL
)
1336 diff
= constant_pointer_difference (lhs1
, ptr2
);
1337 if (DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
1339 diff
= size_binop (PLUS_EXPR
, diff
,
1340 fold_convert (sizetype
, len1
));
1342 /* If the difference between the second and first destination pointer
1343 is not constant, or is bigger than memcpy length, bail out. */
1345 || !tree_fits_uhwi_p (diff
)
1346 || tree_int_cst_lt (len1
, diff
)
1347 || compare_tree_int (diff
, 1024) == 1)
1350 /* Use maximum of difference plus memset length and memcpy length
1351 as the new memcpy length, if it is too big, bail out. */
1352 src_len
= tree_to_uhwi (diff
);
1353 src_len
+= tree_to_uhwi (len2
);
1354 if (src_len
< tree_to_uhwi (len1
))
1355 src_len
= tree_to_uhwi (len1
);
1359 /* If mempcpy value is used elsewhere, bail out, as mempcpy
1360 with bigger length will return different result. */
1361 if (lhs1
!= NULL_TREE
1362 && DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
1363 && (TREE_CODE (lhs1
) != SSA_NAME
1364 || !single_imm_use (lhs1
, &use_p
, &use_stmt
)
1365 || use_stmt
!= stmt2
))
1368 /* If anything reads memory in between memcpy and memset
1369 call, the modified memcpy call might change it. */
1370 vdef
= gimple_vdef (stmt1
);
1372 && (!single_imm_use (vdef
, &use_p
, &use_stmt
)
1373 || use_stmt
!= stmt2
))
1376 ptr1_align
= get_pointer_alignment (ptr1
);
1377 /* Construct the new source string literal. */
1378 src_buf
= XALLOCAVEC (char, src_len
+ 1);
1381 TREE_STRING_POINTER (str1
) + tree_to_uhwi (off1
),
1382 tree_to_uhwi (len1
));
1384 src_buf
[0] = tree_to_shwi (src1
);
1385 memset (src_buf
+ tree_to_uhwi (diff
),
1386 tree_to_shwi (val2
), tree_to_uhwi (len2
));
1387 src_buf
[src_len
] = '\0';
1388 /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
1389 handle embedded '\0's. */
1390 if (strlen (src_buf
) != src_len
)
1392 rtl_profile_for_bb (gimple_bb (stmt2
));
1393 /* If the new memcpy wouldn't be emitted by storing the literal
1394 by pieces, this optimization might enlarge .rodata too much,
1395 as commonly used string literals couldn't be shared any
1397 if (!can_store_by_pieces (src_len
,
1398 builtin_strncpy_read_str
,
1399 src_buf
, ptr1_align
, false))
1402 new_str_cst
= build_string_literal (src_len
, src_buf
);
1405 /* If STMT1 is a mem{,p}cpy call, adjust it and remove
1407 if (lhs1
&& DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
)
1408 gimple_call_set_lhs (stmt1
, NULL_TREE
);
1409 gimple_call_set_arg (stmt1
, 1, new_str_cst
);
1410 gimple_call_set_arg (stmt1
, 2,
1411 build_int_cst (TREE_TYPE (len1
), src_len
));
1412 update_stmt (stmt1
);
1413 unlink_stmt_vdef (stmt2
);
1414 gsi_replace (gsi_p
, gimple_build_nop (), false);
1415 fwprop_invalidate_lattice (gimple_get_lhs (stmt2
));
1416 release_defs (stmt2
);
1417 if (lhs1
&& DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
)
1419 fwprop_invalidate_lattice (lhs1
);
1420 release_ssa_name (lhs1
);
1426 /* Otherwise, if STMT1 is length 1 memcpy optimized into
1427 assignment, remove STMT1 and change memset call into
1429 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt1
);
1431 if (!is_gimple_val (ptr1
))
1432 ptr1
= force_gimple_operand_gsi (gsi_p
, ptr1
, true, NULL_TREE
,
1433 true, GSI_SAME_STMT
);
1434 tree fndecl
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
1435 gimple_call_set_fndecl (stmt2
, fndecl
);
1436 gimple_call_set_fntype (as_a
<gcall
*> (stmt2
),
1437 TREE_TYPE (fndecl
));
1438 gimple_call_set_arg (stmt2
, 0, ptr1
);
1439 gimple_call_set_arg (stmt2
, 1, new_str_cst
);
1440 gimple_call_set_arg (stmt2
, 2,
1441 build_int_cst (TREE_TYPE (len2
), src_len
));
1442 unlink_stmt_vdef (stmt1
);
1443 gsi_remove (&gsi
, true);
1444 fwprop_invalidate_lattice (gimple_get_lhs (stmt1
));
1445 release_defs (stmt1
);
1446 update_stmt (stmt2
);
1457 /* Given a ssa_name in NAME see if it was defined by an assignment and
1458 set CODE to be the code and ARG1 to the first operand on the rhs and ARG2
1459 to the second operand on the rhs. */
1462 defcodefor_name (tree name
, enum tree_code
*code
, tree
*arg1
, tree
*arg2
)
1465 enum tree_code code1
;
1469 enum gimple_rhs_class grhs_class
;
1471 code1
= TREE_CODE (name
);
1475 grhs_class
= get_gimple_rhs_class (code1
);
1477 if (code1
== SSA_NAME
)
1479 def
= SSA_NAME_DEF_STMT (name
);
1481 if (def
&& is_gimple_assign (def
)
1482 && can_propagate_from (def
))
1484 code1
= gimple_assign_rhs_code (def
);
1485 arg11
= gimple_assign_rhs1 (def
);
1486 arg21
= gimple_assign_rhs2 (def
);
1487 arg31
= gimple_assign_rhs3 (def
);
1490 else if (grhs_class
!= GIMPLE_SINGLE_RHS
)
1502 /* Recognize rotation patterns. Return true if a transformation
1503 applied, otherwise return false.
1505 We are looking for X with unsigned type T with bitsize B, OP being
1506 +, | or ^, some type T2 wider than T. For:
1507 (X << CNT1) OP (X >> CNT2) iff CNT1 + CNT2 == B
1508 ((T) ((T2) X << CNT1)) OP ((T) ((T2) X >> CNT2)) iff CNT1 + CNT2 == B
1510 transform these into:
1514 (X << Y) OP (X >> (B - Y))
1515 (X << (int) Y) OP (X >> (int) (B - Y))
1516 ((T) ((T2) X << Y)) OP ((T) ((T2) X >> (B - Y)))
1517 ((T) ((T2) X << (int) Y)) OP ((T) ((T2) X >> (int) (B - Y)))
1518 (X << Y) | (X >> ((-Y) & (B - 1)))
1519 (X << (int) Y) | (X >> (int) ((-Y) & (B - 1)))
1520 ((T) ((T2) X << Y)) | ((T) ((T2) X >> ((-Y) & (B - 1))))
1521 ((T) ((T2) X << (int) Y)) | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
1523 transform these into:
1527 (X << (Y & (B - 1))) | (X >> ((-Y) & (B - 1)))
1528 (X << (int) (Y & (B - 1))) | (X >> (int) ((-Y) & (B - 1)))
1529 ((T) ((T2) X << (Y & (B - 1)))) | ((T) ((T2) X >> ((-Y) & (B - 1))))
1530 ((T) ((T2) X << (int) (Y & (B - 1)))) \
1531 | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
1533 transform these into:
1536 Note, in the patterns with T2 type, the type of OP operands
1537 might be even a signed type, but should have precision B.
1538 Expressions with & (B - 1) should be recognized only if B is
1542 simplify_rotate (gimple_stmt_iterator
*gsi
)
1544 gimple
*stmt
= gsi_stmt (*gsi
);
1545 tree arg
[2], rtype
, rotcnt
= NULL_TREE
;
1546 tree def_arg1
[2], def_arg2
[2];
1547 enum tree_code def_code
[2];
1550 bool swapped_p
= false;
1553 arg
[0] = gimple_assign_rhs1 (stmt
);
1554 arg
[1] = gimple_assign_rhs2 (stmt
);
1555 rtype
= TREE_TYPE (arg
[0]);
1557 /* Only create rotates in complete modes. Other cases are not
1558 expanded properly. */
1559 if (!INTEGRAL_TYPE_P (rtype
)
1560 || !type_has_mode_precision_p (rtype
))
1563 for (i
= 0; i
< 2; i
++)
1564 defcodefor_name (arg
[i
], &def_code
[i
], &def_arg1
[i
], &def_arg2
[i
]);
1566 /* Look through narrowing (or same precision) conversions. */
1567 if (CONVERT_EXPR_CODE_P (def_code
[0])
1568 && CONVERT_EXPR_CODE_P (def_code
[1])
1569 && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1
[0]))
1570 && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1
[1]))
1571 && TYPE_PRECISION (TREE_TYPE (def_arg1
[0]))
1572 == TYPE_PRECISION (TREE_TYPE (def_arg1
[1]))
1573 && TYPE_PRECISION (TREE_TYPE (def_arg1
[0])) >= TYPE_PRECISION (rtype
)
1574 && has_single_use (arg
[0])
1575 && has_single_use (arg
[1]))
1577 for (i
= 0; i
< 2; i
++)
1579 arg
[i
] = def_arg1
[i
];
1580 defcodefor_name (arg
[i
], &def_code
[i
], &def_arg1
[i
], &def_arg2
[i
]);
1585 /* Handle signed rotate; the RSHIFT_EXPR has to be done
1586 in unsigned type but LSHIFT_EXPR could be signed. */
1587 i
= (def_code
[0] == LSHIFT_EXPR
|| def_code
[0] == RSHIFT_EXPR
);
1588 if (CONVERT_EXPR_CODE_P (def_code
[i
])
1589 && (def_code
[1 - i
] == LSHIFT_EXPR
|| def_code
[1 - i
] == RSHIFT_EXPR
)
1590 && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1
[i
]))
1591 && TYPE_PRECISION (rtype
) == TYPE_PRECISION (TREE_TYPE (def_arg1
[i
]))
1592 && has_single_use (arg
[i
]))
1594 arg
[i
] = def_arg1
[i
];
1595 defcodefor_name (arg
[i
], &def_code
[i
], &def_arg1
[i
], &def_arg2
[i
]);
1599 /* One operand has to be LSHIFT_EXPR and one RSHIFT_EXPR. */
1600 for (i
= 0; i
< 2; i
++)
1601 if (def_code
[i
] != LSHIFT_EXPR
&& def_code
[i
] != RSHIFT_EXPR
)
1603 else if (!has_single_use (arg
[i
]))
1605 if (def_code
[0] == def_code
[1])
1608 /* If we've looked through narrowing conversions before, look through
1609 widening conversions from unsigned type with the same precision
1611 if (TYPE_PRECISION (TREE_TYPE (def_arg1
[0])) != TYPE_PRECISION (rtype
))
1612 for (i
= 0; i
< 2; i
++)
1615 enum tree_code code
;
1616 defcodefor_name (def_arg1
[i
], &code
, &tem
, NULL
);
1617 if (!CONVERT_EXPR_CODE_P (code
)
1618 || !INTEGRAL_TYPE_P (TREE_TYPE (tem
))
1619 || TYPE_PRECISION (TREE_TYPE (tem
)) != TYPE_PRECISION (rtype
))
1623 /* Both shifts have to use the same first operand. */
1624 if (!operand_equal_for_phi_arg_p (def_arg1
[0], def_arg1
[1])
1625 || !types_compatible_p (TREE_TYPE (def_arg1
[0]),
1626 TREE_TYPE (def_arg1
[1])))
1628 if ((TYPE_PRECISION (TREE_TYPE (def_arg1
[0]))
1629 != TYPE_PRECISION (TREE_TYPE (def_arg1
[1])))
1630 || (TYPE_UNSIGNED (TREE_TYPE (def_arg1
[0]))
1631 == TYPE_UNSIGNED (TREE_TYPE (def_arg1
[1]))))
1634 /* Handle signed rotate; the RSHIFT_EXPR has to be done
1635 in unsigned type but LSHIFT_EXPR could be signed. */
1636 i
= def_code
[0] != RSHIFT_EXPR
;
1637 if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1
[i
])))
1641 enum tree_code code
;
1642 defcodefor_name (def_arg1
[i
], &code
, &tem
, NULL
);
1643 if (!CONVERT_EXPR_CODE_P (code
)
1644 || !INTEGRAL_TYPE_P (TREE_TYPE (tem
))
1645 || TYPE_PRECISION (TREE_TYPE (tem
)) != TYPE_PRECISION (rtype
))
1648 if (!operand_equal_for_phi_arg_p (def_arg1
[0], def_arg1
[1])
1649 || !types_compatible_p (TREE_TYPE (def_arg1
[0]),
1650 TREE_TYPE (def_arg1
[1])))
1653 else if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1
[0])))
1656 /* CNT1 + CNT2 == B case above. */
1657 if (tree_fits_uhwi_p (def_arg2
[0])
1658 && tree_fits_uhwi_p (def_arg2
[1])
1659 && tree_to_uhwi (def_arg2
[0])
1660 + tree_to_uhwi (def_arg2
[1]) == TYPE_PRECISION (rtype
))
1661 rotcnt
= def_arg2
[0];
1662 else if (TREE_CODE (def_arg2
[0]) != SSA_NAME
1663 || TREE_CODE (def_arg2
[1]) != SSA_NAME
)
1667 tree cdef_arg1
[2], cdef_arg2
[2], def_arg2_alt
[2];
1668 enum tree_code cdef_code
[2];
1669 /* Look through conversion of the shift count argument.
1670 The C/C++ FE cast any shift count argument to integer_type_node.
1671 The only problem might be if the shift count type maximum value
1672 is equal or smaller than number of bits in rtype. */
1673 for (i
= 0; i
< 2; i
++)
1675 def_arg2_alt
[i
] = def_arg2
[i
];
1676 defcodefor_name (def_arg2
[i
], &cdef_code
[i
],
1677 &cdef_arg1
[i
], &cdef_arg2
[i
]);
1678 if (CONVERT_EXPR_CODE_P (cdef_code
[i
])
1679 && INTEGRAL_TYPE_P (TREE_TYPE (cdef_arg1
[i
]))
1680 && TYPE_PRECISION (TREE_TYPE (cdef_arg1
[i
]))
1681 > floor_log2 (TYPE_PRECISION (rtype
))
1682 && type_has_mode_precision_p (TREE_TYPE (cdef_arg1
[i
])))
1684 def_arg2_alt
[i
] = cdef_arg1
[i
];
1685 defcodefor_name (def_arg2_alt
[i
], &cdef_code
[i
],
1686 &cdef_arg1
[i
], &cdef_arg2
[i
]);
1689 for (i
= 0; i
< 2; i
++)
1690 /* Check for one shift count being Y and the other B - Y,
1691 with optional casts. */
1692 if (cdef_code
[i
] == MINUS_EXPR
1693 && tree_fits_shwi_p (cdef_arg1
[i
])
1694 && tree_to_shwi (cdef_arg1
[i
]) == TYPE_PRECISION (rtype
)
1695 && TREE_CODE (cdef_arg2
[i
]) == SSA_NAME
)
1698 enum tree_code code
;
1700 if (cdef_arg2
[i
] == def_arg2
[1 - i
]
1701 || cdef_arg2
[i
] == def_arg2_alt
[1 - i
])
1703 rotcnt
= cdef_arg2
[i
];
1706 defcodefor_name (cdef_arg2
[i
], &code
, &tem
, NULL
);
1707 if (CONVERT_EXPR_CODE_P (code
)
1708 && INTEGRAL_TYPE_P (TREE_TYPE (tem
))
1709 && TYPE_PRECISION (TREE_TYPE (tem
))
1710 > floor_log2 (TYPE_PRECISION (rtype
))
1711 && type_has_mode_precision_p (TREE_TYPE (tem
))
1712 && (tem
== def_arg2
[1 - i
]
1713 || tem
== def_arg2_alt
[1 - i
]))
1719 /* The above sequence isn't safe for Y being 0,
1720 because then one of the shifts triggers undefined behavior.
1721 This alternative is safe even for rotation count of 0.
1722 One shift count is Y and the other (-Y) & (B - 1).
1723 Or one shift count is Y & (B - 1) and the other (-Y) & (B - 1). */
1724 else if (cdef_code
[i
] == BIT_AND_EXPR
1725 && pow2p_hwi (TYPE_PRECISION (rtype
))
1726 && tree_fits_shwi_p (cdef_arg2
[i
])
1727 && tree_to_shwi (cdef_arg2
[i
])
1728 == TYPE_PRECISION (rtype
) - 1
1729 && TREE_CODE (cdef_arg1
[i
]) == SSA_NAME
1730 && gimple_assign_rhs_code (stmt
) == BIT_IOR_EXPR
)
1733 enum tree_code code
;
1735 defcodefor_name (cdef_arg1
[i
], &code
, &tem
, NULL
);
1736 if (CONVERT_EXPR_CODE_P (code
)
1737 && INTEGRAL_TYPE_P (TREE_TYPE (tem
))
1738 && TYPE_PRECISION (TREE_TYPE (tem
))
1739 > floor_log2 (TYPE_PRECISION (rtype
))
1740 && type_has_mode_precision_p (TREE_TYPE (tem
)))
1741 defcodefor_name (tem
, &code
, &tem
, NULL
);
1743 if (code
== NEGATE_EXPR
)
1745 if (tem
== def_arg2
[1 - i
] || tem
== def_arg2_alt
[1 - i
])
1751 defcodefor_name (tem
, &code
, &tem2
, NULL
);
1752 if (CONVERT_EXPR_CODE_P (code
)
1753 && INTEGRAL_TYPE_P (TREE_TYPE (tem2
))
1754 && TYPE_PRECISION (TREE_TYPE (tem2
))
1755 > floor_log2 (TYPE_PRECISION (rtype
))
1756 && type_has_mode_precision_p (TREE_TYPE (tem2
)))
1758 if (tem2
== def_arg2
[1 - i
]
1759 || tem2
== def_arg2_alt
[1 - i
])
1768 if (cdef_code
[1 - i
] == BIT_AND_EXPR
1769 && tree_fits_shwi_p (cdef_arg2
[1 - i
])
1770 && tree_to_shwi (cdef_arg2
[1 - i
])
1771 == TYPE_PRECISION (rtype
) - 1
1772 && TREE_CODE (cdef_arg1
[1 - i
]) == SSA_NAME
)
1774 if (tem
== cdef_arg1
[1 - i
]
1775 || tem2
== cdef_arg1
[1 - i
])
1777 rotcnt
= def_arg2
[1 - i
];
1781 defcodefor_name (cdef_arg1
[1 - i
], &code
, &tem3
, NULL
);
1782 if (CONVERT_EXPR_CODE_P (code
)
1783 && INTEGRAL_TYPE_P (TREE_TYPE (tem3
))
1784 && TYPE_PRECISION (TREE_TYPE (tem3
))
1785 > floor_log2 (TYPE_PRECISION (rtype
))
1786 && type_has_mode_precision_p (TREE_TYPE (tem3
)))
1788 if (tem
== tem3
|| tem2
== tem3
)
1790 rotcnt
= def_arg2
[1 - i
];
1797 if (rotcnt
== NULL_TREE
)
1802 if (!useless_type_conversion_p (TREE_TYPE (def_arg2
[0]),
1803 TREE_TYPE (rotcnt
)))
1805 g
= gimple_build_assign (make_ssa_name (TREE_TYPE (def_arg2
[0])),
1807 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
1808 rotcnt
= gimple_assign_lhs (g
);
1810 lhs
= gimple_assign_lhs (stmt
);
1811 if (!useless_type_conversion_p (rtype
, TREE_TYPE (def_arg1
[0])))
1812 lhs
= make_ssa_name (TREE_TYPE (def_arg1
[0]));
1813 g
= gimple_build_assign (lhs
,
1814 ((def_code
[0] == LSHIFT_EXPR
) ^ swapped_p
)
1815 ? LROTATE_EXPR
: RROTATE_EXPR
, def_arg1
[0], rotcnt
);
1816 if (!useless_type_conversion_p (rtype
, TREE_TYPE (def_arg1
[0])))
1818 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
1819 g
= gimple_build_assign (gimple_assign_lhs (stmt
), NOP_EXPR
, lhs
);
1821 gsi_replace (gsi
, g
, false);
1826 /* Check whether an array contains a valid ctz table. */
1828 check_ctz_array (tree ctor
, unsigned HOST_WIDE_INT mulc
,
1829 HOST_WIDE_INT
&zero_val
, unsigned shift
, unsigned bits
)
1832 unsigned HOST_WIDE_INT i
, mask
;
1833 unsigned matched
= 0;
1835 mask
= ((HOST_WIDE_INT_1U
<< (bits
- shift
)) - 1) << shift
;
1839 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), i
, idx
, elt
)
1841 if (TREE_CODE (idx
) != INTEGER_CST
|| TREE_CODE (elt
) != INTEGER_CST
)
1846 unsigned HOST_WIDE_INT index
= tree_to_shwi (idx
);
1847 HOST_WIDE_INT val
= tree_to_shwi (elt
);
1855 if (val
>= 0 && val
< bits
&& (((mulc
<< val
) & mask
) >> shift
) == index
)
1865 /* Check whether a string contains a valid ctz table. */
1867 check_ctz_string (tree string
, unsigned HOST_WIDE_INT mulc
,
1868 HOST_WIDE_INT
&zero_val
, unsigned shift
, unsigned bits
)
1870 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (string
);
1871 unsigned HOST_WIDE_INT mask
;
1872 unsigned matched
= 0;
1873 const unsigned char *p
= (const unsigned char *) TREE_STRING_POINTER (string
);
1875 if (len
< bits
|| len
> bits
* 2)
1878 mask
= ((HOST_WIDE_INT_1U
<< (bits
- shift
)) - 1) << shift
;
1882 for (unsigned i
= 0; i
< len
; i
++)
1883 if (p
[i
] < bits
&& (((mulc
<< p
[i
]) & mask
) >> shift
) == i
)
1886 return matched
== bits
;
1889 /* Recognize count trailing zeroes idiom.
1890 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
1891 constant which when multiplied by a power of 2 creates a unique value
1892 in the top 5 or 6 bits. This is then indexed into a table which maps it
1893 to the number of trailing zeroes. Array[0] is returned so the caller can
1894 emit an appropriate sequence depending on whether ctz (0) is defined on
1897 optimize_count_trailing_zeroes (tree array_ref
, tree x
, tree mulc
,
1898 tree tshift
, HOST_WIDE_INT
&zero_val
)
1900 tree type
= TREE_TYPE (array_ref
);
1901 tree array
= TREE_OPERAND (array_ref
, 0);
1903 gcc_assert (TREE_CODE (mulc
) == INTEGER_CST
);
1904 gcc_assert (TREE_CODE (tshift
) == INTEGER_CST
);
1906 tree input_type
= TREE_TYPE (x
);
1907 unsigned input_bits
= tree_to_shwi (TYPE_SIZE (input_type
));
1909 /* Check the array element type is not wider than 32 bits and the input is
1910 an unsigned 32-bit or 64-bit type. */
1911 if (TYPE_PRECISION (type
) > 32 || !TYPE_UNSIGNED (input_type
))
1913 if (input_bits
!= 32 && input_bits
!= 64)
1916 if (!direct_internal_fn_supported_p (IFN_CTZ
, input_type
, OPTIMIZE_FOR_BOTH
))
1919 /* Check the lower bound of the array is zero. */
1920 tree low
= array_ref_low_bound (array_ref
);
1921 if (!low
|| !integer_zerop (low
))
1924 unsigned shiftval
= tree_to_shwi (tshift
);
1926 /* Check the shift extracts the top 5..7 bits. */
1927 if (shiftval
< input_bits
- 7 || shiftval
> input_bits
- 5)
1930 tree ctor
= ctor_for_folding (array
);
1934 unsigned HOST_WIDE_INT val
= tree_to_uhwi (mulc
);
1936 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
1937 return check_ctz_array (ctor
, val
, zero_val
, shiftval
, input_bits
);
1939 if (TREE_CODE (ctor
) == STRING_CST
1940 && TYPE_PRECISION (type
) == CHAR_TYPE_SIZE
)
1941 return check_ctz_string (ctor
, val
, zero_val
, shiftval
, input_bits
);
1946 /* Match.pd function to match the ctz expression. */
1947 extern bool gimple_ctz_table_index (tree
, tree
*, tree (*)(tree
));
1950 simplify_count_trailing_zeroes (gimple_stmt_iterator
*gsi
)
1952 gimple
*stmt
= gsi_stmt (*gsi
);
1953 tree array_ref
= gimple_assign_rhs1 (stmt
);
1955 HOST_WIDE_INT zero_val
;
1957 gcc_checking_assert (TREE_CODE (array_ref
) == ARRAY_REF
);
1959 if (!gimple_ctz_table_index (TREE_OPERAND (array_ref
, 1), &res_ops
[0], NULL
))
1962 if (optimize_count_trailing_zeroes (array_ref
, res_ops
[0],
1963 res_ops
[1], res_ops
[2], zero_val
))
1965 tree type
= TREE_TYPE (res_ops
[0]);
1966 HOST_WIDE_INT ctz_val
= 0;
1967 HOST_WIDE_INT type_size
= tree_to_shwi (TYPE_SIZE (type
));
1969 = CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type
), ctz_val
) == 2;
1971 /* If the input value can't be zero, don't special case ctz (0). */
1972 if (tree_expr_nonzero_p (res_ops
[0]))
1979 /* Skip if there is no value defined at zero, or if we can't easily
1980 return the correct value for zero. */
1983 if (zero_val
!= ctz_val
&& !(zero_val
== 0 && ctz_val
== type_size
))
1986 gimple_seq seq
= NULL
;
1988 gcall
*call
= gimple_build_call_internal (IFN_CTZ
, 1, res_ops
[0]);
1989 gimple_set_location (call
, gimple_location (stmt
));
1990 gimple_set_lhs (call
, make_ssa_name (integer_type_node
));
1991 gimple_seq_add_stmt (&seq
, call
);
1993 tree prev_lhs
= gimple_call_lhs (call
);
1995 /* Emit ctz (x) & 31 if ctz (0) is 32 but we need to return 0. */
1996 if (zero_val
== 0 && ctz_val
== type_size
)
1998 g
= gimple_build_assign (make_ssa_name (integer_type_node
),
1999 BIT_AND_EXPR
, prev_lhs
,
2000 build_int_cst (integer_type_node
,
2002 gimple_set_location (g
, gimple_location (stmt
));
2003 gimple_seq_add_stmt (&seq
, g
);
2004 prev_lhs
= gimple_assign_lhs (g
);
2007 g
= gimple_build_assign (gimple_assign_lhs (stmt
), NOP_EXPR
, prev_lhs
);
2008 gimple_seq_add_stmt (&seq
, g
);
2009 gsi_replace_with_seq (gsi
, seq
, true);
2017 /* Combine an element access with a shuffle. Returns true if there were
2018 any changes made, else it returns false. */
2021 simplify_bitfield_ref (gimple_stmt_iterator
*gsi
)
2023 gimple
*stmt
= gsi_stmt (*gsi
);
2028 enum tree_code code
;
2030 op
= gimple_assign_rhs1 (stmt
);
2031 gcc_checking_assert (TREE_CODE (op
) == BIT_FIELD_REF
);
2033 op0
= TREE_OPERAND (op
, 0);
2034 if (TREE_CODE (op0
) != SSA_NAME
2035 || TREE_CODE (TREE_TYPE (op0
)) != VECTOR_TYPE
)
2038 def_stmt
= get_prop_source_stmt (op0
, false, NULL
);
2039 if (!def_stmt
|| !can_propagate_from (def_stmt
))
2042 op1
= TREE_OPERAND (op
, 1);
2043 code
= gimple_assign_rhs_code (def_stmt
);
2044 elem_type
= TREE_TYPE (TREE_TYPE (op0
));
2045 if (TREE_TYPE (op
) != elem_type
)
2048 size
= TREE_INT_CST_LOW (TYPE_SIZE (elem_type
));
2049 if (maybe_ne (bit_field_size (op
), size
))
2052 if (code
== VEC_PERM_EXPR
2053 && constant_multiple_p (bit_field_offset (op
), size
, &idx
))
2056 unsigned HOST_WIDE_INT nelts
;
2057 m
= gimple_assign_rhs3 (def_stmt
);
2058 if (TREE_CODE (m
) != VECTOR_CST
2059 || !VECTOR_CST_NELTS (m
).is_constant (&nelts
))
2061 idx
= TREE_INT_CST_LOW (VECTOR_CST_ELT (m
, idx
));
2065 p
= gimple_assign_rhs1 (def_stmt
);
2069 p
= gimple_assign_rhs2 (def_stmt
);
2072 tem
= build3 (BIT_FIELD_REF
, TREE_TYPE (op
),
2073 unshare_expr (p
), op1
, bitsize_int (idx
* size
));
2074 gimple_assign_set_rhs1 (stmt
, tem
);
2076 update_stmt (gsi_stmt (*gsi
));
2083 /* Determine whether applying the 2 permutations (mask1 then mask2)
2084 gives back one of the input. */
2087 is_combined_permutation_identity (tree mask1
, tree mask2
)
2090 unsigned HOST_WIDE_INT nelts
, i
, j
;
2091 bool maybe_identity1
= true;
2092 bool maybe_identity2
= true;
2094 gcc_checking_assert (TREE_CODE (mask1
) == VECTOR_CST
2095 && TREE_CODE (mask2
) == VECTOR_CST
);
2096 mask
= fold_ternary (VEC_PERM_EXPR
, TREE_TYPE (mask1
), mask1
, mask1
, mask2
);
2097 if (mask
== NULL_TREE
|| TREE_CODE (mask
) != VECTOR_CST
)
2100 if (!VECTOR_CST_NELTS (mask
).is_constant (&nelts
))
2102 for (i
= 0; i
< nelts
; i
++)
2104 tree val
= VECTOR_CST_ELT (mask
, i
);
2105 gcc_assert (TREE_CODE (val
) == INTEGER_CST
);
2106 j
= TREE_INT_CST_LOW (val
) & (2 * nelts
- 1);
2108 maybe_identity2
= false;
2109 else if (j
== i
+ nelts
)
2110 maybe_identity1
= false;
2114 return maybe_identity1
? 1 : maybe_identity2
? 2 : 0;
2117 /* Combine a shuffle with its arguments. Returns 1 if there were any
2118 changes made, 2 if cfg-cleanup needs to run. Else it returns 0. */
2121 simplify_permutation (gimple_stmt_iterator
*gsi
)
2123 gimple
*stmt
= gsi_stmt (*gsi
);
2124 gimple
*def_stmt
= NULL
;
2125 tree op0
, op1
, op2
, op3
, arg0
, arg1
;
2126 enum tree_code code
, code2
= ERROR_MARK
;
2127 bool single_use_op0
= false;
2129 gcc_checking_assert (gimple_assign_rhs_code (stmt
) == VEC_PERM_EXPR
);
2131 op0
= gimple_assign_rhs1 (stmt
);
2132 op1
= gimple_assign_rhs2 (stmt
);
2133 op2
= gimple_assign_rhs3 (stmt
);
2135 if (TREE_CODE (op2
) != VECTOR_CST
)
2138 if (TREE_CODE (op0
) == VECTOR_CST
)
2143 else if (TREE_CODE (op0
) == SSA_NAME
)
2145 def_stmt
= get_prop_source_stmt (op0
, false, &single_use_op0
);
2148 code
= gimple_assign_rhs_code (def_stmt
);
2149 if (code
== VIEW_CONVERT_EXPR
)
2151 tree rhs
= gimple_assign_rhs1 (def_stmt
);
2152 tree name
= TREE_OPERAND (rhs
, 0);
2153 if (TREE_CODE (name
) != SSA_NAME
)
2155 if (!has_single_use (name
))
2156 single_use_op0
= false;
2157 /* Here we update the def_stmt through this VIEW_CONVERT_EXPR,
2158 but still keep the code to indicate it comes from
2159 VIEW_CONVERT_EXPR. */
2160 def_stmt
= SSA_NAME_DEF_STMT (name
);
2161 if (!def_stmt
|| !is_gimple_assign (def_stmt
))
2163 if (gimple_assign_rhs_code (def_stmt
) != CONSTRUCTOR
)
2166 if (!can_propagate_from (def_stmt
))
2168 arg0
= gimple_assign_rhs1 (def_stmt
);
2173 /* Two consecutive shuffles. */
2174 if (code
== VEC_PERM_EXPR
)
2181 op3
= gimple_assign_rhs3 (def_stmt
);
2182 if (TREE_CODE (op3
) != VECTOR_CST
)
2184 ident
= is_combined_permutation_identity (op3
, op2
);
2187 orig
= (ident
== 1) ? gimple_assign_rhs1 (def_stmt
)
2188 : gimple_assign_rhs2 (def_stmt
);
2189 gimple_assign_set_rhs1 (stmt
, unshare_expr (orig
));
2190 gimple_assign_set_rhs_code (stmt
, TREE_CODE (orig
));
2191 gimple_set_num_ops (stmt
, 2);
2193 return remove_prop_source_from_use (op0
) ? 2 : 1;
2195 else if (code
== CONSTRUCTOR
2196 || code
== VECTOR_CST
2197 || code
== VIEW_CONVERT_EXPR
)
2201 if (TREE_CODE (op0
) == SSA_NAME
&& !single_use_op0
)
2204 if (TREE_CODE (op1
) == VECTOR_CST
)
2206 else if (TREE_CODE (op1
) == SSA_NAME
)
2208 gimple
*def_stmt2
= get_prop_source_stmt (op1
, true, NULL
);
2211 code2
= gimple_assign_rhs_code (def_stmt2
);
2212 if (code2
== VIEW_CONVERT_EXPR
)
2214 tree rhs
= gimple_assign_rhs1 (def_stmt2
);
2215 tree name
= TREE_OPERAND (rhs
, 0);
2216 if (TREE_CODE (name
) != SSA_NAME
)
2218 if (!has_single_use (name
))
2220 def_stmt2
= SSA_NAME_DEF_STMT (name
);
2221 if (!def_stmt2
|| !is_gimple_assign (def_stmt2
))
2223 if (gimple_assign_rhs_code (def_stmt2
) != CONSTRUCTOR
)
2226 else if (code2
!= CONSTRUCTOR
&& code2
!= VECTOR_CST
)
2228 if (!can_propagate_from (def_stmt2
))
2230 arg1
= gimple_assign_rhs1 (def_stmt2
);
2237 /* Already used twice in this statement. */
2238 if (TREE_CODE (op0
) == SSA_NAME
&& num_imm_uses (op0
) > 2)
2243 /* If there are any VIEW_CONVERT_EXPRs found when finding permutation
2244 operands source, check whether it's valid to transform and prepare
2245 the required new operands. */
2246 if (code
== VIEW_CONVERT_EXPR
|| code2
== VIEW_CONVERT_EXPR
)
2248 /* Figure out the target vector type to which operands should be
2249 converted. If both are CONSTRUCTOR, the types should be the
2250 same, otherwise, use the one of CONSTRUCTOR. */
2251 tree tgt_type
= NULL_TREE
;
2252 if (code
== VIEW_CONVERT_EXPR
)
2254 gcc_assert (gimple_assign_rhs_code (def_stmt
) == CONSTRUCTOR
);
2256 tgt_type
= TREE_TYPE (arg0
);
2258 if (code2
== VIEW_CONVERT_EXPR
)
2260 tree arg1_type
= TREE_TYPE (arg1
);
2261 if (tgt_type
== NULL_TREE
)
2262 tgt_type
= arg1_type
;
2263 else if (tgt_type
!= arg1_type
)
2267 if (!VECTOR_TYPE_P (tgt_type
))
2269 tree op2_type
= TREE_TYPE (op2
);
2270 /* Should have folded this before. */
2271 gcc_assert (op2_type
!= tgt_type
);
2273 /* Figure out the shrunk factor. */
2274 poly_uint64 tgt_units
= TYPE_VECTOR_SUBPARTS (tgt_type
);
2275 poly_uint64 op2_units
= TYPE_VECTOR_SUBPARTS (op2_type
);
2276 if (maybe_gt (tgt_units
, op2_units
))
2278 unsigned int factor
;
2279 if (!constant_multiple_p (op2_units
, tgt_units
, &factor
))
2282 /* Build the new permutation control vector as target vector. */
2283 vec_perm_builder builder
;
2284 if (!tree_to_vec_perm_builder (&builder
, op2
))
2286 vec_perm_indices
indices (builder
, 2, op2_units
);
2287 vec_perm_indices new_indices
;
2288 if (new_indices
.new_shrunk_vector (indices
, factor
))
2290 tree mask_type
= tgt_type
;
2291 if (!VECTOR_INTEGER_TYPE_P (mask_type
))
2293 tree elem_type
= TREE_TYPE (mask_type
);
2294 unsigned elem_size
= TREE_INT_CST_LOW (TYPE_SIZE (elem_type
));
2295 tree int_type
= build_nonstandard_integer_type (elem_size
, 0);
2296 mask_type
= build_vector_type (int_type
, tgt_units
);
2298 op2
= vec_perm_indices_to_tree (mask_type
, new_indices
);
2303 /* Convert the VECTOR_CST to the appropriate vector type. */
2304 if (tgt_type
!= TREE_TYPE (arg0
))
2305 arg0
= fold_build1 (VIEW_CONVERT_EXPR
, tgt_type
, arg0
);
2306 else if (tgt_type
!= TREE_TYPE (arg1
))
2307 arg1
= fold_build1 (VIEW_CONVERT_EXPR
, tgt_type
, arg1
);
2310 /* VIEW_CONVERT_EXPR should be updated to CONSTRUCTOR before. */
2311 gcc_assert (code
== CONSTRUCTOR
|| code
== VECTOR_CST
);
2313 /* Shuffle of a constructor. */
2315 tree res_type
= TREE_TYPE (arg0
);
2316 tree opt
= fold_ternary (VEC_PERM_EXPR
, res_type
, arg0
, arg1
, op2
);
2318 || (TREE_CODE (opt
) != CONSTRUCTOR
&& TREE_CODE (opt
) != VECTOR_CST
))
2320 /* Found VIEW_CONVERT_EXPR before, need one explicit conversion. */
2321 if (res_type
!= TREE_TYPE (op0
))
2323 tree name
= make_ssa_name (TREE_TYPE (opt
));
2324 gimple
*ass_stmt
= gimple_build_assign (name
, opt
);
2325 gsi_insert_before (gsi
, ass_stmt
, GSI_SAME_STMT
);
2326 opt
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (op0
), name
);
2328 gimple_assign_set_rhs_from_tree (gsi
, opt
);
2329 update_stmt (gsi_stmt (*gsi
));
2330 if (TREE_CODE (op0
) == SSA_NAME
)
2331 ret
= remove_prop_source_from_use (op0
);
2332 if (op0
!= op1
&& TREE_CODE (op1
) == SSA_NAME
)
2333 ret
|= remove_prop_source_from_use (op1
);
2340 /* Get the BIT_FIELD_REF definition of VAL, if any, looking through
2341 conversions with code CONV_CODE or update it if still ERROR_MARK.
2342 Return NULL_TREE if no such matching def was found. */
2345 get_bit_field_ref_def (tree val
, enum tree_code
&conv_code
)
2347 if (TREE_CODE (val
) != SSA_NAME
)
2349 gimple
*def_stmt
= get_prop_source_stmt (val
, false, NULL
);
2352 enum tree_code code
= gimple_assign_rhs_code (def_stmt
);
2353 if (code
== FLOAT_EXPR
2354 || code
== FIX_TRUNC_EXPR
2355 || CONVERT_EXPR_CODE_P (code
))
2357 tree op1
= gimple_assign_rhs1 (def_stmt
);
2358 if (conv_code
== ERROR_MARK
)
2360 else if (conv_code
!= code
)
2362 if (TREE_CODE (op1
) != SSA_NAME
)
2364 def_stmt
= SSA_NAME_DEF_STMT (op1
);
2365 if (! is_gimple_assign (def_stmt
))
2367 code
= gimple_assign_rhs_code (def_stmt
);
2369 if (code
!= BIT_FIELD_REF
)
2371 return gimple_assign_rhs1 (def_stmt
);
2374 /* Recognize a VEC_PERM_EXPR. Returns true if there were any changes. */
2377 simplify_vector_constructor (gimple_stmt_iterator
*gsi
)
2379 gimple
*stmt
= gsi_stmt (*gsi
);
2380 tree op
, orig
[2], type
, elem_type
;
2381 unsigned elem_size
, i
;
2382 unsigned HOST_WIDE_INT nelts
;
2383 unsigned HOST_WIDE_INT refnelts
;
2384 enum tree_code conv_code
;
2385 constructor_elt
*elt
;
2387 op
= gimple_assign_rhs1 (stmt
);
2388 type
= TREE_TYPE (op
);
2389 gcc_checking_assert (TREE_CODE (op
) == CONSTRUCTOR
2390 && TREE_CODE (type
) == VECTOR_TYPE
);
2392 if (!TYPE_VECTOR_SUBPARTS (type
).is_constant (&nelts
))
2394 elem_type
= TREE_TYPE (type
);
2395 elem_size
= TREE_INT_CST_LOW (TYPE_SIZE (elem_type
));
2399 conv_code
= ERROR_MARK
;
2400 bool maybe_ident
= true;
2401 bool maybe_blend
[2] = { true, true };
2402 tree one_constant
= NULL_TREE
;
2403 tree one_nonconstant
= NULL_TREE
;
2404 auto_vec
<tree
> constants
;
2405 constants
.safe_grow_cleared (nelts
, true);
2406 auto_vec
<std::pair
<unsigned, unsigned>, 64> elts
;
2407 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (op
), i
, elt
)
2415 /* Look for elements extracted and possibly converted from
2417 op1
= get_bit_field_ref_def (elt
->value
, conv_code
);
2419 && TREE_CODE ((ref
= TREE_OPERAND (op1
, 0))) == SSA_NAME
2420 && VECTOR_TYPE_P (TREE_TYPE (ref
))
2421 && useless_type_conversion_p (TREE_TYPE (op1
),
2422 TREE_TYPE (TREE_TYPE (ref
)))
2423 && constant_multiple_p (bit_field_offset (op1
),
2424 bit_field_size (op1
), &elem
)
2425 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (ref
)).is_constant (&refnelts
))
2428 for (j
= 0; j
< 2; ++j
)
2433 || useless_type_conversion_p (TREE_TYPE (orig
[0]),
2437 else if (ref
== orig
[j
])
2440 /* Found a suitable vector element. */
2444 if (elem
!= i
|| j
!= 0)
2445 maybe_ident
= false;
2447 maybe_blend
[j
] = false;
2448 elts
.safe_push (std::make_pair (j
, elem
));
2451 /* Else fallthru. */
2453 /* Handle elements not extracted from a vector.
2454 1. constants by permuting with constant vector
2455 2. a unique non-constant element by permuting with a splat vector */
2457 && orig
[1] != error_mark_node
)
2459 orig
[1] = error_mark_node
;
2460 if (CONSTANT_CLASS_P (elt
->value
))
2462 if (one_nonconstant
)
2465 one_constant
= elt
->value
;
2466 constants
[i
] = elt
->value
;
2472 if (!one_nonconstant
)
2473 one_nonconstant
= elt
->value
;
2474 else if (!operand_equal_p (one_nonconstant
, elt
->value
, 0))
2477 elts
.safe_push (std::make_pair (1, i
));
2478 maybe_ident
= false;
2484 || ! VECTOR_TYPE_P (TREE_TYPE (orig
[0])))
2486 refnelts
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (orig
[0])).to_constant ();
2487 /* We currently do not handle larger destination vectors. */
2488 if (refnelts
< nelts
)
2494 = (nelts
!= refnelts
2495 ? (conv_code
!= ERROR_MARK
2496 ? build_vector_type (TREE_TYPE (TREE_TYPE (orig
[0])), nelts
)
2498 : TREE_TYPE (orig
[0]));
2499 if (conv_code
!= ERROR_MARK
2500 && !supportable_convert_operation (conv_code
, type
, conv_src_type
,
2503 /* Only few targets implement direct conversion patterns so try
2504 some simple special cases via VEC_[UN]PACK[_FLOAT]_LO_EXPR. */
2506 tree halfvectype
, dblvectype
;
2507 enum tree_code unpack_op
;
2509 if (!BYTES_BIG_ENDIAN
)
2510 unpack_op
= (FLOAT_TYPE_P (TREE_TYPE (type
))
2511 ? VEC_UNPACK_FLOAT_LO_EXPR
2512 : VEC_UNPACK_LO_EXPR
);
2514 unpack_op
= (FLOAT_TYPE_P (TREE_TYPE (type
))
2515 ? VEC_UNPACK_FLOAT_HI_EXPR
2516 : VEC_UNPACK_HI_EXPR
);
2518 if (CONVERT_EXPR_CODE_P (conv_code
)
2519 && (2 * TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig
[0])))
2520 == TYPE_PRECISION (TREE_TYPE (type
)))
2521 && mode_for_vector (as_a
<scalar_mode
>
2522 (TYPE_MODE (TREE_TYPE (TREE_TYPE (orig
[0])))),
2523 nelts
* 2).exists ()
2525 = build_vector_type (TREE_TYPE (TREE_TYPE (orig
[0])),
2527 /* Only use it for vector modes or for vector booleans
2528 represented as scalar bitmasks. See PR95528. */
2529 && (VECTOR_MODE_P (TYPE_MODE (dblvectype
))
2530 || VECTOR_BOOLEAN_TYPE_P (dblvectype
))
2531 && (optab
= optab_for_tree_code (unpack_op
,
2534 && (optab_handler (optab
, TYPE_MODE (dblvectype
))
2535 != CODE_FOR_nothing
))
2537 gimple_seq stmts
= NULL
;
2539 if (refnelts
== nelts
)
2541 /* ??? Paradoxical subregs don't exist, so insert into
2542 the lower half of a wider zero vector. */
2543 dbl
= gimple_build (&stmts
, BIT_INSERT_EXPR
, dblvectype
,
2544 build_zero_cst (dblvectype
), orig
[0],
2547 else if (refnelts
== 2 * nelts
)
2550 dbl
= gimple_build (&stmts
, BIT_FIELD_REF
, dblvectype
,
2551 orig
[0], TYPE_SIZE (dblvectype
),
2553 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2554 gimple_assign_set_rhs_with_ops (gsi
, unpack_op
, dbl
);
2556 else if (CONVERT_EXPR_CODE_P (conv_code
)
2557 && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig
[0])))
2558 == 2 * TYPE_PRECISION (TREE_TYPE (type
)))
2559 && mode_for_vector (as_a
<scalar_mode
>
2561 (TREE_TYPE (TREE_TYPE (orig
[0])))),
2562 nelts
/ 2).exists ()
2564 = build_vector_type (TREE_TYPE (TREE_TYPE (orig
[0])),
2566 /* Only use it for vector modes or for vector booleans
2567 represented as scalar bitmasks. See PR95528. */
2568 && (VECTOR_MODE_P (TYPE_MODE (halfvectype
))
2569 || VECTOR_BOOLEAN_TYPE_P (halfvectype
))
2570 && (optab
= optab_for_tree_code (VEC_PACK_TRUNC_EXPR
,
2573 && (optab_handler (optab
, TYPE_MODE (halfvectype
))
2574 != CODE_FOR_nothing
))
2576 gimple_seq stmts
= NULL
;
2577 tree low
= gimple_build (&stmts
, BIT_FIELD_REF
, halfvectype
,
2578 orig
[0], TYPE_SIZE (halfvectype
),
2580 tree hig
= gimple_build (&stmts
, BIT_FIELD_REF
, halfvectype
,
2581 orig
[0], TYPE_SIZE (halfvectype
),
2582 TYPE_SIZE (halfvectype
));
2583 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2584 gimple_assign_set_rhs_with_ops (gsi
, VEC_PACK_TRUNC_EXPR
,
2589 update_stmt (gsi_stmt (*gsi
));
2592 if (nelts
!= refnelts
)
2595 = gimple_build_assign (make_ssa_name (conv_src_type
),
2596 build3 (BIT_FIELD_REF
, conv_src_type
,
2597 orig
[0], TYPE_SIZE (conv_src_type
),
2598 bitsize_zero_node
));
2599 gsi_insert_before (gsi
, lowpart
, GSI_SAME_STMT
);
2600 orig
[0] = gimple_assign_lhs (lowpart
);
2602 if (conv_code
== ERROR_MARK
)
2604 tree src_type
= TREE_TYPE (orig
[0]);
2605 if (!useless_type_conversion_p (type
, src_type
))
2607 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type
),
2608 TYPE_VECTOR_SUBPARTS (src_type
))
2609 && useless_type_conversion_p (TREE_TYPE (type
),
2610 TREE_TYPE (src_type
)));
2611 tree rhs
= build1 (VIEW_CONVERT_EXPR
, type
, orig
[0]);
2612 orig
[0] = make_ssa_name (type
);
2613 gassign
*assign
= gimple_build_assign (orig
[0], rhs
);
2614 gsi_insert_before (gsi
, assign
, GSI_SAME_STMT
);
2616 gimple_assign_set_rhs_from_tree (gsi
, orig
[0]);
2619 gimple_assign_set_rhs_with_ops (gsi
, conv_code
, orig
[0],
2620 NULL_TREE
, NULL_TREE
);
2624 /* If we combine a vector with a non-vector avoid cases where
2625 we'll obviously end up with more GIMPLE stmts which is when
2626 we'll later not fold this to a single insert into the vector
2627 and we had a single extract originally. See PR92819. */
2630 && orig
[1] == error_mark_node
2633 tree mask_type
, perm_type
, conv_src_type
;
2634 perm_type
= TREE_TYPE (orig
[0]);
2635 conv_src_type
= (nelts
== refnelts
2637 : build_vector_type (TREE_TYPE (perm_type
), nelts
));
2638 if (conv_code
!= ERROR_MARK
2639 && !supportable_convert_operation (conv_code
, type
, conv_src_type
,
2643 /* Now that we know the number of elements of the source build the
2645 ??? When the second vector has constant values we can shuffle
2646 it and its source indexes to make the permutation supported.
2647 For now it mimics a blend. */
2648 vec_perm_builder
sel (refnelts
, refnelts
, 1);
2649 bool all_same_p
= true;
2650 for (i
= 0; i
< elts
.length (); ++i
)
2652 sel
.quick_push (elts
[i
].second
+ elts
[i
].first
* refnelts
);
2653 all_same_p
&= known_eq (sel
[i
], sel
[0]);
2655 /* And fill the tail with "something". It's really don't care,
2656 and ideally we'd allow VEC_PERM to have a smaller destination
2657 vector. As a heuristic:
2659 (a) if what we have so far duplicates a single element, make the
2662 (b) otherwise preserve a uniform orig[0]. This facilitates
2663 later pattern-matching of VEC_PERM_EXPR to a BIT_INSERT_EXPR. */
2664 for (; i
< refnelts
; ++i
)
2665 sel
.quick_push (all_same_p
2667 : (elts
[0].second
== 0 && elts
[0].first
== 0
2668 ? 0 : refnelts
) + i
);
2669 vec_perm_indices
indices (sel
, orig
[1] ? 2 : 1, refnelts
);
2670 if (!can_vec_perm_const_p (TYPE_MODE (perm_type
), indices
))
2673 = build_vector_type (build_nonstandard_integer_type (elem_size
, 1),
2675 if (GET_MODE_CLASS (TYPE_MODE (mask_type
)) != MODE_VECTOR_INT
2676 || maybe_ne (GET_MODE_SIZE (TYPE_MODE (mask_type
)),
2677 GET_MODE_SIZE (TYPE_MODE (perm_type
))))
2679 tree op2
= vec_perm_indices_to_tree (mask_type
, indices
);
2680 bool converted_orig1
= false;
2681 gimple_seq stmts
= NULL
;
2684 else if (orig
[1] == error_mark_node
2687 /* ??? We can see if we can safely convert to the original
2689 converted_orig1
= conv_code
!= ERROR_MARK
;
2690 orig
[1] = gimple_build_vector_from_val (&stmts
, UNKNOWN_LOCATION
,
2695 else if (orig
[1] == error_mark_node
)
2697 /* ??? See if we can convert the vector to the original type. */
2698 converted_orig1
= conv_code
!= ERROR_MARK
;
2699 unsigned n
= converted_orig1
? nelts
: refnelts
;
2700 tree_vector_builder
vec (converted_orig1
2701 ? type
: perm_type
, n
, 1);
2702 for (unsigned i
= 0; i
< n
; ++i
)
2703 if (i
< nelts
&& constants
[i
])
2704 vec
.quick_push (constants
[i
]);
2706 /* ??? Push a don't-care value. */
2707 vec
.quick_push (one_constant
);
2708 orig
[1] = vec
.build ();
2710 tree blend_op2
= NULL_TREE
;
2711 if (converted_orig1
)
2713 /* Make sure we can do a blend in the target type. */
2714 vec_perm_builder
sel (nelts
, nelts
, 1);
2715 for (i
= 0; i
< elts
.length (); ++i
)
2716 sel
.quick_push (elts
[i
].first
2717 ? elts
[i
].second
+ nelts
: i
);
2718 vec_perm_indices
indices (sel
, 2, nelts
);
2719 if (!can_vec_perm_const_p (TYPE_MODE (type
), indices
))
2722 = build_vector_type (build_nonstandard_integer_type (elem_size
, 1),
2724 if (GET_MODE_CLASS (TYPE_MODE (mask_type
)) != MODE_VECTOR_INT
2725 || maybe_ne (GET_MODE_SIZE (TYPE_MODE (mask_type
)),
2726 GET_MODE_SIZE (TYPE_MODE (type
))))
2728 blend_op2
= vec_perm_indices_to_tree (mask_type
, indices
);
2731 = converted_orig1
? build_zero_cst (perm_type
) : orig
[1];
2732 tree res
= gimple_build (&stmts
, VEC_PERM_EXPR
, perm_type
,
2733 orig
[0], orig1_for_perm
, op2
);
2734 if (nelts
!= refnelts
)
2735 res
= gimple_build (&stmts
, BIT_FIELD_REF
,
2736 conv_code
!= ERROR_MARK
? conv_src_type
: type
,
2737 res
, TYPE_SIZE (type
), bitsize_zero_node
);
2738 if (conv_code
!= ERROR_MARK
)
2739 res
= gimple_build (&stmts
, conv_code
, type
, res
);
2740 else if (!useless_type_conversion_p (type
, TREE_TYPE (res
)))
2742 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type
),
2743 TYPE_VECTOR_SUBPARTS (perm_type
))
2744 && useless_type_conversion_p (TREE_TYPE (type
),
2745 TREE_TYPE (perm_type
)));
2746 res
= gimple_build (&stmts
, VIEW_CONVERT_EXPR
, type
, res
);
2748 /* Blend in the actual constant. */
2749 if (converted_orig1
)
2750 res
= gimple_build (&stmts
, VEC_PERM_EXPR
, type
,
2751 res
, orig
[1], blend_op2
);
2752 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
2753 gimple_assign_set_rhs_with_ops (gsi
, SSA_NAME
, res
);
2755 update_stmt (gsi_stmt (*gsi
));
2760 /* Rewrite the vector load at *GSI to component-wise loads if the load
2761 is only used in BIT_FIELD_REF extractions with eventual intermediate
2765 optimize_vector_load (gimple_stmt_iterator
*gsi
)
2767 gimple
*stmt
= gsi_stmt (*gsi
);
2768 tree lhs
= gimple_assign_lhs (stmt
);
2769 tree rhs
= gimple_assign_rhs1 (stmt
);
2771 /* Gather BIT_FIELD_REFs to rewrite, looking through
2772 VEC_UNPACK_{LO,HI}_EXPR. */
2773 use_operand_p use_p
;
2774 imm_use_iterator iter
;
2775 bool rewrite
= true;
2776 auto_vec
<gimple
*, 8> bf_stmts
;
2777 auto_vec
<tree
, 8> worklist
;
2778 worklist
.quick_push (lhs
);
2781 tree def
= worklist
.pop ();
2782 unsigned HOST_WIDE_INT def_eltsize
2783 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (def
))));
2784 FOR_EACH_IMM_USE_FAST (use_p
, iter
, def
)
2786 gimple
*use_stmt
= USE_STMT (use_p
);
2787 if (is_gimple_debug (use_stmt
))
2789 if (!is_gimple_assign (use_stmt
))
2794 enum tree_code use_code
= gimple_assign_rhs_code (use_stmt
);
2795 tree use_rhs
= gimple_assign_rhs1 (use_stmt
);
2796 if (use_code
== BIT_FIELD_REF
2797 && TREE_OPERAND (use_rhs
, 0) == def
2798 /* If its on the VEC_UNPACK_{HI,LO}_EXPR
2799 def need to verify it is element aligned. */
2801 || (known_eq (bit_field_size (use_rhs
), def_eltsize
)
2802 && constant_multiple_p (bit_field_offset (use_rhs
),
2805 bf_stmts
.safe_push (use_stmt
);
2808 /* Walk through one level of VEC_UNPACK_{LO,HI}_EXPR. */
2810 && (use_code
== VEC_UNPACK_HI_EXPR
2811 || use_code
== VEC_UNPACK_LO_EXPR
)
2814 worklist
.safe_push (gimple_assign_lhs (use_stmt
));
2823 while (!worklist
.is_empty ());
2830 /* We now have all ultimate uses of the load to rewrite in bf_stmts. */
2832 /* Prepare the original ref to be wrapped in adjusted BIT_FIELD_REFs.
2833 For TARGET_MEM_REFs we have to separate the LEA from the reference. */
2834 tree load_rhs
= rhs
;
2835 if (TREE_CODE (load_rhs
) == TARGET_MEM_REF
)
2837 if (TREE_CODE (TREE_OPERAND (load_rhs
, 0)) == ADDR_EXPR
)
2838 mark_addressable (TREE_OPERAND (TREE_OPERAND (load_rhs
, 0), 0));
2839 tree tem
= make_ssa_name (TREE_TYPE (TREE_OPERAND (load_rhs
, 0)));
2841 = gimple_build_assign (tem
, build1 (ADDR_EXPR
, TREE_TYPE (tem
),
2842 unshare_expr (load_rhs
)));
2843 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
2844 load_rhs
= build2_loc (EXPR_LOCATION (load_rhs
),
2845 MEM_REF
, TREE_TYPE (load_rhs
), tem
,
2847 (TREE_TYPE (TREE_OPERAND (load_rhs
, 1)), 0));
2850 /* Rewrite the BIT_FIELD_REFs to be actual loads, re-emitting them at
2851 the place of the original load. */
2852 for (gimple
*use_stmt
: bf_stmts
)
2854 tree bfr
= gimple_assign_rhs1 (use_stmt
);
2855 tree new_rhs
= unshare_expr (load_rhs
);
2856 if (TREE_OPERAND (bfr
, 0) != lhs
)
2858 /* When the BIT_FIELD_REF is on the promoted vector we have to
2859 adjust it and emit a conversion afterwards. */
2861 = SSA_NAME_DEF_STMT (TREE_OPERAND (bfr
, 0));
2862 enum tree_code def_code
2863 = gimple_assign_rhs_code (def_stmt
);
2865 /* The adjusted BIT_FIELD_REF is of the promotion source
2866 vector size and at half of the offset... */
2867 new_rhs
= fold_build3 (BIT_FIELD_REF
,
2868 TREE_TYPE (TREE_TYPE (lhs
)),
2870 TYPE_SIZE (TREE_TYPE (TREE_TYPE (lhs
))),
2871 size_binop (EXACT_DIV_EXPR
,
2872 TREE_OPERAND (bfr
, 2),
2874 /* ... and offsetted by half of the vector if VEC_UNPACK_HI_EXPR. */
2875 if (def_code
== (!BYTES_BIG_ENDIAN
2876 ? VEC_UNPACK_HI_EXPR
: VEC_UNPACK_LO_EXPR
))
2877 TREE_OPERAND (new_rhs
, 2)
2878 = size_binop (PLUS_EXPR
, TREE_OPERAND (new_rhs
, 2),
2879 size_binop (EXACT_DIV_EXPR
,
2880 TYPE_SIZE (TREE_TYPE (lhs
)),
2882 tree tem
= make_ssa_name (TREE_TYPE (TREE_TYPE (lhs
)));
2883 gimple
*new_stmt
= gimple_build_assign (tem
, new_rhs
);
2884 location_t loc
= gimple_location (use_stmt
);
2885 gimple_set_location (new_stmt
, loc
);
2886 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
2887 /* Perform scalar promotion. */
2888 new_stmt
= gimple_build_assign (gimple_assign_lhs (use_stmt
),
2890 gimple_set_location (new_stmt
, loc
);
2891 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
2895 /* When the BIT_FIELD_REF is on the original load result
2896 we can just wrap that. */
2897 tree new_rhs
= fold_build3 (BIT_FIELD_REF
, TREE_TYPE (bfr
),
2898 unshare_expr (load_rhs
),
2899 TREE_OPERAND (bfr
, 1),
2900 TREE_OPERAND (bfr
, 2));
2901 gimple
*new_stmt
= gimple_build_assign (gimple_assign_lhs (use_stmt
),
2903 location_t loc
= gimple_location (use_stmt
);
2904 gimple_set_location (new_stmt
, loc
);
2905 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
2907 gimple_stmt_iterator gsi2
= gsi_for_stmt (use_stmt
);
2908 unlink_stmt_vdef (use_stmt
);
2909 gsi_remove (&gsi2
, true);
2912 /* Finally get rid of the intermediate stmts. */
2914 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
2916 if (is_gimple_debug (use_stmt
))
2918 if (gimple_debug_bind_p (use_stmt
))
2920 gimple_debug_bind_reset_value (use_stmt
);
2921 update_stmt (use_stmt
);
2925 gimple_stmt_iterator gsi2
= gsi_for_stmt (use_stmt
);
2926 unlink_stmt_vdef (use_stmt
);
2927 release_defs (use_stmt
);
2928 gsi_remove (&gsi2
, true);
2930 /* And the original load. */
2931 release_defs (stmt
);
2932 gsi_remove (gsi
, true);
2936 /* Primitive "lattice" function for gimple_simplify. */
2939 fwprop_ssa_val (tree name
)
2941 /* First valueize NAME. */
2942 if (TREE_CODE (name
) == SSA_NAME
2943 && SSA_NAME_VERSION (name
) < lattice
.length ())
2945 tree val
= lattice
[SSA_NAME_VERSION (name
)];
2949 /* We continue matching along SSA use-def edges for SSA names
2950 that are not single-use. Currently there are no patterns
2951 that would cause any issues with that. */
2955 /* Main entry point for the forward propagation and statement combine
2960 const pass_data pass_data_forwprop
=
2962 GIMPLE_PASS
, /* type */
2963 "forwprop", /* name */
2964 OPTGROUP_NONE
, /* optinfo_flags */
2965 TV_TREE_FORWPROP
, /* tv_id */
2966 ( PROP_cfg
| PROP_ssa
), /* properties_required */
2967 0, /* properties_provided */
2968 0, /* properties_destroyed */
2969 0, /* todo_flags_start */
2970 TODO_update_ssa
, /* todo_flags_finish */
2973 class pass_forwprop
: public gimple_opt_pass
2976 pass_forwprop (gcc::context
*ctxt
)
2977 : gimple_opt_pass (pass_data_forwprop
, ctxt
)
2980 /* opt_pass methods: */
2981 opt_pass
* clone () { return new pass_forwprop (m_ctxt
); }
2982 virtual bool gate (function
*) { return flag_tree_forwprop
; }
2983 virtual unsigned int execute (function
*);
2985 }; // class pass_forwprop
2988 pass_forwprop::execute (function
*fun
)
2990 unsigned int todoflags
= 0;
2992 cfg_changed
= false;
2994 /* Combine stmts with the stmts defining their operands. Do that
2995 in an order that guarantees visiting SSA defs before SSA uses. */
2996 lattice
.create (num_ssa_names
);
2997 lattice
.quick_grow_cleared (num_ssa_names
);
2998 int *postorder
= XNEWVEC (int, n_basic_blocks_for_fn (fun
));
2999 int postorder_num
= pre_and_rev_post_order_compute_fn (cfun
, NULL
,
3001 auto_vec
<gimple
*, 4> to_fixup
;
3002 auto_vec
<gimple
*, 32> to_remove
;
3003 to_purge
= BITMAP_ALLOC (NULL
);
3004 for (int i
= 0; i
< postorder_num
; ++i
)
3006 gimple_stmt_iterator gsi
;
3007 basic_block bb
= BASIC_BLOCK_FOR_FN (fun
, postorder
[i
]);
3009 /* Record degenerate PHIs in the lattice. */
3010 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
3013 gphi
*phi
= si
.phi ();
3014 tree res
= gimple_phi_result (phi
);
3015 if (virtual_operand_p (res
))
3018 use_operand_p use_p
;
3020 tree first
= NULL_TREE
;
3021 bool all_same
= true;
3022 FOR_EACH_PHI_ARG (use_p
, phi
, it
, SSA_OP_USE
)
3024 tree use
= USE_FROM_PTR (use_p
);
3027 else if (! operand_equal_p (first
, use
, 0))
3035 if (may_propagate_copy (res
, first
))
3036 to_remove
.safe_push (phi
);
3037 fwprop_set_lattice_val (res
, first
);
3041 /* Apply forward propagation to all stmts in the basic-block.
3042 Note we update GSI within the loop as necessary. */
3043 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
3045 gimple
*stmt
= gsi_stmt (gsi
);
3047 enum tree_code code
;
3049 if (!is_gimple_assign (stmt
))
3055 lhs
= gimple_assign_lhs (stmt
);
3056 rhs
= gimple_assign_rhs1 (stmt
);
3057 code
= gimple_assign_rhs_code (stmt
);
3058 if (TREE_CODE (lhs
) != SSA_NAME
3059 || has_zero_uses (lhs
))
3065 /* If this statement sets an SSA_NAME to an address,
3066 try to propagate the address into the uses of the SSA_NAME. */
3067 if ((code
== ADDR_EXPR
3068 /* Handle pointer conversions on invariant addresses
3069 as well, as this is valid gimple. */
3070 || (CONVERT_EXPR_CODE_P (code
)
3071 && TREE_CODE (rhs
) == ADDR_EXPR
3072 && POINTER_TYPE_P (TREE_TYPE (lhs
))))
3073 && TREE_CODE (TREE_OPERAND (rhs
, 0)) != TARGET_MEM_REF
)
3075 tree base
= get_base_address (TREE_OPERAND (rhs
, 0));
3078 || decl_address_invariant_p (base
))
3079 && !stmt_references_abnormal_ssa_name (stmt
)
3080 && forward_propagate_addr_expr (lhs
, rhs
, true))
3082 fwprop_invalidate_lattice (gimple_get_lhs (stmt
));
3083 release_defs (stmt
);
3084 gsi_remove (&gsi
, true);
3089 else if (code
== POINTER_PLUS_EXPR
)
3091 tree off
= gimple_assign_rhs2 (stmt
);
3092 if (TREE_CODE (off
) == INTEGER_CST
3093 && can_propagate_from (stmt
)
3094 && !simple_iv_increment_p (stmt
)
3095 /* ??? Better adjust the interface to that function
3096 instead of building new trees here. */
3097 && forward_propagate_addr_expr
3099 build1_loc (gimple_location (stmt
),
3100 ADDR_EXPR
, TREE_TYPE (rhs
),
3101 fold_build2 (MEM_REF
,
3102 TREE_TYPE (TREE_TYPE (rhs
)),
3104 fold_convert (ptr_type_node
,
3107 fwprop_invalidate_lattice (gimple_get_lhs (stmt
));
3108 release_defs (stmt
);
3109 gsi_remove (&gsi
, true);
3111 else if (is_gimple_min_invariant (rhs
))
3113 /* Make sure to fold &a[0] + off_1 here. */
3114 fold_stmt_inplace (&gsi
);
3116 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
3122 else if (TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
3123 && gimple_assign_load_p (stmt
)
3124 && !gimple_has_volatile_ops (stmt
)
3125 && (TREE_CODE (gimple_assign_rhs1 (stmt
))
3127 && !stmt_can_throw_internal (cfun
, stmt
))
3129 /* Rewrite loads used only in real/imagpart extractions to
3130 component-wise loads. */
3131 use_operand_p use_p
;
3132 imm_use_iterator iter
;
3133 bool rewrite
= true;
3134 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
3136 gimple
*use_stmt
= USE_STMT (use_p
);
3137 if (is_gimple_debug (use_stmt
))
3139 if (!is_gimple_assign (use_stmt
)
3140 || (gimple_assign_rhs_code (use_stmt
) != REALPART_EXPR
3141 && gimple_assign_rhs_code (use_stmt
) != IMAGPART_EXPR
)
3142 || TREE_OPERAND (gimple_assign_rhs1 (use_stmt
), 0) != lhs
)
3151 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
3153 if (is_gimple_debug (use_stmt
))
3155 if (gimple_debug_bind_p (use_stmt
))
3157 gimple_debug_bind_reset_value (use_stmt
);
3158 update_stmt (use_stmt
);
3163 tree new_rhs
= build1 (gimple_assign_rhs_code (use_stmt
),
3164 TREE_TYPE (TREE_TYPE (rhs
)),
3165 unshare_expr (rhs
));
3167 = gimple_build_assign (gimple_assign_lhs (use_stmt
),
3170 location_t loc
= gimple_location (use_stmt
);
3171 gimple_set_location (new_stmt
, loc
);
3172 gimple_stmt_iterator gsi2
= gsi_for_stmt (use_stmt
);
3173 unlink_stmt_vdef (use_stmt
);
3174 gsi_remove (&gsi2
, true);
3176 gsi_insert_before (&gsi
, new_stmt
, GSI_SAME_STMT
);
3179 release_defs (stmt
);
3180 gsi_remove (&gsi
, true);
3185 else if (TREE_CODE (TREE_TYPE (lhs
)) == VECTOR_TYPE
3186 && (TYPE_MODE (TREE_TYPE (lhs
)) == BLKmode
3187 /* After vector lowering rewrite all loads, but
3188 initially do not since this conflicts with
3189 vector CONSTRUCTOR to shuffle optimization. */
3190 || (fun
->curr_properties
& PROP_gimple_lvec
))
3191 && gimple_assign_load_p (stmt
)
3192 && !gimple_has_volatile_ops (stmt
)
3193 && !stmt_can_throw_internal (cfun
, stmt
)
3194 && (!VAR_P (rhs
) || !DECL_HARD_REGISTER (rhs
)))
3195 optimize_vector_load (&gsi
);
3197 else if (code
== COMPLEX_EXPR
)
3199 /* Rewrite stores of a single-use complex build expression
3200 to component-wise stores. */
3201 use_operand_p use_p
;
3203 if (single_imm_use (lhs
, &use_p
, &use_stmt
)
3204 && gimple_store_p (use_stmt
)
3205 && !gimple_has_volatile_ops (use_stmt
)
3206 && is_gimple_assign (use_stmt
)
3207 && (TREE_CODE (gimple_assign_lhs (use_stmt
))
3210 tree use_lhs
= gimple_assign_lhs (use_stmt
);
3211 if (auto_var_p (use_lhs
))
3212 DECL_NOT_GIMPLE_REG_P (use_lhs
) = 1;
3213 tree new_lhs
= build1 (REALPART_EXPR
,
3214 TREE_TYPE (TREE_TYPE (use_lhs
)),
3215 unshare_expr (use_lhs
));
3216 gimple
*new_stmt
= gimple_build_assign (new_lhs
, rhs
);
3217 location_t loc
= gimple_location (use_stmt
);
3218 gimple_set_location (new_stmt
, loc
);
3219 gimple_set_vuse (new_stmt
, gimple_vuse (use_stmt
));
3220 gimple_set_vdef (new_stmt
, make_ssa_name (gimple_vop (cfun
)));
3221 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
3222 gimple_set_vuse (use_stmt
, gimple_vdef (new_stmt
));
3223 gimple_stmt_iterator gsi2
= gsi_for_stmt (use_stmt
);
3224 gsi_insert_before (&gsi2
, new_stmt
, GSI_SAME_STMT
);
3226 new_lhs
= build1 (IMAGPART_EXPR
,
3227 TREE_TYPE (TREE_TYPE (use_lhs
)),
3228 unshare_expr (use_lhs
));
3229 gimple_assign_set_lhs (use_stmt
, new_lhs
);
3230 gimple_assign_set_rhs1 (use_stmt
, gimple_assign_rhs2 (stmt
));
3231 update_stmt (use_stmt
);
3233 release_defs (stmt
);
3234 gsi_remove (&gsi
, true);
3239 else if (code
== CONSTRUCTOR
3240 && VECTOR_TYPE_P (TREE_TYPE (rhs
))
3241 && TYPE_MODE (TREE_TYPE (rhs
)) == BLKmode
3242 && CONSTRUCTOR_NELTS (rhs
) > 0
3243 && (!VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs
, 0)->value
))
3244 || (TYPE_MODE (TREE_TYPE (CONSTRUCTOR_ELT (rhs
, 0)->value
))
3247 /* Rewrite stores of a single-use vector constructors
3248 to component-wise stores if the mode isn't supported. */
3249 use_operand_p use_p
;
3251 if (single_imm_use (lhs
, &use_p
, &use_stmt
)
3252 && gimple_store_p (use_stmt
)
3253 && !gimple_has_volatile_ops (use_stmt
)
3254 && !stmt_can_throw_internal (cfun
, use_stmt
)
3255 && is_gimple_assign (use_stmt
)
3256 && (TREE_CODE (gimple_assign_lhs (use_stmt
))
3259 tree elt_t
= TREE_TYPE (CONSTRUCTOR_ELT (rhs
, 0)->value
);
3260 unsigned HOST_WIDE_INT elt_w
3261 = tree_to_uhwi (TYPE_SIZE (elt_t
));
3262 unsigned HOST_WIDE_INT n
3263 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs
)));
3264 tree use_lhs
= gimple_assign_lhs (use_stmt
);
3265 if (auto_var_p (use_lhs
))
3266 DECL_NOT_GIMPLE_REG_P (use_lhs
) = 1;
3267 for (unsigned HOST_WIDE_INT bi
= 0; bi
< n
; bi
+= elt_w
)
3269 unsigned HOST_WIDE_INT ci
= bi
/ elt_w
;
3271 if (ci
< CONSTRUCTOR_NELTS (rhs
))
3272 new_rhs
= CONSTRUCTOR_ELT (rhs
, ci
)->value
;
3274 new_rhs
= build_zero_cst (elt_t
);
3275 tree new_lhs
= build3 (BIT_FIELD_REF
,
3277 unshare_expr (use_lhs
),
3278 bitsize_int (elt_w
),
3280 gimple
*new_stmt
= gimple_build_assign (new_lhs
, new_rhs
);
3281 location_t loc
= gimple_location (use_stmt
);
3282 gimple_set_location (new_stmt
, loc
);
3283 gimple_set_vuse (new_stmt
, gimple_vuse (use_stmt
));
3284 gimple_set_vdef (new_stmt
,
3285 make_ssa_name (gimple_vop (cfun
)));
3286 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
3287 gimple_set_vuse (use_stmt
, gimple_vdef (new_stmt
));
3288 gimple_stmt_iterator gsi2
= gsi_for_stmt (use_stmt
);
3289 gsi_insert_before (&gsi2
, new_stmt
, GSI_SAME_STMT
);
3291 gimple_stmt_iterator gsi2
= gsi_for_stmt (use_stmt
);
3292 unlink_stmt_vdef (use_stmt
);
3293 release_defs (use_stmt
);
3294 gsi_remove (&gsi2
, true);
3295 release_defs (stmt
);
3296 gsi_remove (&gsi
, true);
3305 /* Combine stmts with the stmts defining their operands.
3306 Note we update GSI within the loop as necessary. */
3307 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3309 gimple
*stmt
= gsi_stmt (gsi
);
3311 /* Mark stmt as potentially needing revisiting. */
3312 gimple_set_plf (stmt
, GF_PLF_1
, false);
3314 /* Substitute from our lattice. We need to do so only once. */
3315 bool substituted_p
= false;
3318 FOR_EACH_SSA_USE_OPERAND (usep
, stmt
, iter
, SSA_OP_USE
)
3320 tree use
= USE_FROM_PTR (usep
);
3321 tree val
= fwprop_ssa_val (use
);
3322 if (val
&& val
!= use
&& may_propagate_copy (use
, val
))
3324 propagate_value (usep
, val
);
3325 substituted_p
= true;
3329 && is_gimple_assign (stmt
)
3330 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
3331 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt
));
3336 gimple
*orig_stmt
= stmt
= gsi_stmt (gsi
);
3337 bool was_noreturn
= (is_gimple_call (stmt
)
3338 && gimple_call_noreturn_p (stmt
));
3341 if (fold_stmt (&gsi
, fwprop_ssa_val
))
3344 stmt
= gsi_stmt (gsi
);
3345 /* Cleanup the CFG if we simplified a condition to
3347 if (gcond
*cond
= dyn_cast
<gcond
*> (stmt
))
3348 if (gimple_cond_true_p (cond
)
3349 || gimple_cond_false_p (cond
))
3353 if (changed
|| substituted_p
)
3355 if (maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
))
3356 bitmap_set_bit (to_purge
, bb
->index
);
3358 && is_gimple_call (stmt
) && gimple_call_noreturn_p (stmt
))
3359 to_fixup
.safe_push (stmt
);
3361 substituted_p
= false;
3364 switch (gimple_code (stmt
))
3368 tree rhs1
= gimple_assign_rhs1 (stmt
);
3369 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3371 if (code
== COND_EXPR
)
3373 /* In this case the entire COND_EXPR is in rhs1. */
3374 if (forward_propagate_into_cond (&gsi
))
3377 stmt
= gsi_stmt (gsi
);
3380 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3383 did_something
= forward_propagate_into_comparison (&gsi
);
3384 if (maybe_clean_or_replace_eh_stmt (stmt
, gsi_stmt (gsi
)))
3385 bitmap_set_bit (to_purge
, bb
->index
);
3386 if (did_something
== 2)
3388 changed
= did_something
!= 0;
3390 else if ((code
== PLUS_EXPR
3391 || code
== BIT_IOR_EXPR
3392 || code
== BIT_XOR_EXPR
)
3393 && simplify_rotate (&gsi
))
3395 else if (code
== VEC_PERM_EXPR
)
3397 int did_something
= simplify_permutation (&gsi
);
3398 if (did_something
== 2)
3400 changed
= did_something
!= 0;
3402 else if (code
== BIT_FIELD_REF
)
3403 changed
= simplify_bitfield_ref (&gsi
);
3404 else if (code
== CONSTRUCTOR
3405 && TREE_CODE (TREE_TYPE (rhs1
)) == VECTOR_TYPE
)
3406 changed
= simplify_vector_constructor (&gsi
);
3407 else if (code
== ARRAY_REF
)
3408 changed
= simplify_count_trailing_zeroes (&gsi
);
3413 changed
= simplify_gimple_switch (as_a
<gswitch
*> (stmt
));
3418 int did_something
= forward_propagate_into_gimple_cond
3419 (as_a
<gcond
*> (stmt
));
3420 if (did_something
== 2)
3422 changed
= did_something
!= 0;
3428 tree callee
= gimple_call_fndecl (stmt
);
3429 if (callee
!= NULL_TREE
3430 && fndecl_built_in_p (callee
, BUILT_IN_NORMAL
))
3431 changed
= simplify_builtin_call (&gsi
, callee
);
3440 /* If the stmt changed then re-visit it and the statements
3441 inserted before it. */
3442 for (; !gsi_end_p (gsi
); gsi_prev (&gsi
))
3443 if (gimple_plf (gsi_stmt (gsi
), GF_PLF_1
))
3445 if (gsi_end_p (gsi
))
3446 gsi
= gsi_start_bb (bb
);
3453 /* Stmt no longer needs to be revisited. */
3454 stmt
= gsi_stmt (gsi
);
3455 gcc_checking_assert (!gimple_plf (stmt
, GF_PLF_1
));
3456 gimple_set_plf (stmt
, GF_PLF_1
, true);
3458 /* Fill up the lattice. */
3459 if (gimple_assign_single_p (stmt
))
3461 tree lhs
= gimple_assign_lhs (stmt
);
3462 tree rhs
= gimple_assign_rhs1 (stmt
);
3463 if (TREE_CODE (lhs
) == SSA_NAME
)
3466 if (TREE_CODE (rhs
) == SSA_NAME
)
3467 val
= fwprop_ssa_val (rhs
);
3468 else if (is_gimple_min_invariant (rhs
))
3470 /* If we can propagate the lattice-value mark the
3471 stmt for removal. */
3473 && may_propagate_copy (lhs
, val
))
3474 to_remove
.safe_push (stmt
);
3475 fwprop_set_lattice_val (lhs
, val
);
3478 else if (gimple_nop_p (stmt
))
3479 to_remove
.safe_push (stmt
);
3482 /* Substitute in destination PHI arguments. */
3485 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3486 for (gphi_iterator gsi
= gsi_start_phis (e
->dest
);
3487 !gsi_end_p (gsi
); gsi_next (&gsi
))
3489 gphi
*phi
= gsi
.phi ();
3490 use_operand_p use_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
3491 tree arg
= USE_FROM_PTR (use_p
);
3492 if (TREE_CODE (arg
) != SSA_NAME
3493 || virtual_operand_p (arg
))
3495 tree val
= fwprop_ssa_val (arg
);
3497 && may_propagate_copy (arg
, val
))
3498 propagate_value (use_p
, val
);
3504 /* Remove stmts in reverse order to make debug stmt creation possible. */
3505 while (!to_remove
.is_empty())
3507 gimple
*stmt
= to_remove
.pop ();
3508 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3510 fprintf (dump_file
, "Removing dead stmt ");
3511 print_gimple_stmt (dump_file
, stmt
, 0);
3512 fprintf (dump_file
, "\n");
3514 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
3515 if (gimple_code (stmt
) == GIMPLE_PHI
)
3516 remove_phi_node (&gsi
, true);
3519 unlink_stmt_vdef (stmt
);
3520 gsi_remove (&gsi
, true);
3521 release_defs (stmt
);
3525 /* Fixup stmts that became noreturn calls. This may require splitting
3526 blocks and thus isn't possible during the walk. Do this
3527 in reverse order so we don't inadvertedly remove a stmt we want to
3528 fixup by visiting a dominating now noreturn call first. */
3529 while (!to_fixup
.is_empty ())
3531 gimple
*stmt
= to_fixup
.pop ();
3532 if (dump_file
&& dump_flags
& TDF_DETAILS
)
3534 fprintf (dump_file
, "Fixing up noreturn call ");
3535 print_gimple_stmt (dump_file
, stmt
, 0);
3536 fprintf (dump_file
, "\n");
3538 cfg_changed
|= fixup_noreturn_call (stmt
);
3541 cfg_changed
|= gimple_purge_all_dead_eh_edges (to_purge
);
3542 BITMAP_FREE (to_purge
);
3545 todoflags
|= TODO_cleanup_cfg
;
3553 make_pass_forwprop (gcc::context
*ctxt
)
3555 return new pass_forwprop (ctxt
);