1 /* Forward propagation of expressions for single use variables.
2 Copyright (C) 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "basic-block.h"
28 #include "gimple-pretty-print.h"
29 #include "tree-flow.h"
30 #include "tree-pass.h"
31 #include "langhooks.h"
37 /* This pass propagates the RHS of assignment statements into use
38 sites of the LHS of the assignment. It's basically a specialized
39 form of tree combination. It is hoped all of this can disappear
40 when we have a generalized tree combiner.
42 One class of common cases we handle is forward propagating a single use
43 variable into a COND_EXPR.
47 if (x) goto ... else goto ...
49 Will be transformed into:
52 if (a COND b) goto ... else goto ...
54 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
56 Or (assuming c1 and c2 are constants):
60 if (x EQ/NEQ c2) goto ... else goto ...
62 Will be transformed into:
65 if (a EQ/NEQ (c2 - c1)) goto ... else goto ...
67 Similarly for x = a - c1.
73 if (x) goto ... else goto ...
75 Will be transformed into:
78 if (a == 0) goto ... else goto ...
80 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
81 For these cases, we propagate A into all, possibly more than one,
82 COND_EXPRs that use X.
88 if (x) goto ... else goto ...
90 Will be transformed into:
93 if (a != 0) goto ... else goto ...
95 (Assuming a is an integral type and x is a boolean or x is an
96 integral and a is a boolean.)
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.
102 In addition to eliminating the variable and the statement which assigns
103 a value to the variable, we may be able to later thread the jump without
104 adding insane complexity in the dominator optimizer.
106 Also note these transformations can cascade. We handle this by having
107 a worklist of COND_EXPR statements to examine. As we make a change to
108 a statement, we put it back on the worklist to examine on the next
109 iteration of the main loop.
111 A second class of propagation opportunities arises for ADDR_EXPR
122 ptr = (type1*)&type2var;
125 Will get turned into (if type1 and type2 are the same size
126 and neither have volatile on them):
127 res = VIEW_CONVERT_EXPR<type1>(type2var)
132 ptr2 = ptr + <constant>;
136 ptr2 = &x[constant/elementsize];
141 offset = index * element_size;
142 offset_p = (pointer) offset;
143 ptr2 = ptr + offset_p
145 Will get turned into:
153 Provided that decl has known alignment >= 2, will get turned into
157 We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to
158 allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent
161 This will (of course) be extended as other needs arise. */
163 static bool forward_propagate_addr_expr (tree name
, tree rhs
);
165 /* Set to true if we delete dead edges during the optimization. */
166 static bool cfg_changed
;
168 static tree
rhs_to_tree (tree type
, gimple stmt
);
170 /* Get the next statement we can propagate NAME's value into skipping
171 trivial copies. Returns the statement that is suitable as a
172 propagation destination or NULL_TREE if there is no such one.
173 This only returns destinations in a single-use chain. FINAL_NAME_P
174 if non-NULL is written to the ssa name that represents the use. */
177 get_prop_dest_stmt (tree name
, tree
*final_name_p
)
183 /* If name has multiple uses, bail out. */
184 if (!single_imm_use (name
, &use
, &use_stmt
))
187 /* If this is not a trivial copy, we found it. */
188 if (!gimple_assign_ssa_name_copy_p (use_stmt
)
189 || gimple_assign_rhs1 (use_stmt
) != name
)
192 /* Continue searching uses of the copy destination. */
193 name
= gimple_assign_lhs (use_stmt
);
197 *final_name_p
= name
;
202 /* Get the statement we can propagate from into NAME skipping
203 trivial copies. Returns the statement which defines the
204 propagation source or NULL_TREE if there is no such one.
205 If SINGLE_USE_ONLY is set considers only sources which have
206 a single use chain up to NAME. If SINGLE_USE_P is non-null,
207 it is set to whether the chain to NAME is a single use chain
208 or not. SINGLE_USE_P is not written to if SINGLE_USE_ONLY is set. */
211 get_prop_source_stmt (tree name
, bool single_use_only
, bool *single_use_p
)
213 bool single_use
= true;
216 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
218 if (!has_single_use (name
))
225 /* If name is defined by a PHI node or is the default def, bail out. */
226 if (!is_gimple_assign (def_stmt
))
229 /* If def_stmt is not a simple copy, we possibly found it. */
230 if (!gimple_assign_ssa_name_copy_p (def_stmt
))
234 if (!single_use_only
&& single_use_p
)
235 *single_use_p
= single_use
;
237 /* We can look through pointer conversions in the search
238 for a useful stmt for the comparison folding. */
239 rhs
= gimple_assign_rhs1 (def_stmt
);
240 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
241 && TREE_CODE (rhs
) == SSA_NAME
242 && POINTER_TYPE_P (TREE_TYPE (gimple_assign_lhs (def_stmt
)))
243 && POINTER_TYPE_P (TREE_TYPE (rhs
)))
250 /* Continue searching the def of the copy source name. */
251 name
= gimple_assign_rhs1 (def_stmt
);
256 /* Checks if the destination ssa name in DEF_STMT can be used as
257 propagation source. Returns true if so, otherwise false. */
260 can_propagate_from (gimple def_stmt
)
262 gcc_assert (is_gimple_assign (def_stmt
));
264 /* If the rhs has side-effects we cannot propagate from it. */
265 if (gimple_has_volatile_ops (def_stmt
))
268 /* If the rhs is a load we cannot propagate from it. */
269 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt
)) == tcc_reference
270 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt
)) == tcc_declaration
)
273 /* Constants can be always propagated. */
274 if (gimple_assign_single_p (def_stmt
)
275 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt
)))
278 /* We cannot propagate ssa names that occur in abnormal phi nodes. */
279 if (stmt_references_abnormal_ssa_name (def_stmt
))
282 /* If the definition is a conversion of a pointer to a function type,
283 then we can not apply optimizations as some targets require
284 function pointers to be canonicalized and in this case this
285 optimization could eliminate a necessary canonicalization. */
286 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
288 tree rhs
= gimple_assign_rhs1 (def_stmt
);
289 if (POINTER_TYPE_P (TREE_TYPE (rhs
))
290 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs
))) == FUNCTION_TYPE
)
297 /* Remove a chain of dead statements starting at the definition of
298 NAME. The chain is linked via the first operand of the defining statements.
299 If NAME was replaced in its only use then this function can be used
300 to clean up dead stmts. The function handles already released SSA
302 Returns true if cleanup-cfg has to run. */
305 remove_prop_source_from_use (tree name
)
307 gimple_stmt_iterator gsi
;
309 bool cfg_changed
= false;
314 if (SSA_NAME_IN_FREE_LIST (name
)
315 || SSA_NAME_IS_DEFAULT_DEF (name
)
316 || !has_zero_uses (name
))
319 stmt
= SSA_NAME_DEF_STMT (name
);
320 if (gimple_code (stmt
) == GIMPLE_PHI
321 || gimple_has_side_effects (stmt
))
324 bb
= gimple_bb (stmt
);
325 gsi
= gsi_for_stmt (stmt
);
326 unlink_stmt_vdef (stmt
);
327 if (gsi_remove (&gsi
, true))
328 cfg_changed
|= gimple_purge_dead_eh_edges (bb
);
331 name
= is_gimple_assign (stmt
) ? gimple_assign_rhs1 (stmt
) : NULL_TREE
;
332 } while (name
&& TREE_CODE (name
) == SSA_NAME
);
337 /* Return the rhs of a gimple_assign STMT in a form of a single tree,
338 converted to type TYPE.
340 This should disappear, but is needed so we can combine expressions and use
341 the fold() interfaces. Long term, we need to develop folding and combine
342 routines that deal with gimple exclusively . */
345 rhs_to_tree (tree type
, gimple stmt
)
347 location_t loc
= gimple_location (stmt
);
348 enum tree_code code
= gimple_assign_rhs_code (stmt
);
349 if (get_gimple_rhs_class (code
) == GIMPLE_TERNARY_RHS
)
350 return fold_build3_loc (loc
, code
, type
, gimple_assign_rhs1 (stmt
),
351 gimple_assign_rhs2 (stmt
),
352 gimple_assign_rhs3 (stmt
));
353 else if (get_gimple_rhs_class (code
) == GIMPLE_BINARY_RHS
)
354 return fold_build2_loc (loc
, code
, type
, gimple_assign_rhs1 (stmt
),
355 gimple_assign_rhs2 (stmt
));
356 else if (get_gimple_rhs_class (code
) == GIMPLE_UNARY_RHS
)
357 return build1 (code
, type
, gimple_assign_rhs1 (stmt
));
358 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
)
359 return gimple_assign_rhs1 (stmt
);
364 /* Combine OP0 CODE OP1 in the context of a COND_EXPR. Returns
365 the folded result in a form suitable for COND_EXPR_COND or
366 NULL_TREE, if there is no suitable simplified form. If
367 INVARIANT_ONLY is true only gimple_min_invariant results are
368 considered simplified. */
371 combine_cond_expr_cond (gimple stmt
, enum tree_code code
, tree type
,
372 tree op0
, tree op1
, bool invariant_only
)
376 gcc_assert (TREE_CODE_CLASS (code
) == tcc_comparison
);
378 fold_defer_overflow_warnings ();
379 t
= fold_binary_loc (gimple_location (stmt
), code
, type
, op0
, op1
);
382 fold_undefer_overflow_warnings (false, NULL
, 0);
386 /* Require that we got a boolean type out if we put one in. */
387 gcc_assert (TREE_CODE (TREE_TYPE (t
)) == TREE_CODE (type
));
389 /* Canonicalize the combined condition for use in a COND_EXPR. */
390 t
= canonicalize_cond_expr_cond (t
);
392 /* Bail out if we required an invariant but didn't get one. */
393 if (!t
|| (invariant_only
&& !is_gimple_min_invariant (t
)))
395 fold_undefer_overflow_warnings (false, NULL
, 0);
399 fold_undefer_overflow_warnings (!gimple_no_warning_p (stmt
), stmt
, 0);
404 /* Combine the comparison OP0 CODE OP1 at LOC with the defining statements
405 of its operand. Return a new comparison tree or NULL_TREE if there
406 were no simplifying combines. */
409 forward_propagate_into_comparison_1 (gimple stmt
,
410 enum tree_code code
, tree type
,
413 tree tmp
= NULL_TREE
;
414 tree rhs0
= NULL_TREE
, rhs1
= NULL_TREE
;
415 bool single_use0_p
= false, single_use1_p
= false;
417 /* For comparisons use the first operand, that is likely to
418 simplify comparisons against constants. */
419 if (TREE_CODE (op0
) == SSA_NAME
)
421 gimple def_stmt
= get_prop_source_stmt (op0
, false, &single_use0_p
);
422 if (def_stmt
&& can_propagate_from (def_stmt
))
424 rhs0
= rhs_to_tree (TREE_TYPE (op1
), def_stmt
);
425 tmp
= combine_cond_expr_cond (stmt
, code
, type
,
426 rhs0
, op1
, !single_use0_p
);
432 /* If that wasn't successful, try the second operand. */
433 if (TREE_CODE (op1
) == SSA_NAME
)
435 gimple def_stmt
= get_prop_source_stmt (op1
, false, &single_use1_p
);
436 if (def_stmt
&& can_propagate_from (def_stmt
))
438 rhs1
= rhs_to_tree (TREE_TYPE (op0
), def_stmt
);
439 tmp
= combine_cond_expr_cond (stmt
, code
, type
,
440 op0
, rhs1
, !single_use1_p
);
446 /* If that wasn't successful either, try both operands. */
447 if (rhs0
!= NULL_TREE
448 && rhs1
!= NULL_TREE
)
449 tmp
= combine_cond_expr_cond (stmt
, code
, type
,
451 !(single_use0_p
&& single_use1_p
));
456 /* Propagate from the ssa name definition statements of the assignment
457 from a comparison at *GSI into the conditional if that simplifies it.
458 Returns 1 if the stmt was modified and 2 if the CFG needs cleanup,
459 otherwise returns 0. */
462 forward_propagate_into_comparison (gimple_stmt_iterator
*gsi
)
464 gimple stmt
= gsi_stmt (*gsi
);
466 bool cfg_changed
= false;
467 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
468 tree rhs1
= gimple_assign_rhs1 (stmt
);
469 tree rhs2
= gimple_assign_rhs2 (stmt
);
471 /* Combine the comparison with defining statements. */
472 tmp
= forward_propagate_into_comparison_1 (stmt
,
473 gimple_assign_rhs_code (stmt
),
475 if (tmp
&& useless_type_conversion_p (type
, TREE_TYPE (tmp
)))
477 gimple_assign_set_rhs_from_tree (gsi
, tmp
);
479 update_stmt (gsi_stmt (*gsi
));
481 if (TREE_CODE (rhs1
) == SSA_NAME
)
482 cfg_changed
|= remove_prop_source_from_use (rhs1
);
483 if (TREE_CODE (rhs2
) == SSA_NAME
)
484 cfg_changed
|= remove_prop_source_from_use (rhs2
);
485 return cfg_changed
? 2 : 1;
491 /* Propagate from the ssa name definition statements of COND_EXPR
492 in GIMPLE_COND statement STMT into the conditional if that simplifies it.
493 Returns zero if no statement was changed, one if there were
494 changes and two if cfg_cleanup needs to run.
496 This must be kept in sync with forward_propagate_into_cond. */
499 forward_propagate_into_gimple_cond (gimple stmt
)
502 enum tree_code code
= gimple_cond_code (stmt
);
503 bool cfg_changed
= false;
504 tree rhs1
= gimple_cond_lhs (stmt
);
505 tree rhs2
= gimple_cond_rhs (stmt
);
507 /* We can do tree combining on SSA_NAME and comparison expressions. */
508 if (TREE_CODE_CLASS (gimple_cond_code (stmt
)) != tcc_comparison
)
511 tmp
= forward_propagate_into_comparison_1 (stmt
, code
,
516 if (dump_file
&& tmp
)
518 fprintf (dump_file
, " Replaced '");
519 print_gimple_expr (dump_file
, stmt
, 0, 0);
520 fprintf (dump_file
, "' with '");
521 print_generic_expr (dump_file
, tmp
, 0);
522 fprintf (dump_file
, "'\n");
525 gimple_cond_set_condition_from_tree (stmt
, unshare_expr (tmp
));
528 if (TREE_CODE (rhs1
) == SSA_NAME
)
529 cfg_changed
|= remove_prop_source_from_use (rhs1
);
530 if (TREE_CODE (rhs2
) == SSA_NAME
)
531 cfg_changed
|= remove_prop_source_from_use (rhs2
);
532 return (cfg_changed
|| is_gimple_min_invariant (tmp
)) ? 2 : 1;
535 /* Canonicalize _Bool == 0 and _Bool != 1 to _Bool != 0 by swapping edges. */
536 if ((TREE_CODE (TREE_TYPE (rhs1
)) == BOOLEAN_TYPE
537 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
538 && TYPE_PRECISION (TREE_TYPE (rhs1
)) == 1))
540 && integer_zerop (rhs2
))
542 && integer_onep (rhs2
))))
544 basic_block bb
= gimple_bb (stmt
);
545 gimple_cond_set_code (stmt
, NE_EXPR
);
546 gimple_cond_set_rhs (stmt
, build_zero_cst (TREE_TYPE (rhs1
)));
547 EDGE_SUCC (bb
, 0)->flags
^= (EDGE_TRUE_VALUE
|EDGE_FALSE_VALUE
);
548 EDGE_SUCC (bb
, 1)->flags
^= (EDGE_TRUE_VALUE
|EDGE_FALSE_VALUE
);
556 /* Propagate from the ssa name definition statements of COND_EXPR
557 in the rhs of statement STMT into the conditional if that simplifies it.
558 Returns true zero if the stmt was changed. */
561 forward_propagate_into_cond (gimple_stmt_iterator
*gsi_p
)
563 gimple stmt
= gsi_stmt (*gsi_p
);
564 tree tmp
= NULL_TREE
;
565 tree cond
= gimple_assign_rhs1 (stmt
);
568 /* We can do tree combining on SSA_NAME and comparison expressions. */
569 if (COMPARISON_CLASS_P (cond
))
570 tmp
= forward_propagate_into_comparison_1 (stmt
, TREE_CODE (cond
),
572 TREE_OPERAND (cond
, 0),
573 TREE_OPERAND (cond
, 1));
574 else if (TREE_CODE (cond
) == SSA_NAME
)
578 gimple def_stmt
= get_prop_source_stmt (name
, true, NULL
);
579 if (!def_stmt
|| !can_propagate_from (def_stmt
))
582 code
= gimple_assign_rhs_code (def_stmt
);
583 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
584 tmp
= fold_build2_loc (gimple_location (def_stmt
),
587 gimple_assign_rhs1 (def_stmt
),
588 gimple_assign_rhs2 (def_stmt
));
589 else if ((code
== BIT_NOT_EXPR
590 && TYPE_PRECISION (TREE_TYPE (cond
)) == 1)
591 || (code
== BIT_XOR_EXPR
592 && integer_onep (gimple_assign_rhs2 (def_stmt
))))
594 tmp
= gimple_assign_rhs1 (def_stmt
);
600 && is_gimple_condexpr (tmp
))
602 if (dump_file
&& tmp
)
604 fprintf (dump_file
, " Replaced '");
605 print_generic_expr (dump_file
, cond
, 0);
606 fprintf (dump_file
, "' with '");
607 print_generic_expr (dump_file
, tmp
, 0);
608 fprintf (dump_file
, "'\n");
611 if (integer_onep (tmp
))
612 gimple_assign_set_rhs_from_tree (gsi_p
, gimple_assign_rhs2 (stmt
));
613 else if (integer_zerop (tmp
))
614 gimple_assign_set_rhs_from_tree (gsi_p
, gimple_assign_rhs3 (stmt
));
617 gimple_assign_set_rhs1 (stmt
, unshare_expr (tmp
));
620 tree t
= gimple_assign_rhs2 (stmt
);
621 gimple_assign_set_rhs2 (stmt
, gimple_assign_rhs3 (stmt
));
622 gimple_assign_set_rhs3 (stmt
, t
);
625 stmt
= gsi_stmt (*gsi_p
);
634 /* Propagate from the ssa name definition statements of COND_EXPR
635 values in the rhs of statement STMT into the conditional arms
636 if that simplifies it.
637 Returns true if the stmt was changed. */
640 combine_cond_exprs (gimple_stmt_iterator
*gsi_p
)
642 gimple stmt
= gsi_stmt (*gsi_p
);
643 tree cond
, val1
, val2
;
644 bool changed
= false;
646 cond
= gimple_assign_rhs1 (stmt
);
647 val1
= gimple_assign_rhs2 (stmt
);
648 if (TREE_CODE (val1
) == SSA_NAME
)
650 gimple def_stmt
= SSA_NAME_DEF_STMT (val1
);
651 if (is_gimple_assign (def_stmt
)
652 && gimple_assign_rhs_code (def_stmt
) == gimple_assign_rhs_code (stmt
)
653 && operand_equal_p (gimple_assign_rhs1 (def_stmt
), cond
, 0))
655 val1
= unshare_expr (gimple_assign_rhs2 (def_stmt
));
656 gimple_assign_set_rhs2 (stmt
, val1
);
660 val2
= gimple_assign_rhs3 (stmt
);
661 if (TREE_CODE (val2
) == SSA_NAME
)
663 gimple def_stmt
= SSA_NAME_DEF_STMT (val2
);
664 if (is_gimple_assign (def_stmt
)
665 && gimple_assign_rhs_code (def_stmt
) == gimple_assign_rhs_code (stmt
)
666 && operand_equal_p (gimple_assign_rhs1 (def_stmt
), cond
, 0))
668 val2
= unshare_expr (gimple_assign_rhs3 (def_stmt
));
669 gimple_assign_set_rhs3 (stmt
, val2
);
673 if (operand_equal_p (val1
, val2
, 0))
675 gimple_assign_set_rhs_from_tree (gsi_p
, val1
);
676 stmt
= gsi_stmt (*gsi_p
);
686 /* We've just substituted an ADDR_EXPR into stmt. Update all the
687 relevant data structures to match. */
690 tidy_after_forward_propagate_addr (gimple stmt
)
692 /* We may have turned a trapping insn into a non-trapping insn. */
693 if (maybe_clean_or_replace_eh_stmt (stmt
, stmt
)
694 && gimple_purge_dead_eh_edges (gimple_bb (stmt
)))
697 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) == ADDR_EXPR
)
698 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt
));
701 /* NAME is a SSA_NAME representing DEF_RHS which is of the form
702 ADDR_EXPR <whatever>.
704 Try to forward propagate the ADDR_EXPR into the use USE_STMT.
705 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
706 node or for recovery of array indexing from pointer arithmetic.
708 Return true if the propagation was successful (the propagation can
709 be not totally successful, yet things may have been changed). */
712 forward_propagate_addr_expr_1 (tree name
, tree def_rhs
,
713 gimple_stmt_iterator
*use_stmt_gsi
,
716 tree lhs
, rhs
, rhs2
, array_ref
;
717 gimple use_stmt
= gsi_stmt (*use_stmt_gsi
);
718 enum tree_code rhs_code
;
721 gcc_assert (TREE_CODE (def_rhs
) == ADDR_EXPR
);
723 lhs
= gimple_assign_lhs (use_stmt
);
724 rhs_code
= gimple_assign_rhs_code (use_stmt
);
725 rhs
= gimple_assign_rhs1 (use_stmt
);
727 /* Trivial cases. The use statement could be a trivial copy or a
728 useless conversion. Recurse to the uses of the lhs as copyprop does
729 not copy through different variant pointers and FRE does not catch
730 all useless conversions. Treat the case of a single-use name and
731 a conversion to def_rhs type separate, though. */
732 if (TREE_CODE (lhs
) == SSA_NAME
733 && ((rhs_code
== SSA_NAME
&& rhs
== name
)
734 || CONVERT_EXPR_CODE_P (rhs_code
)))
736 /* Only recurse if we don't deal with a single use or we cannot
737 do the propagation to the current statement. In particular
738 we can end up with a conversion needed for a non-invariant
739 address which we cannot do in a single statement. */
741 || (!useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (def_rhs
))
742 && (!is_gimple_min_invariant (def_rhs
)
743 || (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
744 && POINTER_TYPE_P (TREE_TYPE (def_rhs
))
745 && (TYPE_PRECISION (TREE_TYPE (lhs
))
746 > TYPE_PRECISION (TREE_TYPE (def_rhs
)))))))
747 return forward_propagate_addr_expr (lhs
, def_rhs
);
749 gimple_assign_set_rhs1 (use_stmt
, unshare_expr (def_rhs
));
750 if (useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (def_rhs
)))
751 gimple_assign_set_rhs_code (use_stmt
, TREE_CODE (def_rhs
));
753 gimple_assign_set_rhs_code (use_stmt
, NOP_EXPR
);
757 /* Propagate through constant pointer adjustments. */
758 if (TREE_CODE (lhs
) == SSA_NAME
759 && rhs_code
== POINTER_PLUS_EXPR
761 && TREE_CODE (gimple_assign_rhs2 (use_stmt
)) == INTEGER_CST
)
764 /* As we come here with non-invariant addresses in def_rhs we need
765 to make sure we can build a valid constant offsetted address
766 for further propagation. Simply rely on fold building that
767 and check after the fact. */
768 new_def_rhs
= fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (rhs
)),
770 fold_convert (ptr_type_node
,
771 gimple_assign_rhs2 (use_stmt
)));
772 if (TREE_CODE (new_def_rhs
) == MEM_REF
773 && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs
, 0)))
775 new_def_rhs
= build_fold_addr_expr_with_type (new_def_rhs
,
778 /* Recurse. If we could propagate into all uses of lhs do not
779 bother to replace into the current use but just pretend we did. */
780 if (TREE_CODE (new_def_rhs
) == ADDR_EXPR
781 && forward_propagate_addr_expr (lhs
, new_def_rhs
))
784 if (useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (new_def_rhs
)))
785 gimple_assign_set_rhs_with_ops (use_stmt_gsi
, TREE_CODE (new_def_rhs
),
786 new_def_rhs
, NULL_TREE
);
787 else if (is_gimple_min_invariant (new_def_rhs
))
788 gimple_assign_set_rhs_with_ops (use_stmt_gsi
, NOP_EXPR
,
789 new_def_rhs
, NULL_TREE
);
792 gcc_assert (gsi_stmt (*use_stmt_gsi
) == use_stmt
);
793 update_stmt (use_stmt
);
797 /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
798 ADDR_EXPR will not appear on the LHS. */
799 lhs
= gimple_assign_lhs (use_stmt
);
800 while (handled_component_p (lhs
))
801 lhs
= TREE_OPERAND (lhs
, 0);
803 /* Now see if the LHS node is a MEM_REF using NAME. If so,
804 propagate the ADDR_EXPR into the use of NAME and fold the result. */
805 if (TREE_CODE (lhs
) == MEM_REF
806 && TREE_OPERAND (lhs
, 0) == name
)
809 HOST_WIDE_INT def_rhs_offset
;
810 /* If the address is invariant we can always fold it. */
811 if ((def_rhs_base
= get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs
, 0),
814 double_int off
= mem_ref_offset (lhs
);
816 off
+= double_int::from_shwi (def_rhs_offset
);
817 if (TREE_CODE (def_rhs_base
) == MEM_REF
)
819 off
+= mem_ref_offset (def_rhs_base
);
820 new_ptr
= TREE_OPERAND (def_rhs_base
, 0);
823 new_ptr
= build_fold_addr_expr (def_rhs_base
);
824 TREE_OPERAND (lhs
, 0) = new_ptr
;
825 TREE_OPERAND (lhs
, 1)
826 = double_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs
, 1)), off
);
827 tidy_after_forward_propagate_addr (use_stmt
);
828 /* Continue propagating into the RHS if this was not the only use. */
832 /* If the LHS is a plain dereference and the value type is the same as
833 that of the pointed-to type of the address we can put the
834 dereferenced address on the LHS preserving the original alias-type. */
835 else if (gimple_assign_lhs (use_stmt
) == lhs
836 && integer_zerop (TREE_OPERAND (lhs
, 1))
837 && useless_type_conversion_p
838 (TREE_TYPE (TREE_OPERAND (def_rhs
, 0)),
839 TREE_TYPE (gimple_assign_rhs1 (use_stmt
))))
841 tree
*def_rhs_basep
= &TREE_OPERAND (def_rhs
, 0);
842 tree new_offset
, new_base
, saved
, new_lhs
;
843 while (handled_component_p (*def_rhs_basep
))
844 def_rhs_basep
= &TREE_OPERAND (*def_rhs_basep
, 0);
845 saved
= *def_rhs_basep
;
846 if (TREE_CODE (*def_rhs_basep
) == MEM_REF
)
848 new_base
= TREE_OPERAND (*def_rhs_basep
, 0);
849 new_offset
= fold_convert (TREE_TYPE (TREE_OPERAND (lhs
, 1)),
850 TREE_OPERAND (*def_rhs_basep
, 1));
854 new_base
= build_fold_addr_expr (*def_rhs_basep
);
855 new_offset
= TREE_OPERAND (lhs
, 1);
857 *def_rhs_basep
= build2 (MEM_REF
, TREE_TYPE (*def_rhs_basep
),
858 new_base
, new_offset
);
859 TREE_THIS_VOLATILE (*def_rhs_basep
) = TREE_THIS_VOLATILE (lhs
);
860 TREE_SIDE_EFFECTS (*def_rhs_basep
) = TREE_SIDE_EFFECTS (lhs
);
861 TREE_THIS_NOTRAP (*def_rhs_basep
) = TREE_THIS_NOTRAP (lhs
);
862 new_lhs
= unshare_expr (TREE_OPERAND (def_rhs
, 0));
863 gimple_assign_set_lhs (use_stmt
, new_lhs
);
864 TREE_THIS_VOLATILE (new_lhs
) = TREE_THIS_VOLATILE (lhs
);
865 TREE_SIDE_EFFECTS (new_lhs
) = TREE_SIDE_EFFECTS (lhs
);
866 *def_rhs_basep
= saved
;
867 tidy_after_forward_propagate_addr (use_stmt
);
868 /* Continue propagating into the RHS if this was not the
874 /* We can have a struct assignment dereferencing our name twice.
875 Note that we didn't propagate into the lhs to not falsely
876 claim we did when propagating into the rhs. */
880 /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
881 nodes from the RHS. */
882 rhs
= gimple_assign_rhs1 (use_stmt
);
883 if (TREE_CODE (rhs
) == ADDR_EXPR
)
884 rhs
= TREE_OPERAND (rhs
, 0);
885 while (handled_component_p (rhs
))
886 rhs
= TREE_OPERAND (rhs
, 0);
888 /* Now see if the RHS node is a MEM_REF using NAME. If so,
889 propagate the ADDR_EXPR into the use of NAME and fold the result. */
890 if (TREE_CODE (rhs
) == MEM_REF
891 && TREE_OPERAND (rhs
, 0) == name
)
894 HOST_WIDE_INT def_rhs_offset
;
895 if ((def_rhs_base
= get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs
, 0),
898 double_int off
= mem_ref_offset (rhs
);
900 off
+= double_int::from_shwi (def_rhs_offset
);
901 if (TREE_CODE (def_rhs_base
) == MEM_REF
)
903 off
+= mem_ref_offset (def_rhs_base
);
904 new_ptr
= TREE_OPERAND (def_rhs_base
, 0);
907 new_ptr
= build_fold_addr_expr (def_rhs_base
);
908 TREE_OPERAND (rhs
, 0) = new_ptr
;
909 TREE_OPERAND (rhs
, 1)
910 = double_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs
, 1)), off
);
911 fold_stmt_inplace (use_stmt_gsi
);
912 tidy_after_forward_propagate_addr (use_stmt
);
915 /* If the RHS is a plain dereference and the value type is the same as
916 that of the pointed-to type of the address we can put the
917 dereferenced address on the RHS preserving the original alias-type. */
918 else if (gimple_assign_rhs1 (use_stmt
) == rhs
919 && integer_zerop (TREE_OPERAND (rhs
, 1))
920 && useless_type_conversion_p
921 (TREE_TYPE (gimple_assign_lhs (use_stmt
)),
922 TREE_TYPE (TREE_OPERAND (def_rhs
, 0))))
924 tree
*def_rhs_basep
= &TREE_OPERAND (def_rhs
, 0);
925 tree new_offset
, new_base
, saved
, new_rhs
;
926 while (handled_component_p (*def_rhs_basep
))
927 def_rhs_basep
= &TREE_OPERAND (*def_rhs_basep
, 0);
928 saved
= *def_rhs_basep
;
929 if (TREE_CODE (*def_rhs_basep
) == MEM_REF
)
931 new_base
= TREE_OPERAND (*def_rhs_basep
, 0);
932 new_offset
= fold_convert (TREE_TYPE (TREE_OPERAND (rhs
, 1)),
933 TREE_OPERAND (*def_rhs_basep
, 1));
937 new_base
= build_fold_addr_expr (*def_rhs_basep
);
938 new_offset
= TREE_OPERAND (rhs
, 1);
940 *def_rhs_basep
= build2 (MEM_REF
, TREE_TYPE (*def_rhs_basep
),
941 new_base
, new_offset
);
942 TREE_THIS_VOLATILE (*def_rhs_basep
) = TREE_THIS_VOLATILE (rhs
);
943 TREE_SIDE_EFFECTS (*def_rhs_basep
) = TREE_SIDE_EFFECTS (rhs
);
944 TREE_THIS_NOTRAP (*def_rhs_basep
) = TREE_THIS_NOTRAP (rhs
);
945 new_rhs
= unshare_expr (TREE_OPERAND (def_rhs
, 0));
946 gimple_assign_set_rhs1 (use_stmt
, new_rhs
);
947 TREE_THIS_VOLATILE (new_rhs
) = TREE_THIS_VOLATILE (rhs
);
948 TREE_SIDE_EFFECTS (new_rhs
) = TREE_SIDE_EFFECTS (rhs
);
949 *def_rhs_basep
= saved
;
950 fold_stmt_inplace (use_stmt_gsi
);
951 tidy_after_forward_propagate_addr (use_stmt
);
956 /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there
958 if (gimple_assign_rhs_code (use_stmt
) != POINTER_PLUS_EXPR
959 || gimple_assign_rhs1 (use_stmt
) != name
)
962 /* The remaining cases are all for turning pointer arithmetic into
963 array indexing. They only apply when we have the address of
964 element zero in an array. If that is not the case then there
966 array_ref
= TREE_OPERAND (def_rhs
, 0);
967 if ((TREE_CODE (array_ref
) != ARRAY_REF
968 || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref
, 0))) != ARRAY_TYPE
969 || TREE_CODE (TREE_OPERAND (array_ref
, 1)) != INTEGER_CST
)
970 && TREE_CODE (TREE_TYPE (array_ref
)) != ARRAY_TYPE
)
973 rhs2
= gimple_assign_rhs2 (use_stmt
);
974 /* Optimize &x[C1] p+ C2 to &x p+ C3 with C3 = C1 * element_size + C2. */
975 if (TREE_CODE (rhs2
) == INTEGER_CST
)
977 tree new_rhs
= build1_loc (gimple_location (use_stmt
),
978 ADDR_EXPR
, TREE_TYPE (def_rhs
),
979 fold_build2 (MEM_REF
,
980 TREE_TYPE (TREE_TYPE (def_rhs
)),
981 unshare_expr (def_rhs
),
982 fold_convert (ptr_type_node
,
984 gimple_assign_set_rhs_from_tree (use_stmt_gsi
, new_rhs
);
985 use_stmt
= gsi_stmt (*use_stmt_gsi
);
986 update_stmt (use_stmt
);
987 tidy_after_forward_propagate_addr (use_stmt
);
994 /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
996 Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
997 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
998 node or for recovery of array indexing from pointer arithmetic.
999 Returns true, if all uses have been propagated into. */
1002 forward_propagate_addr_expr (tree name
, tree rhs
)
1004 int stmt_loop_depth
= bb_loop_depth (gimple_bb (SSA_NAME_DEF_STMT (name
)));
1005 imm_use_iterator iter
;
1008 bool single_use_p
= has_single_use (name
);
1010 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, name
)
1015 /* If the use is not in a simple assignment statement, then
1016 there is nothing we can do. */
1017 if (gimple_code (use_stmt
) != GIMPLE_ASSIGN
)
1019 if (!is_gimple_debug (use_stmt
))
1024 /* If the use is in a deeper loop nest, then we do not want
1025 to propagate non-invariant ADDR_EXPRs into the loop as that
1026 is likely adding expression evaluations into the loop. */
1027 if (bb_loop_depth (gimple_bb (use_stmt
)) > stmt_loop_depth
1028 && !is_gimple_min_invariant (rhs
))
1035 gimple_stmt_iterator gsi
= gsi_for_stmt (use_stmt
);
1036 result
= forward_propagate_addr_expr_1 (name
, rhs
, &gsi
,
1038 /* If the use has moved to a different statement adjust
1039 the update machinery for the old statement too. */
1040 if (use_stmt
!= gsi_stmt (gsi
))
1042 update_stmt (use_stmt
);
1043 use_stmt
= gsi_stmt (gsi
);
1046 update_stmt (use_stmt
);
1050 /* Remove intermediate now unused copy and conversion chains. */
1051 use_rhs
= gimple_assign_rhs1 (use_stmt
);
1053 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
1054 && TREE_CODE (use_rhs
) == SSA_NAME
1055 && has_zero_uses (gimple_assign_lhs (use_stmt
)))
1057 gimple_stmt_iterator gsi
= gsi_for_stmt (use_stmt
);
1058 release_defs (use_stmt
);
1059 gsi_remove (&gsi
, true);
1063 return all
&& has_zero_uses (name
);
1067 /* Forward propagate the comparison defined in *DEFGSI like
1068 cond_1 = x CMP y to uses of the form
1072 Returns true if stmt is now unused. Advance DEFGSI to the next
1076 forward_propagate_comparison (gimple_stmt_iterator
*defgsi
)
1078 gimple stmt
= gsi_stmt (*defgsi
);
1079 tree name
= gimple_assign_lhs (stmt
);
1081 tree tmp
= NULL_TREE
;
1082 gimple_stmt_iterator gsi
;
1083 enum tree_code code
;
1086 /* Don't propagate ssa names that occur in abnormal phis. */
1087 if ((TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
1088 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt
)))
1089 || (TREE_CODE (gimple_assign_rhs2 (stmt
)) == SSA_NAME
1090 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs2 (stmt
))))
1093 /* Do not un-cse comparisons. But propagate through copies. */
1094 use_stmt
= get_prop_dest_stmt (name
, &name
);
1096 || !is_gimple_assign (use_stmt
))
1099 code
= gimple_assign_rhs_code (use_stmt
);
1100 lhs
= gimple_assign_lhs (use_stmt
);
1101 if (!INTEGRAL_TYPE_P (TREE_TYPE (lhs
)))
1104 /* We can propagate the condition into a statement that
1105 computes the logical negation of the comparison result. */
1106 if ((code
== BIT_NOT_EXPR
1107 && TYPE_PRECISION (TREE_TYPE (lhs
)) == 1)
1108 || (code
== BIT_XOR_EXPR
1109 && integer_onep (gimple_assign_rhs2 (use_stmt
))))
1111 tree type
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
1112 bool nans
= HONOR_NANS (TYPE_MODE (type
));
1113 enum tree_code inv_code
;
1114 inv_code
= invert_tree_comparison (gimple_assign_rhs_code (stmt
), nans
);
1115 if (inv_code
== ERROR_MARK
)
1118 tmp
= build2 (inv_code
, TREE_TYPE (lhs
), gimple_assign_rhs1 (stmt
),
1119 gimple_assign_rhs2 (stmt
));
1124 gsi
= gsi_for_stmt (use_stmt
);
1125 gimple_assign_set_rhs_from_tree (&gsi
, unshare_expr (tmp
));
1126 use_stmt
= gsi_stmt (gsi
);
1127 update_stmt (use_stmt
);
1129 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1131 fprintf (dump_file
, " Replaced '");
1132 print_gimple_expr (dump_file
, stmt
, 0, dump_flags
);
1133 fprintf (dump_file
, "' with '");
1134 print_gimple_expr (dump_file
, use_stmt
, 0, dump_flags
);
1135 fprintf (dump_file
, "'\n");
1138 /* When we remove stmt now the iterator defgsi goes off it's current
1139 sequence, hence advance it now. */
1142 /* Remove defining statements. */
1143 return remove_prop_source_from_use (name
);
1151 /* If we have lhs = ~x (STMT), look and see if earlier we had x = ~y.
1152 If so, we can change STMT into lhs = y which can later be copy
1153 propagated. Similarly for negation.
1155 This could trivially be formulated as a forward propagation
1156 to immediate uses. However, we already had an implementation
1157 from DOM which used backward propagation via the use-def links.
1159 It turns out that backward propagation is actually faster as
1160 there's less work to do for each NOT/NEG expression we find.
1161 Backwards propagation needs to look at the statement in a single
1162 backlink. Forward propagation needs to look at potentially more
1163 than one forward link.
1165 Returns true when the statement was changed. */
1168 simplify_not_neg_expr (gimple_stmt_iterator
*gsi_p
)
1170 gimple stmt
= gsi_stmt (*gsi_p
);
1171 tree rhs
= gimple_assign_rhs1 (stmt
);
1172 gimple rhs_def_stmt
= SSA_NAME_DEF_STMT (rhs
);
1174 /* See if the RHS_DEF_STMT has the same form as our statement. */
1175 if (is_gimple_assign (rhs_def_stmt
)
1176 && gimple_assign_rhs_code (rhs_def_stmt
) == gimple_assign_rhs_code (stmt
))
1178 tree rhs_def_operand
= gimple_assign_rhs1 (rhs_def_stmt
);
1180 /* Verify that RHS_DEF_OPERAND is a suitable SSA_NAME. */
1181 if (TREE_CODE (rhs_def_operand
) == SSA_NAME
1182 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand
))
1184 gimple_assign_set_rhs_from_tree (gsi_p
, rhs_def_operand
);
1185 stmt
= gsi_stmt (*gsi_p
);
1194 /* Helper function for simplify_gimple_switch. Remove case labels that
1195 have values outside the range of the new type. */
1198 simplify_gimple_switch_label_vec (gimple stmt
, tree index_type
)
1200 unsigned int branch_num
= gimple_switch_num_labels (stmt
);
1201 VEC(tree
, heap
) *labels
= VEC_alloc (tree
, heap
, branch_num
);
1202 unsigned int i
, len
;
1204 /* Collect the existing case labels in a VEC, and preprocess it as if
1205 we are gimplifying a GENERIC SWITCH_EXPR. */
1206 for (i
= 1; i
< branch_num
; i
++)
1207 VEC_quick_push (tree
, labels
, gimple_switch_label (stmt
, i
));
1208 preprocess_case_label_vec_for_gimple (labels
, index_type
, NULL
);
1210 /* If any labels were removed, replace the existing case labels
1211 in the GIMPLE_SWITCH statement with the correct ones.
1212 Note that the type updates were done in-place on the case labels,
1213 so we only have to replace the case labels in the GIMPLE_SWITCH
1214 if the number of labels changed. */
1215 len
= VEC_length (tree
, labels
);
1216 if (len
< branch_num
- 1)
1218 bitmap target_blocks
;
1222 /* Corner case: *all* case labels have been removed as being
1223 out-of-range for INDEX_TYPE. Push one label and let the
1224 CFG cleanups deal with this further. */
1229 label
= CASE_LABEL (gimple_switch_default_label (stmt
));
1230 elt
= build_case_label (build_int_cst (index_type
, 0), NULL
, label
);
1231 VEC_quick_push (tree
, labels
, elt
);
1235 for (i
= 0; i
< VEC_length (tree
, labels
); i
++)
1236 gimple_switch_set_label (stmt
, i
+ 1, VEC_index (tree
, labels
, i
));
1237 for (i
++ ; i
< branch_num
; i
++)
1238 gimple_switch_set_label (stmt
, i
, NULL_TREE
);
1239 gimple_switch_set_num_labels (stmt
, len
+ 1);
1241 /* Cleanup any edges that are now dead. */
1242 target_blocks
= BITMAP_ALLOC (NULL
);
1243 for (i
= 0; i
< gimple_switch_num_labels (stmt
); i
++)
1245 tree elt
= gimple_switch_label (stmt
, i
);
1246 basic_block target
= label_to_block (CASE_LABEL (elt
));
1247 bitmap_set_bit (target_blocks
, target
->index
);
1249 for (ei
= ei_start (gimple_bb (stmt
)->succs
); (e
= ei_safe_edge (ei
)); )
1251 if (! bitmap_bit_p (target_blocks
, e
->dest
->index
))
1255 free_dominance_info (CDI_DOMINATORS
);
1260 BITMAP_FREE (target_blocks
);
1263 VEC_free (tree
, heap
, labels
);
1266 /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
1267 the condition which we may be able to optimize better. */
1270 simplify_gimple_switch (gimple stmt
)
1272 tree cond
= gimple_switch_index (stmt
);
1276 /* The optimization that we really care about is removing unnecessary
1277 casts. That will let us do much better in propagating the inferred
1278 constant at the switch target. */
1279 if (TREE_CODE (cond
) == SSA_NAME
)
1281 def_stmt
= SSA_NAME_DEF_STMT (cond
);
1282 if (is_gimple_assign (def_stmt
))
1284 if (gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
)
1289 def
= gimple_assign_rhs1 (def_stmt
);
1291 to
= TREE_TYPE (cond
);
1292 ti
= TREE_TYPE (def
);
1294 /* If we have an extension that preserves value, then we
1295 can copy the source value into the switch. */
1297 need_precision
= TYPE_PRECISION (ti
);
1299 if (! INTEGRAL_TYPE_P (ti
))
1301 else if (TYPE_UNSIGNED (to
) && !TYPE_UNSIGNED (ti
))
1303 else if (!TYPE_UNSIGNED (to
) && TYPE_UNSIGNED (ti
))
1304 need_precision
+= 1;
1305 if (TYPE_PRECISION (to
) < need_precision
)
1310 gimple_switch_set_index (stmt
, def
);
1311 simplify_gimple_switch_label_vec (stmt
, ti
);
1322 /* For pointers p2 and p1 return p2 - p1 if the
1323 difference is known and constant, otherwise return NULL. */
1326 constant_pointer_difference (tree p1
, tree p2
)
1329 #define CPD_ITERATIONS 5
1330 tree exps
[2][CPD_ITERATIONS
];
1331 tree offs
[2][CPD_ITERATIONS
];
1334 for (i
= 0; i
< 2; i
++)
1336 tree p
= i
? p1
: p2
;
1337 tree off
= size_zero_node
;
1339 enum tree_code code
;
1341 /* For each of p1 and p2 we need to iterate at least
1342 twice, to handle ADDR_EXPR directly in p1/p2,
1343 SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc.
1344 on definition's stmt RHS. Iterate a few extra times. */
1348 if (!POINTER_TYPE_P (TREE_TYPE (p
)))
1350 if (TREE_CODE (p
) == ADDR_EXPR
)
1352 tree q
= TREE_OPERAND (p
, 0);
1353 HOST_WIDE_INT offset
;
1354 tree base
= get_addr_base_and_unit_offset (q
, &offset
);
1359 off
= size_binop (PLUS_EXPR
, off
, size_int (offset
));
1361 if (TREE_CODE (q
) == MEM_REF
1362 && TREE_CODE (TREE_OPERAND (q
, 0)) == SSA_NAME
)
1364 p
= TREE_OPERAND (q
, 0);
1365 off
= size_binop (PLUS_EXPR
, off
,
1366 double_int_to_tree (sizetype
,
1367 mem_ref_offset (q
)));
1376 if (TREE_CODE (p
) != SSA_NAME
)
1380 if (j
== CPD_ITERATIONS
)
1382 stmt
= SSA_NAME_DEF_STMT (p
);
1383 if (!is_gimple_assign (stmt
) || gimple_assign_lhs (stmt
) != p
)
1385 code
= gimple_assign_rhs_code (stmt
);
1386 if (code
== POINTER_PLUS_EXPR
)
1388 if (TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
1390 off
= size_binop (PLUS_EXPR
, off
, gimple_assign_rhs2 (stmt
));
1391 p
= gimple_assign_rhs1 (stmt
);
1393 else if (code
== ADDR_EXPR
|| code
== NOP_EXPR
)
1394 p
= gimple_assign_rhs1 (stmt
);
1402 for (i
= 0; i
< cnt
[0]; i
++)
1403 for (j
= 0; j
< cnt
[1]; j
++)
1404 if (exps
[0][i
] == exps
[1][j
])
1405 return size_binop (MINUS_EXPR
, offs
[0][i
], offs
[1][j
]);
1410 /* *GSI_P is a GIMPLE_CALL to a builtin function.
1412 memcpy (p, "abcd", 4);
1413 memset (p + 4, ' ', 3);
1415 memcpy (p, "abcd ", 7);
1416 call if the latter can be stored by pieces during expansion. */
1419 simplify_builtin_call (gimple_stmt_iterator
*gsi_p
, tree callee2
)
1421 gimple stmt1
, stmt2
= gsi_stmt (*gsi_p
);
1422 tree vuse
= gimple_vuse (stmt2
);
1425 stmt1
= SSA_NAME_DEF_STMT (vuse
);
1427 switch (DECL_FUNCTION_CODE (callee2
))
1429 case BUILT_IN_MEMSET
:
1430 if (gimple_call_num_args (stmt2
) != 3
1431 || gimple_call_lhs (stmt2
)
1433 || BITS_PER_UNIT
!= 8)
1438 tree ptr1
, src1
, str1
, off1
, len1
, lhs1
;
1439 tree ptr2
= gimple_call_arg (stmt2
, 0);
1440 tree val2
= gimple_call_arg (stmt2
, 1);
1441 tree len2
= gimple_call_arg (stmt2
, 2);
1442 tree diff
, vdef
, new_str_cst
;
1444 unsigned int ptr1_align
;
1445 unsigned HOST_WIDE_INT src_len
;
1447 use_operand_p use_p
;
1449 if (!host_integerp (val2
, 0)
1450 || !host_integerp (len2
, 1))
1452 if (is_gimple_call (stmt1
))
1454 /* If first stmt is a call, it needs to be memcpy
1455 or mempcpy, with string literal as second argument and
1457 callee1
= gimple_call_fndecl (stmt1
);
1458 if (callee1
== NULL_TREE
1459 || DECL_BUILT_IN_CLASS (callee1
) != BUILT_IN_NORMAL
1460 || gimple_call_num_args (stmt1
) != 3)
1462 if (DECL_FUNCTION_CODE (callee1
) != BUILT_IN_MEMCPY
1463 && DECL_FUNCTION_CODE (callee1
) != BUILT_IN_MEMPCPY
)
1465 ptr1
= gimple_call_arg (stmt1
, 0);
1466 src1
= gimple_call_arg (stmt1
, 1);
1467 len1
= gimple_call_arg (stmt1
, 2);
1468 lhs1
= gimple_call_lhs (stmt1
);
1469 if (!host_integerp (len1
, 1))
1471 str1
= string_constant (src1
, &off1
);
1472 if (str1
== NULL_TREE
)
1474 if (!host_integerp (off1
, 1)
1475 || compare_tree_int (off1
, TREE_STRING_LENGTH (str1
) - 1) > 0
1476 || compare_tree_int (len1
, TREE_STRING_LENGTH (str1
)
1477 - tree_low_cst (off1
, 1)) > 0
1478 || TREE_CODE (TREE_TYPE (str1
)) != ARRAY_TYPE
1479 || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1
)))
1480 != TYPE_MODE (char_type_node
))
1483 else if (gimple_assign_single_p (stmt1
))
1485 /* Otherwise look for length 1 memcpy optimized into
1487 ptr1
= gimple_assign_lhs (stmt1
);
1488 src1
= gimple_assign_rhs1 (stmt1
);
1489 if (TREE_CODE (ptr1
) != MEM_REF
1490 || TYPE_MODE (TREE_TYPE (ptr1
)) != TYPE_MODE (char_type_node
)
1491 || !host_integerp (src1
, 0))
1493 ptr1
= build_fold_addr_expr (ptr1
);
1494 callee1
= NULL_TREE
;
1495 len1
= size_one_node
;
1497 off1
= size_zero_node
;
1503 diff
= constant_pointer_difference (ptr1
, ptr2
);
1504 if (diff
== NULL
&& lhs1
!= NULL
)
1506 diff
= constant_pointer_difference (lhs1
, ptr2
);
1507 if (DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
1509 diff
= size_binop (PLUS_EXPR
, diff
,
1510 fold_convert (sizetype
, len1
));
1512 /* If the difference between the second and first destination pointer
1513 is not constant, or is bigger than memcpy length, bail out. */
1515 || !host_integerp (diff
, 1)
1516 || tree_int_cst_lt (len1
, diff
))
1519 /* Use maximum of difference plus memset length and memcpy length
1520 as the new memcpy length, if it is too big, bail out. */
1521 src_len
= tree_low_cst (diff
, 1);
1522 src_len
+= tree_low_cst (len2
, 1);
1523 if (src_len
< (unsigned HOST_WIDE_INT
) tree_low_cst (len1
, 1))
1524 src_len
= tree_low_cst (len1
, 1);
1528 /* If mempcpy value is used elsewhere, bail out, as mempcpy
1529 with bigger length will return different result. */
1530 if (lhs1
!= NULL_TREE
1531 && DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
1532 && (TREE_CODE (lhs1
) != SSA_NAME
1533 || !single_imm_use (lhs1
, &use_p
, &use_stmt
)
1534 || use_stmt
!= stmt2
))
1537 /* If anything reads memory in between memcpy and memset
1538 call, the modified memcpy call might change it. */
1539 vdef
= gimple_vdef (stmt1
);
1541 && (!single_imm_use (vdef
, &use_p
, &use_stmt
)
1542 || use_stmt
!= stmt2
))
1545 ptr1_align
= get_pointer_alignment (ptr1
);
1546 /* Construct the new source string literal. */
1547 src_buf
= XALLOCAVEC (char, src_len
+ 1);
1550 TREE_STRING_POINTER (str1
) + tree_low_cst (off1
, 1),
1551 tree_low_cst (len1
, 1));
1553 src_buf
[0] = tree_low_cst (src1
, 0);
1554 memset (src_buf
+ tree_low_cst (diff
, 1),
1555 tree_low_cst (val2
, 0), tree_low_cst (len2
, 1));
1556 src_buf
[src_len
] = '\0';
1557 /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
1558 handle embedded '\0's. */
1559 if (strlen (src_buf
) != src_len
)
1561 rtl_profile_for_bb (gimple_bb (stmt2
));
1562 /* If the new memcpy wouldn't be emitted by storing the literal
1563 by pieces, this optimization might enlarge .rodata too much,
1564 as commonly used string literals couldn't be shared any
1566 if (!can_store_by_pieces (src_len
,
1567 builtin_strncpy_read_str
,
1568 src_buf
, ptr1_align
, false))
1571 new_str_cst
= build_string_literal (src_len
, src_buf
);
1574 /* If STMT1 is a mem{,p}cpy call, adjust it and remove
1576 if (lhs1
&& DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
)
1577 gimple_call_set_lhs (stmt1
, NULL_TREE
);
1578 gimple_call_set_arg (stmt1
, 1, new_str_cst
);
1579 gimple_call_set_arg (stmt1
, 2,
1580 build_int_cst (TREE_TYPE (len1
), src_len
));
1581 update_stmt (stmt1
);
1582 unlink_stmt_vdef (stmt2
);
1583 gsi_remove (gsi_p
, true);
1584 release_defs (stmt2
);
1585 if (lhs1
&& DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
)
1586 release_ssa_name (lhs1
);
1591 /* Otherwise, if STMT1 is length 1 memcpy optimized into
1592 assignment, remove STMT1 and change memset call into
1594 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt1
);
1596 if (!is_gimple_val (ptr1
))
1597 ptr1
= force_gimple_operand_gsi (gsi_p
, ptr1
, true, NULL_TREE
,
1598 true, GSI_SAME_STMT
);
1599 gimple_call_set_fndecl (stmt2
,
1600 builtin_decl_explicit (BUILT_IN_MEMCPY
));
1601 gimple_call_set_arg (stmt2
, 0, ptr1
);
1602 gimple_call_set_arg (stmt2
, 1, new_str_cst
);
1603 gimple_call_set_arg (stmt2
, 2,
1604 build_int_cst (TREE_TYPE (len2
), src_len
));
1605 unlink_stmt_vdef (stmt1
);
1606 gsi_remove (&gsi
, true);
1607 release_defs (stmt1
);
1608 update_stmt (stmt2
);
1619 /* Checks if expression has type of one-bit precision, or is a known
1620 truth-valued expression. */
1622 truth_valued_ssa_name (tree name
)
1625 tree type
= TREE_TYPE (name
);
1627 if (!INTEGRAL_TYPE_P (type
))
1629 /* Don't check here for BOOLEAN_TYPE as the precision isn't
1630 necessarily one and so ~X is not equal to !X. */
1631 if (TYPE_PRECISION (type
) == 1)
1633 def
= SSA_NAME_DEF_STMT (name
);
1634 if (is_gimple_assign (def
))
1635 return truth_value_p (gimple_assign_rhs_code (def
));
1639 /* Helper routine for simplify_bitwise_binary_1 function.
1640 Return for the SSA name NAME the expression X if it mets condition
1641 NAME = !X. Otherwise return NULL_TREE.
1642 Detected patterns for NAME = !X are:
1643 !X and X == 0 for X with integral type.
1644 X ^ 1, X != 1,or ~X for X with integral type with precision of one. */
1646 lookup_logical_inverted_value (tree name
)
1649 enum tree_code code
;
1652 /* If name has none-intergal type, or isn't a SSA_NAME, then
1654 if (TREE_CODE (name
) != SSA_NAME
1655 || !INTEGRAL_TYPE_P (TREE_TYPE (name
)))
1657 def
= SSA_NAME_DEF_STMT (name
);
1658 if (!is_gimple_assign (def
))
1661 code
= gimple_assign_rhs_code (def
);
1662 op1
= gimple_assign_rhs1 (def
);
1665 /* Get for EQ_EXPR or BIT_XOR_EXPR operation the second operand.
1666 If CODE isn't an EQ_EXPR, BIT_XOR_EXPR, or BIT_NOT_EXPR, then return. */
1667 if (code
== EQ_EXPR
|| code
== NE_EXPR
1668 || code
== BIT_XOR_EXPR
)
1669 op2
= gimple_assign_rhs2 (def
);
1674 if (truth_valued_ssa_name (name
))
1678 /* Check if we have X == 0 and X has an integral type. */
1679 if (!INTEGRAL_TYPE_P (TREE_TYPE (op1
)))
1681 if (integer_zerop (op2
))
1685 /* Check if we have X != 1 and X is a truth-valued. */
1686 if (!INTEGRAL_TYPE_P (TREE_TYPE (op1
)))
1688 if (integer_onep (op2
) && truth_valued_ssa_name (op1
))
1692 /* Check if we have X ^ 1 and X is truth valued. */
1693 if (integer_onep (op2
) && truth_valued_ssa_name (op1
))
1703 /* Optimize ARG1 CODE ARG2 to a constant for bitwise binary
1704 operations CODE, if one operand has the logically inverted
1705 value of the other. */
1707 simplify_bitwise_binary_1 (enum tree_code code
, tree type
,
1708 tree arg1
, tree arg2
)
1712 /* If CODE isn't a bitwise binary operation, return NULL_TREE. */
1713 if (code
!= BIT_AND_EXPR
&& code
!= BIT_IOR_EXPR
1714 && code
!= BIT_XOR_EXPR
)
1717 /* First check if operands ARG1 and ARG2 are equal. If so
1718 return NULL_TREE as this optimization is handled fold_stmt. */
1721 /* See if we have in arguments logical-not patterns. */
1722 if (((anot
= lookup_logical_inverted_value (arg1
)) == NULL_TREE
1724 && ((anot
= lookup_logical_inverted_value (arg2
)) == NULL_TREE
1729 if (code
== BIT_AND_EXPR
)
1730 return fold_convert (type
, integer_zero_node
);
1731 /* X | !X -> 1 and X ^ !X -> 1, if X is truth-valued. */
1732 if (truth_valued_ssa_name (anot
))
1733 return fold_convert (type
, integer_one_node
);
1735 /* ??? Otherwise result is (X != 0 ? X : 1). not handled. */
1739 /* Given a ssa_name in NAME see if it was defined by an assignment and
1740 set CODE to be the code and ARG1 to the first operand on the rhs and ARG2
1741 to the second operand on the rhs. */
1744 defcodefor_name (tree name
, enum tree_code
*code
, tree
*arg1
, tree
*arg2
)
1747 enum tree_code code1
;
1751 enum gimple_rhs_class grhs_class
;
1753 code1
= TREE_CODE (name
);
1756 grhs_class
= get_gimple_rhs_class (code1
);
1758 if (code1
== SSA_NAME
)
1760 def
= SSA_NAME_DEF_STMT (name
);
1762 if (def
&& is_gimple_assign (def
)
1763 && can_propagate_from (def
))
1765 code1
= gimple_assign_rhs_code (def
);
1766 arg11
= gimple_assign_rhs1 (def
);
1767 arg21
= gimple_assign_rhs2 (def
);
1768 arg31
= gimple_assign_rhs2 (def
);
1771 else if (grhs_class
== GIMPLE_TERNARY_RHS
1772 || GIMPLE_BINARY_RHS
1774 || GIMPLE_SINGLE_RHS
)
1775 extract_ops_from_tree_1 (name
, &code1
, &arg11
, &arg21
, &arg31
);
1781 /* Ignore arg3 currently. */
1784 /* Simplify bitwise binary operations.
1785 Return true if a transformation applied, otherwise return false. */
1788 simplify_bitwise_binary (gimple_stmt_iterator
*gsi
)
1790 gimple stmt
= gsi_stmt (*gsi
);
1791 tree arg1
= gimple_assign_rhs1 (stmt
);
1792 tree arg2
= gimple_assign_rhs2 (stmt
);
1793 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1795 tree def1_arg1
, def1_arg2
, def2_arg1
, def2_arg2
;
1796 enum tree_code def1_code
, def2_code
;
1798 defcodefor_name (arg1
, &def1_code
, &def1_arg1
, &def1_arg2
);
1799 defcodefor_name (arg2
, &def2_code
, &def2_arg1
, &def2_arg2
);
1801 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST)). */
1802 if (TREE_CODE (arg2
) == INTEGER_CST
1803 && CONVERT_EXPR_CODE_P (def1_code
)
1804 && INTEGRAL_TYPE_P (TREE_TYPE (def1_arg1
))
1805 && int_fits_type_p (arg2
, TREE_TYPE (def1_arg1
)))
1808 tree tem
= make_ssa_name (TREE_TYPE (def1_arg1
), NULL
);
1810 gimple_build_assign_with_ops (code
, tem
, def1_arg1
,
1811 fold_convert_loc (gimple_location (stmt
),
1812 TREE_TYPE (def1_arg1
),
1814 gimple_set_location (newop
, gimple_location (stmt
));
1815 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
1816 gimple_assign_set_rhs_with_ops_1 (gsi
, NOP_EXPR
,
1817 tem
, NULL_TREE
, NULL_TREE
);
1818 update_stmt (gsi_stmt (*gsi
));
1822 /* For bitwise binary operations apply operand conversions to the
1823 binary operation result instead of to the operands. This allows
1824 to combine successive conversions and bitwise binary operations. */
1825 if (CONVERT_EXPR_CODE_P (def1_code
)
1826 && CONVERT_EXPR_CODE_P (def2_code
)
1827 && types_compatible_p (TREE_TYPE (def1_arg1
), TREE_TYPE (def2_arg1
))
1828 /* Make sure that the conversion widens the operands, or has same
1829 precision, or that it changes the operation to a bitfield
1831 && ((TYPE_PRECISION (TREE_TYPE (def1_arg1
))
1832 <= TYPE_PRECISION (TREE_TYPE (arg1
)))
1833 || (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (arg1
)))
1835 || (TYPE_PRECISION (TREE_TYPE (arg1
))
1836 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg1
))))))
1839 tree tem
= make_ssa_name (TREE_TYPE (def1_arg1
), NULL
);
1840 newop
= gimple_build_assign_with_ops (code
, tem
, def1_arg1
, def2_arg1
);
1841 gimple_set_location (newop
, gimple_location (stmt
));
1842 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
1843 gimple_assign_set_rhs_with_ops_1 (gsi
, NOP_EXPR
,
1844 tem
, NULL_TREE
, NULL_TREE
);
1845 update_stmt (gsi_stmt (*gsi
));
1850 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
1851 if (def1_code
== def2_code
1852 && def1_code
== BIT_AND_EXPR
1853 && operand_equal_for_phi_arg_p (def1_arg2
,
1859 tree inner
= fold_build2 (code
, TREE_TYPE (arg2
), a
, c
);
1860 /* If A OP0 C (this usually means C is the same as A) is 0
1861 then fold it down correctly. */
1862 if (integer_zerop (inner
))
1864 gimple_assign_set_rhs_from_tree (gsi
, inner
);
1868 /* If A OP0 C (this usually means C is the same as A) is a ssa_name
1869 then fold it down correctly. */
1870 else if (TREE_CODE (inner
) == SSA_NAME
)
1872 tree outer
= fold_build2 (def1_code
, TREE_TYPE (inner
),
1874 gimple_assign_set_rhs_from_tree (gsi
, outer
);
1882 tem
= make_ssa_name (TREE_TYPE (arg2
), NULL
);
1883 newop
= gimple_build_assign_with_ops (code
, tem
, a
, c
);
1884 gimple_set_location (newop
, gimple_location (stmt
));
1885 /* Make sure to re-process the new stmt as it's walking upwards. */
1886 gsi_insert_before (gsi
, newop
, GSI_NEW_STMT
);
1887 gimple_assign_set_rhs1 (stmt
, tem
);
1888 gimple_assign_set_rhs2 (stmt
, b
);
1889 gimple_assign_set_rhs_code (stmt
, def1_code
);
1895 /* (a | CST1) & CST2 -> (a & CST2) | (CST1 & CST2). */
1896 if (code
== BIT_AND_EXPR
1897 && def1_code
== BIT_IOR_EXPR
1898 && TREE_CODE (arg2
) == INTEGER_CST
1899 && TREE_CODE (def1_arg2
) == INTEGER_CST
)
1901 tree cst
= fold_build2 (BIT_AND_EXPR
, TREE_TYPE (arg2
),
1905 if (integer_zerop (cst
))
1907 gimple_assign_set_rhs1 (stmt
, def1_arg1
);
1911 tem
= make_ssa_name (TREE_TYPE (arg2
), NULL
);
1912 newop
= gimple_build_assign_with_ops (BIT_AND_EXPR
,
1913 tem
, def1_arg1
, arg2
);
1914 gimple_set_location (newop
, gimple_location (stmt
));
1915 /* Make sure to re-process the new stmt as it's walking upwards. */
1916 gsi_insert_before (gsi
, newop
, GSI_NEW_STMT
);
1917 gimple_assign_set_rhs1 (stmt
, tem
);
1918 gimple_assign_set_rhs2 (stmt
, cst
);
1919 gimple_assign_set_rhs_code (stmt
, BIT_IOR_EXPR
);
1924 /* Combine successive equal operations with constants. */
1925 if ((code
== BIT_AND_EXPR
1926 || code
== BIT_IOR_EXPR
1927 || code
== BIT_XOR_EXPR
)
1928 && def1_code
== code
1929 && TREE_CODE (arg2
) == INTEGER_CST
1930 && TREE_CODE (def1_arg2
) == INTEGER_CST
)
1932 tree cst
= fold_build2 (code
, TREE_TYPE (arg2
),
1934 gimple_assign_set_rhs1 (stmt
, def1_arg1
);
1935 gimple_assign_set_rhs2 (stmt
, cst
);
1940 /* Canonicalize X ^ ~0 to ~X. */
1941 if (code
== BIT_XOR_EXPR
1942 && TREE_CODE (arg2
) == INTEGER_CST
1943 && integer_all_onesp (arg2
))
1945 gimple_assign_set_rhs_with_ops (gsi
, BIT_NOT_EXPR
, arg1
, NULL_TREE
);
1946 gcc_assert (gsi_stmt (*gsi
) == stmt
);
1951 /* Try simple folding for X op !X, and X op X. */
1952 res
= simplify_bitwise_binary_1 (code
, TREE_TYPE (arg1
), arg1
, arg2
);
1953 if (res
!= NULL_TREE
)
1955 gimple_assign_set_rhs_from_tree (gsi
, res
);
1956 update_stmt (gsi_stmt (*gsi
));
1960 if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
)
1962 enum tree_code ocode
= code
== BIT_AND_EXPR
? BIT_IOR_EXPR
: BIT_AND_EXPR
;
1963 if (def1_code
== ocode
)
1966 enum tree_code coden
;
1968 /* ( X | Y) & X -> X */
1969 /* ( X & Y) | X -> X */
1973 gimple_assign_set_rhs_from_tree (gsi
, x
);
1974 update_stmt (gsi_stmt (*gsi
));
1978 defcodefor_name (def1_arg1
, &coden
, &a1
, &a2
);
1979 /* (~X | Y) & X -> X & Y */
1980 /* (~X & Y) | X -> X | Y */
1981 if (coden
== BIT_NOT_EXPR
&& a1
== x
)
1983 gimple_assign_set_rhs_with_ops (gsi
, code
,
1985 gcc_assert (gsi_stmt (*gsi
) == stmt
);
1989 defcodefor_name (def1_arg2
, &coden
, &a1
, &a2
);
1990 /* (Y | ~X) & X -> X & Y */
1991 /* (Y & ~X) | X -> X | Y */
1992 if (coden
== BIT_NOT_EXPR
&& a1
== x
)
1994 gimple_assign_set_rhs_with_ops (gsi
, code
,
1996 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2001 if (def2_code
== ocode
)
2003 enum tree_code coden
;
2006 /* X & ( X | Y) -> X */
2007 /* X | ( X & Y) -> X */
2011 gimple_assign_set_rhs_from_tree (gsi
, x
);
2012 update_stmt (gsi_stmt (*gsi
));
2015 defcodefor_name (def2_arg1
, &coden
, &a1
, NULL
);
2016 /* (~X | Y) & X -> X & Y */
2017 /* (~X & Y) | X -> X | Y */
2018 if (coden
== BIT_NOT_EXPR
&& a1
== x
)
2020 gimple_assign_set_rhs_with_ops (gsi
, code
,
2022 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2026 defcodefor_name (def2_arg2
, &coden
, &a1
, NULL
);
2027 /* (Y | ~X) & X -> X & Y */
2028 /* (Y & ~X) | X -> X | Y */
2029 if (coden
== BIT_NOT_EXPR
&& a1
== x
)
2031 gimple_assign_set_rhs_with_ops (gsi
, code
,
2033 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2044 /* Perform re-associations of the plus or minus statement STMT that are
2045 always permitted. Returns true if the CFG was changed. */
2048 associate_plusminus (gimple_stmt_iterator
*gsi
)
2050 gimple stmt
= gsi_stmt (*gsi
);
2051 tree rhs1
= gimple_assign_rhs1 (stmt
);
2052 tree rhs2
= gimple_assign_rhs2 (stmt
);
2053 enum tree_code code
= gimple_assign_rhs_code (stmt
);
2056 /* We can't reassociate at all for saturating types. */
2057 if (TYPE_SATURATING (TREE_TYPE (rhs1
)))
2060 /* First contract negates. */
2065 /* A +- (-B) -> A -+ B. */
2066 if (TREE_CODE (rhs2
) == SSA_NAME
)
2068 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs2
);
2069 if (is_gimple_assign (def_stmt
)
2070 && gimple_assign_rhs_code (def_stmt
) == NEGATE_EXPR
2071 && can_propagate_from (def_stmt
))
2073 code
= (code
== MINUS_EXPR
) ? PLUS_EXPR
: MINUS_EXPR
;
2074 gimple_assign_set_rhs_code (stmt
, code
);
2075 rhs2
= gimple_assign_rhs1 (def_stmt
);
2076 gimple_assign_set_rhs2 (stmt
, rhs2
);
2077 gimple_set_modified (stmt
, true);
2082 /* (-A) + B -> B - A. */
2083 if (TREE_CODE (rhs1
) == SSA_NAME
2084 && code
== PLUS_EXPR
)
2086 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs1
);
2087 if (is_gimple_assign (def_stmt
)
2088 && gimple_assign_rhs_code (def_stmt
) == NEGATE_EXPR
2089 && can_propagate_from (def_stmt
))
2092 gimple_assign_set_rhs_code (stmt
, code
);
2094 gimple_assign_set_rhs1 (stmt
, rhs1
);
2095 rhs2
= gimple_assign_rhs1 (def_stmt
);
2096 gimple_assign_set_rhs2 (stmt
, rhs2
);
2097 gimple_set_modified (stmt
, true);
2104 /* We can't reassociate floating-point or fixed-point plus or minus
2105 because of saturation to +-Inf. */
2106 if (FLOAT_TYPE_P (TREE_TYPE (rhs1
))
2107 || FIXED_POINT_TYPE_P (TREE_TYPE (rhs1
)))
2110 /* Second match patterns that allow contracting a plus-minus pair
2111 irrespective of overflow issues.
2113 (A +- B) - A -> +- B
2115 (CST +- A) +- CST -> CST +- A
2116 (A + CST) +- CST -> A + CST
2119 A - (A +- B) -> -+ B
2120 A +- (B +- A) -> +- B
2121 CST +- (CST +- A) -> CST +- A
2122 CST +- (A +- CST) -> CST +- A
2125 via commutating the addition and contracting operations to zero
2126 by reassociation. */
2128 if (TREE_CODE (rhs1
) == SSA_NAME
)
2130 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs1
);
2131 if (is_gimple_assign (def_stmt
) && can_propagate_from (def_stmt
))
2133 enum tree_code def_code
= gimple_assign_rhs_code (def_stmt
);
2134 if (def_code
== PLUS_EXPR
2135 || def_code
== MINUS_EXPR
)
2137 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2138 tree def_rhs2
= gimple_assign_rhs2 (def_stmt
);
2139 if (operand_equal_p (def_rhs1
, rhs2
, 0)
2140 && code
== MINUS_EXPR
)
2142 /* (A +- B) - A -> +- B. */
2143 code
= ((def_code
== PLUS_EXPR
)
2144 ? TREE_CODE (def_rhs2
) : NEGATE_EXPR
);
2147 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2148 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2149 gimple_set_modified (stmt
, true);
2151 else if (operand_equal_p (def_rhs2
, rhs2
, 0)
2152 && code
!= def_code
)
2154 /* (A +- B) -+ B -> A. */
2155 code
= TREE_CODE (def_rhs1
);
2158 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2159 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2160 gimple_set_modified (stmt
, true);
2162 else if (TREE_CODE (rhs2
) == INTEGER_CST
2163 && TREE_CODE (def_rhs1
) == INTEGER_CST
)
2165 /* (CST +- A) +- CST -> CST +- A. */
2166 tree cst
= fold_binary (code
, TREE_TYPE (rhs1
),
2168 if (cst
&& !TREE_OVERFLOW (cst
))
2171 gimple_assign_set_rhs_code (stmt
, code
);
2173 gimple_assign_set_rhs1 (stmt
, rhs1
);
2175 gimple_assign_set_rhs2 (stmt
, rhs2
);
2176 gimple_set_modified (stmt
, true);
2179 else if (TREE_CODE (rhs2
) == INTEGER_CST
2180 && TREE_CODE (def_rhs2
) == INTEGER_CST
2181 && def_code
== PLUS_EXPR
)
2183 /* (A + CST) +- CST -> A + CST. */
2184 tree cst
= fold_binary (code
, TREE_TYPE (rhs1
),
2186 if (cst
&& !TREE_OVERFLOW (cst
))
2189 gimple_assign_set_rhs_code (stmt
, code
);
2191 gimple_assign_set_rhs1 (stmt
, rhs1
);
2193 gimple_assign_set_rhs2 (stmt
, rhs2
);
2194 gimple_set_modified (stmt
, true);
2198 else if (def_code
== BIT_NOT_EXPR
2199 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
2201 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2202 if (code
== PLUS_EXPR
2203 && operand_equal_p (def_rhs1
, rhs2
, 0))
2207 rhs1
= build_int_cst_type (TREE_TYPE (rhs2
), -1);
2209 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2210 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2211 gimple_set_modified (stmt
, true);
2213 else if (code
== PLUS_EXPR
2214 && integer_onep (rhs1
))
2220 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2221 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2222 gimple_set_modified (stmt
, true);
2228 if (rhs2
&& TREE_CODE (rhs2
) == SSA_NAME
)
2230 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs2
);
2231 if (is_gimple_assign (def_stmt
) && can_propagate_from (def_stmt
))
2233 enum tree_code def_code
= gimple_assign_rhs_code (def_stmt
);
2234 if (def_code
== PLUS_EXPR
2235 || def_code
== MINUS_EXPR
)
2237 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2238 tree def_rhs2
= gimple_assign_rhs2 (def_stmt
);
2239 if (operand_equal_p (def_rhs1
, rhs1
, 0)
2240 && code
== MINUS_EXPR
)
2242 /* A - (A +- B) -> -+ B. */
2243 code
= ((def_code
== PLUS_EXPR
)
2244 ? NEGATE_EXPR
: TREE_CODE (def_rhs2
));
2247 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2248 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2249 gimple_set_modified (stmt
, true);
2251 else if (operand_equal_p (def_rhs2
, rhs1
, 0)
2252 && code
!= def_code
)
2254 /* A +- (B +- A) -> +- B. */
2255 code
= ((code
== PLUS_EXPR
)
2256 ? TREE_CODE (def_rhs1
) : NEGATE_EXPR
);
2259 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2260 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2261 gimple_set_modified (stmt
, true);
2263 else if (TREE_CODE (rhs1
) == INTEGER_CST
2264 && TREE_CODE (def_rhs1
) == INTEGER_CST
)
2266 /* CST +- (CST +- A) -> CST +- A. */
2267 tree cst
= fold_binary (code
, TREE_TYPE (rhs2
),
2269 if (cst
&& !TREE_OVERFLOW (cst
))
2271 code
= (code
== def_code
? PLUS_EXPR
: MINUS_EXPR
);
2272 gimple_assign_set_rhs_code (stmt
, code
);
2274 gimple_assign_set_rhs1 (stmt
, rhs1
);
2276 gimple_assign_set_rhs2 (stmt
, rhs2
);
2277 gimple_set_modified (stmt
, true);
2280 else if (TREE_CODE (rhs1
) == INTEGER_CST
2281 && TREE_CODE (def_rhs2
) == INTEGER_CST
)
2283 /* CST +- (A +- CST) -> CST +- A. */
2284 tree cst
= fold_binary (def_code
== code
2285 ? PLUS_EXPR
: MINUS_EXPR
,
2288 if (cst
&& !TREE_OVERFLOW (cst
))
2291 gimple_assign_set_rhs1 (stmt
, rhs1
);
2293 gimple_assign_set_rhs2 (stmt
, rhs2
);
2294 gimple_set_modified (stmt
, true);
2298 else if (def_code
== BIT_NOT_EXPR
2299 && INTEGRAL_TYPE_P (TREE_TYPE (rhs2
)))
2301 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2302 if (code
== PLUS_EXPR
2303 && operand_equal_p (def_rhs1
, rhs1
, 0))
2307 rhs1
= build_int_cst_type (TREE_TYPE (rhs1
), -1);
2309 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2310 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2311 gimple_set_modified (stmt
, true);
2318 if (gimple_modified_p (stmt
))
2320 fold_stmt_inplace (gsi
);
2322 if (maybe_clean_or_replace_eh_stmt (stmt
, stmt
)
2323 && gimple_purge_dead_eh_edges (gimple_bb (stmt
)))
2330 /* Associate operands of a POINTER_PLUS_EXPR assignmen at *GSI. Returns
2331 true if anything changed, false otherwise. */
2334 associate_pointerplus (gimple_stmt_iterator
*gsi
)
2336 gimple stmt
= gsi_stmt (*gsi
);
2338 tree ptr
, rhs
, algn
;
2341 tem = (sizetype) ptr;
2345 and produce the simpler and easier to analyze with respect to alignment
2346 ... = ptr & ~algn; */
2347 ptr
= gimple_assign_rhs1 (stmt
);
2348 rhs
= gimple_assign_rhs2 (stmt
);
2349 if (TREE_CODE (rhs
) != SSA_NAME
)
2351 def_stmt
= SSA_NAME_DEF_STMT (rhs
);
2352 if (!is_gimple_assign (def_stmt
)
2353 || gimple_assign_rhs_code (def_stmt
) != NEGATE_EXPR
)
2355 rhs
= gimple_assign_rhs1 (def_stmt
);
2356 if (TREE_CODE (rhs
) != SSA_NAME
)
2358 def_stmt
= SSA_NAME_DEF_STMT (rhs
);
2359 if (!is_gimple_assign (def_stmt
)
2360 || gimple_assign_rhs_code (def_stmt
) != BIT_AND_EXPR
)
2362 rhs
= gimple_assign_rhs1 (def_stmt
);
2363 algn
= gimple_assign_rhs2 (def_stmt
);
2364 if (TREE_CODE (rhs
) != SSA_NAME
2365 || TREE_CODE (algn
) != INTEGER_CST
)
2367 def_stmt
= SSA_NAME_DEF_STMT (rhs
);
2368 if (!is_gimple_assign (def_stmt
)
2369 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
2371 if (gimple_assign_rhs1 (def_stmt
) != ptr
)
2374 algn
= double_int_to_tree (TREE_TYPE (ptr
), ~tree_to_double_int (algn
));
2375 gimple_assign_set_rhs_with_ops (gsi
, BIT_AND_EXPR
, ptr
, algn
);
2376 fold_stmt_inplace (gsi
);
2382 /* Combine two conversions in a row for the second conversion at *GSI.
2383 Returns 1 if there were any changes made, 2 if cfg-cleanup needs to
2384 run. Else it returns 0. */
2387 combine_conversions (gimple_stmt_iterator
*gsi
)
2389 gimple stmt
= gsi_stmt (*gsi
);
2392 enum tree_code code
= gimple_assign_rhs_code (stmt
);
2393 enum tree_code code2
;
2395 gcc_checking_assert (CONVERT_EXPR_CODE_P (code
)
2396 || code
== FLOAT_EXPR
2397 || code
== FIX_TRUNC_EXPR
);
2399 lhs
= gimple_assign_lhs (stmt
);
2400 op0
= gimple_assign_rhs1 (stmt
);
2401 if (useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
)))
2403 gimple_assign_set_rhs_code (stmt
, TREE_CODE (op0
));
2407 if (TREE_CODE (op0
) != SSA_NAME
)
2410 def_stmt
= SSA_NAME_DEF_STMT (op0
);
2411 if (!is_gimple_assign (def_stmt
))
2414 code2
= gimple_assign_rhs_code (def_stmt
);
2416 if (CONVERT_EXPR_CODE_P (code2
) || code2
== FLOAT_EXPR
)
2418 tree defop0
= gimple_assign_rhs1 (def_stmt
);
2419 tree type
= TREE_TYPE (lhs
);
2420 tree inside_type
= TREE_TYPE (defop0
);
2421 tree inter_type
= TREE_TYPE (op0
);
2422 int inside_int
= INTEGRAL_TYPE_P (inside_type
);
2423 int inside_ptr
= POINTER_TYPE_P (inside_type
);
2424 int inside_float
= FLOAT_TYPE_P (inside_type
);
2425 int inside_vec
= TREE_CODE (inside_type
) == VECTOR_TYPE
;
2426 unsigned int inside_prec
= TYPE_PRECISION (inside_type
);
2427 int inside_unsignedp
= TYPE_UNSIGNED (inside_type
);
2428 int inter_int
= INTEGRAL_TYPE_P (inter_type
);
2429 int inter_ptr
= POINTER_TYPE_P (inter_type
);
2430 int inter_float
= FLOAT_TYPE_P (inter_type
);
2431 int inter_vec
= TREE_CODE (inter_type
) == VECTOR_TYPE
;
2432 unsigned int inter_prec
= TYPE_PRECISION (inter_type
);
2433 int inter_unsignedp
= TYPE_UNSIGNED (inter_type
);
2434 int final_int
= INTEGRAL_TYPE_P (type
);
2435 int final_ptr
= POINTER_TYPE_P (type
);
2436 int final_float
= FLOAT_TYPE_P (type
);
2437 int final_vec
= TREE_CODE (type
) == VECTOR_TYPE
;
2438 unsigned int final_prec
= TYPE_PRECISION (type
);
2439 int final_unsignedp
= TYPE_UNSIGNED (type
);
2441 /* Don't propagate ssa names that occur in abnormal phis. */
2442 if (TREE_CODE (defop0
) == SSA_NAME
2443 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (defop0
))
2446 /* In addition to the cases of two conversions in a row
2447 handled below, if we are converting something to its own
2448 type via an object of identical or wider precision, neither
2449 conversion is needed. */
2450 if (useless_type_conversion_p (type
, inside_type
)
2451 && (((inter_int
|| inter_ptr
) && final_int
)
2452 || (inter_float
&& final_float
))
2453 && inter_prec
>= final_prec
)
2455 gimple_assign_set_rhs1 (stmt
, unshare_expr (defop0
));
2456 gimple_assign_set_rhs_code (stmt
, TREE_CODE (defop0
));
2458 return remove_prop_source_from_use (op0
) ? 2 : 1;
2461 /* Likewise, if the intermediate and initial types are either both
2462 float or both integer, we don't need the middle conversion if the
2463 former is wider than the latter and doesn't change the signedness
2464 (for integers). Avoid this if the final type is a pointer since
2465 then we sometimes need the middle conversion. Likewise if the
2466 final type has a precision not equal to the size of its mode. */
2467 if (((inter_int
&& inside_int
)
2468 || (inter_float
&& inside_float
)
2469 || (inter_vec
&& inside_vec
))
2470 && inter_prec
>= inside_prec
2471 && (inter_float
|| inter_vec
2472 || inter_unsignedp
== inside_unsignedp
)
2473 && ! (final_prec
!= GET_MODE_PRECISION (TYPE_MODE (type
))
2474 && TYPE_MODE (type
) == TYPE_MODE (inter_type
))
2476 && (! final_vec
|| inter_prec
== inside_prec
))
2478 gimple_assign_set_rhs1 (stmt
, defop0
);
2480 return remove_prop_source_from_use (op0
) ? 2 : 1;
2483 /* If we have a sign-extension of a zero-extended value, we can
2484 replace that by a single zero-extension. Likewise if the
2485 final conversion does not change precision we can drop the
2486 intermediate conversion. */
2487 if (inside_int
&& inter_int
&& final_int
2488 && ((inside_prec
< inter_prec
&& inter_prec
< final_prec
2489 && inside_unsignedp
&& !inter_unsignedp
)
2490 || final_prec
== inter_prec
))
2492 gimple_assign_set_rhs1 (stmt
, defop0
);
2494 return remove_prop_source_from_use (op0
) ? 2 : 1;
2497 /* Two conversions in a row are not needed unless:
2498 - some conversion is floating-point (overstrict for now), or
2499 - some conversion is a vector (overstrict for now), or
2500 - the intermediate type is narrower than both initial and
2502 - the intermediate type and innermost type differ in signedness,
2503 and the outermost type is wider than the intermediate, or
2504 - the initial type is a pointer type and the precisions of the
2505 intermediate and final types differ, or
2506 - the final type is a pointer type and the precisions of the
2507 initial and intermediate types differ. */
2508 if (! inside_float
&& ! inter_float
&& ! final_float
2509 && ! inside_vec
&& ! inter_vec
&& ! final_vec
2510 && (inter_prec
>= inside_prec
|| inter_prec
>= final_prec
)
2511 && ! (inside_int
&& inter_int
2512 && inter_unsignedp
!= inside_unsignedp
2513 && inter_prec
< final_prec
)
2514 && ((inter_unsignedp
&& inter_prec
> inside_prec
)
2515 == (final_unsignedp
&& final_prec
> inter_prec
))
2516 && ! (inside_ptr
&& inter_prec
!= final_prec
)
2517 && ! (final_ptr
&& inside_prec
!= inter_prec
)
2518 && ! (final_prec
!= GET_MODE_PRECISION (TYPE_MODE (type
))
2519 && TYPE_MODE (type
) == TYPE_MODE (inter_type
)))
2521 gimple_assign_set_rhs1 (stmt
, defop0
);
2523 return remove_prop_source_from_use (op0
) ? 2 : 1;
2526 /* A truncation to an unsigned type should be canonicalized as
2527 bitwise and of a mask. */
2528 if (final_int
&& inter_int
&& inside_int
2529 && final_prec
== inside_prec
2530 && final_prec
> inter_prec
2534 tem
= fold_build2 (BIT_AND_EXPR
, inside_type
,
2537 (inside_type
, double_int::mask (inter_prec
)));
2538 if (!useless_type_conversion_p (type
, inside_type
))
2540 tem
= force_gimple_operand_gsi (gsi
, tem
, true, NULL_TREE
, true,
2542 gimple_assign_set_rhs1 (stmt
, tem
);
2545 gimple_assign_set_rhs_from_tree (gsi
, tem
);
2546 update_stmt (gsi_stmt (*gsi
));
2550 /* If we are converting an integer to a floating-point that can
2551 represent it exactly and back to an integer, we can skip the
2552 floating-point conversion. */
2553 if (inside_int
&& inter_float
&& final_int
&&
2554 (unsigned) significand_size (TYPE_MODE (inter_type
))
2555 >= inside_prec
- !inside_unsignedp
)
2557 if (useless_type_conversion_p (type
, inside_type
))
2559 gimple_assign_set_rhs1 (stmt
, unshare_expr (defop0
));
2560 gimple_assign_set_rhs_code (stmt
, TREE_CODE (defop0
));
2562 return remove_prop_source_from_use (op0
) ? 2 : 1;
2566 gimple_assign_set_rhs1 (stmt
, defop0
);
2567 gimple_assign_set_rhs_code (stmt
, CONVERT_EXPR
);
2569 return remove_prop_source_from_use (op0
) ? 2 : 1;
2577 /* Combine an element access with a shuffle. Returns true if there were
2578 any changes made, else it returns false. */
2581 simplify_bitfield_ref (gimple_stmt_iterator
*gsi
)
2583 gimple stmt
= gsi_stmt (*gsi
);
2585 tree op
, op0
, op1
, op2
;
2587 unsigned idx
, n
, size
;
2588 enum tree_code code
;
2590 op
= gimple_assign_rhs1 (stmt
);
2591 gcc_checking_assert (TREE_CODE (op
) == BIT_FIELD_REF
);
2593 op0
= TREE_OPERAND (op
, 0);
2594 if (TREE_CODE (op0
) != SSA_NAME
2595 || TREE_CODE (TREE_TYPE (op0
)) != VECTOR_TYPE
)
2598 elem_type
= TREE_TYPE (TREE_TYPE (op0
));
2599 if (TREE_TYPE (op
) != elem_type
)
2602 size
= TREE_INT_CST_LOW (TYPE_SIZE (elem_type
));
2603 op1
= TREE_OPERAND (op
, 1);
2604 n
= TREE_INT_CST_LOW (op1
) / size
;
2608 def_stmt
= SSA_NAME_DEF_STMT (op0
);
2609 if (!def_stmt
|| !is_gimple_assign (def_stmt
)
2610 || !can_propagate_from (def_stmt
))
2613 op2
= TREE_OPERAND (op
, 2);
2614 idx
= TREE_INT_CST_LOW (op2
) / size
;
2616 code
= gimple_assign_rhs_code (def_stmt
);
2618 if (code
== VEC_PERM_EXPR
)
2620 tree p
, m
, index
, tem
;
2622 m
= gimple_assign_rhs3 (def_stmt
);
2623 if (TREE_CODE (m
) != VECTOR_CST
)
2625 nelts
= VECTOR_CST_NELTS (m
);
2626 idx
= TREE_INT_CST_LOW (VECTOR_CST_ELT (m
, idx
));
2630 p
= gimple_assign_rhs1 (def_stmt
);
2634 p
= gimple_assign_rhs2 (def_stmt
);
2637 index
= build_int_cst (TREE_TYPE (TREE_TYPE (m
)), idx
* size
);
2638 tem
= build3 (BIT_FIELD_REF
, TREE_TYPE (op
),
2639 unshare_expr (p
), op1
, index
);
2640 gimple_assign_set_rhs1 (stmt
, tem
);
2642 update_stmt (gsi_stmt (*gsi
));
2649 /* Determine whether applying the 2 permutations (mask1 then mask2)
2650 gives back one of the input. */
2653 is_combined_permutation_identity (tree mask1
, tree mask2
)
2656 unsigned int nelts
, i
, j
;
2657 bool maybe_identity1
= true;
2658 bool maybe_identity2
= true;
2660 gcc_checking_assert (TREE_CODE (mask1
) == VECTOR_CST
2661 && TREE_CODE (mask2
) == VECTOR_CST
);
2662 mask
= fold_ternary (VEC_PERM_EXPR
, TREE_TYPE (mask1
), mask1
, mask1
, mask2
);
2663 gcc_assert (TREE_CODE (mask
) == VECTOR_CST
);
2665 nelts
= VECTOR_CST_NELTS (mask
);
2666 for (i
= 0; i
< nelts
; i
++)
2668 tree val
= VECTOR_CST_ELT (mask
, i
);
2669 gcc_assert (TREE_CODE (val
) == INTEGER_CST
);
2670 j
= TREE_INT_CST_LOW (val
) & (2 * nelts
- 1);
2672 maybe_identity2
= false;
2673 else if (j
== i
+ nelts
)
2674 maybe_identity1
= false;
2678 return maybe_identity1
? 1 : maybe_identity2
? 2 : 0;
2681 /* Combine a shuffle with its arguments. Returns 1 if there were any
2682 changes made, 2 if cfg-cleanup needs to run. Else it returns 0. */
2685 simplify_permutation (gimple_stmt_iterator
*gsi
)
2687 gimple stmt
= gsi_stmt (*gsi
);
2689 tree op0
, op1
, op2
, op3
, arg0
, arg1
;
2690 enum tree_code code
;
2692 gcc_checking_assert (gimple_assign_rhs_code (stmt
) == VEC_PERM_EXPR
);
2694 op0
= gimple_assign_rhs1 (stmt
);
2695 op1
= gimple_assign_rhs2 (stmt
);
2696 op2
= gimple_assign_rhs3 (stmt
);
2698 if (TREE_CODE (op2
) != VECTOR_CST
)
2701 if (TREE_CODE (op0
) == VECTOR_CST
)
2706 else if (TREE_CODE (op0
) == SSA_NAME
)
2708 def_stmt
= SSA_NAME_DEF_STMT (op0
);
2709 if (!def_stmt
|| !is_gimple_assign (def_stmt
)
2710 || !can_propagate_from (def_stmt
))
2713 code
= gimple_assign_rhs_code (def_stmt
);
2714 arg0
= gimple_assign_rhs1 (def_stmt
);
2719 /* Two consecutive shuffles. */
2720 if (code
== VEC_PERM_EXPR
)
2727 op3
= gimple_assign_rhs3 (def_stmt
);
2728 if (TREE_CODE (op3
) != VECTOR_CST
)
2730 ident
= is_combined_permutation_identity (op3
, op2
);
2733 orig
= (ident
== 1) ? gimple_assign_rhs1 (def_stmt
)
2734 : gimple_assign_rhs2 (def_stmt
);
2735 gimple_assign_set_rhs1 (stmt
, unshare_expr (orig
));
2736 gimple_assign_set_rhs_code (stmt
, TREE_CODE (orig
));
2737 gimple_set_num_ops (stmt
, 2);
2739 return remove_prop_source_from_use (op0
) ? 2 : 1;
2742 /* Shuffle of a constructor. */
2743 else if (code
== CONSTRUCTOR
|| code
== VECTOR_CST
)
2749 if (TREE_CODE (op0
) == SSA_NAME
&& !has_single_use (op0
))
2752 if (TREE_CODE (op1
) == VECTOR_CST
)
2754 else if (TREE_CODE (op1
) == SSA_NAME
)
2756 enum tree_code code2
;
2758 if (!has_single_use (op1
))
2761 gimple def_stmt2
= SSA_NAME_DEF_STMT (op1
);
2762 if (!def_stmt2
|| !is_gimple_assign (def_stmt2
)
2763 || !can_propagate_from (def_stmt2
))
2766 code2
= gimple_assign_rhs_code (def_stmt2
);
2767 if (code2
!= CONSTRUCTOR
&& code2
!= VECTOR_CST
)
2769 arg1
= gimple_assign_rhs1 (def_stmt2
);
2776 /* Already used twice in this statement. */
2777 if (TREE_CODE (op0
) == SSA_NAME
&& num_imm_uses (op0
) > 2)
2781 opt
= fold_ternary (VEC_PERM_EXPR
, TREE_TYPE(op0
), arg0
, arg1
, op2
);
2783 || (TREE_CODE (opt
) != CONSTRUCTOR
&& TREE_CODE(opt
) != VECTOR_CST
))
2785 gimple_assign_set_rhs_from_tree (gsi
, opt
);
2786 update_stmt (gsi_stmt (*gsi
));
2787 if (TREE_CODE (op0
) == SSA_NAME
)
2788 ret
= remove_prop_source_from_use (op0
);
2789 if (op0
!= op1
&& TREE_CODE (op1
) == SSA_NAME
)
2790 ret
|= remove_prop_source_from_use (op1
);
2797 /* Main entry point for the forward propagation and statement combine
2801 ssa_forward_propagate_and_combine (void)
2804 unsigned int todoflags
= 0;
2806 cfg_changed
= false;
2810 gimple_stmt_iterator gsi
;
2812 /* Apply forward propagation to all stmts in the basic-block.
2813 Note we update GSI within the loop as necessary. */
2814 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
2816 gimple stmt
= gsi_stmt (gsi
);
2818 enum tree_code code
;
2820 if (!is_gimple_assign (stmt
))
2826 lhs
= gimple_assign_lhs (stmt
);
2827 rhs
= gimple_assign_rhs1 (stmt
);
2828 code
= gimple_assign_rhs_code (stmt
);
2829 if (TREE_CODE (lhs
) != SSA_NAME
2830 || has_zero_uses (lhs
))
2836 /* If this statement sets an SSA_NAME to an address,
2837 try to propagate the address into the uses of the SSA_NAME. */
2838 if (code
== ADDR_EXPR
2839 /* Handle pointer conversions on invariant addresses
2840 as well, as this is valid gimple. */
2841 || (CONVERT_EXPR_CODE_P (code
)
2842 && TREE_CODE (rhs
) == ADDR_EXPR
2843 && POINTER_TYPE_P (TREE_TYPE (lhs
))))
2845 tree base
= get_base_address (TREE_OPERAND (rhs
, 0));
2848 || decl_address_invariant_p (base
))
2849 && !stmt_references_abnormal_ssa_name (stmt
)
2850 && forward_propagate_addr_expr (lhs
, rhs
))
2852 release_defs (stmt
);
2853 todoflags
|= TODO_remove_unused_locals
;
2854 gsi_remove (&gsi
, true);
2859 else if (code
== POINTER_PLUS_EXPR
)
2861 tree off
= gimple_assign_rhs2 (stmt
);
2862 if (TREE_CODE (off
) == INTEGER_CST
2863 && can_propagate_from (stmt
)
2864 && !simple_iv_increment_p (stmt
)
2865 /* ??? Better adjust the interface to that function
2866 instead of building new trees here. */
2867 && forward_propagate_addr_expr
2869 build1_loc (gimple_location (stmt
),
2870 ADDR_EXPR
, TREE_TYPE (rhs
),
2871 fold_build2 (MEM_REF
,
2872 TREE_TYPE (TREE_TYPE (rhs
)),
2874 fold_convert (ptr_type_node
,
2877 release_defs (stmt
);
2878 todoflags
|= TODO_remove_unused_locals
;
2879 gsi_remove (&gsi
, true);
2881 else if (is_gimple_min_invariant (rhs
))
2883 /* Make sure to fold &a[0] + off_1 here. */
2884 fold_stmt_inplace (&gsi
);
2886 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
2892 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
2894 if (forward_propagate_comparison (&gsi
))
2901 /* Combine stmts with the stmts defining their operands.
2902 Note we update GSI within the loop as necessary. */
2903 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);)
2905 gimple stmt
= gsi_stmt (gsi
);
2906 bool changed
= false;
2908 /* Mark stmt as potentially needing revisiting. */
2909 gimple_set_plf (stmt
, GF_PLF_1
, false);
2911 switch (gimple_code (stmt
))
2915 tree rhs1
= gimple_assign_rhs1 (stmt
);
2916 enum tree_code code
= gimple_assign_rhs_code (stmt
);
2918 if ((code
== BIT_NOT_EXPR
2919 || code
== NEGATE_EXPR
)
2920 && TREE_CODE (rhs1
) == SSA_NAME
)
2921 changed
= simplify_not_neg_expr (&gsi
);
2922 else if (code
== COND_EXPR
2923 || code
== VEC_COND_EXPR
)
2925 /* In this case the entire COND_EXPR is in rhs1. */
2926 if (forward_propagate_into_cond (&gsi
)
2927 || combine_cond_exprs (&gsi
))
2930 stmt
= gsi_stmt (gsi
);
2933 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
2936 did_something
= forward_propagate_into_comparison (&gsi
);
2937 if (did_something
== 2)
2939 changed
= did_something
!= 0;
2941 else if (code
== BIT_AND_EXPR
2942 || code
== BIT_IOR_EXPR
2943 || code
== BIT_XOR_EXPR
)
2944 changed
= simplify_bitwise_binary (&gsi
);
2945 else if (code
== PLUS_EXPR
2946 || code
== MINUS_EXPR
)
2947 changed
= associate_plusminus (&gsi
);
2948 else if (code
== POINTER_PLUS_EXPR
)
2949 changed
= associate_pointerplus (&gsi
);
2950 else if (CONVERT_EXPR_CODE_P (code
)
2951 || code
== FLOAT_EXPR
2952 || code
== FIX_TRUNC_EXPR
)
2954 int did_something
= combine_conversions (&gsi
);
2955 if (did_something
== 2)
2957 changed
= did_something
!= 0;
2959 else if (code
== VEC_PERM_EXPR
)
2961 int did_something
= simplify_permutation (&gsi
);
2962 if (did_something
== 2)
2964 changed
= did_something
!= 0;
2966 else if (code
== BIT_FIELD_REF
)
2967 changed
= simplify_bitfield_ref (&gsi
);
2972 changed
= simplify_gimple_switch (stmt
);
2978 did_something
= forward_propagate_into_gimple_cond (stmt
);
2979 if (did_something
== 2)
2981 changed
= did_something
!= 0;
2987 tree callee
= gimple_call_fndecl (stmt
);
2988 if (callee
!= NULL_TREE
2989 && DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
2990 changed
= simplify_builtin_call (&gsi
, callee
);
2999 /* If the stmt changed then re-visit it and the statements
3000 inserted before it. */
3001 for (; !gsi_end_p (gsi
); gsi_prev (&gsi
))
3002 if (gimple_plf (gsi_stmt (gsi
), GF_PLF_1
))
3004 if (gsi_end_p (gsi
))
3005 gsi
= gsi_start_bb (bb
);
3011 /* Stmt no longer needs to be revisited. */
3012 gimple_set_plf (stmt
, GF_PLF_1
, true);
3019 todoflags
|= TODO_cleanup_cfg
;
3026 gate_forwprop (void)
3028 return flag_tree_forwprop
;
3031 struct gimple_opt_pass pass_forwprop
=
3035 "forwprop", /* name */
3036 gate_forwprop
, /* gate */
3037 ssa_forward_propagate_and_combine
, /* execute */
3040 0, /* static_pass_number */
3041 TV_TREE_FORWPROP
, /* tv_id */
3042 PROP_cfg
| PROP_ssa
, /* properties_required */
3043 0, /* properties_provided */
3044 0, /* properties_destroyed */
3045 0, /* todo_flags_start */
3048 | TODO_verify_ssa
/* todo_flags_finish */