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 #include "tree-ssa-propagate.h"
39 /* This pass propagates the RHS of assignment statements into use
40 sites of the LHS of the assignment. It's basically a specialized
41 form of tree combination. It is hoped all of this can disappear
42 when we have a generalized tree combiner.
44 One class of common cases we handle is forward propagating a single use
45 variable into a COND_EXPR.
49 if (x) goto ... else goto ...
51 Will be transformed into:
54 if (a COND b) goto ... else goto ...
56 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
58 Or (assuming c1 and c2 are constants):
62 if (x EQ/NEQ c2) goto ... else goto ...
64 Will be transformed into:
67 if (a EQ/NEQ (c2 - c1)) goto ... else goto ...
69 Similarly for x = a - c1.
75 if (x) goto ... else goto ...
77 Will be transformed into:
80 if (a == 0) goto ... else goto ...
82 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
83 For these cases, we propagate A into all, possibly more than one,
84 COND_EXPRs that use X.
90 if (x) goto ... else goto ...
92 Will be transformed into:
95 if (a != 0) goto ... else goto ...
97 (Assuming a is an integral type and x is a boolean or x is an
98 integral and a is a boolean.)
100 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
101 For these cases, we propagate A into all, possibly more than one,
102 COND_EXPRs that use X.
104 In addition to eliminating the variable and the statement which assigns
105 a value to the variable, we may be able to later thread the jump without
106 adding insane complexity in the dominator optimizer.
108 Also note these transformations can cascade. We handle this by having
109 a worklist of COND_EXPR statements to examine. As we make a change to
110 a statement, we put it back on the worklist to examine on the next
111 iteration of the main loop.
113 A second class of propagation opportunities arises for ADDR_EXPR
124 ptr = (type1*)&type2var;
127 Will get turned into (if type1 and type2 are the same size
128 and neither have volatile on them):
129 res = VIEW_CONVERT_EXPR<type1>(type2var)
134 ptr2 = ptr + <constant>;
138 ptr2 = &x[constant/elementsize];
143 offset = index * element_size;
144 offset_p = (pointer) offset;
145 ptr2 = ptr + offset_p
147 Will get turned into:
155 Provided that decl has known alignment >= 2, will get turned into
159 We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to
160 allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent
163 This will (of course) be extended as other needs arise. */
165 static bool forward_propagate_addr_expr (tree name
, tree rhs
);
167 /* Set to true if we delete dead edges during the optimization. */
168 static bool cfg_changed
;
170 static tree
rhs_to_tree (tree type
, gimple stmt
);
172 /* Get the next statement we can propagate NAME's value into skipping
173 trivial copies. Returns the statement that is suitable as a
174 propagation destination or NULL_TREE if there is no such one.
175 This only returns destinations in a single-use chain. FINAL_NAME_P
176 if non-NULL is written to the ssa name that represents the use. */
179 get_prop_dest_stmt (tree name
, tree
*final_name_p
)
185 /* If name has multiple uses, bail out. */
186 if (!single_imm_use (name
, &use
, &use_stmt
))
189 /* If this is not a trivial copy, we found it. */
190 if (!gimple_assign_ssa_name_copy_p (use_stmt
)
191 || gimple_assign_rhs1 (use_stmt
) != name
)
194 /* Continue searching uses of the copy destination. */
195 name
= gimple_assign_lhs (use_stmt
);
199 *final_name_p
= name
;
204 /* Get the statement we can propagate from into NAME skipping
205 trivial copies. Returns the statement which defines the
206 propagation source or NULL_TREE if there is no such one.
207 If SINGLE_USE_ONLY is set considers only sources which have
208 a single use chain up to NAME. If SINGLE_USE_P is non-null,
209 it is set to whether the chain to NAME is a single use chain
210 or not. SINGLE_USE_P is not written to if SINGLE_USE_ONLY is set. */
213 get_prop_source_stmt (tree name
, bool single_use_only
, bool *single_use_p
)
215 bool single_use
= true;
218 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
220 if (!has_single_use (name
))
227 /* If name is defined by a PHI node or is the default def, bail out. */
228 if (!is_gimple_assign (def_stmt
))
231 /* If def_stmt is a simple copy, continue looking. */
232 if (gimple_assign_rhs_code (def_stmt
) == SSA_NAME
)
233 name
= gimple_assign_rhs1 (def_stmt
);
236 if (!single_use_only
&& single_use_p
)
237 *single_use_p
= single_use
;
244 /* Checks if the destination ssa name in DEF_STMT can be used as
245 propagation source. Returns true if so, otherwise false. */
248 can_propagate_from (gimple def_stmt
)
250 gcc_assert (is_gimple_assign (def_stmt
));
252 /* If the rhs has side-effects we cannot propagate from it. */
253 if (gimple_has_volatile_ops (def_stmt
))
256 /* If the rhs is a load we cannot propagate from it. */
257 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt
)) == tcc_reference
258 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt
)) == tcc_declaration
)
261 /* Constants can be always propagated. */
262 if (gimple_assign_single_p (def_stmt
)
263 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt
)))
266 /* We cannot propagate ssa names that occur in abnormal phi nodes. */
267 if (stmt_references_abnormal_ssa_name (def_stmt
))
270 /* If the definition is a conversion of a pointer to a function type,
271 then we can not apply optimizations as some targets require
272 function pointers to be canonicalized and in this case this
273 optimization could eliminate a necessary canonicalization. */
274 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
276 tree rhs
= gimple_assign_rhs1 (def_stmt
);
277 if (POINTER_TYPE_P (TREE_TYPE (rhs
))
278 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs
))) == FUNCTION_TYPE
)
285 /* Remove a chain of dead statements starting at the definition of
286 NAME. The chain is linked via the first operand of the defining statements.
287 If NAME was replaced in its only use then this function can be used
288 to clean up dead stmts. The function handles already released SSA
290 Returns true if cleanup-cfg has to run. */
293 remove_prop_source_from_use (tree name
)
295 gimple_stmt_iterator gsi
;
297 bool cfg_changed
= false;
302 if (SSA_NAME_IN_FREE_LIST (name
)
303 || SSA_NAME_IS_DEFAULT_DEF (name
)
304 || !has_zero_uses (name
))
307 stmt
= SSA_NAME_DEF_STMT (name
);
308 if (gimple_code (stmt
) == GIMPLE_PHI
309 || gimple_has_side_effects (stmt
))
312 bb
= gimple_bb (stmt
);
313 gsi
= gsi_for_stmt (stmt
);
314 unlink_stmt_vdef (stmt
);
315 if (gsi_remove (&gsi
, true))
316 cfg_changed
|= gimple_purge_dead_eh_edges (bb
);
319 name
= is_gimple_assign (stmt
) ? gimple_assign_rhs1 (stmt
) : NULL_TREE
;
320 } while (name
&& TREE_CODE (name
) == SSA_NAME
);
325 /* Return the rhs of a gimple_assign STMT in a form of a single tree,
326 converted to type TYPE.
328 This should disappear, but is needed so we can combine expressions and use
329 the fold() interfaces. Long term, we need to develop folding and combine
330 routines that deal with gimple exclusively . */
333 rhs_to_tree (tree type
, gimple stmt
)
335 location_t loc
= gimple_location (stmt
);
336 enum tree_code code
= gimple_assign_rhs_code (stmt
);
337 if (get_gimple_rhs_class (code
) == GIMPLE_TERNARY_RHS
)
338 return fold_build3_loc (loc
, code
, type
, gimple_assign_rhs1 (stmt
),
339 gimple_assign_rhs2 (stmt
),
340 gimple_assign_rhs3 (stmt
));
341 else if (get_gimple_rhs_class (code
) == GIMPLE_BINARY_RHS
)
342 return fold_build2_loc (loc
, code
, type
, gimple_assign_rhs1 (stmt
),
343 gimple_assign_rhs2 (stmt
));
344 else if (get_gimple_rhs_class (code
) == GIMPLE_UNARY_RHS
)
345 return build1 (code
, type
, gimple_assign_rhs1 (stmt
));
346 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
)
347 return gimple_assign_rhs1 (stmt
);
352 /* Combine OP0 CODE OP1 in the context of a COND_EXPR. Returns
353 the folded result in a form suitable for COND_EXPR_COND or
354 NULL_TREE, if there is no suitable simplified form. If
355 INVARIANT_ONLY is true only gimple_min_invariant results are
356 considered simplified. */
359 combine_cond_expr_cond (gimple stmt
, enum tree_code code
, tree type
,
360 tree op0
, tree op1
, bool invariant_only
)
364 gcc_assert (TREE_CODE_CLASS (code
) == tcc_comparison
);
366 fold_defer_overflow_warnings ();
367 t
= fold_binary_loc (gimple_location (stmt
), code
, type
, op0
, op1
);
370 fold_undefer_overflow_warnings (false, NULL
, 0);
374 /* Require that we got a boolean type out if we put one in. */
375 gcc_assert (TREE_CODE (TREE_TYPE (t
)) == TREE_CODE (type
));
377 /* Canonicalize the combined condition for use in a COND_EXPR. */
378 t
= canonicalize_cond_expr_cond (t
);
380 /* Bail out if we required an invariant but didn't get one. */
381 if (!t
|| (invariant_only
&& !is_gimple_min_invariant (t
)))
383 fold_undefer_overflow_warnings (false, NULL
, 0);
387 fold_undefer_overflow_warnings (!gimple_no_warning_p (stmt
), stmt
, 0);
392 /* Combine the comparison OP0 CODE OP1 at LOC with the defining statements
393 of its operand. Return a new comparison tree or NULL_TREE if there
394 were no simplifying combines. */
397 forward_propagate_into_comparison_1 (gimple stmt
,
398 enum tree_code code
, tree type
,
401 tree tmp
= NULL_TREE
;
402 tree rhs0
= NULL_TREE
, rhs1
= NULL_TREE
;
403 bool single_use0_p
= false, single_use1_p
= false;
405 /* For comparisons use the first operand, that is likely to
406 simplify comparisons against constants. */
407 if (TREE_CODE (op0
) == SSA_NAME
)
409 gimple def_stmt
= get_prop_source_stmt (op0
, false, &single_use0_p
);
410 if (def_stmt
&& can_propagate_from (def_stmt
))
412 rhs0
= rhs_to_tree (TREE_TYPE (op1
), def_stmt
);
413 tmp
= combine_cond_expr_cond (stmt
, code
, type
,
414 rhs0
, op1
, !single_use0_p
);
420 /* If that wasn't successful, try the second operand. */
421 if (TREE_CODE (op1
) == SSA_NAME
)
423 gimple def_stmt
= get_prop_source_stmt (op1
, false, &single_use1_p
);
424 if (def_stmt
&& can_propagate_from (def_stmt
))
426 rhs1
= rhs_to_tree (TREE_TYPE (op0
), def_stmt
);
427 tmp
= combine_cond_expr_cond (stmt
, code
, type
,
428 op0
, rhs1
, !single_use1_p
);
434 /* If that wasn't successful either, try both operands. */
435 if (rhs0
!= NULL_TREE
436 && rhs1
!= NULL_TREE
)
437 tmp
= combine_cond_expr_cond (stmt
, code
, type
,
439 !(single_use0_p
&& single_use1_p
));
444 /* Propagate from the ssa name definition statements of the assignment
445 from a comparison at *GSI into the conditional if that simplifies it.
446 Returns 1 if the stmt was modified and 2 if the CFG needs cleanup,
447 otherwise returns 0. */
450 forward_propagate_into_comparison (gimple_stmt_iterator
*gsi
)
452 gimple stmt
= gsi_stmt (*gsi
);
454 bool cfg_changed
= false;
455 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
456 tree rhs1
= gimple_assign_rhs1 (stmt
);
457 tree rhs2
= gimple_assign_rhs2 (stmt
);
459 /* Combine the comparison with defining statements. */
460 tmp
= forward_propagate_into_comparison_1 (stmt
,
461 gimple_assign_rhs_code (stmt
),
463 if (tmp
&& useless_type_conversion_p (type
, TREE_TYPE (tmp
)))
465 gimple_assign_set_rhs_from_tree (gsi
, tmp
);
467 update_stmt (gsi_stmt (*gsi
));
469 if (TREE_CODE (rhs1
) == SSA_NAME
)
470 cfg_changed
|= remove_prop_source_from_use (rhs1
);
471 if (TREE_CODE (rhs2
) == SSA_NAME
)
472 cfg_changed
|= remove_prop_source_from_use (rhs2
);
473 return cfg_changed
? 2 : 1;
479 /* Propagate from the ssa name definition statements of COND_EXPR
480 in GIMPLE_COND statement STMT into the conditional if that simplifies it.
481 Returns zero if no statement was changed, one if there were
482 changes and two if cfg_cleanup needs to run.
484 This must be kept in sync with forward_propagate_into_cond. */
487 forward_propagate_into_gimple_cond (gimple stmt
)
490 enum tree_code code
= gimple_cond_code (stmt
);
491 bool cfg_changed
= false;
492 tree rhs1
= gimple_cond_lhs (stmt
);
493 tree rhs2
= gimple_cond_rhs (stmt
);
495 /* We can do tree combining on SSA_NAME and comparison expressions. */
496 if (TREE_CODE_CLASS (gimple_cond_code (stmt
)) != tcc_comparison
)
499 tmp
= forward_propagate_into_comparison_1 (stmt
, code
,
504 if (dump_file
&& tmp
)
506 fprintf (dump_file
, " Replaced '");
507 print_gimple_expr (dump_file
, stmt
, 0, 0);
508 fprintf (dump_file
, "' with '");
509 print_generic_expr (dump_file
, tmp
, 0);
510 fprintf (dump_file
, "'\n");
513 gimple_cond_set_condition_from_tree (stmt
, unshare_expr (tmp
));
516 if (TREE_CODE (rhs1
) == SSA_NAME
)
517 cfg_changed
|= remove_prop_source_from_use (rhs1
);
518 if (TREE_CODE (rhs2
) == SSA_NAME
)
519 cfg_changed
|= remove_prop_source_from_use (rhs2
);
520 return (cfg_changed
|| is_gimple_min_invariant (tmp
)) ? 2 : 1;
523 /* Canonicalize _Bool == 0 and _Bool != 1 to _Bool != 0 by swapping edges. */
524 if ((TREE_CODE (TREE_TYPE (rhs1
)) == BOOLEAN_TYPE
525 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
526 && TYPE_PRECISION (TREE_TYPE (rhs1
)) == 1))
528 && integer_zerop (rhs2
))
530 && integer_onep (rhs2
))))
532 basic_block bb
= gimple_bb (stmt
);
533 gimple_cond_set_code (stmt
, NE_EXPR
);
534 gimple_cond_set_rhs (stmt
, build_zero_cst (TREE_TYPE (rhs1
)));
535 EDGE_SUCC (bb
, 0)->flags
^= (EDGE_TRUE_VALUE
|EDGE_FALSE_VALUE
);
536 EDGE_SUCC (bb
, 1)->flags
^= (EDGE_TRUE_VALUE
|EDGE_FALSE_VALUE
);
544 /* Propagate from the ssa name definition statements of COND_EXPR
545 in the rhs of statement STMT into the conditional if that simplifies it.
546 Returns true zero if the stmt was changed. */
549 forward_propagate_into_cond (gimple_stmt_iterator
*gsi_p
)
551 gimple stmt
= gsi_stmt (*gsi_p
);
552 tree tmp
= NULL_TREE
;
553 tree cond
= gimple_assign_rhs1 (stmt
);
556 /* We can do tree combining on SSA_NAME and comparison expressions. */
557 if (COMPARISON_CLASS_P (cond
))
558 tmp
= forward_propagate_into_comparison_1 (stmt
, TREE_CODE (cond
),
560 TREE_OPERAND (cond
, 0),
561 TREE_OPERAND (cond
, 1));
562 else if (TREE_CODE (cond
) == SSA_NAME
)
566 gimple def_stmt
= get_prop_source_stmt (name
, true, NULL
);
567 if (!def_stmt
|| !can_propagate_from (def_stmt
))
570 code
= gimple_assign_rhs_code (def_stmt
);
571 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
572 tmp
= fold_build2_loc (gimple_location (def_stmt
),
575 gimple_assign_rhs1 (def_stmt
),
576 gimple_assign_rhs2 (def_stmt
));
577 else if ((code
== BIT_NOT_EXPR
578 && TYPE_PRECISION (TREE_TYPE (cond
)) == 1)
579 || (code
== BIT_XOR_EXPR
580 && integer_onep (gimple_assign_rhs2 (def_stmt
))))
582 tmp
= gimple_assign_rhs1 (def_stmt
);
588 && is_gimple_condexpr (tmp
))
590 if (dump_file
&& tmp
)
592 fprintf (dump_file
, " Replaced '");
593 print_generic_expr (dump_file
, cond
, 0);
594 fprintf (dump_file
, "' with '");
595 print_generic_expr (dump_file
, tmp
, 0);
596 fprintf (dump_file
, "'\n");
599 if (integer_onep (tmp
))
600 gimple_assign_set_rhs_from_tree (gsi_p
, gimple_assign_rhs2 (stmt
));
601 else if (integer_zerop (tmp
))
602 gimple_assign_set_rhs_from_tree (gsi_p
, gimple_assign_rhs3 (stmt
));
605 gimple_assign_set_rhs1 (stmt
, unshare_expr (tmp
));
608 tree t
= gimple_assign_rhs2 (stmt
);
609 gimple_assign_set_rhs2 (stmt
, gimple_assign_rhs3 (stmt
));
610 gimple_assign_set_rhs3 (stmt
, t
);
613 stmt
= gsi_stmt (*gsi_p
);
622 /* Propagate from the ssa name definition statements of COND_EXPR
623 values in the rhs of statement STMT into the conditional arms
624 if that simplifies it.
625 Returns true if the stmt was changed. */
628 combine_cond_exprs (gimple_stmt_iterator
*gsi_p
)
630 gimple stmt
= gsi_stmt (*gsi_p
);
631 tree cond
, val1
, val2
;
632 bool changed
= false;
634 cond
= gimple_assign_rhs1 (stmt
);
635 val1
= gimple_assign_rhs2 (stmt
);
636 if (TREE_CODE (val1
) == SSA_NAME
)
638 gimple def_stmt
= SSA_NAME_DEF_STMT (val1
);
639 if (is_gimple_assign (def_stmt
)
640 && gimple_assign_rhs_code (def_stmt
) == gimple_assign_rhs_code (stmt
)
641 && operand_equal_p (gimple_assign_rhs1 (def_stmt
), cond
, 0))
643 val1
= unshare_expr (gimple_assign_rhs2 (def_stmt
));
644 gimple_assign_set_rhs2 (stmt
, val1
);
648 val2
= gimple_assign_rhs3 (stmt
);
649 if (TREE_CODE (val2
) == SSA_NAME
)
651 gimple def_stmt
= SSA_NAME_DEF_STMT (val2
);
652 if (is_gimple_assign (def_stmt
)
653 && gimple_assign_rhs_code (def_stmt
) == gimple_assign_rhs_code (stmt
)
654 && operand_equal_p (gimple_assign_rhs1 (def_stmt
), cond
, 0))
656 val2
= unshare_expr (gimple_assign_rhs3 (def_stmt
));
657 gimple_assign_set_rhs3 (stmt
, val2
);
661 if (operand_equal_p (val1
, val2
, 0))
663 gimple_assign_set_rhs_from_tree (gsi_p
, val1
);
664 stmt
= gsi_stmt (*gsi_p
);
674 /* We've just substituted an ADDR_EXPR into stmt. Update all the
675 relevant data structures to match. */
678 tidy_after_forward_propagate_addr (gimple stmt
)
680 /* We may have turned a trapping insn into a non-trapping insn. */
681 if (maybe_clean_or_replace_eh_stmt (stmt
, stmt
)
682 && gimple_purge_dead_eh_edges (gimple_bb (stmt
)))
685 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) == ADDR_EXPR
)
686 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt
));
689 /* NAME is a SSA_NAME representing DEF_RHS which is of the form
690 ADDR_EXPR <whatever>.
692 Try to forward propagate the ADDR_EXPR into the use USE_STMT.
693 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
694 node or for recovery of array indexing from pointer arithmetic.
696 Return true if the propagation was successful (the propagation can
697 be not totally successful, yet things may have been changed). */
700 forward_propagate_addr_expr_1 (tree name
, tree def_rhs
,
701 gimple_stmt_iterator
*use_stmt_gsi
,
704 tree lhs
, rhs
, rhs2
, array_ref
;
705 gimple use_stmt
= gsi_stmt (*use_stmt_gsi
);
706 enum tree_code rhs_code
;
709 gcc_assert (TREE_CODE (def_rhs
) == ADDR_EXPR
);
711 lhs
= gimple_assign_lhs (use_stmt
);
712 rhs_code
= gimple_assign_rhs_code (use_stmt
);
713 rhs
= gimple_assign_rhs1 (use_stmt
);
715 /* Trivial cases. The use statement could be a trivial copy or a
716 useless conversion. Recurse to the uses of the lhs as copyprop does
717 not copy through different variant pointers and FRE does not catch
718 all useless conversions. Treat the case of a single-use name and
719 a conversion to def_rhs type separate, though. */
720 if (TREE_CODE (lhs
) == SSA_NAME
721 && ((rhs_code
== SSA_NAME
&& rhs
== name
)
722 || CONVERT_EXPR_CODE_P (rhs_code
)))
724 /* Only recurse if we don't deal with a single use or we cannot
725 do the propagation to the current statement. In particular
726 we can end up with a conversion needed for a non-invariant
727 address which we cannot do in a single statement. */
729 || (!useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (def_rhs
))
730 && (!is_gimple_min_invariant (def_rhs
)
731 || (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
732 && POINTER_TYPE_P (TREE_TYPE (def_rhs
))
733 && (TYPE_PRECISION (TREE_TYPE (lhs
))
734 > TYPE_PRECISION (TREE_TYPE (def_rhs
)))))))
735 return forward_propagate_addr_expr (lhs
, def_rhs
);
737 gimple_assign_set_rhs1 (use_stmt
, unshare_expr (def_rhs
));
738 if (useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (def_rhs
)))
739 gimple_assign_set_rhs_code (use_stmt
, TREE_CODE (def_rhs
));
741 gimple_assign_set_rhs_code (use_stmt
, NOP_EXPR
);
745 /* Propagate through constant pointer adjustments. */
746 if (TREE_CODE (lhs
) == SSA_NAME
747 && rhs_code
== POINTER_PLUS_EXPR
749 && TREE_CODE (gimple_assign_rhs2 (use_stmt
)) == INTEGER_CST
)
752 /* As we come here with non-invariant addresses in def_rhs we need
753 to make sure we can build a valid constant offsetted address
754 for further propagation. Simply rely on fold building that
755 and check after the fact. */
756 new_def_rhs
= fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (rhs
)),
758 fold_convert (ptr_type_node
,
759 gimple_assign_rhs2 (use_stmt
)));
760 if (TREE_CODE (new_def_rhs
) == MEM_REF
761 && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs
, 0)))
763 new_def_rhs
= build_fold_addr_expr_with_type (new_def_rhs
,
766 /* Recurse. If we could propagate into all uses of lhs do not
767 bother to replace into the current use but just pretend we did. */
768 if (TREE_CODE (new_def_rhs
) == ADDR_EXPR
769 && forward_propagate_addr_expr (lhs
, new_def_rhs
))
772 if (useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (new_def_rhs
)))
773 gimple_assign_set_rhs_with_ops (use_stmt_gsi
, TREE_CODE (new_def_rhs
),
774 new_def_rhs
, NULL_TREE
);
775 else if (is_gimple_min_invariant (new_def_rhs
))
776 gimple_assign_set_rhs_with_ops (use_stmt_gsi
, NOP_EXPR
,
777 new_def_rhs
, NULL_TREE
);
780 gcc_assert (gsi_stmt (*use_stmt_gsi
) == use_stmt
);
781 update_stmt (use_stmt
);
785 /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
786 ADDR_EXPR will not appear on the LHS. */
787 lhs
= gimple_assign_lhs (use_stmt
);
788 while (handled_component_p (lhs
))
789 lhs
= TREE_OPERAND (lhs
, 0);
791 /* Now see if the LHS node is a MEM_REF using NAME. If so,
792 propagate the ADDR_EXPR into the use of NAME and fold the result. */
793 if (TREE_CODE (lhs
) == MEM_REF
794 && TREE_OPERAND (lhs
, 0) == name
)
797 HOST_WIDE_INT def_rhs_offset
;
798 /* If the address is invariant we can always fold it. */
799 if ((def_rhs_base
= get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs
, 0),
802 double_int off
= mem_ref_offset (lhs
);
804 off
+= double_int::from_shwi (def_rhs_offset
);
805 if (TREE_CODE (def_rhs_base
) == MEM_REF
)
807 off
+= mem_ref_offset (def_rhs_base
);
808 new_ptr
= TREE_OPERAND (def_rhs_base
, 0);
811 new_ptr
= build_fold_addr_expr (def_rhs_base
);
812 TREE_OPERAND (lhs
, 0) = new_ptr
;
813 TREE_OPERAND (lhs
, 1)
814 = double_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs
, 1)), off
);
815 tidy_after_forward_propagate_addr (use_stmt
);
816 /* Continue propagating into the RHS if this was not the only use. */
820 /* If the LHS is a plain dereference and the value type is the same as
821 that of the pointed-to type of the address we can put the
822 dereferenced address on the LHS preserving the original alias-type. */
823 else if (gimple_assign_lhs (use_stmt
) == lhs
824 && integer_zerop (TREE_OPERAND (lhs
, 1))
825 && useless_type_conversion_p
826 (TREE_TYPE (TREE_OPERAND (def_rhs
, 0)),
827 TREE_TYPE (gimple_assign_rhs1 (use_stmt
))))
829 tree
*def_rhs_basep
= &TREE_OPERAND (def_rhs
, 0);
830 tree new_offset
, new_base
, saved
, new_lhs
;
831 while (handled_component_p (*def_rhs_basep
))
832 def_rhs_basep
= &TREE_OPERAND (*def_rhs_basep
, 0);
833 saved
= *def_rhs_basep
;
834 if (TREE_CODE (*def_rhs_basep
) == MEM_REF
)
836 new_base
= TREE_OPERAND (*def_rhs_basep
, 0);
837 new_offset
= fold_convert (TREE_TYPE (TREE_OPERAND (lhs
, 1)),
838 TREE_OPERAND (*def_rhs_basep
, 1));
842 new_base
= build_fold_addr_expr (*def_rhs_basep
);
843 new_offset
= TREE_OPERAND (lhs
, 1);
845 *def_rhs_basep
= build2 (MEM_REF
, TREE_TYPE (*def_rhs_basep
),
846 new_base
, new_offset
);
847 TREE_THIS_VOLATILE (*def_rhs_basep
) = TREE_THIS_VOLATILE (lhs
);
848 TREE_SIDE_EFFECTS (*def_rhs_basep
) = TREE_SIDE_EFFECTS (lhs
);
849 TREE_THIS_NOTRAP (*def_rhs_basep
) = TREE_THIS_NOTRAP (lhs
);
850 new_lhs
= unshare_expr (TREE_OPERAND (def_rhs
, 0));
851 gimple_assign_set_lhs (use_stmt
, new_lhs
);
852 TREE_THIS_VOLATILE (new_lhs
) = TREE_THIS_VOLATILE (lhs
);
853 TREE_SIDE_EFFECTS (new_lhs
) = TREE_SIDE_EFFECTS (lhs
);
854 *def_rhs_basep
= saved
;
855 tidy_after_forward_propagate_addr (use_stmt
);
856 /* Continue propagating into the RHS if this was not the
862 /* We can have a struct assignment dereferencing our name twice.
863 Note that we didn't propagate into the lhs to not falsely
864 claim we did when propagating into the rhs. */
868 /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
869 nodes from the RHS. */
870 rhs
= gimple_assign_rhs1 (use_stmt
);
871 if (TREE_CODE (rhs
) == ADDR_EXPR
)
872 rhs
= TREE_OPERAND (rhs
, 0);
873 while (handled_component_p (rhs
))
874 rhs
= TREE_OPERAND (rhs
, 0);
876 /* Now see if the RHS node is a MEM_REF using NAME. If so,
877 propagate the ADDR_EXPR into the use of NAME and fold the result. */
878 if (TREE_CODE (rhs
) == MEM_REF
879 && TREE_OPERAND (rhs
, 0) == name
)
882 HOST_WIDE_INT def_rhs_offset
;
883 if ((def_rhs_base
= get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs
, 0),
886 double_int off
= mem_ref_offset (rhs
);
888 off
+= double_int::from_shwi (def_rhs_offset
);
889 if (TREE_CODE (def_rhs_base
) == MEM_REF
)
891 off
+= mem_ref_offset (def_rhs_base
);
892 new_ptr
= TREE_OPERAND (def_rhs_base
, 0);
895 new_ptr
= build_fold_addr_expr (def_rhs_base
);
896 TREE_OPERAND (rhs
, 0) = new_ptr
;
897 TREE_OPERAND (rhs
, 1)
898 = double_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs
, 1)), off
);
899 fold_stmt_inplace (use_stmt_gsi
);
900 tidy_after_forward_propagate_addr (use_stmt
);
903 /* If the RHS is a plain dereference and the value type is the same as
904 that of the pointed-to type of the address we can put the
905 dereferenced address on the RHS preserving the original alias-type. */
906 else if (gimple_assign_rhs1 (use_stmt
) == rhs
907 && integer_zerop (TREE_OPERAND (rhs
, 1))
908 && useless_type_conversion_p
909 (TREE_TYPE (gimple_assign_lhs (use_stmt
)),
910 TREE_TYPE (TREE_OPERAND (def_rhs
, 0))))
912 tree
*def_rhs_basep
= &TREE_OPERAND (def_rhs
, 0);
913 tree new_offset
, new_base
, saved
, new_rhs
;
914 while (handled_component_p (*def_rhs_basep
))
915 def_rhs_basep
= &TREE_OPERAND (*def_rhs_basep
, 0);
916 saved
= *def_rhs_basep
;
917 if (TREE_CODE (*def_rhs_basep
) == MEM_REF
)
919 new_base
= TREE_OPERAND (*def_rhs_basep
, 0);
920 new_offset
= fold_convert (TREE_TYPE (TREE_OPERAND (rhs
, 1)),
921 TREE_OPERAND (*def_rhs_basep
, 1));
925 new_base
= build_fold_addr_expr (*def_rhs_basep
);
926 new_offset
= TREE_OPERAND (rhs
, 1);
928 *def_rhs_basep
= build2 (MEM_REF
, TREE_TYPE (*def_rhs_basep
),
929 new_base
, new_offset
);
930 TREE_THIS_VOLATILE (*def_rhs_basep
) = TREE_THIS_VOLATILE (rhs
);
931 TREE_SIDE_EFFECTS (*def_rhs_basep
) = TREE_SIDE_EFFECTS (rhs
);
932 TREE_THIS_NOTRAP (*def_rhs_basep
) = TREE_THIS_NOTRAP (rhs
);
933 new_rhs
= unshare_expr (TREE_OPERAND (def_rhs
, 0));
934 gimple_assign_set_rhs1 (use_stmt
, new_rhs
);
935 TREE_THIS_VOLATILE (new_rhs
) = TREE_THIS_VOLATILE (rhs
);
936 TREE_SIDE_EFFECTS (new_rhs
) = TREE_SIDE_EFFECTS (rhs
);
937 *def_rhs_basep
= saved
;
938 fold_stmt_inplace (use_stmt_gsi
);
939 tidy_after_forward_propagate_addr (use_stmt
);
944 /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there
946 if (gimple_assign_rhs_code (use_stmt
) != POINTER_PLUS_EXPR
947 || gimple_assign_rhs1 (use_stmt
) != name
)
950 /* The remaining cases are all for turning pointer arithmetic into
951 array indexing. They only apply when we have the address of
952 element zero in an array. If that is not the case then there
954 array_ref
= TREE_OPERAND (def_rhs
, 0);
955 if ((TREE_CODE (array_ref
) != ARRAY_REF
956 || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref
, 0))) != ARRAY_TYPE
957 || TREE_CODE (TREE_OPERAND (array_ref
, 1)) != INTEGER_CST
)
958 && TREE_CODE (TREE_TYPE (array_ref
)) != ARRAY_TYPE
)
961 rhs2
= gimple_assign_rhs2 (use_stmt
);
962 /* Optimize &x[C1] p+ C2 to &x p+ C3 with C3 = C1 * element_size + C2. */
963 if (TREE_CODE (rhs2
) == INTEGER_CST
)
965 tree new_rhs
= build1_loc (gimple_location (use_stmt
),
966 ADDR_EXPR
, TREE_TYPE (def_rhs
),
967 fold_build2 (MEM_REF
,
968 TREE_TYPE (TREE_TYPE (def_rhs
)),
969 unshare_expr (def_rhs
),
970 fold_convert (ptr_type_node
,
972 gimple_assign_set_rhs_from_tree (use_stmt_gsi
, new_rhs
);
973 use_stmt
= gsi_stmt (*use_stmt_gsi
);
974 update_stmt (use_stmt
);
975 tidy_after_forward_propagate_addr (use_stmt
);
982 /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
984 Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
985 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
986 node or for recovery of array indexing from pointer arithmetic.
987 Returns true, if all uses have been propagated into. */
990 forward_propagate_addr_expr (tree name
, tree rhs
)
992 int stmt_loop_depth
= bb_loop_depth (gimple_bb (SSA_NAME_DEF_STMT (name
)));
993 imm_use_iterator iter
;
996 bool single_use_p
= has_single_use (name
);
998 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, name
)
1003 /* If the use is not in a simple assignment statement, then
1004 there is nothing we can do. */
1005 if (gimple_code (use_stmt
) != GIMPLE_ASSIGN
)
1007 if (!is_gimple_debug (use_stmt
))
1012 /* If the use is in a deeper loop nest, then we do not want
1013 to propagate non-invariant ADDR_EXPRs into the loop as that
1014 is likely adding expression evaluations into the loop. */
1015 if (bb_loop_depth (gimple_bb (use_stmt
)) > stmt_loop_depth
1016 && !is_gimple_min_invariant (rhs
))
1023 gimple_stmt_iterator gsi
= gsi_for_stmt (use_stmt
);
1024 result
= forward_propagate_addr_expr_1 (name
, rhs
, &gsi
,
1026 /* If the use has moved to a different statement adjust
1027 the update machinery for the old statement too. */
1028 if (use_stmt
!= gsi_stmt (gsi
))
1030 update_stmt (use_stmt
);
1031 use_stmt
= gsi_stmt (gsi
);
1034 update_stmt (use_stmt
);
1038 /* Remove intermediate now unused copy and conversion chains. */
1039 use_rhs
= gimple_assign_rhs1 (use_stmt
);
1041 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
1042 && TREE_CODE (use_rhs
) == SSA_NAME
1043 && has_zero_uses (gimple_assign_lhs (use_stmt
)))
1045 gimple_stmt_iterator gsi
= gsi_for_stmt (use_stmt
);
1046 release_defs (use_stmt
);
1047 gsi_remove (&gsi
, true);
1051 return all
&& has_zero_uses (name
);
1055 /* Forward propagate the comparison defined in *DEFGSI like
1056 cond_1 = x CMP y to uses of the form
1060 Returns true if stmt is now unused. Advance DEFGSI to the next
1064 forward_propagate_comparison (gimple_stmt_iterator
*defgsi
)
1066 gimple stmt
= gsi_stmt (*defgsi
);
1067 tree name
= gimple_assign_lhs (stmt
);
1069 tree tmp
= NULL_TREE
;
1070 gimple_stmt_iterator gsi
;
1071 enum tree_code code
;
1074 /* Don't propagate ssa names that occur in abnormal phis. */
1075 if ((TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
1076 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt
)))
1077 || (TREE_CODE (gimple_assign_rhs2 (stmt
)) == SSA_NAME
1078 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs2 (stmt
))))
1081 /* Do not un-cse comparisons. But propagate through copies. */
1082 use_stmt
= get_prop_dest_stmt (name
, &name
);
1084 || !is_gimple_assign (use_stmt
))
1087 code
= gimple_assign_rhs_code (use_stmt
);
1088 lhs
= gimple_assign_lhs (use_stmt
);
1089 if (!INTEGRAL_TYPE_P (TREE_TYPE (lhs
)))
1092 /* We can propagate the condition into a statement that
1093 computes the logical negation of the comparison result. */
1094 if ((code
== BIT_NOT_EXPR
1095 && TYPE_PRECISION (TREE_TYPE (lhs
)) == 1)
1096 || (code
== BIT_XOR_EXPR
1097 && integer_onep (gimple_assign_rhs2 (use_stmt
))))
1099 tree type
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
1100 bool nans
= HONOR_NANS (TYPE_MODE (type
));
1101 enum tree_code inv_code
;
1102 inv_code
= invert_tree_comparison (gimple_assign_rhs_code (stmt
), nans
);
1103 if (inv_code
== ERROR_MARK
)
1106 tmp
= build2 (inv_code
, TREE_TYPE (lhs
), gimple_assign_rhs1 (stmt
),
1107 gimple_assign_rhs2 (stmt
));
1112 gsi
= gsi_for_stmt (use_stmt
);
1113 gimple_assign_set_rhs_from_tree (&gsi
, unshare_expr (tmp
));
1114 use_stmt
= gsi_stmt (gsi
);
1115 update_stmt (use_stmt
);
1117 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1119 fprintf (dump_file
, " Replaced '");
1120 print_gimple_expr (dump_file
, stmt
, 0, dump_flags
);
1121 fprintf (dump_file
, "' with '");
1122 print_gimple_expr (dump_file
, use_stmt
, 0, dump_flags
);
1123 fprintf (dump_file
, "'\n");
1126 /* When we remove stmt now the iterator defgsi goes off it's current
1127 sequence, hence advance it now. */
1130 /* Remove defining statements. */
1131 return remove_prop_source_from_use (name
);
1139 /* If we have lhs = ~x (STMT), look and see if earlier we had x = ~y.
1140 If so, we can change STMT into lhs = y which can later be copy
1141 propagated. Similarly for negation.
1143 This could trivially be formulated as a forward propagation
1144 to immediate uses. However, we already had an implementation
1145 from DOM which used backward propagation via the use-def links.
1147 It turns out that backward propagation is actually faster as
1148 there's less work to do for each NOT/NEG expression we find.
1149 Backwards propagation needs to look at the statement in a single
1150 backlink. Forward propagation needs to look at potentially more
1151 than one forward link.
1153 Returns true when the statement was changed. */
1156 simplify_not_neg_expr (gimple_stmt_iterator
*gsi_p
)
1158 gimple stmt
= gsi_stmt (*gsi_p
);
1159 tree rhs
= gimple_assign_rhs1 (stmt
);
1160 gimple rhs_def_stmt
= SSA_NAME_DEF_STMT (rhs
);
1162 /* See if the RHS_DEF_STMT has the same form as our statement. */
1163 if (is_gimple_assign (rhs_def_stmt
)
1164 && gimple_assign_rhs_code (rhs_def_stmt
) == gimple_assign_rhs_code (stmt
))
1166 tree rhs_def_operand
= gimple_assign_rhs1 (rhs_def_stmt
);
1168 /* Verify that RHS_DEF_OPERAND is a suitable SSA_NAME. */
1169 if (TREE_CODE (rhs_def_operand
) == SSA_NAME
1170 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand
))
1172 gimple_assign_set_rhs_from_tree (gsi_p
, rhs_def_operand
);
1173 stmt
= gsi_stmt (*gsi_p
);
1182 /* Helper function for simplify_gimple_switch. Remove case labels that
1183 have values outside the range of the new type. */
1186 simplify_gimple_switch_label_vec (gimple stmt
, tree index_type
)
1188 unsigned int branch_num
= gimple_switch_num_labels (stmt
);
1189 VEC(tree
, heap
) *labels
= VEC_alloc (tree
, heap
, branch_num
);
1190 unsigned int i
, len
;
1192 /* Collect the existing case labels in a VEC, and preprocess it as if
1193 we are gimplifying a GENERIC SWITCH_EXPR. */
1194 for (i
= 1; i
< branch_num
; i
++)
1195 VEC_quick_push (tree
, labels
, gimple_switch_label (stmt
, i
));
1196 preprocess_case_label_vec_for_gimple (labels
, index_type
, NULL
);
1198 /* If any labels were removed, replace the existing case labels
1199 in the GIMPLE_SWITCH statement with the correct ones.
1200 Note that the type updates were done in-place on the case labels,
1201 so we only have to replace the case labels in the GIMPLE_SWITCH
1202 if the number of labels changed. */
1203 len
= VEC_length (tree
, labels
);
1204 if (len
< branch_num
- 1)
1206 bitmap target_blocks
;
1210 /* Corner case: *all* case labels have been removed as being
1211 out-of-range for INDEX_TYPE. Push one label and let the
1212 CFG cleanups deal with this further. */
1217 label
= CASE_LABEL (gimple_switch_default_label (stmt
));
1218 elt
= build_case_label (build_int_cst (index_type
, 0), NULL
, label
);
1219 VEC_quick_push (tree
, labels
, elt
);
1223 for (i
= 0; i
< VEC_length (tree
, labels
); i
++)
1224 gimple_switch_set_label (stmt
, i
+ 1, VEC_index (tree
, labels
, i
));
1225 for (i
++ ; i
< branch_num
; i
++)
1226 gimple_switch_set_label (stmt
, i
, NULL_TREE
);
1227 gimple_switch_set_num_labels (stmt
, len
+ 1);
1229 /* Cleanup any edges that are now dead. */
1230 target_blocks
= BITMAP_ALLOC (NULL
);
1231 for (i
= 0; i
< gimple_switch_num_labels (stmt
); i
++)
1233 tree elt
= gimple_switch_label (stmt
, i
);
1234 basic_block target
= label_to_block (CASE_LABEL (elt
));
1235 bitmap_set_bit (target_blocks
, target
->index
);
1237 for (ei
= ei_start (gimple_bb (stmt
)->succs
); (e
= ei_safe_edge (ei
)); )
1239 if (! bitmap_bit_p (target_blocks
, e
->dest
->index
))
1243 free_dominance_info (CDI_DOMINATORS
);
1248 BITMAP_FREE (target_blocks
);
1251 VEC_free (tree
, heap
, labels
);
1254 /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
1255 the condition which we may be able to optimize better. */
1258 simplify_gimple_switch (gimple stmt
)
1260 tree cond
= gimple_switch_index (stmt
);
1264 /* The optimization that we really care about is removing unnecessary
1265 casts. That will let us do much better in propagating the inferred
1266 constant at the switch target. */
1267 if (TREE_CODE (cond
) == SSA_NAME
)
1269 def_stmt
= SSA_NAME_DEF_STMT (cond
);
1270 if (is_gimple_assign (def_stmt
))
1272 if (gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
)
1277 def
= gimple_assign_rhs1 (def_stmt
);
1279 to
= TREE_TYPE (cond
);
1280 ti
= TREE_TYPE (def
);
1282 /* If we have an extension that preserves value, then we
1283 can copy the source value into the switch. */
1285 need_precision
= TYPE_PRECISION (ti
);
1287 if (! INTEGRAL_TYPE_P (ti
))
1289 else if (TYPE_UNSIGNED (to
) && !TYPE_UNSIGNED (ti
))
1291 else if (!TYPE_UNSIGNED (to
) && TYPE_UNSIGNED (ti
))
1292 need_precision
+= 1;
1293 if (TYPE_PRECISION (to
) < need_precision
)
1298 gimple_switch_set_index (stmt
, def
);
1299 simplify_gimple_switch_label_vec (stmt
, ti
);
1310 /* For pointers p2 and p1 return p2 - p1 if the
1311 difference is known and constant, otherwise return NULL. */
1314 constant_pointer_difference (tree p1
, tree p2
)
1317 #define CPD_ITERATIONS 5
1318 tree exps
[2][CPD_ITERATIONS
];
1319 tree offs
[2][CPD_ITERATIONS
];
1322 for (i
= 0; i
< 2; i
++)
1324 tree p
= i
? p1
: p2
;
1325 tree off
= size_zero_node
;
1327 enum tree_code code
;
1329 /* For each of p1 and p2 we need to iterate at least
1330 twice, to handle ADDR_EXPR directly in p1/p2,
1331 SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc.
1332 on definition's stmt RHS. Iterate a few extra times. */
1336 if (!POINTER_TYPE_P (TREE_TYPE (p
)))
1338 if (TREE_CODE (p
) == ADDR_EXPR
)
1340 tree q
= TREE_OPERAND (p
, 0);
1341 HOST_WIDE_INT offset
;
1342 tree base
= get_addr_base_and_unit_offset (q
, &offset
);
1347 off
= size_binop (PLUS_EXPR
, off
, size_int (offset
));
1349 if (TREE_CODE (q
) == MEM_REF
1350 && TREE_CODE (TREE_OPERAND (q
, 0)) == SSA_NAME
)
1352 p
= TREE_OPERAND (q
, 0);
1353 off
= size_binop (PLUS_EXPR
, off
,
1354 double_int_to_tree (sizetype
,
1355 mem_ref_offset (q
)));
1364 if (TREE_CODE (p
) != SSA_NAME
)
1368 if (j
== CPD_ITERATIONS
)
1370 stmt
= SSA_NAME_DEF_STMT (p
);
1371 if (!is_gimple_assign (stmt
) || gimple_assign_lhs (stmt
) != p
)
1373 code
= gimple_assign_rhs_code (stmt
);
1374 if (code
== POINTER_PLUS_EXPR
)
1376 if (TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
1378 off
= size_binop (PLUS_EXPR
, off
, gimple_assign_rhs2 (stmt
));
1379 p
= gimple_assign_rhs1 (stmt
);
1381 else if (code
== ADDR_EXPR
|| code
== NOP_EXPR
)
1382 p
= gimple_assign_rhs1 (stmt
);
1390 for (i
= 0; i
< cnt
[0]; i
++)
1391 for (j
= 0; j
< cnt
[1]; j
++)
1392 if (exps
[0][i
] == exps
[1][j
])
1393 return size_binop (MINUS_EXPR
, offs
[0][i
], offs
[1][j
]);
1398 /* *GSI_P is a GIMPLE_CALL to a builtin function.
1400 memcpy (p, "abcd", 4);
1401 memset (p + 4, ' ', 3);
1403 memcpy (p, "abcd ", 7);
1404 call if the latter can be stored by pieces during expansion. */
1407 simplify_builtin_call (gimple_stmt_iterator
*gsi_p
, tree callee2
)
1409 gimple stmt1
, stmt2
= gsi_stmt (*gsi_p
);
1410 tree vuse
= gimple_vuse (stmt2
);
1413 stmt1
= SSA_NAME_DEF_STMT (vuse
);
1415 switch (DECL_FUNCTION_CODE (callee2
))
1417 case BUILT_IN_MEMSET
:
1418 if (gimple_call_num_args (stmt2
) != 3
1419 || gimple_call_lhs (stmt2
)
1421 || BITS_PER_UNIT
!= 8)
1426 tree ptr1
, src1
, str1
, off1
, len1
, lhs1
;
1427 tree ptr2
= gimple_call_arg (stmt2
, 0);
1428 tree val2
= gimple_call_arg (stmt2
, 1);
1429 tree len2
= gimple_call_arg (stmt2
, 2);
1430 tree diff
, vdef
, new_str_cst
;
1432 unsigned int ptr1_align
;
1433 unsigned HOST_WIDE_INT src_len
;
1435 use_operand_p use_p
;
1437 if (!host_integerp (val2
, 0)
1438 || !host_integerp (len2
, 1))
1440 if (is_gimple_call (stmt1
))
1442 /* If first stmt is a call, it needs to be memcpy
1443 or mempcpy, with string literal as second argument and
1445 callee1
= gimple_call_fndecl (stmt1
);
1446 if (callee1
== NULL_TREE
1447 || DECL_BUILT_IN_CLASS (callee1
) != BUILT_IN_NORMAL
1448 || gimple_call_num_args (stmt1
) != 3)
1450 if (DECL_FUNCTION_CODE (callee1
) != BUILT_IN_MEMCPY
1451 && DECL_FUNCTION_CODE (callee1
) != BUILT_IN_MEMPCPY
)
1453 ptr1
= gimple_call_arg (stmt1
, 0);
1454 src1
= gimple_call_arg (stmt1
, 1);
1455 len1
= gimple_call_arg (stmt1
, 2);
1456 lhs1
= gimple_call_lhs (stmt1
);
1457 if (!host_integerp (len1
, 1))
1459 str1
= string_constant (src1
, &off1
);
1460 if (str1
== NULL_TREE
)
1462 if (!host_integerp (off1
, 1)
1463 || compare_tree_int (off1
, TREE_STRING_LENGTH (str1
) - 1) > 0
1464 || compare_tree_int (len1
, TREE_STRING_LENGTH (str1
)
1465 - tree_low_cst (off1
, 1)) > 0
1466 || TREE_CODE (TREE_TYPE (str1
)) != ARRAY_TYPE
1467 || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1
)))
1468 != TYPE_MODE (char_type_node
))
1471 else if (gimple_assign_single_p (stmt1
))
1473 /* Otherwise look for length 1 memcpy optimized into
1475 ptr1
= gimple_assign_lhs (stmt1
);
1476 src1
= gimple_assign_rhs1 (stmt1
);
1477 if (TREE_CODE (ptr1
) != MEM_REF
1478 || TYPE_MODE (TREE_TYPE (ptr1
)) != TYPE_MODE (char_type_node
)
1479 || !host_integerp (src1
, 0))
1481 ptr1
= build_fold_addr_expr (ptr1
);
1482 callee1
= NULL_TREE
;
1483 len1
= size_one_node
;
1485 off1
= size_zero_node
;
1491 diff
= constant_pointer_difference (ptr1
, ptr2
);
1492 if (diff
== NULL
&& lhs1
!= NULL
)
1494 diff
= constant_pointer_difference (lhs1
, ptr2
);
1495 if (DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
1497 diff
= size_binop (PLUS_EXPR
, diff
,
1498 fold_convert (sizetype
, len1
));
1500 /* If the difference between the second and first destination pointer
1501 is not constant, or is bigger than memcpy length, bail out. */
1503 || !host_integerp (diff
, 1)
1504 || tree_int_cst_lt (len1
, diff
))
1507 /* Use maximum of difference plus memset length and memcpy length
1508 as the new memcpy length, if it is too big, bail out. */
1509 src_len
= tree_low_cst (diff
, 1);
1510 src_len
+= tree_low_cst (len2
, 1);
1511 if (src_len
< (unsigned HOST_WIDE_INT
) tree_low_cst (len1
, 1))
1512 src_len
= tree_low_cst (len1
, 1);
1516 /* If mempcpy value is used elsewhere, bail out, as mempcpy
1517 with bigger length will return different result. */
1518 if (lhs1
!= NULL_TREE
1519 && DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
1520 && (TREE_CODE (lhs1
) != SSA_NAME
1521 || !single_imm_use (lhs1
, &use_p
, &use_stmt
)
1522 || use_stmt
!= stmt2
))
1525 /* If anything reads memory in between memcpy and memset
1526 call, the modified memcpy call might change it. */
1527 vdef
= gimple_vdef (stmt1
);
1529 && (!single_imm_use (vdef
, &use_p
, &use_stmt
)
1530 || use_stmt
!= stmt2
))
1533 ptr1_align
= get_pointer_alignment (ptr1
);
1534 /* Construct the new source string literal. */
1535 src_buf
= XALLOCAVEC (char, src_len
+ 1);
1538 TREE_STRING_POINTER (str1
) + tree_low_cst (off1
, 1),
1539 tree_low_cst (len1
, 1));
1541 src_buf
[0] = tree_low_cst (src1
, 0);
1542 memset (src_buf
+ tree_low_cst (diff
, 1),
1543 tree_low_cst (val2
, 0), tree_low_cst (len2
, 1));
1544 src_buf
[src_len
] = '\0';
1545 /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
1546 handle embedded '\0's. */
1547 if (strlen (src_buf
) != src_len
)
1549 rtl_profile_for_bb (gimple_bb (stmt2
));
1550 /* If the new memcpy wouldn't be emitted by storing the literal
1551 by pieces, this optimization might enlarge .rodata too much,
1552 as commonly used string literals couldn't be shared any
1554 if (!can_store_by_pieces (src_len
,
1555 builtin_strncpy_read_str
,
1556 src_buf
, ptr1_align
, false))
1559 new_str_cst
= build_string_literal (src_len
, src_buf
);
1562 /* If STMT1 is a mem{,p}cpy call, adjust it and remove
1564 if (lhs1
&& DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
)
1565 gimple_call_set_lhs (stmt1
, NULL_TREE
);
1566 gimple_call_set_arg (stmt1
, 1, new_str_cst
);
1567 gimple_call_set_arg (stmt1
, 2,
1568 build_int_cst (TREE_TYPE (len1
), src_len
));
1569 update_stmt (stmt1
);
1570 unlink_stmt_vdef (stmt2
);
1571 gsi_remove (gsi_p
, true);
1572 release_defs (stmt2
);
1573 if (lhs1
&& DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
)
1574 release_ssa_name (lhs1
);
1579 /* Otherwise, if STMT1 is length 1 memcpy optimized into
1580 assignment, remove STMT1 and change memset call into
1582 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt1
);
1584 if (!is_gimple_val (ptr1
))
1585 ptr1
= force_gimple_operand_gsi (gsi_p
, ptr1
, true, NULL_TREE
,
1586 true, GSI_SAME_STMT
);
1587 gimple_call_set_fndecl (stmt2
,
1588 builtin_decl_explicit (BUILT_IN_MEMCPY
));
1589 gimple_call_set_arg (stmt2
, 0, ptr1
);
1590 gimple_call_set_arg (stmt2
, 1, new_str_cst
);
1591 gimple_call_set_arg (stmt2
, 2,
1592 build_int_cst (TREE_TYPE (len2
), src_len
));
1593 unlink_stmt_vdef (stmt1
);
1594 gsi_remove (&gsi
, true);
1595 release_defs (stmt1
);
1596 update_stmt (stmt2
);
1607 /* Checks if expression has type of one-bit precision, or is a known
1608 truth-valued expression. */
1610 truth_valued_ssa_name (tree name
)
1613 tree type
= TREE_TYPE (name
);
1615 if (!INTEGRAL_TYPE_P (type
))
1617 /* Don't check here for BOOLEAN_TYPE as the precision isn't
1618 necessarily one and so ~X is not equal to !X. */
1619 if (TYPE_PRECISION (type
) == 1)
1621 def
= SSA_NAME_DEF_STMT (name
);
1622 if (is_gimple_assign (def
))
1623 return truth_value_p (gimple_assign_rhs_code (def
));
1627 /* Helper routine for simplify_bitwise_binary_1 function.
1628 Return for the SSA name NAME the expression X if it mets condition
1629 NAME = !X. Otherwise return NULL_TREE.
1630 Detected patterns for NAME = !X are:
1631 !X and X == 0 for X with integral type.
1632 X ^ 1, X != 1,or ~X for X with integral type with precision of one. */
1634 lookup_logical_inverted_value (tree name
)
1637 enum tree_code code
;
1640 /* If name has none-intergal type, or isn't a SSA_NAME, then
1642 if (TREE_CODE (name
) != SSA_NAME
1643 || !INTEGRAL_TYPE_P (TREE_TYPE (name
)))
1645 def
= SSA_NAME_DEF_STMT (name
);
1646 if (!is_gimple_assign (def
))
1649 code
= gimple_assign_rhs_code (def
);
1650 op1
= gimple_assign_rhs1 (def
);
1653 /* Get for EQ_EXPR or BIT_XOR_EXPR operation the second operand.
1654 If CODE isn't an EQ_EXPR, BIT_XOR_EXPR, or BIT_NOT_EXPR, then return. */
1655 if (code
== EQ_EXPR
|| code
== NE_EXPR
1656 || code
== BIT_XOR_EXPR
)
1657 op2
= gimple_assign_rhs2 (def
);
1662 if (truth_valued_ssa_name (name
))
1666 /* Check if we have X == 0 and X has an integral type. */
1667 if (!INTEGRAL_TYPE_P (TREE_TYPE (op1
)))
1669 if (integer_zerop (op2
))
1673 /* Check if we have X != 1 and X is a truth-valued. */
1674 if (!INTEGRAL_TYPE_P (TREE_TYPE (op1
)))
1676 if (integer_onep (op2
) && truth_valued_ssa_name (op1
))
1680 /* Check if we have X ^ 1 and X is truth valued. */
1681 if (integer_onep (op2
) && truth_valued_ssa_name (op1
))
1691 /* Optimize ARG1 CODE ARG2 to a constant for bitwise binary
1692 operations CODE, if one operand has the logically inverted
1693 value of the other. */
1695 simplify_bitwise_binary_1 (enum tree_code code
, tree type
,
1696 tree arg1
, tree arg2
)
1700 /* If CODE isn't a bitwise binary operation, return NULL_TREE. */
1701 if (code
!= BIT_AND_EXPR
&& code
!= BIT_IOR_EXPR
1702 && code
!= BIT_XOR_EXPR
)
1705 /* First check if operands ARG1 and ARG2 are equal. If so
1706 return NULL_TREE as this optimization is handled fold_stmt. */
1709 /* See if we have in arguments logical-not patterns. */
1710 if (((anot
= lookup_logical_inverted_value (arg1
)) == NULL_TREE
1712 && ((anot
= lookup_logical_inverted_value (arg2
)) == NULL_TREE
1717 if (code
== BIT_AND_EXPR
)
1718 return fold_convert (type
, integer_zero_node
);
1719 /* X | !X -> 1 and X ^ !X -> 1, if X is truth-valued. */
1720 if (truth_valued_ssa_name (anot
))
1721 return fold_convert (type
, integer_one_node
);
1723 /* ??? Otherwise result is (X != 0 ? X : 1). not handled. */
1727 /* Given a ssa_name in NAME see if it was defined by an assignment and
1728 set CODE to be the code and ARG1 to the first operand on the rhs and ARG2
1729 to the second operand on the rhs. */
1732 defcodefor_name (tree name
, enum tree_code
*code
, tree
*arg1
, tree
*arg2
)
1735 enum tree_code code1
;
1739 enum gimple_rhs_class grhs_class
;
1741 code1
= TREE_CODE (name
);
1744 grhs_class
= get_gimple_rhs_class (code1
);
1746 if (code1
== SSA_NAME
)
1748 def
= SSA_NAME_DEF_STMT (name
);
1750 if (def
&& is_gimple_assign (def
)
1751 && can_propagate_from (def
))
1753 code1
= gimple_assign_rhs_code (def
);
1754 arg11
= gimple_assign_rhs1 (def
);
1755 arg21
= gimple_assign_rhs2 (def
);
1756 arg31
= gimple_assign_rhs2 (def
);
1759 else if (grhs_class
== GIMPLE_TERNARY_RHS
1760 || GIMPLE_BINARY_RHS
1762 || GIMPLE_SINGLE_RHS
)
1763 extract_ops_from_tree_1 (name
, &code1
, &arg11
, &arg21
, &arg31
);
1769 /* Ignore arg3 currently. */
1772 /* Simplify bitwise binary operations.
1773 Return true if a transformation applied, otherwise return false. */
1776 simplify_bitwise_binary (gimple_stmt_iterator
*gsi
)
1778 gimple stmt
= gsi_stmt (*gsi
);
1779 tree arg1
= gimple_assign_rhs1 (stmt
);
1780 tree arg2
= gimple_assign_rhs2 (stmt
);
1781 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1783 tree def1_arg1
, def1_arg2
, def2_arg1
, def2_arg2
;
1784 enum tree_code def1_code
, def2_code
;
1786 defcodefor_name (arg1
, &def1_code
, &def1_arg1
, &def1_arg2
);
1787 defcodefor_name (arg2
, &def2_code
, &def2_arg1
, &def2_arg2
);
1789 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST)). */
1790 if (TREE_CODE (arg2
) == INTEGER_CST
1791 && CONVERT_EXPR_CODE_P (def1_code
)
1792 && INTEGRAL_TYPE_P (TREE_TYPE (def1_arg1
))
1793 && int_fits_type_p (arg2
, TREE_TYPE (def1_arg1
)))
1796 tree tem
= make_ssa_name (TREE_TYPE (def1_arg1
), NULL
);
1798 gimple_build_assign_with_ops (code
, tem
, def1_arg1
,
1799 fold_convert_loc (gimple_location (stmt
),
1800 TREE_TYPE (def1_arg1
),
1802 gimple_set_location (newop
, gimple_location (stmt
));
1803 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
1804 gimple_assign_set_rhs_with_ops_1 (gsi
, NOP_EXPR
,
1805 tem
, NULL_TREE
, NULL_TREE
);
1806 update_stmt (gsi_stmt (*gsi
));
1810 /* For bitwise binary operations apply operand conversions to the
1811 binary operation result instead of to the operands. This allows
1812 to combine successive conversions and bitwise binary operations. */
1813 if (CONVERT_EXPR_CODE_P (def1_code
)
1814 && CONVERT_EXPR_CODE_P (def2_code
)
1815 && types_compatible_p (TREE_TYPE (def1_arg1
), TREE_TYPE (def2_arg1
))
1816 /* Make sure that the conversion widens the operands, or has same
1817 precision, or that it changes the operation to a bitfield
1819 && ((TYPE_PRECISION (TREE_TYPE (def1_arg1
))
1820 <= TYPE_PRECISION (TREE_TYPE (arg1
)))
1821 || (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (arg1
)))
1823 || (TYPE_PRECISION (TREE_TYPE (arg1
))
1824 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg1
))))))
1827 tree tem
= make_ssa_name (TREE_TYPE (def1_arg1
), NULL
);
1828 newop
= gimple_build_assign_with_ops (code
, tem
, def1_arg1
, def2_arg1
);
1829 gimple_set_location (newop
, gimple_location (stmt
));
1830 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
1831 gimple_assign_set_rhs_with_ops_1 (gsi
, NOP_EXPR
,
1832 tem
, NULL_TREE
, NULL_TREE
);
1833 update_stmt (gsi_stmt (*gsi
));
1838 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
1839 if (def1_code
== def2_code
1840 && def1_code
== BIT_AND_EXPR
1841 && operand_equal_for_phi_arg_p (def1_arg2
,
1847 tree inner
= fold_build2 (code
, TREE_TYPE (arg2
), a
, c
);
1848 /* If A OP0 C (this usually means C is the same as A) is 0
1849 then fold it down correctly. */
1850 if (integer_zerop (inner
))
1852 gimple_assign_set_rhs_from_tree (gsi
, inner
);
1856 /* If A OP0 C (this usually means C is the same as A) is a ssa_name
1857 then fold it down correctly. */
1858 else if (TREE_CODE (inner
) == SSA_NAME
)
1860 tree outer
= fold_build2 (def1_code
, TREE_TYPE (inner
),
1862 gimple_assign_set_rhs_from_tree (gsi
, outer
);
1870 tem
= make_ssa_name (TREE_TYPE (arg2
), NULL
);
1871 newop
= gimple_build_assign_with_ops (code
, tem
, a
, c
);
1872 gimple_set_location (newop
, gimple_location (stmt
));
1873 /* Make sure to re-process the new stmt as it's walking upwards. */
1874 gsi_insert_before (gsi
, newop
, GSI_NEW_STMT
);
1875 gimple_assign_set_rhs1 (stmt
, tem
);
1876 gimple_assign_set_rhs2 (stmt
, b
);
1877 gimple_assign_set_rhs_code (stmt
, def1_code
);
1883 /* (a | CST1) & CST2 -> (a & CST2) | (CST1 & CST2). */
1884 if (code
== BIT_AND_EXPR
1885 && def1_code
== BIT_IOR_EXPR
1886 && TREE_CODE (arg2
) == INTEGER_CST
1887 && TREE_CODE (def1_arg2
) == INTEGER_CST
)
1889 tree cst
= fold_build2 (BIT_AND_EXPR
, TREE_TYPE (arg2
),
1893 if (integer_zerop (cst
))
1895 gimple_assign_set_rhs1 (stmt
, def1_arg1
);
1899 tem
= make_ssa_name (TREE_TYPE (arg2
), NULL
);
1900 newop
= gimple_build_assign_with_ops (BIT_AND_EXPR
,
1901 tem
, def1_arg1
, arg2
);
1902 gimple_set_location (newop
, gimple_location (stmt
));
1903 /* Make sure to re-process the new stmt as it's walking upwards. */
1904 gsi_insert_before (gsi
, newop
, GSI_NEW_STMT
);
1905 gimple_assign_set_rhs1 (stmt
, tem
);
1906 gimple_assign_set_rhs2 (stmt
, cst
);
1907 gimple_assign_set_rhs_code (stmt
, BIT_IOR_EXPR
);
1912 /* Combine successive equal operations with constants. */
1913 if ((code
== BIT_AND_EXPR
1914 || code
== BIT_IOR_EXPR
1915 || code
== BIT_XOR_EXPR
)
1916 && def1_code
== code
1917 && TREE_CODE (arg2
) == INTEGER_CST
1918 && TREE_CODE (def1_arg2
) == INTEGER_CST
)
1920 tree cst
= fold_build2 (code
, TREE_TYPE (arg2
),
1922 gimple_assign_set_rhs1 (stmt
, def1_arg1
);
1923 gimple_assign_set_rhs2 (stmt
, cst
);
1928 /* Canonicalize X ^ ~0 to ~X. */
1929 if (code
== BIT_XOR_EXPR
1930 && TREE_CODE (arg2
) == INTEGER_CST
1931 && integer_all_onesp (arg2
))
1933 gimple_assign_set_rhs_with_ops (gsi
, BIT_NOT_EXPR
, arg1
, NULL_TREE
);
1934 gcc_assert (gsi_stmt (*gsi
) == stmt
);
1939 /* Try simple folding for X op !X, and X op X. */
1940 res
= simplify_bitwise_binary_1 (code
, TREE_TYPE (arg1
), arg1
, arg2
);
1941 if (res
!= NULL_TREE
)
1943 gimple_assign_set_rhs_from_tree (gsi
, res
);
1944 update_stmt (gsi_stmt (*gsi
));
1948 if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
)
1950 enum tree_code ocode
= code
== BIT_AND_EXPR
? BIT_IOR_EXPR
: BIT_AND_EXPR
;
1951 if (def1_code
== ocode
)
1954 enum tree_code coden
;
1956 /* ( X | Y) & X -> X */
1957 /* ( X & Y) | X -> X */
1961 gimple_assign_set_rhs_from_tree (gsi
, x
);
1962 update_stmt (gsi_stmt (*gsi
));
1966 defcodefor_name (def1_arg1
, &coden
, &a1
, &a2
);
1967 /* (~X | Y) & X -> X & Y */
1968 /* (~X & Y) | X -> X | Y */
1969 if (coden
== BIT_NOT_EXPR
&& a1
== x
)
1971 gimple_assign_set_rhs_with_ops (gsi
, code
,
1973 gcc_assert (gsi_stmt (*gsi
) == stmt
);
1977 defcodefor_name (def1_arg2
, &coden
, &a1
, &a2
);
1978 /* (Y | ~X) & X -> X & Y */
1979 /* (Y & ~X) | X -> X | Y */
1980 if (coden
== BIT_NOT_EXPR
&& a1
== x
)
1982 gimple_assign_set_rhs_with_ops (gsi
, code
,
1984 gcc_assert (gsi_stmt (*gsi
) == stmt
);
1989 if (def2_code
== ocode
)
1991 enum tree_code coden
;
1994 /* X & ( X | Y) -> X */
1995 /* X | ( X & Y) -> X */
1999 gimple_assign_set_rhs_from_tree (gsi
, x
);
2000 update_stmt (gsi_stmt (*gsi
));
2003 defcodefor_name (def2_arg1
, &coden
, &a1
, NULL
);
2004 /* (~X | Y) & X -> X & Y */
2005 /* (~X & Y) | X -> X | Y */
2006 if (coden
== BIT_NOT_EXPR
&& a1
== x
)
2008 gimple_assign_set_rhs_with_ops (gsi
, code
,
2010 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2014 defcodefor_name (def2_arg2
, &coden
, &a1
, NULL
);
2015 /* (Y | ~X) & X -> X & Y */
2016 /* (Y & ~X) | X -> X | Y */
2017 if (coden
== BIT_NOT_EXPR
&& a1
== x
)
2019 gimple_assign_set_rhs_with_ops (gsi
, code
,
2021 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2032 /* Perform re-associations of the plus or minus statement STMT that are
2033 always permitted. Returns true if the CFG was changed. */
2036 associate_plusminus (gimple_stmt_iterator
*gsi
)
2038 gimple stmt
= gsi_stmt (*gsi
);
2039 tree rhs1
= gimple_assign_rhs1 (stmt
);
2040 tree rhs2
= gimple_assign_rhs2 (stmt
);
2041 enum tree_code code
= gimple_assign_rhs_code (stmt
);
2044 /* We can't reassociate at all for saturating types. */
2045 if (TYPE_SATURATING (TREE_TYPE (rhs1
)))
2048 /* First contract negates. */
2053 /* A +- (-B) -> A -+ B. */
2054 if (TREE_CODE (rhs2
) == SSA_NAME
)
2056 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs2
);
2057 if (is_gimple_assign (def_stmt
)
2058 && gimple_assign_rhs_code (def_stmt
) == NEGATE_EXPR
2059 && can_propagate_from (def_stmt
))
2061 code
= (code
== MINUS_EXPR
) ? PLUS_EXPR
: MINUS_EXPR
;
2062 gimple_assign_set_rhs_code (stmt
, code
);
2063 rhs2
= gimple_assign_rhs1 (def_stmt
);
2064 gimple_assign_set_rhs2 (stmt
, rhs2
);
2065 gimple_set_modified (stmt
, true);
2070 /* (-A) + B -> B - A. */
2071 if (TREE_CODE (rhs1
) == SSA_NAME
2072 && code
== PLUS_EXPR
)
2074 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs1
);
2075 if (is_gimple_assign (def_stmt
)
2076 && gimple_assign_rhs_code (def_stmt
) == NEGATE_EXPR
2077 && can_propagate_from (def_stmt
))
2080 gimple_assign_set_rhs_code (stmt
, code
);
2082 gimple_assign_set_rhs1 (stmt
, rhs1
);
2083 rhs2
= gimple_assign_rhs1 (def_stmt
);
2084 gimple_assign_set_rhs2 (stmt
, rhs2
);
2085 gimple_set_modified (stmt
, true);
2092 /* We can't reassociate floating-point or fixed-point plus or minus
2093 because of saturation to +-Inf. */
2094 if (FLOAT_TYPE_P (TREE_TYPE (rhs1
))
2095 || FIXED_POINT_TYPE_P (TREE_TYPE (rhs1
)))
2098 /* Second match patterns that allow contracting a plus-minus pair
2099 irrespective of overflow issues.
2101 (A +- B) - A -> +- B
2103 (CST +- A) +- CST -> CST +- A
2104 (A + CST) +- CST -> A + CST
2107 A - (A +- B) -> -+ B
2108 A +- (B +- A) -> +- B
2109 CST +- (CST +- A) -> CST +- A
2110 CST +- (A +- CST) -> CST +- A
2113 via commutating the addition and contracting operations to zero
2114 by reassociation. */
2116 if (TREE_CODE (rhs1
) == SSA_NAME
)
2118 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs1
);
2119 if (is_gimple_assign (def_stmt
) && can_propagate_from (def_stmt
))
2121 enum tree_code def_code
= gimple_assign_rhs_code (def_stmt
);
2122 if (def_code
== PLUS_EXPR
2123 || def_code
== MINUS_EXPR
)
2125 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2126 tree def_rhs2
= gimple_assign_rhs2 (def_stmt
);
2127 if (operand_equal_p (def_rhs1
, rhs2
, 0)
2128 && code
== MINUS_EXPR
)
2130 /* (A +- B) - A -> +- B. */
2131 code
= ((def_code
== PLUS_EXPR
)
2132 ? TREE_CODE (def_rhs2
) : NEGATE_EXPR
);
2135 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2136 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2137 gimple_set_modified (stmt
, true);
2139 else if (operand_equal_p (def_rhs2
, rhs2
, 0)
2140 && code
!= def_code
)
2142 /* (A +- B) -+ B -> A. */
2143 code
= TREE_CODE (def_rhs1
);
2146 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2147 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2148 gimple_set_modified (stmt
, true);
2150 else if (TREE_CODE (rhs2
) == INTEGER_CST
2151 && TREE_CODE (def_rhs1
) == INTEGER_CST
)
2153 /* (CST +- A) +- CST -> CST +- A. */
2154 tree cst
= fold_binary (code
, TREE_TYPE (rhs1
),
2156 if (cst
&& !TREE_OVERFLOW (cst
))
2159 gimple_assign_set_rhs_code (stmt
, code
);
2161 gimple_assign_set_rhs1 (stmt
, rhs1
);
2163 gimple_assign_set_rhs2 (stmt
, rhs2
);
2164 gimple_set_modified (stmt
, true);
2167 else if (TREE_CODE (rhs2
) == INTEGER_CST
2168 && TREE_CODE (def_rhs2
) == INTEGER_CST
2169 && def_code
== PLUS_EXPR
)
2171 /* (A + CST) +- CST -> A + CST. */
2172 tree cst
= fold_binary (code
, TREE_TYPE (rhs1
),
2174 if (cst
&& !TREE_OVERFLOW (cst
))
2177 gimple_assign_set_rhs_code (stmt
, code
);
2179 gimple_assign_set_rhs1 (stmt
, rhs1
);
2181 gimple_assign_set_rhs2 (stmt
, rhs2
);
2182 gimple_set_modified (stmt
, true);
2186 else if (def_code
== BIT_NOT_EXPR
2187 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
2189 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2190 if (code
== PLUS_EXPR
2191 && operand_equal_p (def_rhs1
, rhs2
, 0))
2195 rhs1
= build_int_cst_type (TREE_TYPE (rhs2
), -1);
2197 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2198 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2199 gimple_set_modified (stmt
, true);
2201 else if (code
== PLUS_EXPR
2202 && integer_onep (rhs1
))
2208 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2209 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2210 gimple_set_modified (stmt
, true);
2216 if (rhs2
&& TREE_CODE (rhs2
) == SSA_NAME
)
2218 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs2
);
2219 if (is_gimple_assign (def_stmt
) && can_propagate_from (def_stmt
))
2221 enum tree_code def_code
= gimple_assign_rhs_code (def_stmt
);
2222 if (def_code
== PLUS_EXPR
2223 || def_code
== MINUS_EXPR
)
2225 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2226 tree def_rhs2
= gimple_assign_rhs2 (def_stmt
);
2227 if (operand_equal_p (def_rhs1
, rhs1
, 0)
2228 && code
== MINUS_EXPR
)
2230 /* A - (A +- B) -> -+ B. */
2231 code
= ((def_code
== PLUS_EXPR
)
2232 ? NEGATE_EXPR
: TREE_CODE (def_rhs2
));
2235 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2236 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2237 gimple_set_modified (stmt
, true);
2239 else if (operand_equal_p (def_rhs2
, rhs1
, 0)
2240 && code
!= def_code
)
2242 /* A +- (B +- A) -> +- B. */
2243 code
= ((code
== PLUS_EXPR
)
2244 ? TREE_CODE (def_rhs1
) : NEGATE_EXPR
);
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 (TREE_CODE (rhs1
) == INTEGER_CST
2252 && TREE_CODE (def_rhs1
) == INTEGER_CST
)
2254 /* CST +- (CST +- A) -> CST +- A. */
2255 tree cst
= fold_binary (code
, TREE_TYPE (rhs2
),
2257 if (cst
&& !TREE_OVERFLOW (cst
))
2259 code
= (code
== def_code
? PLUS_EXPR
: MINUS_EXPR
);
2260 gimple_assign_set_rhs_code (stmt
, code
);
2262 gimple_assign_set_rhs1 (stmt
, rhs1
);
2264 gimple_assign_set_rhs2 (stmt
, rhs2
);
2265 gimple_set_modified (stmt
, true);
2268 else if (TREE_CODE (rhs1
) == INTEGER_CST
2269 && TREE_CODE (def_rhs2
) == INTEGER_CST
)
2271 /* CST +- (A +- CST) -> CST +- A. */
2272 tree cst
= fold_binary (def_code
== code
2273 ? PLUS_EXPR
: MINUS_EXPR
,
2276 if (cst
&& !TREE_OVERFLOW (cst
))
2279 gimple_assign_set_rhs1 (stmt
, rhs1
);
2281 gimple_assign_set_rhs2 (stmt
, rhs2
);
2282 gimple_set_modified (stmt
, true);
2286 else if (def_code
== BIT_NOT_EXPR
2287 && INTEGRAL_TYPE_P (TREE_TYPE (rhs2
)))
2289 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2290 if (code
== PLUS_EXPR
2291 && operand_equal_p (def_rhs1
, rhs1
, 0))
2295 rhs1
= build_int_cst_type (TREE_TYPE (rhs1
), -1);
2297 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2298 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2299 gimple_set_modified (stmt
, true);
2306 if (gimple_modified_p (stmt
))
2308 fold_stmt_inplace (gsi
);
2310 if (maybe_clean_or_replace_eh_stmt (stmt
, stmt
)
2311 && gimple_purge_dead_eh_edges (gimple_bb (stmt
)))
2318 /* Associate operands of a POINTER_PLUS_EXPR assignmen at *GSI. Returns
2319 true if anything changed, false otherwise. */
2322 associate_pointerplus (gimple_stmt_iterator
*gsi
)
2324 gimple stmt
= gsi_stmt (*gsi
);
2326 tree ptr
, rhs
, algn
;
2329 tem = (sizetype) ptr;
2333 and produce the simpler and easier to analyze with respect to alignment
2334 ... = ptr & ~algn; */
2335 ptr
= gimple_assign_rhs1 (stmt
);
2336 rhs
= gimple_assign_rhs2 (stmt
);
2337 if (TREE_CODE (rhs
) != SSA_NAME
)
2339 def_stmt
= SSA_NAME_DEF_STMT (rhs
);
2340 if (!is_gimple_assign (def_stmt
)
2341 || gimple_assign_rhs_code (def_stmt
) != NEGATE_EXPR
)
2343 rhs
= gimple_assign_rhs1 (def_stmt
);
2344 if (TREE_CODE (rhs
) != SSA_NAME
)
2346 def_stmt
= SSA_NAME_DEF_STMT (rhs
);
2347 if (!is_gimple_assign (def_stmt
)
2348 || gimple_assign_rhs_code (def_stmt
) != BIT_AND_EXPR
)
2350 rhs
= gimple_assign_rhs1 (def_stmt
);
2351 algn
= gimple_assign_rhs2 (def_stmt
);
2352 if (TREE_CODE (rhs
) != SSA_NAME
2353 || TREE_CODE (algn
) != INTEGER_CST
)
2355 def_stmt
= SSA_NAME_DEF_STMT (rhs
);
2356 if (!is_gimple_assign (def_stmt
)
2357 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
2359 if (gimple_assign_rhs1 (def_stmt
) != ptr
)
2362 algn
= double_int_to_tree (TREE_TYPE (ptr
), ~tree_to_double_int (algn
));
2363 gimple_assign_set_rhs_with_ops (gsi
, BIT_AND_EXPR
, ptr
, algn
);
2364 fold_stmt_inplace (gsi
);
2370 /* Combine two conversions in a row for the second conversion at *GSI.
2371 Returns 1 if there were any changes made, 2 if cfg-cleanup needs to
2372 run. Else it returns 0. */
2375 combine_conversions (gimple_stmt_iterator
*gsi
)
2377 gimple stmt
= gsi_stmt (*gsi
);
2380 enum tree_code code
= gimple_assign_rhs_code (stmt
);
2381 enum tree_code code2
;
2383 gcc_checking_assert (CONVERT_EXPR_CODE_P (code
)
2384 || code
== FLOAT_EXPR
2385 || code
== FIX_TRUNC_EXPR
);
2387 lhs
= gimple_assign_lhs (stmt
);
2388 op0
= gimple_assign_rhs1 (stmt
);
2389 if (useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
)))
2391 gimple_assign_set_rhs_code (stmt
, TREE_CODE (op0
));
2395 if (TREE_CODE (op0
) != SSA_NAME
)
2398 def_stmt
= SSA_NAME_DEF_STMT (op0
);
2399 if (!is_gimple_assign (def_stmt
))
2402 code2
= gimple_assign_rhs_code (def_stmt
);
2404 if (CONVERT_EXPR_CODE_P (code2
) || code2
== FLOAT_EXPR
)
2406 tree defop0
= gimple_assign_rhs1 (def_stmt
);
2407 tree type
= TREE_TYPE (lhs
);
2408 tree inside_type
= TREE_TYPE (defop0
);
2409 tree inter_type
= TREE_TYPE (op0
);
2410 int inside_int
= INTEGRAL_TYPE_P (inside_type
);
2411 int inside_ptr
= POINTER_TYPE_P (inside_type
);
2412 int inside_float
= FLOAT_TYPE_P (inside_type
);
2413 int inside_vec
= TREE_CODE (inside_type
) == VECTOR_TYPE
;
2414 unsigned int inside_prec
= TYPE_PRECISION (inside_type
);
2415 int inside_unsignedp
= TYPE_UNSIGNED (inside_type
);
2416 int inter_int
= INTEGRAL_TYPE_P (inter_type
);
2417 int inter_ptr
= POINTER_TYPE_P (inter_type
);
2418 int inter_float
= FLOAT_TYPE_P (inter_type
);
2419 int inter_vec
= TREE_CODE (inter_type
) == VECTOR_TYPE
;
2420 unsigned int inter_prec
= TYPE_PRECISION (inter_type
);
2421 int inter_unsignedp
= TYPE_UNSIGNED (inter_type
);
2422 int final_int
= INTEGRAL_TYPE_P (type
);
2423 int final_ptr
= POINTER_TYPE_P (type
);
2424 int final_float
= FLOAT_TYPE_P (type
);
2425 int final_vec
= TREE_CODE (type
) == VECTOR_TYPE
;
2426 unsigned int final_prec
= TYPE_PRECISION (type
);
2427 int final_unsignedp
= TYPE_UNSIGNED (type
);
2429 /* Don't propagate ssa names that occur in abnormal phis. */
2430 if (TREE_CODE (defop0
) == SSA_NAME
2431 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (defop0
))
2434 /* In addition to the cases of two conversions in a row
2435 handled below, if we are converting something to its own
2436 type via an object of identical or wider precision, neither
2437 conversion is needed. */
2438 if (useless_type_conversion_p (type
, inside_type
)
2439 && (((inter_int
|| inter_ptr
) && final_int
)
2440 || (inter_float
&& final_float
))
2441 && inter_prec
>= final_prec
)
2443 gimple_assign_set_rhs1 (stmt
, unshare_expr (defop0
));
2444 gimple_assign_set_rhs_code (stmt
, TREE_CODE (defop0
));
2446 return remove_prop_source_from_use (op0
) ? 2 : 1;
2449 /* Likewise, if the intermediate and initial types are either both
2450 float or both integer, we don't need the middle conversion if the
2451 former is wider than the latter and doesn't change the signedness
2452 (for integers). Avoid this if the final type is a pointer since
2453 then we sometimes need the middle conversion. Likewise if the
2454 final type has a precision not equal to the size of its mode. */
2455 if (((inter_int
&& inside_int
)
2456 || (inter_float
&& inside_float
)
2457 || (inter_vec
&& inside_vec
))
2458 && inter_prec
>= inside_prec
2459 && (inter_float
|| inter_vec
2460 || inter_unsignedp
== inside_unsignedp
)
2461 && ! (final_prec
!= GET_MODE_PRECISION (TYPE_MODE (type
))
2462 && TYPE_MODE (type
) == TYPE_MODE (inter_type
))
2464 && (! final_vec
|| inter_prec
== inside_prec
))
2466 gimple_assign_set_rhs1 (stmt
, defop0
);
2468 return remove_prop_source_from_use (op0
) ? 2 : 1;
2471 /* If we have a sign-extension of a zero-extended value, we can
2472 replace that by a single zero-extension. Likewise if the
2473 final conversion does not change precision we can drop the
2474 intermediate conversion. */
2475 if (inside_int
&& inter_int
&& final_int
2476 && ((inside_prec
< inter_prec
&& inter_prec
< final_prec
2477 && inside_unsignedp
&& !inter_unsignedp
)
2478 || final_prec
== inter_prec
))
2480 gimple_assign_set_rhs1 (stmt
, defop0
);
2482 return remove_prop_source_from_use (op0
) ? 2 : 1;
2485 /* Two conversions in a row are not needed unless:
2486 - some conversion is floating-point (overstrict for now), or
2487 - some conversion is a vector (overstrict for now), or
2488 - the intermediate type is narrower than both initial and
2490 - the intermediate type and innermost type differ in signedness,
2491 and the outermost type is wider than the intermediate, or
2492 - the initial type is a pointer type and the precisions of the
2493 intermediate and final types differ, or
2494 - the final type is a pointer type and the precisions of the
2495 initial and intermediate types differ. */
2496 if (! inside_float
&& ! inter_float
&& ! final_float
2497 && ! inside_vec
&& ! inter_vec
&& ! final_vec
2498 && (inter_prec
>= inside_prec
|| inter_prec
>= final_prec
)
2499 && ! (inside_int
&& inter_int
2500 && inter_unsignedp
!= inside_unsignedp
2501 && inter_prec
< final_prec
)
2502 && ((inter_unsignedp
&& inter_prec
> inside_prec
)
2503 == (final_unsignedp
&& final_prec
> inter_prec
))
2504 && ! (inside_ptr
&& inter_prec
!= final_prec
)
2505 && ! (final_ptr
&& inside_prec
!= inter_prec
)
2506 && ! (final_prec
!= GET_MODE_PRECISION (TYPE_MODE (type
))
2507 && TYPE_MODE (type
) == TYPE_MODE (inter_type
)))
2509 gimple_assign_set_rhs1 (stmt
, defop0
);
2511 return remove_prop_source_from_use (op0
) ? 2 : 1;
2514 /* A truncation to an unsigned type should be canonicalized as
2515 bitwise and of a mask. */
2516 if (final_int
&& inter_int
&& inside_int
2517 && final_prec
== inside_prec
2518 && final_prec
> inter_prec
2522 tem
= fold_build2 (BIT_AND_EXPR
, inside_type
,
2525 (inside_type
, double_int::mask (inter_prec
)));
2526 if (!useless_type_conversion_p (type
, inside_type
))
2528 tem
= force_gimple_operand_gsi (gsi
, tem
, true, NULL_TREE
, true,
2530 gimple_assign_set_rhs1 (stmt
, tem
);
2533 gimple_assign_set_rhs_from_tree (gsi
, tem
);
2534 update_stmt (gsi_stmt (*gsi
));
2538 /* If we are converting an integer to a floating-point that can
2539 represent it exactly and back to an integer, we can skip the
2540 floating-point conversion. */
2541 if (inside_int
&& inter_float
&& final_int
&&
2542 (unsigned) significand_size (TYPE_MODE (inter_type
))
2543 >= inside_prec
- !inside_unsignedp
)
2545 if (useless_type_conversion_p (type
, inside_type
))
2547 gimple_assign_set_rhs1 (stmt
, unshare_expr (defop0
));
2548 gimple_assign_set_rhs_code (stmt
, TREE_CODE (defop0
));
2550 return remove_prop_source_from_use (op0
) ? 2 : 1;
2554 gimple_assign_set_rhs1 (stmt
, defop0
);
2555 gimple_assign_set_rhs_code (stmt
, CONVERT_EXPR
);
2557 return remove_prop_source_from_use (op0
) ? 2 : 1;
2565 /* Combine an element access with a shuffle. Returns true if there were
2566 any changes made, else it returns false. */
2569 simplify_bitfield_ref (gimple_stmt_iterator
*gsi
)
2571 gimple stmt
= gsi_stmt (*gsi
);
2573 tree op
, op0
, op1
, op2
;
2575 unsigned idx
, n
, size
;
2576 enum tree_code code
;
2578 op
= gimple_assign_rhs1 (stmt
);
2579 gcc_checking_assert (TREE_CODE (op
) == BIT_FIELD_REF
);
2581 op0
= TREE_OPERAND (op
, 0);
2582 if (TREE_CODE (op0
) != SSA_NAME
2583 || TREE_CODE (TREE_TYPE (op0
)) != VECTOR_TYPE
)
2586 def_stmt
= get_prop_source_stmt (op0
, false, NULL
);
2587 if (!def_stmt
|| !can_propagate_from (def_stmt
))
2590 op1
= TREE_OPERAND (op
, 1);
2591 op2
= TREE_OPERAND (op
, 2);
2592 code
= gimple_assign_rhs_code (def_stmt
);
2594 if (code
== CONSTRUCTOR
)
2596 tree tem
= fold_ternary (BIT_FIELD_REF
, TREE_TYPE (op
),
2597 gimple_assign_rhs1 (def_stmt
), op1
, op2
);
2598 if (!tem
|| !valid_gimple_rhs_p (tem
))
2600 gimple_assign_set_rhs_from_tree (gsi
, tem
);
2601 update_stmt (gsi_stmt (*gsi
));
2605 elem_type
= TREE_TYPE (TREE_TYPE (op0
));
2606 if (TREE_TYPE (op
) != elem_type
)
2609 size
= TREE_INT_CST_LOW (TYPE_SIZE (elem_type
));
2610 n
= TREE_INT_CST_LOW (op1
) / size
;
2613 idx
= TREE_INT_CST_LOW (op2
) / size
;
2615 if (code
== VEC_PERM_EXPR
)
2617 tree p
, m
, index
, tem
;
2619 m
= gimple_assign_rhs3 (def_stmt
);
2620 if (TREE_CODE (m
) != VECTOR_CST
)
2622 nelts
= VECTOR_CST_NELTS (m
);
2623 idx
= TREE_INT_CST_LOW (VECTOR_CST_ELT (m
, idx
));
2627 p
= gimple_assign_rhs1 (def_stmt
);
2631 p
= gimple_assign_rhs2 (def_stmt
);
2634 index
= build_int_cst (TREE_TYPE (TREE_TYPE (m
)), idx
* size
);
2635 tem
= build3 (BIT_FIELD_REF
, TREE_TYPE (op
),
2636 unshare_expr (p
), op1
, index
);
2637 gimple_assign_set_rhs1 (stmt
, tem
);
2639 update_stmt (gsi_stmt (*gsi
));
2646 /* Determine whether applying the 2 permutations (mask1 then mask2)
2647 gives back one of the input. */
2650 is_combined_permutation_identity (tree mask1
, tree mask2
)
2653 unsigned int nelts
, i
, j
;
2654 bool maybe_identity1
= true;
2655 bool maybe_identity2
= true;
2657 gcc_checking_assert (TREE_CODE (mask1
) == VECTOR_CST
2658 && TREE_CODE (mask2
) == VECTOR_CST
);
2659 mask
= fold_ternary (VEC_PERM_EXPR
, TREE_TYPE (mask1
), mask1
, mask1
, mask2
);
2660 gcc_assert (TREE_CODE (mask
) == VECTOR_CST
);
2662 nelts
= VECTOR_CST_NELTS (mask
);
2663 for (i
= 0; i
< nelts
; i
++)
2665 tree val
= VECTOR_CST_ELT (mask
, i
);
2666 gcc_assert (TREE_CODE (val
) == INTEGER_CST
);
2667 j
= TREE_INT_CST_LOW (val
) & (2 * nelts
- 1);
2669 maybe_identity2
= false;
2670 else if (j
== i
+ nelts
)
2671 maybe_identity1
= false;
2675 return maybe_identity1
? 1 : maybe_identity2
? 2 : 0;
2678 /* Combine a shuffle with its arguments. Returns 1 if there were any
2679 changes made, 2 if cfg-cleanup needs to run. Else it returns 0. */
2682 simplify_permutation (gimple_stmt_iterator
*gsi
)
2684 gimple stmt
= gsi_stmt (*gsi
);
2686 tree op0
, op1
, op2
, op3
, arg0
, arg1
;
2687 enum tree_code code
;
2688 bool single_use_op0
= false;
2690 gcc_checking_assert (gimple_assign_rhs_code (stmt
) == VEC_PERM_EXPR
);
2692 op0
= gimple_assign_rhs1 (stmt
);
2693 op1
= gimple_assign_rhs2 (stmt
);
2694 op2
= gimple_assign_rhs3 (stmt
);
2696 if (TREE_CODE (op2
) != VECTOR_CST
)
2699 if (TREE_CODE (op0
) == VECTOR_CST
)
2704 else if (TREE_CODE (op0
) == SSA_NAME
)
2706 def_stmt
= get_prop_source_stmt (op0
, false, &single_use_op0
);
2707 if (!def_stmt
|| !can_propagate_from (def_stmt
))
2710 code
= gimple_assign_rhs_code (def_stmt
);
2711 arg0
= gimple_assign_rhs1 (def_stmt
);
2716 /* Two consecutive shuffles. */
2717 if (code
== VEC_PERM_EXPR
)
2724 op3
= gimple_assign_rhs3 (def_stmt
);
2725 if (TREE_CODE (op3
) != VECTOR_CST
)
2727 ident
= is_combined_permutation_identity (op3
, op2
);
2730 orig
= (ident
== 1) ? gimple_assign_rhs1 (def_stmt
)
2731 : gimple_assign_rhs2 (def_stmt
);
2732 gimple_assign_set_rhs1 (stmt
, unshare_expr (orig
));
2733 gimple_assign_set_rhs_code (stmt
, TREE_CODE (orig
));
2734 gimple_set_num_ops (stmt
, 2);
2736 return remove_prop_source_from_use (op0
) ? 2 : 1;
2739 /* Shuffle of a constructor. */
2740 else if (code
== CONSTRUCTOR
|| code
== VECTOR_CST
)
2746 if (TREE_CODE (op0
) == SSA_NAME
&& !single_use_op0
)
2749 if (TREE_CODE (op1
) == VECTOR_CST
)
2751 else if (TREE_CODE (op1
) == SSA_NAME
)
2753 enum tree_code code2
;
2755 gimple def_stmt2
= get_prop_source_stmt (op1
, true, NULL
);
2756 if (!def_stmt2
|| !can_propagate_from (def_stmt2
))
2759 code2
= gimple_assign_rhs_code (def_stmt2
);
2760 if (code2
!= CONSTRUCTOR
&& code2
!= VECTOR_CST
)
2762 arg1
= gimple_assign_rhs1 (def_stmt2
);
2769 /* Already used twice in this statement. */
2770 if (TREE_CODE (op0
) == SSA_NAME
&& num_imm_uses (op0
) > 2)
2774 opt
= fold_ternary (VEC_PERM_EXPR
, TREE_TYPE(op0
), arg0
, arg1
, op2
);
2776 || (TREE_CODE (opt
) != CONSTRUCTOR
&& TREE_CODE(opt
) != VECTOR_CST
))
2778 gimple_assign_set_rhs_from_tree (gsi
, opt
);
2779 update_stmt (gsi_stmt (*gsi
));
2780 if (TREE_CODE (op0
) == SSA_NAME
)
2781 ret
= remove_prop_source_from_use (op0
);
2782 if (op0
!= op1
&& TREE_CODE (op1
) == SSA_NAME
)
2783 ret
|= remove_prop_source_from_use (op1
);
2790 /* Recognize a VEC_PERM_EXPR. Returns true if there were any changes. */
2793 simplify_vector_constructor (gimple_stmt_iterator
*gsi
)
2795 gimple stmt
= gsi_stmt (*gsi
);
2797 tree op
, op2
, orig
, type
, elem_type
;
2798 unsigned elem_size
, nelts
, i
;
2799 enum tree_code code
;
2800 constructor_elt
*elt
;
2804 gcc_checking_assert (gimple_assign_rhs_code (stmt
) == CONSTRUCTOR
);
2806 op
= gimple_assign_rhs1 (stmt
);
2807 type
= TREE_TYPE (op
);
2808 gcc_checking_assert (TREE_CODE (type
) == VECTOR_TYPE
);
2810 nelts
= TYPE_VECTOR_SUBPARTS (type
);
2811 elem_type
= TREE_TYPE (type
);
2812 elem_size
= TREE_INT_CST_LOW (TYPE_SIZE (elem_type
));
2814 sel
= XALLOCAVEC (unsigned char, nelts
);
2817 FOR_EACH_VEC_ELT (constructor_elt
, CONSTRUCTOR_ELTS (op
), i
, elt
)
2824 if (TREE_CODE (elt
->value
) != SSA_NAME
)
2826 def_stmt
= get_prop_source_stmt (elt
->value
, false, NULL
);
2829 code
= gimple_assign_rhs_code (def_stmt
);
2830 if (code
!= BIT_FIELD_REF
)
2832 op1
= gimple_assign_rhs1 (def_stmt
);
2833 ref
= TREE_OPERAND (op1
, 0);
2841 if (TREE_CODE (ref
) != SSA_NAME
)
2845 if (TREE_INT_CST_LOW (TREE_OPERAND (op1
, 1)) != elem_size
)
2847 sel
[i
] = TREE_INT_CST_LOW (TREE_OPERAND (op1
, 2)) / elem_size
;
2848 if (sel
[i
] != i
) maybe_ident
= false;
2854 gimple_assign_set_rhs_from_tree (gsi
, orig
);
2857 tree mask_type
, *mask_elts
;
2859 if (!can_vec_perm_p (TYPE_MODE (type
), false, sel
))
2862 = build_vector_type (build_nonstandard_integer_type (elem_size
, 1),
2864 if (GET_MODE_CLASS (TYPE_MODE (mask_type
)) != MODE_VECTOR_INT
2865 || GET_MODE_SIZE (TYPE_MODE (mask_type
))
2866 != GET_MODE_SIZE (TYPE_MODE (type
)))
2868 mask_elts
= XALLOCAVEC (tree
, nelts
);
2869 for (i
= 0; i
< nelts
; i
++)
2870 mask_elts
[i
] = build_int_cst (TREE_TYPE (mask_type
), sel
[i
]);
2871 op2
= build_vector (mask_type
, mask_elts
);
2872 gimple_assign_set_rhs_with_ops_1 (gsi
, VEC_PERM_EXPR
, orig
, orig
, op2
);
2874 update_stmt (gsi_stmt (*gsi
));
2878 /* Main entry point for the forward propagation and statement combine
2882 ssa_forward_propagate_and_combine (void)
2885 unsigned int todoflags
= 0;
2887 cfg_changed
= false;
2891 gimple_stmt_iterator gsi
;
2893 /* Apply forward propagation to all stmts in the basic-block.
2894 Note we update GSI within the loop as necessary. */
2895 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
2897 gimple stmt
= gsi_stmt (gsi
);
2899 enum tree_code code
;
2901 if (!is_gimple_assign (stmt
))
2907 lhs
= gimple_assign_lhs (stmt
);
2908 rhs
= gimple_assign_rhs1 (stmt
);
2909 code
= gimple_assign_rhs_code (stmt
);
2910 if (TREE_CODE (lhs
) != SSA_NAME
2911 || has_zero_uses (lhs
))
2917 /* If this statement sets an SSA_NAME to an address,
2918 try to propagate the address into the uses of the SSA_NAME. */
2919 if (code
== ADDR_EXPR
2920 /* Handle pointer conversions on invariant addresses
2921 as well, as this is valid gimple. */
2922 || (CONVERT_EXPR_CODE_P (code
)
2923 && TREE_CODE (rhs
) == ADDR_EXPR
2924 && POINTER_TYPE_P (TREE_TYPE (lhs
))))
2926 tree base
= get_base_address (TREE_OPERAND (rhs
, 0));
2929 || decl_address_invariant_p (base
))
2930 && !stmt_references_abnormal_ssa_name (stmt
)
2931 && forward_propagate_addr_expr (lhs
, rhs
))
2933 release_defs (stmt
);
2934 todoflags
|= TODO_remove_unused_locals
;
2935 gsi_remove (&gsi
, true);
2940 else if (code
== POINTER_PLUS_EXPR
)
2942 tree off
= gimple_assign_rhs2 (stmt
);
2943 if (TREE_CODE (off
) == INTEGER_CST
2944 && can_propagate_from (stmt
)
2945 && !simple_iv_increment_p (stmt
)
2946 /* ??? Better adjust the interface to that function
2947 instead of building new trees here. */
2948 && forward_propagate_addr_expr
2950 build1_loc (gimple_location (stmt
),
2951 ADDR_EXPR
, TREE_TYPE (rhs
),
2952 fold_build2 (MEM_REF
,
2953 TREE_TYPE (TREE_TYPE (rhs
)),
2955 fold_convert (ptr_type_node
,
2958 release_defs (stmt
);
2959 todoflags
|= TODO_remove_unused_locals
;
2960 gsi_remove (&gsi
, true);
2962 else if (is_gimple_min_invariant (rhs
))
2964 /* Make sure to fold &a[0] + off_1 here. */
2965 fold_stmt_inplace (&gsi
);
2967 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
2973 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
2975 if (forward_propagate_comparison (&gsi
))
2982 /* Combine stmts with the stmts defining their operands.
2983 Note we update GSI within the loop as necessary. */
2984 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);)
2986 gimple stmt
= gsi_stmt (gsi
);
2987 bool changed
= false;
2989 /* Mark stmt as potentially needing revisiting. */
2990 gimple_set_plf (stmt
, GF_PLF_1
, false);
2992 switch (gimple_code (stmt
))
2996 tree rhs1
= gimple_assign_rhs1 (stmt
);
2997 enum tree_code code
= gimple_assign_rhs_code (stmt
);
2999 if ((code
== BIT_NOT_EXPR
3000 || code
== NEGATE_EXPR
)
3001 && TREE_CODE (rhs1
) == SSA_NAME
)
3002 changed
= simplify_not_neg_expr (&gsi
);
3003 else if (code
== COND_EXPR
3004 || code
== VEC_COND_EXPR
)
3006 /* In this case the entire COND_EXPR is in rhs1. */
3007 if (forward_propagate_into_cond (&gsi
)
3008 || combine_cond_exprs (&gsi
))
3011 stmt
= gsi_stmt (gsi
);
3014 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3017 did_something
= forward_propagate_into_comparison (&gsi
);
3018 if (did_something
== 2)
3020 changed
= did_something
!= 0;
3022 else if (code
== BIT_AND_EXPR
3023 || code
== BIT_IOR_EXPR
3024 || code
== BIT_XOR_EXPR
)
3025 changed
= simplify_bitwise_binary (&gsi
);
3026 else if (code
== PLUS_EXPR
3027 || code
== MINUS_EXPR
)
3028 changed
= associate_plusminus (&gsi
);
3029 else if (code
== POINTER_PLUS_EXPR
)
3030 changed
= associate_pointerplus (&gsi
);
3031 else if (CONVERT_EXPR_CODE_P (code
)
3032 || code
== FLOAT_EXPR
3033 || code
== FIX_TRUNC_EXPR
)
3035 int did_something
= combine_conversions (&gsi
);
3036 if (did_something
== 2)
3038 changed
= did_something
!= 0;
3040 else if (code
== VEC_PERM_EXPR
)
3042 int did_something
= simplify_permutation (&gsi
);
3043 if (did_something
== 2)
3045 changed
= did_something
!= 0;
3047 else if (code
== BIT_FIELD_REF
)
3048 changed
= simplify_bitfield_ref (&gsi
);
3049 else if (code
== CONSTRUCTOR
3050 && TREE_CODE (TREE_TYPE (rhs1
)) == VECTOR_TYPE
)
3051 changed
= simplify_vector_constructor (&gsi
);
3056 changed
= simplify_gimple_switch (stmt
);
3062 did_something
= forward_propagate_into_gimple_cond (stmt
);
3063 if (did_something
== 2)
3065 changed
= did_something
!= 0;
3071 tree callee
= gimple_call_fndecl (stmt
);
3072 if (callee
!= NULL_TREE
3073 && DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
3074 changed
= simplify_builtin_call (&gsi
, callee
);
3083 /* If the stmt changed then re-visit it and the statements
3084 inserted before it. */
3085 for (; !gsi_end_p (gsi
); gsi_prev (&gsi
))
3086 if (gimple_plf (gsi_stmt (gsi
), GF_PLF_1
))
3088 if (gsi_end_p (gsi
))
3089 gsi
= gsi_start_bb (bb
);
3095 /* Stmt no longer needs to be revisited. */
3096 gimple_set_plf (stmt
, GF_PLF_1
, true);
3103 todoflags
|= TODO_cleanup_cfg
;
3110 gate_forwprop (void)
3112 return flag_tree_forwprop
;
3115 struct gimple_opt_pass pass_forwprop
=
3119 "forwprop", /* name */
3120 gate_forwprop
, /* gate */
3121 ssa_forward_propagate_and_combine
, /* execute */
3124 0, /* static_pass_number */
3125 TV_TREE_FORWPROP
, /* tv_id */
3126 PROP_cfg
| PROP_ssa
, /* properties_required */
3127 0, /* properties_provided */
3128 0, /* properties_destroyed */
3129 0, /* todo_flags_start */
3132 | TODO_verify_ssa
/* todo_flags_finish */