1 /* Forward propagation of expressions for single use variables.
2 Copyright (C) 2004-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
25 #include "stor-layout.h"
27 #include "basic-block.h"
28 #include "gimple-pretty-print.h"
29 #include "tree-ssa-alias.h"
30 #include "internal-fn.h"
31 #include "gimple-fold.h"
33 #include "gimple-expr.h"
37 #include "gimple-iterator.h"
38 #include "gimplify-me.h"
39 #include "gimple-ssa.h"
41 #include "tree-phinodes.h"
42 #include "ssa-iterators.h"
43 #include "stringpool.h"
44 #include "tree-ssanames.h"
47 #include "tree-pass.h"
48 #include "langhooks.h"
53 #include "tree-ssa-propagate.h"
54 #include "tree-ssa-dom.h"
56 /* This pass propagates the RHS of assignment statements into use
57 sites of the LHS of the assignment. It's basically a specialized
58 form of tree combination. It is hoped all of this can disappear
59 when we have a generalized tree combiner.
61 One class of common cases we handle is forward propagating a single use
62 variable into a COND_EXPR.
66 if (x) goto ... else goto ...
68 Will be transformed into:
71 if (a COND b) goto ... else goto ...
73 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
75 Or (assuming c1 and c2 are constants):
79 if (x EQ/NEQ c2) goto ... else goto ...
81 Will be transformed into:
84 if (a EQ/NEQ (c2 - c1)) goto ... else goto ...
86 Similarly for x = a - c1.
92 if (x) goto ... else goto ...
94 Will be transformed into:
97 if (a == 0) goto ... else goto ...
99 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
100 For these cases, we propagate A into all, possibly more than one,
101 COND_EXPRs that use X.
107 if (x) goto ... else goto ...
109 Will be transformed into:
112 if (a != 0) goto ... else goto ...
114 (Assuming a is an integral type and x is a boolean or x is an
115 integral and a is a boolean.)
117 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
118 For these cases, we propagate A into all, possibly more than one,
119 COND_EXPRs that use X.
121 In addition to eliminating the variable and the statement which assigns
122 a value to the variable, we may be able to later thread the jump without
123 adding insane complexity in the dominator optimizer.
125 Also note these transformations can cascade. We handle this by having
126 a worklist of COND_EXPR statements to examine. As we make a change to
127 a statement, we put it back on the worklist to examine on the next
128 iteration of the main loop.
130 A second class of propagation opportunities arises for ADDR_EXPR
141 ptr = (type1*)&type2var;
144 Will get turned into (if type1 and type2 are the same size
145 and neither have volatile on them):
146 res = VIEW_CONVERT_EXPR<type1>(type2var)
151 ptr2 = ptr + <constant>;
155 ptr2 = &x[constant/elementsize];
160 offset = index * element_size;
161 offset_p = (pointer) offset;
162 ptr2 = ptr + offset_p
164 Will get turned into:
172 Provided that decl has known alignment >= 2, will get turned into
176 We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to
177 allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent
180 This will (of course) be extended as other needs arise. */
182 static bool forward_propagate_addr_expr (tree
, tree
, bool);
184 /* Set to true if we delete dead edges during the optimization. */
185 static bool cfg_changed
;
187 static tree
rhs_to_tree (tree type
, gimple stmt
);
189 /* Get the next statement we can propagate NAME's value into skipping
190 trivial copies. Returns the statement that is suitable as a
191 propagation destination or NULL_TREE if there is no such one.
192 This only returns destinations in a single-use chain. FINAL_NAME_P
193 if non-NULL is written to the ssa name that represents the use. */
196 get_prop_dest_stmt (tree name
, tree
*final_name_p
)
202 /* If name has multiple uses, bail out. */
203 if (!single_imm_use (name
, &use
, &use_stmt
))
206 /* If this is not a trivial copy, we found it. */
207 if (!gimple_assign_ssa_name_copy_p (use_stmt
)
208 || gimple_assign_rhs1 (use_stmt
) != name
)
211 /* Continue searching uses of the copy destination. */
212 name
= gimple_assign_lhs (use_stmt
);
216 *final_name_p
= name
;
221 /* Get the statement we can propagate from into NAME skipping
222 trivial copies. Returns the statement which defines the
223 propagation source or NULL_TREE if there is no such one.
224 If SINGLE_USE_ONLY is set considers only sources which have
225 a single use chain up to NAME. If SINGLE_USE_P is non-null,
226 it is set to whether the chain to NAME is a single use chain
227 or not. SINGLE_USE_P is not written to if SINGLE_USE_ONLY is set. */
230 get_prop_source_stmt (tree name
, bool single_use_only
, bool *single_use_p
)
232 bool single_use
= true;
235 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
237 if (!has_single_use (name
))
244 /* If name is defined by a PHI node or is the default def, bail out. */
245 if (!is_gimple_assign (def_stmt
))
248 /* If def_stmt is a simple copy, continue looking. */
249 if (gimple_assign_rhs_code (def_stmt
) == SSA_NAME
)
250 name
= gimple_assign_rhs1 (def_stmt
);
253 if (!single_use_only
&& single_use_p
)
254 *single_use_p
= single_use
;
261 /* Checks if the destination ssa name in DEF_STMT can be used as
262 propagation source. Returns true if so, otherwise false. */
265 can_propagate_from (gimple def_stmt
)
267 gcc_assert (is_gimple_assign (def_stmt
));
269 /* If the rhs has side-effects we cannot propagate from it. */
270 if (gimple_has_volatile_ops (def_stmt
))
273 /* If the rhs is a load we cannot propagate from it. */
274 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt
)) == tcc_reference
275 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt
)) == tcc_declaration
)
278 /* Constants can be always propagated. */
279 if (gimple_assign_single_p (def_stmt
)
280 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt
)))
283 /* We cannot propagate ssa names that occur in abnormal phi nodes. */
284 if (stmt_references_abnormal_ssa_name (def_stmt
))
287 /* If the definition is a conversion of a pointer to a function type,
288 then we can not apply optimizations as some targets require
289 function pointers to be canonicalized and in this case this
290 optimization could eliminate a necessary canonicalization. */
291 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
293 tree rhs
= gimple_assign_rhs1 (def_stmt
);
294 if (POINTER_TYPE_P (TREE_TYPE (rhs
))
295 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs
))) == FUNCTION_TYPE
)
302 /* Remove a chain of dead statements starting at the definition of
303 NAME. The chain is linked via the first operand of the defining statements.
304 If NAME was replaced in its only use then this function can be used
305 to clean up dead stmts. The function handles already released SSA
307 Returns true if cleanup-cfg has to run. */
310 remove_prop_source_from_use (tree name
)
312 gimple_stmt_iterator gsi
;
314 bool cfg_changed
= false;
319 if (SSA_NAME_IN_FREE_LIST (name
)
320 || SSA_NAME_IS_DEFAULT_DEF (name
)
321 || !has_zero_uses (name
))
324 stmt
= SSA_NAME_DEF_STMT (name
);
325 if (gimple_code (stmt
) == GIMPLE_PHI
326 || gimple_has_side_effects (stmt
))
329 bb
= gimple_bb (stmt
);
330 gsi
= gsi_for_stmt (stmt
);
331 unlink_stmt_vdef (stmt
);
332 if (gsi_remove (&gsi
, true))
333 cfg_changed
|= gimple_purge_dead_eh_edges (bb
);
336 name
= is_gimple_assign (stmt
) ? gimple_assign_rhs1 (stmt
) : NULL_TREE
;
337 } while (name
&& TREE_CODE (name
) == SSA_NAME
);
342 /* Return the rhs of a gimple_assign STMT in a form of a single tree,
343 converted to type TYPE.
345 This should disappear, but is needed so we can combine expressions and use
346 the fold() interfaces. Long term, we need to develop folding and combine
347 routines that deal with gimple exclusively . */
350 rhs_to_tree (tree type
, gimple stmt
)
352 location_t loc
= gimple_location (stmt
);
353 enum tree_code code
= gimple_assign_rhs_code (stmt
);
354 if (get_gimple_rhs_class (code
) == GIMPLE_TERNARY_RHS
)
355 return fold_build3_loc (loc
, code
, type
, gimple_assign_rhs1 (stmt
),
356 gimple_assign_rhs2 (stmt
),
357 gimple_assign_rhs3 (stmt
));
358 else if (get_gimple_rhs_class (code
) == GIMPLE_BINARY_RHS
)
359 return fold_build2_loc (loc
, code
, type
, gimple_assign_rhs1 (stmt
),
360 gimple_assign_rhs2 (stmt
));
361 else if (get_gimple_rhs_class (code
) == GIMPLE_UNARY_RHS
)
362 return build1 (code
, type
, gimple_assign_rhs1 (stmt
));
363 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
)
364 return gimple_assign_rhs1 (stmt
);
369 /* Combine OP0 CODE OP1 in the context of a COND_EXPR. Returns
370 the folded result in a form suitable for COND_EXPR_COND or
371 NULL_TREE, if there is no suitable simplified form. If
372 INVARIANT_ONLY is true only gimple_min_invariant results are
373 considered simplified. */
376 combine_cond_expr_cond (gimple stmt
, enum tree_code code
, tree type
,
377 tree op0
, tree op1
, bool invariant_only
)
381 gcc_assert (TREE_CODE_CLASS (code
) == tcc_comparison
);
383 fold_defer_overflow_warnings ();
384 t
= fold_binary_loc (gimple_location (stmt
), code
, type
, op0
, op1
);
387 fold_undefer_overflow_warnings (false, NULL
, 0);
391 /* Require that we got a boolean type out if we put one in. */
392 gcc_assert (TREE_CODE (TREE_TYPE (t
)) == TREE_CODE (type
));
394 /* Canonicalize the combined condition for use in a COND_EXPR. */
395 t
= canonicalize_cond_expr_cond (t
);
397 /* Bail out if we required an invariant but didn't get one. */
398 if (!t
|| (invariant_only
&& !is_gimple_min_invariant (t
)))
400 fold_undefer_overflow_warnings (false, NULL
, 0);
404 fold_undefer_overflow_warnings (!gimple_no_warning_p (stmt
), stmt
, 0);
409 /* Combine the comparison OP0 CODE OP1 at LOC with the defining statements
410 of its operand. Return a new comparison tree or NULL_TREE if there
411 were no simplifying combines. */
414 forward_propagate_into_comparison_1 (gimple stmt
,
415 enum tree_code code
, tree type
,
418 tree tmp
= NULL_TREE
;
419 tree rhs0
= NULL_TREE
, rhs1
= NULL_TREE
;
420 bool single_use0_p
= false, single_use1_p
= false;
422 /* For comparisons use the first operand, that is likely to
423 simplify comparisons against constants. */
424 if (TREE_CODE (op0
) == SSA_NAME
)
426 gimple def_stmt
= get_prop_source_stmt (op0
, false, &single_use0_p
);
427 if (def_stmt
&& can_propagate_from (def_stmt
))
429 rhs0
= rhs_to_tree (TREE_TYPE (op1
), def_stmt
);
430 tmp
= combine_cond_expr_cond (stmt
, code
, type
,
431 rhs0
, op1
, !single_use0_p
);
437 /* If that wasn't successful, try the second operand. */
438 if (TREE_CODE (op1
) == SSA_NAME
)
440 gimple def_stmt
= get_prop_source_stmt (op1
, false, &single_use1_p
);
441 if (def_stmt
&& can_propagate_from (def_stmt
))
443 rhs1
= rhs_to_tree (TREE_TYPE (op0
), def_stmt
);
444 tmp
= combine_cond_expr_cond (stmt
, code
, type
,
445 op0
, rhs1
, !single_use1_p
);
451 /* If that wasn't successful either, try both operands. */
452 if (rhs0
!= NULL_TREE
453 && rhs1
!= NULL_TREE
)
454 tmp
= combine_cond_expr_cond (stmt
, code
, type
,
456 !(single_use0_p
&& single_use1_p
));
461 /* Propagate from the ssa name definition statements of the assignment
462 from a comparison at *GSI into the conditional if that simplifies it.
463 Returns 1 if the stmt was modified and 2 if the CFG needs cleanup,
464 otherwise returns 0. */
467 forward_propagate_into_comparison (gimple_stmt_iterator
*gsi
)
469 gimple stmt
= gsi_stmt (*gsi
);
471 bool cfg_changed
= false;
472 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
473 tree rhs1
= gimple_assign_rhs1 (stmt
);
474 tree rhs2
= gimple_assign_rhs2 (stmt
);
476 /* Combine the comparison with defining statements. */
477 tmp
= forward_propagate_into_comparison_1 (stmt
,
478 gimple_assign_rhs_code (stmt
),
480 if (tmp
&& useless_type_conversion_p (type
, TREE_TYPE (tmp
)))
482 gimple_assign_set_rhs_from_tree (gsi
, tmp
);
484 update_stmt (gsi_stmt (*gsi
));
486 if (TREE_CODE (rhs1
) == SSA_NAME
)
487 cfg_changed
|= remove_prop_source_from_use (rhs1
);
488 if (TREE_CODE (rhs2
) == SSA_NAME
)
489 cfg_changed
|= remove_prop_source_from_use (rhs2
);
490 return cfg_changed
? 2 : 1;
496 /* Propagate from the ssa name definition statements of COND_EXPR
497 in GIMPLE_COND statement STMT into the conditional if that simplifies it.
498 Returns zero if no statement was changed, one if there were
499 changes and two if cfg_cleanup needs to run.
501 This must be kept in sync with forward_propagate_into_cond. */
504 forward_propagate_into_gimple_cond (gimple stmt
)
507 enum tree_code code
= gimple_cond_code (stmt
);
508 bool cfg_changed
= false;
509 tree rhs1
= gimple_cond_lhs (stmt
);
510 tree rhs2
= gimple_cond_rhs (stmt
);
512 /* We can do tree combining on SSA_NAME and comparison expressions. */
513 if (TREE_CODE_CLASS (gimple_cond_code (stmt
)) != tcc_comparison
)
516 tmp
= forward_propagate_into_comparison_1 (stmt
, code
,
521 if (dump_file
&& tmp
)
523 fprintf (dump_file
, " Replaced '");
524 print_gimple_expr (dump_file
, stmt
, 0, 0);
525 fprintf (dump_file
, "' with '");
526 print_generic_expr (dump_file
, tmp
, 0);
527 fprintf (dump_file
, "'\n");
530 gimple_cond_set_condition_from_tree (stmt
, unshare_expr (tmp
));
533 if (TREE_CODE (rhs1
) == SSA_NAME
)
534 cfg_changed
|= remove_prop_source_from_use (rhs1
);
535 if (TREE_CODE (rhs2
) == SSA_NAME
)
536 cfg_changed
|= remove_prop_source_from_use (rhs2
);
537 return (cfg_changed
|| is_gimple_min_invariant (tmp
)) ? 2 : 1;
540 /* Canonicalize _Bool == 0 and _Bool != 1 to _Bool != 0 by swapping edges. */
541 if ((TREE_CODE (TREE_TYPE (rhs1
)) == BOOLEAN_TYPE
542 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
543 && TYPE_PRECISION (TREE_TYPE (rhs1
)) == 1))
545 && integer_zerop (rhs2
))
547 && integer_onep (rhs2
))))
549 basic_block bb
= gimple_bb (stmt
);
550 gimple_cond_set_code (stmt
, NE_EXPR
);
551 gimple_cond_set_rhs (stmt
, build_zero_cst (TREE_TYPE (rhs1
)));
552 EDGE_SUCC (bb
, 0)->flags
^= (EDGE_TRUE_VALUE
|EDGE_FALSE_VALUE
);
553 EDGE_SUCC (bb
, 1)->flags
^= (EDGE_TRUE_VALUE
|EDGE_FALSE_VALUE
);
561 /* Propagate from the ssa name definition statements of COND_EXPR
562 in the rhs of statement STMT into the conditional if that simplifies it.
563 Returns true zero if the stmt was changed. */
566 forward_propagate_into_cond (gimple_stmt_iterator
*gsi_p
)
568 gimple stmt
= gsi_stmt (*gsi_p
);
569 tree tmp
= NULL_TREE
;
570 tree cond
= gimple_assign_rhs1 (stmt
);
571 enum tree_code code
= gimple_assign_rhs_code (stmt
);
574 /* We can do tree combining on SSA_NAME and comparison expressions. */
575 if (COMPARISON_CLASS_P (cond
))
576 tmp
= forward_propagate_into_comparison_1 (stmt
, TREE_CODE (cond
),
578 TREE_OPERAND (cond
, 0),
579 TREE_OPERAND (cond
, 1));
580 else if (TREE_CODE (cond
) == SSA_NAME
)
582 enum tree_code def_code
;
584 gimple def_stmt
= get_prop_source_stmt (name
, true, NULL
);
585 if (!def_stmt
|| !can_propagate_from (def_stmt
))
588 def_code
= gimple_assign_rhs_code (def_stmt
);
589 if (TREE_CODE_CLASS (def_code
) == tcc_comparison
)
590 tmp
= fold_build2_loc (gimple_location (def_stmt
),
593 gimple_assign_rhs1 (def_stmt
),
594 gimple_assign_rhs2 (def_stmt
));
595 else if (code
== COND_EXPR
596 && ((def_code
== BIT_NOT_EXPR
597 && TYPE_PRECISION (TREE_TYPE (cond
)) == 1)
598 || (def_code
== BIT_XOR_EXPR
599 && integer_onep (gimple_assign_rhs2 (def_stmt
)))))
601 tmp
= gimple_assign_rhs1 (def_stmt
);
607 && is_gimple_condexpr (tmp
))
609 if (dump_file
&& tmp
)
611 fprintf (dump_file
, " Replaced '");
612 print_generic_expr (dump_file
, cond
, 0);
613 fprintf (dump_file
, "' with '");
614 print_generic_expr (dump_file
, tmp
, 0);
615 fprintf (dump_file
, "'\n");
618 if ((code
== VEC_COND_EXPR
) ? integer_all_onesp (tmp
)
619 : integer_onep (tmp
))
620 gimple_assign_set_rhs_from_tree (gsi_p
, gimple_assign_rhs2 (stmt
));
621 else if (integer_zerop (tmp
))
622 gimple_assign_set_rhs_from_tree (gsi_p
, gimple_assign_rhs3 (stmt
));
625 gimple_assign_set_rhs1 (stmt
, unshare_expr (tmp
));
628 tree t
= gimple_assign_rhs2 (stmt
);
629 gimple_assign_set_rhs2 (stmt
, gimple_assign_rhs3 (stmt
));
630 gimple_assign_set_rhs3 (stmt
, t
);
633 stmt
= gsi_stmt (*gsi_p
);
642 /* Propagate from the ssa name definition statements of COND_EXPR
643 values in the rhs of statement STMT into the conditional arms
644 if that simplifies it.
645 Returns true if the stmt was changed. */
648 combine_cond_exprs (gimple_stmt_iterator
*gsi_p
)
650 gimple stmt
= gsi_stmt (*gsi_p
);
651 tree cond
, val1
, val2
;
652 bool changed
= false;
654 cond
= gimple_assign_rhs1 (stmt
);
655 val1
= gimple_assign_rhs2 (stmt
);
656 if (TREE_CODE (val1
) == SSA_NAME
)
658 gimple def_stmt
= SSA_NAME_DEF_STMT (val1
);
659 if (is_gimple_assign (def_stmt
)
660 && gimple_assign_rhs_code (def_stmt
) == gimple_assign_rhs_code (stmt
)
661 && operand_equal_p (gimple_assign_rhs1 (def_stmt
), cond
, 0))
663 val1
= unshare_expr (gimple_assign_rhs2 (def_stmt
));
664 gimple_assign_set_rhs2 (stmt
, val1
);
668 val2
= gimple_assign_rhs3 (stmt
);
669 if (TREE_CODE (val2
) == SSA_NAME
)
671 gimple def_stmt
= SSA_NAME_DEF_STMT (val2
);
672 if (is_gimple_assign (def_stmt
)
673 && gimple_assign_rhs_code (def_stmt
) == gimple_assign_rhs_code (stmt
)
674 && operand_equal_p (gimple_assign_rhs1 (def_stmt
), cond
, 0))
676 val2
= unshare_expr (gimple_assign_rhs3 (def_stmt
));
677 gimple_assign_set_rhs3 (stmt
, val2
);
681 if (operand_equal_p (val1
, val2
, 0))
683 gimple_assign_set_rhs_from_tree (gsi_p
, val1
);
684 stmt
= gsi_stmt (*gsi_p
);
694 /* We've just substituted an ADDR_EXPR into stmt. Update all the
695 relevant data structures to match. */
698 tidy_after_forward_propagate_addr (gimple stmt
)
700 /* We may have turned a trapping insn into a non-trapping insn. */
701 if (maybe_clean_or_replace_eh_stmt (stmt
, stmt
)
702 && gimple_purge_dead_eh_edges (gimple_bb (stmt
)))
705 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) == ADDR_EXPR
)
706 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt
));
709 /* NAME is a SSA_NAME representing DEF_RHS which is of the form
710 ADDR_EXPR <whatever>.
712 Try to forward propagate the ADDR_EXPR into the use USE_STMT.
713 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
714 node or for recovery of array indexing from pointer arithmetic.
716 Return true if the propagation was successful (the propagation can
717 be not totally successful, yet things may have been changed). */
720 forward_propagate_addr_expr_1 (tree name
, tree def_rhs
,
721 gimple_stmt_iterator
*use_stmt_gsi
,
724 tree lhs
, rhs
, rhs2
, array_ref
;
725 gimple use_stmt
= gsi_stmt (*use_stmt_gsi
);
726 enum tree_code rhs_code
;
729 gcc_assert (TREE_CODE (def_rhs
) == ADDR_EXPR
);
731 lhs
= gimple_assign_lhs (use_stmt
);
732 rhs_code
= gimple_assign_rhs_code (use_stmt
);
733 rhs
= gimple_assign_rhs1 (use_stmt
);
735 /* Do not perform copy-propagation but recurse through copy chains. */
736 if (TREE_CODE (lhs
) == SSA_NAME
737 && rhs_code
== SSA_NAME
)
738 return forward_propagate_addr_expr (lhs
, def_rhs
, single_use_p
);
740 /* The use statement could be a conversion. Recurse to the uses of the
741 lhs as copyprop does not copy through pointer to integer to pointer
742 conversions and FRE does not catch all cases either.
743 Treat the case of a single-use name and
744 a conversion to def_rhs type separate, though. */
745 if (TREE_CODE (lhs
) == SSA_NAME
746 && CONVERT_EXPR_CODE_P (rhs_code
))
748 /* If there is a point in a conversion chain where the types match
749 so we can remove a conversion re-materialize the address here
752 && useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (def_rhs
)))
754 gimple_assign_set_rhs1 (use_stmt
, unshare_expr (def_rhs
));
755 gimple_assign_set_rhs_code (use_stmt
, TREE_CODE (def_rhs
));
759 /* Else recurse if the conversion preserves the address value. */
760 if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
761 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
762 && (TYPE_PRECISION (TREE_TYPE (lhs
))
763 >= TYPE_PRECISION (TREE_TYPE (def_rhs
))))
764 return forward_propagate_addr_expr (lhs
, def_rhs
, single_use_p
);
769 /* If this isn't a conversion chain from this on we only can propagate
770 into compatible pointer contexts. */
771 if (!types_compatible_p (TREE_TYPE (name
), TREE_TYPE (def_rhs
)))
774 /* Propagate through constant pointer adjustments. */
775 if (TREE_CODE (lhs
) == SSA_NAME
776 && rhs_code
== POINTER_PLUS_EXPR
778 && TREE_CODE (gimple_assign_rhs2 (use_stmt
)) == INTEGER_CST
)
781 /* As we come here with non-invariant addresses in def_rhs we need
782 to make sure we can build a valid constant offsetted address
783 for further propagation. Simply rely on fold building that
784 and check after the fact. */
785 new_def_rhs
= fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (rhs
)),
787 fold_convert (ptr_type_node
,
788 gimple_assign_rhs2 (use_stmt
)));
789 if (TREE_CODE (new_def_rhs
) == MEM_REF
790 && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs
, 0)))
792 new_def_rhs
= build_fold_addr_expr_with_type (new_def_rhs
,
795 /* Recurse. If we could propagate into all uses of lhs do not
796 bother to replace into the current use but just pretend we did. */
797 if (TREE_CODE (new_def_rhs
) == ADDR_EXPR
798 && forward_propagate_addr_expr (lhs
, new_def_rhs
, single_use_p
))
801 if (useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (new_def_rhs
)))
802 gimple_assign_set_rhs_with_ops (use_stmt_gsi
, TREE_CODE (new_def_rhs
),
803 new_def_rhs
, NULL_TREE
);
804 else if (is_gimple_min_invariant (new_def_rhs
))
805 gimple_assign_set_rhs_with_ops (use_stmt_gsi
, NOP_EXPR
,
806 new_def_rhs
, NULL_TREE
);
809 gcc_assert (gsi_stmt (*use_stmt_gsi
) == use_stmt
);
810 update_stmt (use_stmt
);
814 /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
815 ADDR_EXPR will not appear on the LHS. */
816 tree
*lhsp
= gimple_assign_lhs_ptr (use_stmt
);
817 while (handled_component_p (*lhsp
))
818 lhsp
= &TREE_OPERAND (*lhsp
, 0);
821 /* Now see if the LHS node is a MEM_REF using NAME. If so,
822 propagate the ADDR_EXPR into the use of NAME and fold the result. */
823 if (TREE_CODE (lhs
) == MEM_REF
824 && TREE_OPERAND (lhs
, 0) == name
)
827 HOST_WIDE_INT def_rhs_offset
;
828 /* If the address is invariant we can always fold it. */
829 if ((def_rhs_base
= get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs
, 0),
832 double_int off
= mem_ref_offset (lhs
);
834 off
+= double_int::from_shwi (def_rhs_offset
);
835 if (TREE_CODE (def_rhs_base
) == MEM_REF
)
837 off
+= mem_ref_offset (def_rhs_base
);
838 new_ptr
= TREE_OPERAND (def_rhs_base
, 0);
841 new_ptr
= build_fold_addr_expr (def_rhs_base
);
842 TREE_OPERAND (lhs
, 0) = new_ptr
;
843 TREE_OPERAND (lhs
, 1)
844 = double_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs
, 1)), off
);
845 tidy_after_forward_propagate_addr (use_stmt
);
846 /* Continue propagating into the RHS if this was not the only use. */
850 /* If the LHS is a plain dereference and the value type is the same as
851 that of the pointed-to type of the address we can put the
852 dereferenced address on the LHS preserving the original alias-type. */
853 else if (integer_zerop (TREE_OPERAND (lhs
, 1))
854 && ((gimple_assign_lhs (use_stmt
) == lhs
855 && useless_type_conversion_p
856 (TREE_TYPE (TREE_OPERAND (def_rhs
, 0)),
857 TREE_TYPE (gimple_assign_rhs1 (use_stmt
))))
858 || types_compatible_p (TREE_TYPE (lhs
),
859 TREE_TYPE (TREE_OPERAND (def_rhs
, 0))))
860 /* Don't forward anything into clobber stmts if it would result
861 in the lhs no longer being a MEM_REF. */
862 && (!gimple_clobber_p (use_stmt
)
863 || TREE_CODE (TREE_OPERAND (def_rhs
, 0)) == MEM_REF
))
865 tree
*def_rhs_basep
= &TREE_OPERAND (def_rhs
, 0);
866 tree new_offset
, new_base
, saved
, new_lhs
;
867 while (handled_component_p (*def_rhs_basep
))
868 def_rhs_basep
= &TREE_OPERAND (*def_rhs_basep
, 0);
869 saved
= *def_rhs_basep
;
870 if (TREE_CODE (*def_rhs_basep
) == MEM_REF
)
872 new_base
= TREE_OPERAND (*def_rhs_basep
, 0);
873 new_offset
= fold_convert (TREE_TYPE (TREE_OPERAND (lhs
, 1)),
874 TREE_OPERAND (*def_rhs_basep
, 1));
878 new_base
= build_fold_addr_expr (*def_rhs_basep
);
879 new_offset
= TREE_OPERAND (lhs
, 1);
881 *def_rhs_basep
= build2 (MEM_REF
, TREE_TYPE (*def_rhs_basep
),
882 new_base
, new_offset
);
883 TREE_THIS_VOLATILE (*def_rhs_basep
) = TREE_THIS_VOLATILE (lhs
);
884 TREE_SIDE_EFFECTS (*def_rhs_basep
) = TREE_SIDE_EFFECTS (lhs
);
885 TREE_THIS_NOTRAP (*def_rhs_basep
) = TREE_THIS_NOTRAP (lhs
);
886 new_lhs
= unshare_expr (TREE_OPERAND (def_rhs
, 0));
888 TREE_THIS_VOLATILE (new_lhs
) = TREE_THIS_VOLATILE (lhs
);
889 TREE_SIDE_EFFECTS (new_lhs
) = TREE_SIDE_EFFECTS (lhs
);
890 *def_rhs_basep
= saved
;
891 tidy_after_forward_propagate_addr (use_stmt
);
892 /* Continue propagating into the RHS if this was not the
898 /* We can have a struct assignment dereferencing our name twice.
899 Note that we didn't propagate into the lhs to not falsely
900 claim we did when propagating into the rhs. */
904 /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
905 nodes from the RHS. */
906 tree
*rhsp
= gimple_assign_rhs1_ptr (use_stmt
);
907 if (TREE_CODE (*rhsp
) == ADDR_EXPR
)
908 rhsp
= &TREE_OPERAND (*rhsp
, 0);
909 while (handled_component_p (*rhsp
))
910 rhsp
= &TREE_OPERAND (*rhsp
, 0);
913 /* Now see if the RHS node is a MEM_REF using NAME. If so,
914 propagate the ADDR_EXPR into the use of NAME and fold the result. */
915 if (TREE_CODE (rhs
) == MEM_REF
916 && TREE_OPERAND (rhs
, 0) == name
)
919 HOST_WIDE_INT def_rhs_offset
;
920 if ((def_rhs_base
= get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs
, 0),
923 double_int off
= mem_ref_offset (rhs
);
925 off
+= double_int::from_shwi (def_rhs_offset
);
926 if (TREE_CODE (def_rhs_base
) == MEM_REF
)
928 off
+= mem_ref_offset (def_rhs_base
);
929 new_ptr
= TREE_OPERAND (def_rhs_base
, 0);
932 new_ptr
= build_fold_addr_expr (def_rhs_base
);
933 TREE_OPERAND (rhs
, 0) = new_ptr
;
934 TREE_OPERAND (rhs
, 1)
935 = double_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs
, 1)), off
);
936 fold_stmt_inplace (use_stmt_gsi
);
937 tidy_after_forward_propagate_addr (use_stmt
);
940 /* If the RHS is a plain dereference and the value type is the same as
941 that of the pointed-to type of the address we can put the
942 dereferenced address on the RHS preserving the original alias-type. */
943 else if (integer_zerop (TREE_OPERAND (rhs
, 1))
944 && ((gimple_assign_rhs1 (use_stmt
) == rhs
945 && useless_type_conversion_p
946 (TREE_TYPE (gimple_assign_lhs (use_stmt
)),
947 TREE_TYPE (TREE_OPERAND (def_rhs
, 0))))
948 || types_compatible_p (TREE_TYPE (rhs
),
949 TREE_TYPE (TREE_OPERAND (def_rhs
, 0)))))
951 tree
*def_rhs_basep
= &TREE_OPERAND (def_rhs
, 0);
952 tree new_offset
, new_base
, saved
, new_rhs
;
953 while (handled_component_p (*def_rhs_basep
))
954 def_rhs_basep
= &TREE_OPERAND (*def_rhs_basep
, 0);
955 saved
= *def_rhs_basep
;
956 if (TREE_CODE (*def_rhs_basep
) == MEM_REF
)
958 new_base
= TREE_OPERAND (*def_rhs_basep
, 0);
959 new_offset
= fold_convert (TREE_TYPE (TREE_OPERAND (rhs
, 1)),
960 TREE_OPERAND (*def_rhs_basep
, 1));
964 new_base
= build_fold_addr_expr (*def_rhs_basep
);
965 new_offset
= TREE_OPERAND (rhs
, 1);
967 *def_rhs_basep
= build2 (MEM_REF
, TREE_TYPE (*def_rhs_basep
),
968 new_base
, new_offset
);
969 TREE_THIS_VOLATILE (*def_rhs_basep
) = TREE_THIS_VOLATILE (rhs
);
970 TREE_SIDE_EFFECTS (*def_rhs_basep
) = TREE_SIDE_EFFECTS (rhs
);
971 TREE_THIS_NOTRAP (*def_rhs_basep
) = TREE_THIS_NOTRAP (rhs
);
972 new_rhs
= unshare_expr (TREE_OPERAND (def_rhs
, 0));
974 TREE_THIS_VOLATILE (new_rhs
) = TREE_THIS_VOLATILE (rhs
);
975 TREE_SIDE_EFFECTS (new_rhs
) = TREE_SIDE_EFFECTS (rhs
);
976 *def_rhs_basep
= saved
;
977 fold_stmt_inplace (use_stmt_gsi
);
978 tidy_after_forward_propagate_addr (use_stmt
);
983 /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there
985 if (gimple_assign_rhs_code (use_stmt
) != POINTER_PLUS_EXPR
986 || gimple_assign_rhs1 (use_stmt
) != name
)
989 /* The remaining cases are all for turning pointer arithmetic into
990 array indexing. They only apply when we have the address of
991 element zero in an array. If that is not the case then there
993 array_ref
= TREE_OPERAND (def_rhs
, 0);
994 if ((TREE_CODE (array_ref
) != ARRAY_REF
995 || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref
, 0))) != ARRAY_TYPE
996 || TREE_CODE (TREE_OPERAND (array_ref
, 1)) != INTEGER_CST
)
997 && TREE_CODE (TREE_TYPE (array_ref
)) != ARRAY_TYPE
)
1000 rhs2
= gimple_assign_rhs2 (use_stmt
);
1001 /* Optimize &x[C1] p+ C2 to &x p+ C3 with C3 = C1 * element_size + C2. */
1002 if (TREE_CODE (rhs2
) == INTEGER_CST
)
1004 tree new_rhs
= build1_loc (gimple_location (use_stmt
),
1005 ADDR_EXPR
, TREE_TYPE (def_rhs
),
1006 fold_build2 (MEM_REF
,
1007 TREE_TYPE (TREE_TYPE (def_rhs
)),
1008 unshare_expr (def_rhs
),
1009 fold_convert (ptr_type_node
,
1011 gimple_assign_set_rhs_from_tree (use_stmt_gsi
, new_rhs
);
1012 use_stmt
= gsi_stmt (*use_stmt_gsi
);
1013 update_stmt (use_stmt
);
1014 tidy_after_forward_propagate_addr (use_stmt
);
1021 /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
1023 Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
1024 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
1025 node or for recovery of array indexing from pointer arithmetic.
1027 PARENT_SINGLE_USE_P tells if, when in a recursive invocation, NAME was
1028 the single use in the previous invocation. Pass true when calling
1031 Returns true, if all uses have been propagated into. */
1034 forward_propagate_addr_expr (tree name
, tree rhs
, bool parent_single_use_p
)
1036 imm_use_iterator iter
;
1039 bool single_use_p
= parent_single_use_p
&& has_single_use (name
);
1041 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, name
)
1046 /* If the use is not in a simple assignment statement, then
1047 there is nothing we can do. */
1048 if (!is_gimple_assign (use_stmt
))
1050 if (!is_gimple_debug (use_stmt
))
1055 gimple_stmt_iterator gsi
= gsi_for_stmt (use_stmt
);
1056 result
= forward_propagate_addr_expr_1 (name
, rhs
, &gsi
,
1058 /* If the use has moved to a different statement adjust
1059 the update machinery for the old statement too. */
1060 if (use_stmt
!= gsi_stmt (gsi
))
1062 update_stmt (use_stmt
);
1063 use_stmt
= gsi_stmt (gsi
);
1065 update_stmt (use_stmt
);
1068 /* Remove intermediate now unused copy and conversion chains. */
1069 use_rhs
= gimple_assign_rhs1 (use_stmt
);
1071 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
1072 && TREE_CODE (use_rhs
) == SSA_NAME
1073 && has_zero_uses (gimple_assign_lhs (use_stmt
)))
1075 gimple_stmt_iterator gsi
= gsi_for_stmt (use_stmt
);
1076 release_defs (use_stmt
);
1077 gsi_remove (&gsi
, true);
1081 return all
&& has_zero_uses (name
);
1085 /* Forward propagate the comparison defined in *DEFGSI like
1086 cond_1 = x CMP y to uses of the form
1090 Returns true if stmt is now unused. Advance DEFGSI to the next
1094 forward_propagate_comparison (gimple_stmt_iterator
*defgsi
)
1096 gimple stmt
= gsi_stmt (*defgsi
);
1097 tree name
= gimple_assign_lhs (stmt
);
1099 tree tmp
= NULL_TREE
;
1100 gimple_stmt_iterator gsi
;
1101 enum tree_code code
;
1104 /* Don't propagate ssa names that occur in abnormal phis. */
1105 if ((TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
1106 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt
)))
1107 || (TREE_CODE (gimple_assign_rhs2 (stmt
)) == SSA_NAME
1108 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs2 (stmt
))))
1111 /* Do not un-cse comparisons. But propagate through copies. */
1112 use_stmt
= get_prop_dest_stmt (name
, &name
);
1114 || !is_gimple_assign (use_stmt
))
1117 code
= gimple_assign_rhs_code (use_stmt
);
1118 lhs
= gimple_assign_lhs (use_stmt
);
1119 if (!INTEGRAL_TYPE_P (TREE_TYPE (lhs
)))
1122 /* We can propagate the condition into a statement that
1123 computes the logical negation of the comparison result. */
1124 if ((code
== BIT_NOT_EXPR
1125 && TYPE_PRECISION (TREE_TYPE (lhs
)) == 1)
1126 || (code
== BIT_XOR_EXPR
1127 && integer_onep (gimple_assign_rhs2 (use_stmt
))))
1129 tree type
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
1130 bool nans
= HONOR_NANS (TYPE_MODE (type
));
1131 enum tree_code inv_code
;
1132 inv_code
= invert_tree_comparison (gimple_assign_rhs_code (stmt
), nans
);
1133 if (inv_code
== ERROR_MARK
)
1136 tmp
= build2 (inv_code
, TREE_TYPE (lhs
), gimple_assign_rhs1 (stmt
),
1137 gimple_assign_rhs2 (stmt
));
1142 gsi
= gsi_for_stmt (use_stmt
);
1143 gimple_assign_set_rhs_from_tree (&gsi
, unshare_expr (tmp
));
1144 use_stmt
= gsi_stmt (gsi
);
1145 update_stmt (use_stmt
);
1147 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1149 fprintf (dump_file
, " Replaced '");
1150 print_gimple_expr (dump_file
, stmt
, 0, dump_flags
);
1151 fprintf (dump_file
, "' with '");
1152 print_gimple_expr (dump_file
, use_stmt
, 0, dump_flags
);
1153 fprintf (dump_file
, "'\n");
1156 /* When we remove stmt now the iterator defgsi goes off it's current
1157 sequence, hence advance it now. */
1160 /* Remove defining statements. */
1161 return remove_prop_source_from_use (name
);
1169 /* GSI_P points to a statement which performs a narrowing integral
1172 Look for cases like:
1182 If T is narrower than X's type and C merely masks off bits outside
1183 of (T) and nothing else.
1185 Normally we'd let DCE remove the dead statement. But no DCE runs
1186 after the last forwprop/combine pass, so we remove the obviously
1187 dead code ourselves.
1189 Return TRUE if a change was made, FALSE otherwise. */
1192 simplify_conversion_from_bitmask (gimple_stmt_iterator
*gsi_p
)
1194 gimple stmt
= gsi_stmt (*gsi_p
);
1195 gimple rhs_def_stmt
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
1197 /* See if the input for the conversion was set via a BIT_AND_EXPR and
1198 the only use of the BIT_AND_EXPR result is the conversion. */
1199 if (is_gimple_assign (rhs_def_stmt
)
1200 && gimple_assign_rhs_code (rhs_def_stmt
) == BIT_AND_EXPR
1201 && has_single_use (gimple_assign_lhs (rhs_def_stmt
)))
1203 tree rhs_def_operand1
= gimple_assign_rhs1 (rhs_def_stmt
);
1204 tree rhs_def_operand2
= gimple_assign_rhs2 (rhs_def_stmt
);
1205 tree lhs_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1207 /* Now verify suitability of the BIT_AND_EXPR's operands.
1208 The first must be an SSA_NAME that we can propagate and the
1209 second must be an integer constant that masks out all the
1210 bits outside the final result's type, but nothing else. */
1211 if (TREE_CODE (rhs_def_operand1
) == SSA_NAME
1212 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand1
)
1213 && TREE_CODE (rhs_def_operand2
) == INTEGER_CST
1214 && operand_equal_p (rhs_def_operand2
,
1215 build_low_bits_mask (TREE_TYPE (rhs_def_operand2
),
1216 TYPE_PRECISION (lhs_type
)),
1219 /* This is an optimizable case. Replace the source operand
1220 in the conversion with the first source operand of the
1222 gimple_assign_set_rhs1 (stmt
, rhs_def_operand1
);
1223 stmt
= gsi_stmt (*gsi_p
);
1226 /* There is no DCE after the last forwprop pass. It's
1227 easy to clean up the first order effects here. */
1228 gimple_stmt_iterator si
;
1229 si
= gsi_for_stmt (rhs_def_stmt
);
1230 gsi_remove (&si
, true);
1231 release_defs (rhs_def_stmt
);
1240 /* If we have lhs = ~x (STMT), look and see if earlier we had x = ~y.
1241 If so, we can change STMT into lhs = y which can later be copy
1242 propagated. Similarly for negation.
1244 This could trivially be formulated as a forward propagation
1245 to immediate uses. However, we already had an implementation
1246 from DOM which used backward propagation via the use-def links.
1248 It turns out that backward propagation is actually faster as
1249 there's less work to do for each NOT/NEG expression we find.
1250 Backwards propagation needs to look at the statement in a single
1251 backlink. Forward propagation needs to look at potentially more
1252 than one forward link.
1254 Returns true when the statement was changed. */
1257 simplify_not_neg_expr (gimple_stmt_iterator
*gsi_p
)
1259 gimple stmt
= gsi_stmt (*gsi_p
);
1260 tree rhs
= gimple_assign_rhs1 (stmt
);
1261 gimple rhs_def_stmt
= SSA_NAME_DEF_STMT (rhs
);
1263 /* See if the RHS_DEF_STMT has the same form as our statement. */
1264 if (is_gimple_assign (rhs_def_stmt
)
1265 && gimple_assign_rhs_code (rhs_def_stmt
) == gimple_assign_rhs_code (stmt
))
1267 tree rhs_def_operand
= gimple_assign_rhs1 (rhs_def_stmt
);
1269 /* Verify that RHS_DEF_OPERAND is a suitable SSA_NAME. */
1270 if (TREE_CODE (rhs_def_operand
) == SSA_NAME
1271 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand
))
1273 gimple_assign_set_rhs_from_tree (gsi_p
, rhs_def_operand
);
1274 stmt
= gsi_stmt (*gsi_p
);
1283 /* Helper function for simplify_gimple_switch. Remove case labels that
1284 have values outside the range of the new type. */
1287 simplify_gimple_switch_label_vec (gimple stmt
, tree index_type
)
1289 unsigned int branch_num
= gimple_switch_num_labels (stmt
);
1290 auto_vec
<tree
> labels (branch_num
);
1291 unsigned int i
, len
;
1293 /* Collect the existing case labels in a VEC, and preprocess it as if
1294 we are gimplifying a GENERIC SWITCH_EXPR. */
1295 for (i
= 1; i
< branch_num
; i
++)
1296 labels
.quick_push (gimple_switch_label (stmt
, i
));
1297 preprocess_case_label_vec_for_gimple (labels
, index_type
, NULL
);
1299 /* If any labels were removed, replace the existing case labels
1300 in the GIMPLE_SWITCH statement with the correct ones.
1301 Note that the type updates were done in-place on the case labels,
1302 so we only have to replace the case labels in the GIMPLE_SWITCH
1303 if the number of labels changed. */
1304 len
= labels
.length ();
1305 if (len
< branch_num
- 1)
1307 bitmap target_blocks
;
1311 /* Corner case: *all* case labels have been removed as being
1312 out-of-range for INDEX_TYPE. Push one label and let the
1313 CFG cleanups deal with this further. */
1318 label
= CASE_LABEL (gimple_switch_default_label (stmt
));
1319 elt
= build_case_label (build_int_cst (index_type
, 0), NULL
, label
);
1320 labels
.quick_push (elt
);
1324 for (i
= 0; i
< labels
.length (); i
++)
1325 gimple_switch_set_label (stmt
, i
+ 1, labels
[i
]);
1326 for (i
++ ; i
< branch_num
; i
++)
1327 gimple_switch_set_label (stmt
, i
, NULL_TREE
);
1328 gimple_switch_set_num_labels (stmt
, len
+ 1);
1330 /* Cleanup any edges that are now dead. */
1331 target_blocks
= BITMAP_ALLOC (NULL
);
1332 for (i
= 0; i
< gimple_switch_num_labels (stmt
); i
++)
1334 tree elt
= gimple_switch_label (stmt
, i
);
1335 basic_block target
= label_to_block (CASE_LABEL (elt
));
1336 bitmap_set_bit (target_blocks
, target
->index
);
1338 for (ei
= ei_start (gimple_bb (stmt
)->succs
); (e
= ei_safe_edge (ei
)); )
1340 if (! bitmap_bit_p (target_blocks
, e
->dest
->index
))
1344 free_dominance_info (CDI_DOMINATORS
);
1349 BITMAP_FREE (target_blocks
);
1353 /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
1354 the condition which we may be able to optimize better. */
1357 simplify_gimple_switch (gimple stmt
)
1359 tree cond
= gimple_switch_index (stmt
);
1363 /* The optimization that we really care about is removing unnecessary
1364 casts. That will let us do much better in propagating the inferred
1365 constant at the switch target. */
1366 if (TREE_CODE (cond
) == SSA_NAME
)
1368 def_stmt
= SSA_NAME_DEF_STMT (cond
);
1369 if (is_gimple_assign (def_stmt
))
1371 if (gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
)
1376 def
= gimple_assign_rhs1 (def_stmt
);
1378 to
= TREE_TYPE (cond
);
1379 ti
= TREE_TYPE (def
);
1381 /* If we have an extension that preserves value, then we
1382 can copy the source value into the switch. */
1384 need_precision
= TYPE_PRECISION (ti
);
1386 if (! INTEGRAL_TYPE_P (ti
))
1388 else if (TYPE_UNSIGNED (to
) && !TYPE_UNSIGNED (ti
))
1390 else if (!TYPE_UNSIGNED (to
) && TYPE_UNSIGNED (ti
))
1391 need_precision
+= 1;
1392 if (TYPE_PRECISION (to
) < need_precision
)
1397 gimple_switch_set_index (stmt
, def
);
1398 simplify_gimple_switch_label_vec (stmt
, ti
);
1409 /* For pointers p2 and p1 return p2 - p1 if the
1410 difference is known and constant, otherwise return NULL. */
1413 constant_pointer_difference (tree p1
, tree p2
)
1416 #define CPD_ITERATIONS 5
1417 tree exps
[2][CPD_ITERATIONS
];
1418 tree offs
[2][CPD_ITERATIONS
];
1421 for (i
= 0; i
< 2; i
++)
1423 tree p
= i
? p1
: p2
;
1424 tree off
= size_zero_node
;
1426 enum tree_code code
;
1428 /* For each of p1 and p2 we need to iterate at least
1429 twice, to handle ADDR_EXPR directly in p1/p2,
1430 SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc.
1431 on definition's stmt RHS. Iterate a few extra times. */
1435 if (!POINTER_TYPE_P (TREE_TYPE (p
)))
1437 if (TREE_CODE (p
) == ADDR_EXPR
)
1439 tree q
= TREE_OPERAND (p
, 0);
1440 HOST_WIDE_INT offset
;
1441 tree base
= get_addr_base_and_unit_offset (q
, &offset
);
1446 off
= size_binop (PLUS_EXPR
, off
, size_int (offset
));
1448 if (TREE_CODE (q
) == MEM_REF
1449 && TREE_CODE (TREE_OPERAND (q
, 0)) == SSA_NAME
)
1451 p
= TREE_OPERAND (q
, 0);
1452 off
= size_binop (PLUS_EXPR
, off
,
1453 double_int_to_tree (sizetype
,
1454 mem_ref_offset (q
)));
1463 if (TREE_CODE (p
) != SSA_NAME
)
1467 if (j
== CPD_ITERATIONS
)
1469 stmt
= SSA_NAME_DEF_STMT (p
);
1470 if (!is_gimple_assign (stmt
) || gimple_assign_lhs (stmt
) != p
)
1472 code
= gimple_assign_rhs_code (stmt
);
1473 if (code
== POINTER_PLUS_EXPR
)
1475 if (TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
1477 off
= size_binop (PLUS_EXPR
, off
, gimple_assign_rhs2 (stmt
));
1478 p
= gimple_assign_rhs1 (stmt
);
1480 else if (code
== ADDR_EXPR
|| code
== NOP_EXPR
)
1481 p
= gimple_assign_rhs1 (stmt
);
1489 for (i
= 0; i
< cnt
[0]; i
++)
1490 for (j
= 0; j
< cnt
[1]; j
++)
1491 if (exps
[0][i
] == exps
[1][j
])
1492 return size_binop (MINUS_EXPR
, offs
[0][i
], offs
[1][j
]);
1497 /* *GSI_P is a GIMPLE_CALL to a builtin function.
1499 memcpy (p, "abcd", 4);
1500 memset (p + 4, ' ', 3);
1502 memcpy (p, "abcd ", 7);
1503 call if the latter can be stored by pieces during expansion. */
1506 simplify_builtin_call (gimple_stmt_iterator
*gsi_p
, tree callee2
)
1508 gimple stmt1
, stmt2
= gsi_stmt (*gsi_p
);
1509 tree vuse
= gimple_vuse (stmt2
);
1512 stmt1
= SSA_NAME_DEF_STMT (vuse
);
1514 switch (DECL_FUNCTION_CODE (callee2
))
1516 case BUILT_IN_MEMSET
:
1517 if (gimple_call_num_args (stmt2
) != 3
1518 || gimple_call_lhs (stmt2
)
1520 || BITS_PER_UNIT
!= 8)
1525 tree ptr1
, src1
, str1
, off1
, len1
, lhs1
;
1526 tree ptr2
= gimple_call_arg (stmt2
, 0);
1527 tree val2
= gimple_call_arg (stmt2
, 1);
1528 tree len2
= gimple_call_arg (stmt2
, 2);
1529 tree diff
, vdef
, new_str_cst
;
1531 unsigned int ptr1_align
;
1532 unsigned HOST_WIDE_INT src_len
;
1534 use_operand_p use_p
;
1536 if (!tree_fits_shwi_p (val2
)
1537 || !tree_fits_uhwi_p (len2
)
1538 || compare_tree_int (len2
, 1024) == 1)
1540 if (is_gimple_call (stmt1
))
1542 /* If first stmt is a call, it needs to be memcpy
1543 or mempcpy, with string literal as second argument and
1545 callee1
= gimple_call_fndecl (stmt1
);
1546 if (callee1
== NULL_TREE
1547 || DECL_BUILT_IN_CLASS (callee1
) != BUILT_IN_NORMAL
1548 || gimple_call_num_args (stmt1
) != 3)
1550 if (DECL_FUNCTION_CODE (callee1
) != BUILT_IN_MEMCPY
1551 && DECL_FUNCTION_CODE (callee1
) != BUILT_IN_MEMPCPY
)
1553 ptr1
= gimple_call_arg (stmt1
, 0);
1554 src1
= gimple_call_arg (stmt1
, 1);
1555 len1
= gimple_call_arg (stmt1
, 2);
1556 lhs1
= gimple_call_lhs (stmt1
);
1557 if (!tree_fits_uhwi_p (len1
))
1559 str1
= string_constant (src1
, &off1
);
1560 if (str1
== NULL_TREE
)
1562 if (!tree_fits_uhwi_p (off1
)
1563 || compare_tree_int (off1
, TREE_STRING_LENGTH (str1
) - 1) > 0
1564 || compare_tree_int (len1
, TREE_STRING_LENGTH (str1
)
1565 - tree_to_uhwi (off1
)) > 0
1566 || TREE_CODE (TREE_TYPE (str1
)) != ARRAY_TYPE
1567 || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1
)))
1568 != TYPE_MODE (char_type_node
))
1571 else if (gimple_assign_single_p (stmt1
))
1573 /* Otherwise look for length 1 memcpy optimized into
1575 ptr1
= gimple_assign_lhs (stmt1
);
1576 src1
= gimple_assign_rhs1 (stmt1
);
1577 if (TREE_CODE (ptr1
) != MEM_REF
1578 || TYPE_MODE (TREE_TYPE (ptr1
)) != TYPE_MODE (char_type_node
)
1579 || !tree_fits_shwi_p (src1
))
1581 ptr1
= build_fold_addr_expr (ptr1
);
1582 callee1
= NULL_TREE
;
1583 len1
= size_one_node
;
1585 off1
= size_zero_node
;
1591 diff
= constant_pointer_difference (ptr1
, ptr2
);
1592 if (diff
== NULL
&& lhs1
!= NULL
)
1594 diff
= constant_pointer_difference (lhs1
, ptr2
);
1595 if (DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
1597 diff
= size_binop (PLUS_EXPR
, diff
,
1598 fold_convert (sizetype
, len1
));
1600 /* If the difference between the second and first destination pointer
1601 is not constant, or is bigger than memcpy length, bail out. */
1603 || !tree_fits_uhwi_p (diff
)
1604 || tree_int_cst_lt (len1
, diff
)
1605 || compare_tree_int (diff
, 1024) == 1)
1608 /* Use maximum of difference plus memset length and memcpy length
1609 as the new memcpy length, if it is too big, bail out. */
1610 src_len
= tree_to_uhwi (diff
);
1611 src_len
+= tree_to_uhwi (len2
);
1612 if (src_len
< tree_to_uhwi (len1
))
1613 src_len
= tree_to_uhwi (len1
);
1617 /* If mempcpy value is used elsewhere, bail out, as mempcpy
1618 with bigger length will return different result. */
1619 if (lhs1
!= NULL_TREE
1620 && DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
1621 && (TREE_CODE (lhs1
) != SSA_NAME
1622 || !single_imm_use (lhs1
, &use_p
, &use_stmt
)
1623 || use_stmt
!= stmt2
))
1626 /* If anything reads memory in between memcpy and memset
1627 call, the modified memcpy call might change it. */
1628 vdef
= gimple_vdef (stmt1
);
1630 && (!single_imm_use (vdef
, &use_p
, &use_stmt
)
1631 || use_stmt
!= stmt2
))
1634 ptr1_align
= get_pointer_alignment (ptr1
);
1635 /* Construct the new source string literal. */
1636 src_buf
= XALLOCAVEC (char, src_len
+ 1);
1639 TREE_STRING_POINTER (str1
) + tree_to_uhwi (off1
),
1640 tree_to_uhwi (len1
));
1642 src_buf
[0] = tree_to_shwi (src1
);
1643 memset (src_buf
+ tree_to_uhwi (diff
),
1644 tree_to_shwi (val2
), tree_to_uhwi (len2
));
1645 src_buf
[src_len
] = '\0';
1646 /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
1647 handle embedded '\0's. */
1648 if (strlen (src_buf
) != src_len
)
1650 rtl_profile_for_bb (gimple_bb (stmt2
));
1651 /* If the new memcpy wouldn't be emitted by storing the literal
1652 by pieces, this optimization might enlarge .rodata too much,
1653 as commonly used string literals couldn't be shared any
1655 if (!can_store_by_pieces (src_len
,
1656 builtin_strncpy_read_str
,
1657 src_buf
, ptr1_align
, false))
1660 new_str_cst
= build_string_literal (src_len
, src_buf
);
1663 /* If STMT1 is a mem{,p}cpy call, adjust it and remove
1665 if (lhs1
&& DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
)
1666 gimple_call_set_lhs (stmt1
, NULL_TREE
);
1667 gimple_call_set_arg (stmt1
, 1, new_str_cst
);
1668 gimple_call_set_arg (stmt1
, 2,
1669 build_int_cst (TREE_TYPE (len1
), src_len
));
1670 update_stmt (stmt1
);
1671 unlink_stmt_vdef (stmt2
);
1672 gsi_remove (gsi_p
, true);
1673 release_defs (stmt2
);
1674 if (lhs1
&& DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
)
1675 release_ssa_name (lhs1
);
1680 /* Otherwise, if STMT1 is length 1 memcpy optimized into
1681 assignment, remove STMT1 and change memset call into
1683 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt1
);
1685 if (!is_gimple_val (ptr1
))
1686 ptr1
= force_gimple_operand_gsi (gsi_p
, ptr1
, true, NULL_TREE
,
1687 true, GSI_SAME_STMT
);
1688 gimple_call_set_fndecl (stmt2
,
1689 builtin_decl_explicit (BUILT_IN_MEMCPY
));
1690 gimple_call_set_arg (stmt2
, 0, ptr1
);
1691 gimple_call_set_arg (stmt2
, 1, new_str_cst
);
1692 gimple_call_set_arg (stmt2
, 2,
1693 build_int_cst (TREE_TYPE (len2
), src_len
));
1694 unlink_stmt_vdef (stmt1
);
1695 gsi_remove (&gsi
, true);
1696 release_defs (stmt1
);
1697 update_stmt (stmt2
);
1708 /* Checks if expression has type of one-bit precision, or is a known
1709 truth-valued expression. */
1711 truth_valued_ssa_name (tree name
)
1714 tree type
= TREE_TYPE (name
);
1716 if (!INTEGRAL_TYPE_P (type
))
1718 /* Don't check here for BOOLEAN_TYPE as the precision isn't
1719 necessarily one and so ~X is not equal to !X. */
1720 if (TYPE_PRECISION (type
) == 1)
1722 def
= SSA_NAME_DEF_STMT (name
);
1723 if (is_gimple_assign (def
))
1724 return truth_value_p (gimple_assign_rhs_code (def
));
1728 /* Helper routine for simplify_bitwise_binary_1 function.
1729 Return for the SSA name NAME the expression X if it mets condition
1730 NAME = !X. Otherwise return NULL_TREE.
1731 Detected patterns for NAME = !X are:
1732 !X and X == 0 for X with integral type.
1733 X ^ 1, X != 1,or ~X for X with integral type with precision of one. */
1735 lookup_logical_inverted_value (tree name
)
1738 enum tree_code code
;
1741 /* If name has none-intergal type, or isn't a SSA_NAME, then
1743 if (TREE_CODE (name
) != SSA_NAME
1744 || !INTEGRAL_TYPE_P (TREE_TYPE (name
)))
1746 def
= SSA_NAME_DEF_STMT (name
);
1747 if (!is_gimple_assign (def
))
1750 code
= gimple_assign_rhs_code (def
);
1751 op1
= gimple_assign_rhs1 (def
);
1754 /* Get for EQ_EXPR or BIT_XOR_EXPR operation the second operand.
1755 If CODE isn't an EQ_EXPR, BIT_XOR_EXPR, or BIT_NOT_EXPR, then return. */
1756 if (code
== EQ_EXPR
|| code
== NE_EXPR
1757 || code
== BIT_XOR_EXPR
)
1758 op2
= gimple_assign_rhs2 (def
);
1763 if (truth_valued_ssa_name (name
))
1767 /* Check if we have X == 0 and X has an integral type. */
1768 if (!INTEGRAL_TYPE_P (TREE_TYPE (op1
)))
1770 if (integer_zerop (op2
))
1774 /* Check if we have X != 1 and X is a truth-valued. */
1775 if (!INTEGRAL_TYPE_P (TREE_TYPE (op1
)))
1777 if (integer_onep (op2
) && truth_valued_ssa_name (op1
))
1781 /* Check if we have X ^ 1 and X is truth valued. */
1782 if (integer_onep (op2
) && truth_valued_ssa_name (op1
))
1792 /* Optimize ARG1 CODE ARG2 to a constant for bitwise binary
1793 operations CODE, if one operand has the logically inverted
1794 value of the other. */
1796 simplify_bitwise_binary_1 (enum tree_code code
, tree type
,
1797 tree arg1
, tree arg2
)
1801 /* If CODE isn't a bitwise binary operation, return NULL_TREE. */
1802 if (code
!= BIT_AND_EXPR
&& code
!= BIT_IOR_EXPR
1803 && code
!= BIT_XOR_EXPR
)
1806 /* First check if operands ARG1 and ARG2 are equal. If so
1807 return NULL_TREE as this optimization is handled fold_stmt. */
1810 /* See if we have in arguments logical-not patterns. */
1811 if (((anot
= lookup_logical_inverted_value (arg1
)) == NULL_TREE
1813 && ((anot
= lookup_logical_inverted_value (arg2
)) == NULL_TREE
1818 if (code
== BIT_AND_EXPR
)
1819 return fold_convert (type
, integer_zero_node
);
1820 /* X | !X -> 1 and X ^ !X -> 1, if X is truth-valued. */
1821 if (truth_valued_ssa_name (anot
))
1822 return fold_convert (type
, integer_one_node
);
1824 /* ??? Otherwise result is (X != 0 ? X : 1). not handled. */
1828 /* Given a ssa_name in NAME see if it was defined by an assignment and
1829 set CODE to be the code and ARG1 to the first operand on the rhs and ARG2
1830 to the second operand on the rhs. */
1833 defcodefor_name (tree name
, enum tree_code
*code
, tree
*arg1
, tree
*arg2
)
1836 enum tree_code code1
;
1840 enum gimple_rhs_class grhs_class
;
1842 code1
= TREE_CODE (name
);
1845 grhs_class
= get_gimple_rhs_class (code1
);
1847 if (code1
== SSA_NAME
)
1849 def
= SSA_NAME_DEF_STMT (name
);
1851 if (def
&& is_gimple_assign (def
)
1852 && can_propagate_from (def
))
1854 code1
= gimple_assign_rhs_code (def
);
1855 arg11
= gimple_assign_rhs1 (def
);
1856 arg21
= gimple_assign_rhs2 (def
);
1857 arg31
= gimple_assign_rhs2 (def
);
1860 else if (grhs_class
== GIMPLE_TERNARY_RHS
1861 || GIMPLE_BINARY_RHS
1863 || GIMPLE_SINGLE_RHS
)
1864 extract_ops_from_tree_1 (name
, &code1
, &arg11
, &arg21
, &arg31
);
1870 /* Ignore arg3 currently. */
1873 /* Return true if a conversion of an operand from type FROM to type TO
1874 should be applied after performing the operation instead. */
1877 hoist_conversion_for_bitop_p (tree to
, tree from
)
1879 /* That's a good idea if the conversion widens the operand, thus
1880 after hoisting the conversion the operation will be narrower. */
1881 if (TYPE_PRECISION (from
) < TYPE_PRECISION (to
))
1884 /* It's also a good idea if the conversion is to a non-integer mode. */
1885 if (GET_MODE_CLASS (TYPE_MODE (to
)) != MODE_INT
)
1888 /* Or if the precision of TO is not the same as the precision
1890 if (TYPE_PRECISION (to
) != GET_MODE_PRECISION (TYPE_MODE (to
)))
1896 /* GSI points to a statement of the form
1898 result = OP0 CODE OP1
1900 Where OP0 and OP1 are single bit SSA_NAMEs and CODE is either
1901 BIT_AND_EXPR or BIT_IOR_EXPR.
1903 If OP0 is fed by a bitwise negation of another single bit SSA_NAME,
1904 then we can simplify the two statements into a single LT_EXPR or LE_EXPR
1905 when code is BIT_AND_EXPR and BIT_IOR_EXPR respectively.
1907 If a simplification is made, return TRUE, else return FALSE. */
1909 simplify_bitwise_binary_boolean (gimple_stmt_iterator
*gsi
,
1910 enum tree_code code
,
1913 gimple op0_def_stmt
= SSA_NAME_DEF_STMT (op0
);
1915 if (!is_gimple_assign (op0_def_stmt
)
1916 || (gimple_assign_rhs_code (op0_def_stmt
) != BIT_NOT_EXPR
))
1919 tree x
= gimple_assign_rhs1 (op0_def_stmt
);
1920 if (TREE_CODE (x
) == SSA_NAME
1921 && INTEGRAL_TYPE_P (TREE_TYPE (x
))
1922 && TYPE_PRECISION (TREE_TYPE (x
)) == 1
1923 && TYPE_UNSIGNED (TREE_TYPE (x
)) == TYPE_UNSIGNED (TREE_TYPE (op1
)))
1925 enum tree_code newcode
;
1927 gimple stmt
= gsi_stmt (*gsi
);
1928 gimple_assign_set_rhs1 (stmt
, x
);
1929 gimple_assign_set_rhs2 (stmt
, op1
);
1930 if (code
== BIT_AND_EXPR
)
1931 newcode
= TYPE_UNSIGNED (TREE_TYPE (x
)) ? LT_EXPR
: GT_EXPR
;
1933 newcode
= TYPE_UNSIGNED (TREE_TYPE (x
)) ? LE_EXPR
: GE_EXPR
;
1934 gimple_assign_set_rhs_code (stmt
, newcode
);
1942 /* Simplify bitwise binary operations.
1943 Return true if a transformation applied, otherwise return false. */
1946 simplify_bitwise_binary (gimple_stmt_iterator
*gsi
)
1948 gimple stmt
= gsi_stmt (*gsi
);
1949 tree arg1
= gimple_assign_rhs1 (stmt
);
1950 tree arg2
= gimple_assign_rhs2 (stmt
);
1951 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1953 tree def1_arg1
, def1_arg2
, def2_arg1
, def2_arg2
;
1954 enum tree_code def1_code
, def2_code
;
1956 defcodefor_name (arg1
, &def1_code
, &def1_arg1
, &def1_arg2
);
1957 defcodefor_name (arg2
, &def2_code
, &def2_arg1
, &def2_arg2
);
1959 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1961 if (TREE_CODE (arg2
) == INTEGER_CST
1962 && CONVERT_EXPR_CODE_P (def1_code
)
1963 && hoist_conversion_for_bitop_p (TREE_TYPE (arg1
), TREE_TYPE (def1_arg1
))
1964 && INTEGRAL_TYPE_P (TREE_TYPE (def1_arg1
))
1965 && int_fits_type_p (arg2
, TREE_TYPE (def1_arg1
)))
1968 tree tem
= make_ssa_name (TREE_TYPE (def1_arg1
), NULL
);
1970 gimple_build_assign_with_ops (code
, tem
, def1_arg1
,
1971 fold_convert_loc (gimple_location (stmt
),
1972 TREE_TYPE (def1_arg1
),
1974 gimple_set_location (newop
, gimple_location (stmt
));
1975 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
1976 gimple_assign_set_rhs_with_ops_1 (gsi
, NOP_EXPR
,
1977 tem
, NULL_TREE
, NULL_TREE
);
1978 update_stmt (gsi_stmt (*gsi
));
1982 /* For bitwise binary operations apply operand conversions to the
1983 binary operation result instead of to the operands. This allows
1984 to combine successive conversions and bitwise binary operations. */
1985 if (CONVERT_EXPR_CODE_P (def1_code
)
1986 && CONVERT_EXPR_CODE_P (def2_code
)
1987 && types_compatible_p (TREE_TYPE (def1_arg1
), TREE_TYPE (def2_arg1
))
1988 && hoist_conversion_for_bitop_p (TREE_TYPE (arg1
), TREE_TYPE (def1_arg1
)))
1991 tree tem
= make_ssa_name (TREE_TYPE (def1_arg1
), NULL
);
1992 newop
= gimple_build_assign_with_ops (code
, tem
, def1_arg1
, def2_arg1
);
1993 gimple_set_location (newop
, gimple_location (stmt
));
1994 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
1995 gimple_assign_set_rhs_with_ops_1 (gsi
, NOP_EXPR
,
1996 tem
, NULL_TREE
, NULL_TREE
);
1997 update_stmt (gsi_stmt (*gsi
));
2002 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
2003 if (def1_code
== def2_code
2004 && def1_code
== BIT_AND_EXPR
2005 && operand_equal_for_phi_arg_p (def1_arg2
,
2011 tree inner
= fold_build2 (code
, TREE_TYPE (arg2
), a
, c
);
2012 /* If A OP0 C (this usually means C is the same as A) is 0
2013 then fold it down correctly. */
2014 if (integer_zerop (inner
))
2016 gimple_assign_set_rhs_from_tree (gsi
, inner
);
2020 /* If A OP0 C (this usually means C is the same as A) is a ssa_name
2021 then fold it down correctly. */
2022 else if (TREE_CODE (inner
) == SSA_NAME
)
2024 tree outer
= fold_build2 (def1_code
, TREE_TYPE (inner
),
2026 gimple_assign_set_rhs_from_tree (gsi
, outer
);
2034 tem
= make_ssa_name (TREE_TYPE (arg2
), NULL
);
2035 newop
= gimple_build_assign_with_ops (code
, tem
, a
, c
);
2036 gimple_set_location (newop
, gimple_location (stmt
));
2037 /* Make sure to re-process the new stmt as it's walking upwards. */
2038 gsi_insert_before (gsi
, newop
, GSI_NEW_STMT
);
2039 gimple_assign_set_rhs1 (stmt
, tem
);
2040 gimple_assign_set_rhs2 (stmt
, b
);
2041 gimple_assign_set_rhs_code (stmt
, def1_code
);
2047 /* (a | CST1) & CST2 -> (a & CST2) | (CST1 & CST2). */
2048 if (code
== BIT_AND_EXPR
2049 && def1_code
== BIT_IOR_EXPR
2050 && CONSTANT_CLASS_P (arg2
)
2051 && CONSTANT_CLASS_P (def1_arg2
))
2053 tree cst
= fold_build2 (BIT_AND_EXPR
, TREE_TYPE (arg2
),
2057 if (integer_zerop (cst
))
2059 gimple_assign_set_rhs1 (stmt
, def1_arg1
);
2063 tem
= make_ssa_name (TREE_TYPE (arg2
), NULL
);
2064 newop
= gimple_build_assign_with_ops (BIT_AND_EXPR
,
2065 tem
, def1_arg1
, arg2
);
2066 gimple_set_location (newop
, gimple_location (stmt
));
2067 /* Make sure to re-process the new stmt as it's walking upwards. */
2068 gsi_insert_before (gsi
, newop
, GSI_NEW_STMT
);
2069 gimple_assign_set_rhs1 (stmt
, tem
);
2070 gimple_assign_set_rhs2 (stmt
, cst
);
2071 gimple_assign_set_rhs_code (stmt
, BIT_IOR_EXPR
);
2076 /* Combine successive equal operations with constants. */
2077 if ((code
== BIT_AND_EXPR
2078 || code
== BIT_IOR_EXPR
2079 || code
== BIT_XOR_EXPR
)
2080 && def1_code
== code
2081 && CONSTANT_CLASS_P (arg2
)
2082 && CONSTANT_CLASS_P (def1_arg2
))
2084 tree cst
= fold_build2 (code
, TREE_TYPE (arg2
),
2086 gimple_assign_set_rhs1 (stmt
, def1_arg1
);
2087 gimple_assign_set_rhs2 (stmt
, cst
);
2092 /* Canonicalize X ^ ~0 to ~X. */
2093 if (code
== BIT_XOR_EXPR
2094 && integer_all_onesp (arg2
))
2096 gimple_assign_set_rhs_with_ops (gsi
, BIT_NOT_EXPR
, arg1
, NULL_TREE
);
2097 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2102 /* Try simple folding for X op !X, and X op X. */
2103 res
= simplify_bitwise_binary_1 (code
, TREE_TYPE (arg1
), arg1
, arg2
);
2104 if (res
!= NULL_TREE
)
2106 gimple_assign_set_rhs_from_tree (gsi
, res
);
2107 update_stmt (gsi_stmt (*gsi
));
2111 if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
)
2113 enum tree_code ocode
= code
== BIT_AND_EXPR
? BIT_IOR_EXPR
: BIT_AND_EXPR
;
2114 if (def1_code
== ocode
)
2117 enum tree_code coden
;
2119 /* ( X | Y) & X -> X */
2120 /* ( X & Y) | X -> X */
2124 gimple_assign_set_rhs_from_tree (gsi
, x
);
2125 update_stmt (gsi_stmt (*gsi
));
2129 defcodefor_name (def1_arg1
, &coden
, &a1
, &a2
);
2130 /* (~X | Y) & X -> X & Y */
2131 /* (~X & Y) | X -> X | Y */
2132 if (coden
== BIT_NOT_EXPR
&& a1
== x
)
2134 gimple_assign_set_rhs_with_ops (gsi
, code
,
2136 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2140 defcodefor_name (def1_arg2
, &coden
, &a1
, &a2
);
2141 /* (Y | ~X) & X -> X & Y */
2142 /* (Y & ~X) | X -> X | Y */
2143 if (coden
== BIT_NOT_EXPR
&& a1
== x
)
2145 gimple_assign_set_rhs_with_ops (gsi
, code
,
2147 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2152 if (def2_code
== ocode
)
2154 enum tree_code coden
;
2157 /* X & ( X | Y) -> X */
2158 /* X | ( X & Y) -> X */
2162 gimple_assign_set_rhs_from_tree (gsi
, x
);
2163 update_stmt (gsi_stmt (*gsi
));
2166 defcodefor_name (def2_arg1
, &coden
, &a1
, NULL
);
2167 /* (~X | Y) & X -> X & Y */
2168 /* (~X & Y) | X -> X | Y */
2169 if (coden
== BIT_NOT_EXPR
&& a1
== x
)
2171 gimple_assign_set_rhs_with_ops (gsi
, code
,
2173 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2177 defcodefor_name (def2_arg2
, &coden
, &a1
, NULL
);
2178 /* (Y | ~X) & X -> X & Y */
2179 /* (Y & ~X) | X -> X | Y */
2180 if (coden
== BIT_NOT_EXPR
&& a1
== x
)
2182 gimple_assign_set_rhs_with_ops (gsi
, code
,
2184 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2190 /* If arg1 and arg2 are booleans (or any single bit type)
2191 then try to simplify:
2198 But only do this if our result feeds into a comparison as
2199 this transformation is not always a win, particularly on
2200 targets with and-not instructions. */
2201 if (TREE_CODE (arg1
) == SSA_NAME
2202 && TREE_CODE (arg2
) == SSA_NAME
2203 && INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
2204 && TYPE_PRECISION (TREE_TYPE (arg1
)) == 1
2205 && TYPE_PRECISION (TREE_TYPE (arg2
)) == 1
2206 && (TYPE_UNSIGNED (TREE_TYPE (arg1
))
2207 == TYPE_UNSIGNED (TREE_TYPE (arg2
))))
2209 use_operand_p use_p
;
2212 if (single_imm_use (gimple_assign_lhs (stmt
), &use_p
, &use_stmt
))
2214 if (gimple_code (use_stmt
) == GIMPLE_COND
2215 && gimple_cond_lhs (use_stmt
) == gimple_assign_lhs (stmt
)
2216 && integer_zerop (gimple_cond_rhs (use_stmt
))
2217 && gimple_cond_code (use_stmt
) == NE_EXPR
)
2219 if (simplify_bitwise_binary_boolean (gsi
, code
, arg1
, arg2
))
2221 if (simplify_bitwise_binary_boolean (gsi
, code
, arg2
, arg1
))
2231 /* Recognize rotation patterns. Return true if a transformation
2232 applied, otherwise return false.
2234 We are looking for X with unsigned type T with bitsize B, OP being
2235 +, | or ^, some type T2 wider than T and
2236 (X << CNT1) OP (X >> CNT2) iff CNT1 + CNT2 == B
2237 ((T) ((T2) X << CNT1)) OP ((T) ((T2) X >> CNT2)) iff CNT1 + CNT2 == B
2238 (X << Y) OP (X >> (B - Y))
2239 (X << (int) Y) OP (X >> (int) (B - Y))
2240 ((T) ((T2) X << Y)) OP ((T) ((T2) X >> (B - Y)))
2241 ((T) ((T2) X << (int) Y)) OP ((T) ((T2) X >> (int) (B - Y)))
2242 (X << Y) | (X >> ((-Y) & (B - 1)))
2243 (X << (int) Y) | (X >> (int) ((-Y) & (B - 1)))
2244 ((T) ((T2) X << Y)) | ((T) ((T2) X >> ((-Y) & (B - 1))))
2245 ((T) ((T2) X << (int) Y)) | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
2247 and transform these into:
2251 Note, in the patterns with T2 type, the type of OP operands
2252 might be even a signed type, but should have precision B. */
2255 simplify_rotate (gimple_stmt_iterator
*gsi
)
2257 gimple stmt
= gsi_stmt (*gsi
);
2258 tree arg
[2], rtype
, rotcnt
= NULL_TREE
;
2259 tree def_arg1
[2], def_arg2
[2];
2260 enum tree_code def_code
[2];
2263 bool swapped_p
= false;
2266 arg
[0] = gimple_assign_rhs1 (stmt
);
2267 arg
[1] = gimple_assign_rhs2 (stmt
);
2268 rtype
= TREE_TYPE (arg
[0]);
2270 /* Only create rotates in complete modes. Other cases are not
2271 expanded properly. */
2272 if (!INTEGRAL_TYPE_P (rtype
)
2273 || TYPE_PRECISION (rtype
) != GET_MODE_PRECISION (TYPE_MODE (rtype
)))
2276 for (i
= 0; i
< 2; i
++)
2277 defcodefor_name (arg
[i
], &def_code
[i
], &def_arg1
[i
], &def_arg2
[i
]);
2279 /* Look through narrowing conversions. */
2280 if (CONVERT_EXPR_CODE_P (def_code
[0])
2281 && CONVERT_EXPR_CODE_P (def_code
[1])
2282 && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1
[0]))
2283 && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1
[1]))
2284 && TYPE_PRECISION (TREE_TYPE (def_arg1
[0]))
2285 == TYPE_PRECISION (TREE_TYPE (def_arg1
[1]))
2286 && TYPE_PRECISION (TREE_TYPE (def_arg1
[0])) > TYPE_PRECISION (rtype
)
2287 && has_single_use (arg
[0])
2288 && has_single_use (arg
[1]))
2290 for (i
= 0; i
< 2; i
++)
2292 arg
[i
] = def_arg1
[i
];
2293 defcodefor_name (arg
[i
], &def_code
[i
], &def_arg1
[i
], &def_arg2
[i
]);
2297 /* One operand has to be LSHIFT_EXPR and one RSHIFT_EXPR. */
2298 for (i
= 0; i
< 2; i
++)
2299 if (def_code
[i
] != LSHIFT_EXPR
&& def_code
[i
] != RSHIFT_EXPR
)
2301 else if (!has_single_use (arg
[i
]))
2303 if (def_code
[0] == def_code
[1])
2306 /* If we've looked through narrowing conversions before, look through
2307 widening conversions from unsigned type with the same precision
2309 if (TYPE_PRECISION (TREE_TYPE (def_arg1
[0])) != TYPE_PRECISION (rtype
))
2310 for (i
= 0; i
< 2; i
++)
2313 enum tree_code code
;
2314 defcodefor_name (def_arg1
[i
], &code
, &tem
, NULL
);
2315 if (!CONVERT_EXPR_CODE_P (code
)
2316 || !INTEGRAL_TYPE_P (TREE_TYPE (tem
))
2317 || TYPE_PRECISION (TREE_TYPE (tem
)) != TYPE_PRECISION (rtype
))
2321 /* Both shifts have to use the same first operand. */
2322 if (TREE_CODE (def_arg1
[0]) != SSA_NAME
|| def_arg1
[0] != def_arg1
[1])
2324 if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1
[0])))
2327 /* CNT1 + CNT2 == B case above. */
2328 if (tree_fits_uhwi_p (def_arg2
[0])
2329 && tree_fits_uhwi_p (def_arg2
[1])
2330 && tree_to_uhwi (def_arg2
[0])
2331 + tree_to_uhwi (def_arg2
[1]) == TYPE_PRECISION (rtype
))
2332 rotcnt
= def_arg2
[0];
2333 else if (TREE_CODE (def_arg2
[0]) != SSA_NAME
2334 || TREE_CODE (def_arg2
[1]) != SSA_NAME
)
2338 tree cdef_arg1
[2], cdef_arg2
[2], def_arg2_alt
[2];
2339 enum tree_code cdef_code
[2];
2340 /* Look through conversion of the shift count argument.
2341 The C/C++ FE cast any shift count argument to integer_type_node.
2342 The only problem might be if the shift count type maximum value
2343 is equal or smaller than number of bits in rtype. */
2344 for (i
= 0; i
< 2; i
++)
2346 def_arg2_alt
[i
] = def_arg2
[i
];
2347 defcodefor_name (def_arg2
[i
], &cdef_code
[i
],
2348 &cdef_arg1
[i
], &cdef_arg2
[i
]);
2349 if (CONVERT_EXPR_CODE_P (cdef_code
[i
])
2350 && INTEGRAL_TYPE_P (TREE_TYPE (cdef_arg1
[i
]))
2351 && TYPE_PRECISION (TREE_TYPE (cdef_arg1
[i
]))
2352 > floor_log2 (TYPE_PRECISION (rtype
))
2353 && TYPE_PRECISION (TREE_TYPE (cdef_arg1
[i
]))
2354 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (cdef_arg1
[i
]))))
2356 def_arg2_alt
[i
] = cdef_arg1
[i
];
2357 defcodefor_name (def_arg2_alt
[i
], &cdef_code
[i
],
2358 &cdef_arg1
[i
], &cdef_arg2
[i
]);
2361 for (i
= 0; i
< 2; i
++)
2362 /* Check for one shift count being Y and the other B - Y,
2363 with optional casts. */
2364 if (cdef_code
[i
] == MINUS_EXPR
2365 && tree_fits_shwi_p (cdef_arg1
[i
])
2366 && tree_to_shwi (cdef_arg1
[i
]) == TYPE_PRECISION (rtype
)
2367 && TREE_CODE (cdef_arg2
[i
]) == SSA_NAME
)
2370 enum tree_code code
;
2372 if (cdef_arg2
[i
] == def_arg2
[1 - i
]
2373 || cdef_arg2
[i
] == def_arg2_alt
[1 - i
])
2375 rotcnt
= cdef_arg2
[i
];
2378 defcodefor_name (cdef_arg2
[i
], &code
, &tem
, NULL
);
2379 if (CONVERT_EXPR_CODE_P (code
)
2380 && INTEGRAL_TYPE_P (TREE_TYPE (tem
))
2381 && TYPE_PRECISION (TREE_TYPE (tem
))
2382 > floor_log2 (TYPE_PRECISION (rtype
))
2383 && TYPE_PRECISION (TREE_TYPE (tem
))
2384 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (tem
)))
2385 && (tem
== def_arg2
[1 - i
]
2386 || tem
== def_arg2_alt
[1 - i
]))
2392 /* The above sequence isn't safe for Y being 0,
2393 because then one of the shifts triggers undefined behavior.
2394 This alternative is safe even for rotation count of 0.
2395 One shift count is Y and the other (-Y) & (B - 1). */
2396 else if (cdef_code
[i
] == BIT_AND_EXPR
2397 && tree_fits_shwi_p (cdef_arg2
[i
])
2398 && tree_to_shwi (cdef_arg2
[i
])
2399 == TYPE_PRECISION (rtype
) - 1
2400 && TREE_CODE (cdef_arg1
[i
]) == SSA_NAME
2401 && gimple_assign_rhs_code (stmt
) == BIT_IOR_EXPR
)
2404 enum tree_code code
;
2406 defcodefor_name (cdef_arg1
[i
], &code
, &tem
, NULL
);
2407 if (CONVERT_EXPR_CODE_P (code
)
2408 && INTEGRAL_TYPE_P (TREE_TYPE (tem
))
2409 && TYPE_PRECISION (TREE_TYPE (tem
))
2410 > floor_log2 (TYPE_PRECISION (rtype
))
2411 && TYPE_PRECISION (TREE_TYPE (tem
))
2412 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (tem
))))
2413 defcodefor_name (tem
, &code
, &tem
, NULL
);
2415 if (code
== NEGATE_EXPR
)
2417 if (tem
== def_arg2
[1 - i
] || tem
== def_arg2_alt
[1 - i
])
2422 defcodefor_name (tem
, &code
, &tem
, NULL
);
2423 if (CONVERT_EXPR_CODE_P (code
)
2424 && INTEGRAL_TYPE_P (TREE_TYPE (tem
))
2425 && TYPE_PRECISION (TREE_TYPE (tem
))
2426 > floor_log2 (TYPE_PRECISION (rtype
))
2427 && TYPE_PRECISION (TREE_TYPE (tem
))
2428 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (tem
)))
2429 && (tem
== def_arg2
[1 - i
]
2430 || tem
== def_arg2_alt
[1 - i
]))
2437 if (rotcnt
== NULL_TREE
)
2442 if (!useless_type_conversion_p (TREE_TYPE (def_arg2
[0]),
2443 TREE_TYPE (rotcnt
)))
2445 g
= gimple_build_assign_with_ops (NOP_EXPR
,
2446 make_ssa_name (TREE_TYPE (def_arg2
[0]),
2449 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
2450 rotcnt
= gimple_assign_lhs (g
);
2452 lhs
= gimple_assign_lhs (stmt
);
2453 if (!useless_type_conversion_p (rtype
, TREE_TYPE (def_arg1
[0])))
2454 lhs
= make_ssa_name (TREE_TYPE (def_arg1
[0]), NULL
);
2455 g
= gimple_build_assign_with_ops (((def_code
[0] == LSHIFT_EXPR
) ^ swapped_p
)
2456 ? LROTATE_EXPR
: RROTATE_EXPR
,
2457 lhs
, def_arg1
[0], rotcnt
);
2458 if (!useless_type_conversion_p (rtype
, TREE_TYPE (def_arg1
[0])))
2460 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
2461 g
= gimple_build_assign_with_ops (NOP_EXPR
, gimple_assign_lhs (stmt
),
2464 gsi_replace (gsi
, g
, false);
2468 /* Perform re-associations of the plus or minus statement STMT that are
2469 always permitted. Returns true if the CFG was changed. */
2472 associate_plusminus (gimple_stmt_iterator
*gsi
)
2474 gimple stmt
= gsi_stmt (*gsi
);
2475 tree rhs1
= gimple_assign_rhs1 (stmt
);
2476 tree rhs2
= gimple_assign_rhs2 (stmt
);
2477 enum tree_code code
= gimple_assign_rhs_code (stmt
);
2480 /* We can't reassociate at all for saturating types. */
2481 if (TYPE_SATURATING (TREE_TYPE (rhs1
)))
2484 /* First contract negates. */
2489 /* A +- (-B) -> A -+ B. */
2490 if (TREE_CODE (rhs2
) == SSA_NAME
)
2492 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs2
);
2493 if (is_gimple_assign (def_stmt
)
2494 && gimple_assign_rhs_code (def_stmt
) == NEGATE_EXPR
2495 && can_propagate_from (def_stmt
))
2497 code
= (code
== MINUS_EXPR
) ? PLUS_EXPR
: MINUS_EXPR
;
2498 gimple_assign_set_rhs_code (stmt
, code
);
2499 rhs2
= gimple_assign_rhs1 (def_stmt
);
2500 gimple_assign_set_rhs2 (stmt
, rhs2
);
2501 gimple_set_modified (stmt
, true);
2506 /* (-A) + B -> B - A. */
2507 if (TREE_CODE (rhs1
) == SSA_NAME
2508 && code
== PLUS_EXPR
)
2510 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs1
);
2511 if (is_gimple_assign (def_stmt
)
2512 && gimple_assign_rhs_code (def_stmt
) == NEGATE_EXPR
2513 && can_propagate_from (def_stmt
))
2516 gimple_assign_set_rhs_code (stmt
, code
);
2518 gimple_assign_set_rhs1 (stmt
, rhs1
);
2519 rhs2
= gimple_assign_rhs1 (def_stmt
);
2520 gimple_assign_set_rhs2 (stmt
, rhs2
);
2521 gimple_set_modified (stmt
, true);
2528 /* We can't reassociate floating-point or fixed-point plus or minus
2529 because of saturation to +-Inf. */
2530 if (FLOAT_TYPE_P (TREE_TYPE (rhs1
))
2531 || FIXED_POINT_TYPE_P (TREE_TYPE (rhs1
)))
2534 /* Second match patterns that allow contracting a plus-minus pair
2535 irrespective of overflow issues.
2537 (A +- B) - A -> +- B
2539 (CST +- A) +- CST -> CST +- A
2540 (A +- CST) +- CST -> A +- CST
2543 A - (A +- B) -> -+ B
2544 A +- (B +- A) -> +- B
2545 CST +- (CST +- A) -> CST +- A
2546 CST +- (A +- CST) -> CST +- A
2548 (T)(P + A) - (T)P -> (T)A
2550 via commutating the addition and contracting operations to zero
2551 by reassociation. */
2553 if (TREE_CODE (rhs1
) == SSA_NAME
)
2555 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs1
);
2556 if (is_gimple_assign (def_stmt
) && can_propagate_from (def_stmt
))
2558 enum tree_code def_code
= gimple_assign_rhs_code (def_stmt
);
2559 if (def_code
== PLUS_EXPR
2560 || def_code
== MINUS_EXPR
)
2562 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2563 tree def_rhs2
= gimple_assign_rhs2 (def_stmt
);
2564 if (operand_equal_p (def_rhs1
, rhs2
, 0)
2565 && code
== MINUS_EXPR
)
2567 /* (A +- B) - A -> +- B. */
2568 code
= ((def_code
== PLUS_EXPR
)
2569 ? TREE_CODE (def_rhs2
) : NEGATE_EXPR
);
2572 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2573 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2574 gimple_set_modified (stmt
, true);
2576 else if (operand_equal_p (def_rhs2
, rhs2
, 0)
2577 && code
!= def_code
)
2579 /* (A +- B) -+ B -> A. */
2580 code
= TREE_CODE (def_rhs1
);
2583 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2584 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2585 gimple_set_modified (stmt
, true);
2587 else if (CONSTANT_CLASS_P (rhs2
)
2588 && CONSTANT_CLASS_P (def_rhs1
))
2590 /* (CST +- A) +- CST -> CST +- A. */
2591 tree cst
= fold_binary (code
, TREE_TYPE (rhs1
),
2593 if (cst
&& !TREE_OVERFLOW (cst
))
2596 gimple_assign_set_rhs_code (stmt
, code
);
2598 gimple_assign_set_rhs1 (stmt
, rhs1
);
2600 gimple_assign_set_rhs2 (stmt
, rhs2
);
2601 gimple_set_modified (stmt
, true);
2604 else if (CONSTANT_CLASS_P (rhs2
)
2605 && CONSTANT_CLASS_P (def_rhs2
))
2607 /* (A +- CST) +- CST -> A +- CST. */
2608 enum tree_code mix
= (code
== def_code
)
2609 ? PLUS_EXPR
: MINUS_EXPR
;
2610 tree cst
= fold_binary (mix
, TREE_TYPE (rhs1
),
2612 if (cst
&& !TREE_OVERFLOW (cst
))
2615 gimple_assign_set_rhs_code (stmt
, code
);
2617 gimple_assign_set_rhs1 (stmt
, rhs1
);
2619 gimple_assign_set_rhs2 (stmt
, rhs2
);
2620 gimple_set_modified (stmt
, true);
2624 else if (def_code
== BIT_NOT_EXPR
&& code
== PLUS_EXPR
)
2626 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2627 if (operand_equal_p (def_rhs1
, rhs2
, 0))
2630 rhs1
= build_all_ones_cst (TREE_TYPE (rhs2
));
2632 code
= TREE_CODE (rhs1
);
2633 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2634 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2635 gimple_set_modified (stmt
, true);
2637 else if ((TREE_CODE (TREE_TYPE (rhs2
)) != COMPLEX_TYPE
2638 && integer_onep (rhs2
))
2639 || (TREE_CODE (rhs2
) == COMPLEX_CST
2640 && integer_onep (TREE_REALPART (rhs2
))
2641 && integer_onep (TREE_IMAGPART (rhs2
))))
2647 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2648 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2649 gimple_set_modified (stmt
, true);
2652 else if (CONVERT_EXPR_CODE_P (def_code
) && code
== MINUS_EXPR
2653 && TREE_CODE (rhs2
) == SSA_NAME
)
2655 /* (T)(ptr + adj) - (T)ptr -> (T)adj. */
2656 gimple def_stmt2
= SSA_NAME_DEF_STMT (rhs2
);
2657 if (TREE_CODE (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
2658 && is_gimple_assign (def_stmt2
)
2659 && can_propagate_from (def_stmt2
)
2660 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
2661 && TREE_CODE (gimple_assign_rhs1 (def_stmt2
)) == SSA_NAME
)
2663 /* Now we have (T)A - (T)ptr. */
2664 tree ptr
= gimple_assign_rhs1 (def_stmt2
);
2665 def_stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (def_stmt
));
2666 if (is_gimple_assign (def_stmt2
)
2667 && gimple_assign_rhs_code (def_stmt2
) == POINTER_PLUS_EXPR
2668 && gimple_assign_rhs1 (def_stmt2
) == ptr
)
2670 /* And finally (T)(ptr + X) - (T)ptr. */
2671 tree adj
= gimple_assign_rhs2 (def_stmt2
);
2672 /* If the conversion of the pointer adjustment to the
2673 final type requires a sign- or zero-extension we
2674 have to punt - it is not defined which one is
2676 if (TYPE_PRECISION (TREE_TYPE (rhs1
))
2677 <= TYPE_PRECISION (TREE_TYPE (adj
))
2678 || (TREE_CODE (adj
) == INTEGER_CST
2679 && tree_int_cst_sign_bit (adj
) == 0))
2681 if (useless_type_conversion_p (TREE_TYPE (rhs1
),
2684 code
= TREE_CODE (adj
);
2693 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
,
2695 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2696 gimple_set_modified (stmt
, true);
2704 if (rhs2
&& TREE_CODE (rhs2
) == SSA_NAME
)
2706 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs2
);
2707 if (is_gimple_assign (def_stmt
) && can_propagate_from (def_stmt
))
2709 enum tree_code def_code
= gimple_assign_rhs_code (def_stmt
);
2710 if (def_code
== PLUS_EXPR
2711 || def_code
== MINUS_EXPR
)
2713 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2714 tree def_rhs2
= gimple_assign_rhs2 (def_stmt
);
2715 if (operand_equal_p (def_rhs1
, rhs1
, 0)
2716 && code
== MINUS_EXPR
)
2718 /* A - (A +- B) -> -+ B. */
2719 code
= ((def_code
== PLUS_EXPR
)
2720 ? NEGATE_EXPR
: TREE_CODE (def_rhs2
));
2723 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2724 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2725 gimple_set_modified (stmt
, true);
2727 else if (operand_equal_p (def_rhs2
, rhs1
, 0)
2728 && code
!= def_code
)
2730 /* A +- (B +- A) -> +- B. */
2731 code
= ((code
== PLUS_EXPR
)
2732 ? TREE_CODE (def_rhs1
) : NEGATE_EXPR
);
2735 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2736 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2737 gimple_set_modified (stmt
, true);
2739 else if (CONSTANT_CLASS_P (rhs1
)
2740 && CONSTANT_CLASS_P (def_rhs1
))
2742 /* CST +- (CST +- A) -> CST +- A. */
2743 tree cst
= fold_binary (code
, TREE_TYPE (rhs2
),
2745 if (cst
&& !TREE_OVERFLOW (cst
))
2747 code
= (code
== def_code
? PLUS_EXPR
: MINUS_EXPR
);
2748 gimple_assign_set_rhs_code (stmt
, code
);
2750 gimple_assign_set_rhs1 (stmt
, rhs1
);
2752 gimple_assign_set_rhs2 (stmt
, rhs2
);
2753 gimple_set_modified (stmt
, true);
2756 else if (CONSTANT_CLASS_P (rhs1
)
2757 && CONSTANT_CLASS_P (def_rhs2
))
2759 /* CST +- (A +- CST) -> CST +- A. */
2760 tree cst
= fold_binary (def_code
== code
2761 ? PLUS_EXPR
: MINUS_EXPR
,
2764 if (cst
&& !TREE_OVERFLOW (cst
))
2767 gimple_assign_set_rhs1 (stmt
, rhs1
);
2769 gimple_assign_set_rhs2 (stmt
, rhs2
);
2770 gimple_set_modified (stmt
, true);
2774 else if (def_code
== BIT_NOT_EXPR
)
2776 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2777 if (code
== PLUS_EXPR
2778 && operand_equal_p (def_rhs1
, rhs1
, 0))
2781 rhs1
= build_all_ones_cst (TREE_TYPE (rhs1
));
2783 code
= TREE_CODE (rhs1
);
2784 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2785 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2786 gimple_set_modified (stmt
, true);
2793 if (gimple_modified_p (stmt
))
2795 fold_stmt_inplace (gsi
);
2803 /* Associate operands of a POINTER_PLUS_EXPR assignmen at *GSI. Returns
2804 true if anything changed, false otherwise. */
2807 associate_pointerplus_align (gimple_stmt_iterator
*gsi
)
2809 gimple stmt
= gsi_stmt (*gsi
);
2811 tree ptr
, rhs
, algn
;
2814 tem = (sizetype) ptr;
2818 and produce the simpler and easier to analyze with respect to alignment
2819 ... = ptr & ~algn; */
2820 ptr
= gimple_assign_rhs1 (stmt
);
2821 rhs
= gimple_assign_rhs2 (stmt
);
2822 if (TREE_CODE (rhs
) != SSA_NAME
)
2824 def_stmt
= SSA_NAME_DEF_STMT (rhs
);
2825 if (!is_gimple_assign (def_stmt
)
2826 || gimple_assign_rhs_code (def_stmt
) != NEGATE_EXPR
)
2828 rhs
= gimple_assign_rhs1 (def_stmt
);
2829 if (TREE_CODE (rhs
) != SSA_NAME
)
2831 def_stmt
= SSA_NAME_DEF_STMT (rhs
);
2832 if (!is_gimple_assign (def_stmt
)
2833 || gimple_assign_rhs_code (def_stmt
) != BIT_AND_EXPR
)
2835 rhs
= gimple_assign_rhs1 (def_stmt
);
2836 algn
= gimple_assign_rhs2 (def_stmt
);
2837 if (TREE_CODE (rhs
) != SSA_NAME
2838 || TREE_CODE (algn
) != INTEGER_CST
)
2840 def_stmt
= SSA_NAME_DEF_STMT (rhs
);
2841 if (!is_gimple_assign (def_stmt
)
2842 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
2844 if (gimple_assign_rhs1 (def_stmt
) != ptr
)
2847 algn
= double_int_to_tree (TREE_TYPE (ptr
), ~tree_to_double_int (algn
));
2848 gimple_assign_set_rhs_with_ops (gsi
, BIT_AND_EXPR
, ptr
, algn
);
2849 fold_stmt_inplace (gsi
);
2855 /* Associate operands of a POINTER_PLUS_EXPR assignmen at *GSI. Returns
2856 true if anything changed, false otherwise. */
2859 associate_pointerplus_diff (gimple_stmt_iterator
*gsi
)
2861 gimple stmt
= gsi_stmt (*gsi
);
2869 tem4 = (unsigned long) tem3;
2873 ptr1
= gimple_assign_rhs1 (stmt
);
2874 rhs
= gimple_assign_rhs2 (stmt
);
2875 if (TREE_CODE (rhs
) != SSA_NAME
)
2877 gimple minus
= SSA_NAME_DEF_STMT (rhs
);
2878 /* Conditionally look through a sign-changing conversion. */
2879 if (is_gimple_assign (minus
)
2880 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (minus
))
2881 && (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (minus
)))
2882 == TYPE_PRECISION (TREE_TYPE (rhs
)))
2883 && TREE_CODE (gimple_assign_rhs1 (minus
)) == SSA_NAME
)
2884 minus
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (minus
));
2885 if (!is_gimple_assign (minus
))
2887 if (gimple_assign_rhs_code (minus
) != MINUS_EXPR
)
2889 rhs
= gimple_assign_rhs2 (minus
);
2890 if (TREE_CODE (rhs
) != SSA_NAME
)
2892 def_stmt
= SSA_NAME_DEF_STMT (rhs
);
2893 if (!is_gimple_assign (def_stmt
)
2894 || ! CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
2895 || gimple_assign_rhs1 (def_stmt
) != ptr1
)
2897 rhs
= gimple_assign_rhs1 (minus
);
2898 if (TREE_CODE (rhs
) != SSA_NAME
)
2900 def_stmt
= SSA_NAME_DEF_STMT (rhs
);
2901 if (!is_gimple_assign (def_stmt
)
2902 || ! CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
2904 rhs
= gimple_assign_rhs1 (def_stmt
);
2905 if (! useless_type_conversion_p (TREE_TYPE (ptr1
), TREE_TYPE (rhs
)))
2908 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (rhs
), rhs
, NULL_TREE
);
2914 /* Associate operands of a POINTER_PLUS_EXPR assignmen at *GSI. Returns
2915 true if anything changed, false otherwise. */
2918 associate_pointerplus (gimple_stmt_iterator
*gsi
)
2920 gimple stmt
= gsi_stmt (*gsi
);
2922 tree ptr
, off1
, off2
;
2924 if (associate_pointerplus_align (gsi
)
2925 || associate_pointerplus_diff (gsi
))
2928 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2929 ptr
= gimple_assign_rhs1 (stmt
);
2930 off1
= gimple_assign_rhs2 (stmt
);
2931 if (TREE_CODE (ptr
) != SSA_NAME
2932 || !has_single_use (ptr
))
2934 def_stmt
= SSA_NAME_DEF_STMT (ptr
);
2935 if (!is_gimple_assign (def_stmt
)
2936 || gimple_assign_rhs_code (def_stmt
) != POINTER_PLUS_EXPR
2937 || !can_propagate_from (def_stmt
))
2939 ptr
= gimple_assign_rhs1 (def_stmt
);
2940 off2
= gimple_assign_rhs2 (def_stmt
);
2941 if (!types_compatible_p (TREE_TYPE (off1
), TREE_TYPE (off2
)))
2944 tree off
= make_ssa_name (TREE_TYPE (off1
), NULL
);
2945 gimple ostmt
= gimple_build_assign_with_ops (PLUS_EXPR
, off
, off1
, off2
);
2946 gsi_insert_before (gsi
, ostmt
, GSI_SAME_STMT
);
2948 gimple_assign_set_rhs_with_ops (gsi
, POINTER_PLUS_EXPR
, ptr
, off
);
2954 /* Combine two conversions in a row for the second conversion at *GSI.
2955 Returns 1 if there were any changes made, 2 if cfg-cleanup needs to
2956 run. Else it returns 0. */
2959 combine_conversions (gimple_stmt_iterator
*gsi
)
2961 gimple stmt
= gsi_stmt (*gsi
);
2964 enum tree_code code
= gimple_assign_rhs_code (stmt
);
2965 enum tree_code code2
;
2967 gcc_checking_assert (CONVERT_EXPR_CODE_P (code
)
2968 || code
== FLOAT_EXPR
2969 || code
== FIX_TRUNC_EXPR
);
2971 lhs
= gimple_assign_lhs (stmt
);
2972 op0
= gimple_assign_rhs1 (stmt
);
2973 if (useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
)))
2975 gimple_assign_set_rhs_code (stmt
, TREE_CODE (op0
));
2979 if (TREE_CODE (op0
) != SSA_NAME
)
2982 def_stmt
= SSA_NAME_DEF_STMT (op0
);
2983 if (!is_gimple_assign (def_stmt
))
2986 code2
= gimple_assign_rhs_code (def_stmt
);
2988 if (CONVERT_EXPR_CODE_P (code2
) || code2
== FLOAT_EXPR
)
2990 tree defop0
= gimple_assign_rhs1 (def_stmt
);
2991 tree type
= TREE_TYPE (lhs
);
2992 tree inside_type
= TREE_TYPE (defop0
);
2993 tree inter_type
= TREE_TYPE (op0
);
2994 int inside_int
= INTEGRAL_TYPE_P (inside_type
);
2995 int inside_ptr
= POINTER_TYPE_P (inside_type
);
2996 int inside_float
= FLOAT_TYPE_P (inside_type
);
2997 int inside_vec
= TREE_CODE (inside_type
) == VECTOR_TYPE
;
2998 unsigned int inside_prec
= TYPE_PRECISION (inside_type
);
2999 int inside_unsignedp
= TYPE_UNSIGNED (inside_type
);
3000 int inter_int
= INTEGRAL_TYPE_P (inter_type
);
3001 int inter_ptr
= POINTER_TYPE_P (inter_type
);
3002 int inter_float
= FLOAT_TYPE_P (inter_type
);
3003 int inter_vec
= TREE_CODE (inter_type
) == VECTOR_TYPE
;
3004 unsigned int inter_prec
= TYPE_PRECISION (inter_type
);
3005 int inter_unsignedp
= TYPE_UNSIGNED (inter_type
);
3006 int final_int
= INTEGRAL_TYPE_P (type
);
3007 int final_ptr
= POINTER_TYPE_P (type
);
3008 int final_float
= FLOAT_TYPE_P (type
);
3009 int final_vec
= TREE_CODE (type
) == VECTOR_TYPE
;
3010 unsigned int final_prec
= TYPE_PRECISION (type
);
3011 int final_unsignedp
= TYPE_UNSIGNED (type
);
3013 /* Don't propagate ssa names that occur in abnormal phis. */
3014 if (TREE_CODE (defop0
) == SSA_NAME
3015 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (defop0
))
3018 /* In addition to the cases of two conversions in a row
3019 handled below, if we are converting something to its own
3020 type via an object of identical or wider precision, neither
3021 conversion is needed. */
3022 if (useless_type_conversion_p (type
, inside_type
)
3023 && (((inter_int
|| inter_ptr
) && final_int
)
3024 || (inter_float
&& final_float
))
3025 && inter_prec
>= final_prec
)
3027 gimple_assign_set_rhs1 (stmt
, unshare_expr (defop0
));
3028 gimple_assign_set_rhs_code (stmt
, TREE_CODE (defop0
));
3030 return remove_prop_source_from_use (op0
) ? 2 : 1;
3033 /* Likewise, if the intermediate and initial types are either both
3034 float or both integer, we don't need the middle conversion if the
3035 former is wider than the latter and doesn't change the signedness
3036 (for integers). Avoid this if the final type is a pointer since
3037 then we sometimes need the middle conversion. Likewise if the
3038 final type has a precision not equal to the size of its mode. */
3039 if (((inter_int
&& inside_int
) || (inter_float
&& inside_float
))
3040 && (final_int
|| final_float
)
3041 && inter_prec
>= inside_prec
3042 && (inter_float
|| inter_unsignedp
== inside_unsignedp
)
3043 && ! (final_prec
!= GET_MODE_PRECISION (TYPE_MODE (type
))
3044 && TYPE_MODE (type
) == TYPE_MODE (inter_type
)))
3046 gimple_assign_set_rhs1 (stmt
, defop0
);
3048 return remove_prop_source_from_use (op0
) ? 2 : 1;
3051 /* If we have a sign-extension of a zero-extended value, we can
3052 replace that by a single zero-extension. Likewise if the
3053 final conversion does not change precision we can drop the
3054 intermediate conversion. */
3055 if (inside_int
&& inter_int
&& final_int
3056 && ((inside_prec
< inter_prec
&& inter_prec
< final_prec
3057 && inside_unsignedp
&& !inter_unsignedp
)
3058 || final_prec
== inter_prec
))
3060 gimple_assign_set_rhs1 (stmt
, defop0
);
3062 return remove_prop_source_from_use (op0
) ? 2 : 1;
3065 /* Two conversions in a row are not needed unless:
3066 - some conversion is floating-point (overstrict for now), or
3067 - some conversion is a vector (overstrict for now), or
3068 - the intermediate type is narrower than both initial and
3070 - the intermediate type and innermost type differ in signedness,
3071 and the outermost type is wider than the intermediate, or
3072 - the initial type is a pointer type and the precisions of the
3073 intermediate and final types differ, or
3074 - the final type is a pointer type and the precisions of the
3075 initial and intermediate types differ. */
3076 if (! inside_float
&& ! inter_float
&& ! final_float
3077 && ! inside_vec
&& ! inter_vec
&& ! final_vec
3078 && (inter_prec
>= inside_prec
|| inter_prec
>= final_prec
)
3079 && ! (inside_int
&& inter_int
3080 && inter_unsignedp
!= inside_unsignedp
3081 && inter_prec
< final_prec
)
3082 && ((inter_unsignedp
&& inter_prec
> inside_prec
)
3083 == (final_unsignedp
&& final_prec
> inter_prec
))
3084 && ! (inside_ptr
&& inter_prec
!= final_prec
)
3085 && ! (final_ptr
&& inside_prec
!= inter_prec
)
3086 && ! (final_prec
!= GET_MODE_PRECISION (TYPE_MODE (type
))
3087 && TYPE_MODE (type
) == TYPE_MODE (inter_type
)))
3089 gimple_assign_set_rhs1 (stmt
, defop0
);
3091 return remove_prop_source_from_use (op0
) ? 2 : 1;
3094 /* A truncation to an unsigned type should be canonicalized as
3095 bitwise and of a mask. */
3096 if (final_int
&& inter_int
&& inside_int
3097 && final_prec
== inside_prec
3098 && final_prec
> inter_prec
3102 tem
= fold_build2 (BIT_AND_EXPR
, inside_type
,
3105 (inside_type
, double_int::mask (inter_prec
)));
3106 if (!useless_type_conversion_p (type
, inside_type
))
3108 tem
= force_gimple_operand_gsi (gsi
, tem
, true, NULL_TREE
, true,
3110 gimple_assign_set_rhs1 (stmt
, tem
);
3113 gimple_assign_set_rhs_from_tree (gsi
, tem
);
3114 update_stmt (gsi_stmt (*gsi
));
3118 /* If we are converting an integer to a floating-point that can
3119 represent it exactly and back to an integer, we can skip the
3120 floating-point conversion. */
3121 if (inside_int
&& inter_float
&& final_int
&&
3122 (unsigned) significand_size (TYPE_MODE (inter_type
))
3123 >= inside_prec
- !inside_unsignedp
)
3125 if (useless_type_conversion_p (type
, inside_type
))
3127 gimple_assign_set_rhs1 (stmt
, unshare_expr (defop0
));
3128 gimple_assign_set_rhs_code (stmt
, TREE_CODE (defop0
));
3130 return remove_prop_source_from_use (op0
) ? 2 : 1;
3134 gimple_assign_set_rhs1 (stmt
, defop0
);
3135 gimple_assign_set_rhs_code (stmt
, CONVERT_EXPR
);
3137 return remove_prop_source_from_use (op0
) ? 2 : 1;
3145 /* Combine VIEW_CONVERT_EXPRs with their defining statement. */
3148 simplify_vce (gimple_stmt_iterator
*gsi
)
3150 gimple stmt
= gsi_stmt (*gsi
);
3151 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
3153 /* Drop useless VIEW_CONVERT_EXPRs. */
3154 tree op
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
3155 if (useless_type_conversion_p (type
, TREE_TYPE (op
)))
3157 gimple_assign_set_rhs1 (stmt
, op
);
3162 if (TREE_CODE (op
) != SSA_NAME
)
3165 gimple def_stmt
= SSA_NAME_DEF_STMT (op
);
3166 if (!is_gimple_assign (def_stmt
))
3169 tree def_op
= gimple_assign_rhs1 (def_stmt
);
3170 switch (gimple_assign_rhs_code (def_stmt
))
3173 /* Strip integral conversions that do not change the precision. */
3174 if ((INTEGRAL_TYPE_P (TREE_TYPE (op
))
3175 || POINTER_TYPE_P (TREE_TYPE (op
)))
3176 && (INTEGRAL_TYPE_P (TREE_TYPE (def_op
))
3177 || POINTER_TYPE_P (TREE_TYPE (def_op
)))
3178 && (TYPE_PRECISION (TREE_TYPE (op
))
3179 == TYPE_PRECISION (TREE_TYPE (def_op
)))
3180 && (TYPE_SIZE (TREE_TYPE (op
))
3181 == TYPE_SIZE (TREE_TYPE (def_op
))))
3183 TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0) = def_op
;
3189 case VIEW_CONVERT_EXPR
:
3190 /* Series of VIEW_CONVERT_EXPRs on register operands can
3192 if (TREE_CODE (TREE_OPERAND (def_op
, 0)) == SSA_NAME
)
3194 if (useless_type_conversion_p (type
,
3195 TREE_TYPE (TREE_OPERAND (def_op
, 0))))
3196 gimple_assign_set_rhs1 (stmt
, TREE_OPERAND (def_op
, 0));
3198 TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0)
3199 = TREE_OPERAND (def_op
, 0);
3210 /* Combine an element access with a shuffle. Returns true if there were
3211 any changes made, else it returns false. */
3214 simplify_bitfield_ref (gimple_stmt_iterator
*gsi
)
3216 gimple stmt
= gsi_stmt (*gsi
);
3218 tree op
, op0
, op1
, op2
;
3220 unsigned idx
, n
, size
;
3221 enum tree_code code
;
3223 op
= gimple_assign_rhs1 (stmt
);
3224 gcc_checking_assert (TREE_CODE (op
) == BIT_FIELD_REF
);
3226 op0
= TREE_OPERAND (op
, 0);
3227 if (TREE_CODE (op0
) != SSA_NAME
3228 || TREE_CODE (TREE_TYPE (op0
)) != VECTOR_TYPE
)
3231 def_stmt
= get_prop_source_stmt (op0
, false, NULL
);
3232 if (!def_stmt
|| !can_propagate_from (def_stmt
))
3235 op1
= TREE_OPERAND (op
, 1);
3236 op2
= TREE_OPERAND (op
, 2);
3237 code
= gimple_assign_rhs_code (def_stmt
);
3239 if (code
== CONSTRUCTOR
)
3241 tree tem
= fold_ternary (BIT_FIELD_REF
, TREE_TYPE (op
),
3242 gimple_assign_rhs1 (def_stmt
), op1
, op2
);
3243 if (!tem
|| !valid_gimple_rhs_p (tem
))
3245 gimple_assign_set_rhs_from_tree (gsi
, tem
);
3246 update_stmt (gsi_stmt (*gsi
));
3250 elem_type
= TREE_TYPE (TREE_TYPE (op0
));
3251 if (TREE_TYPE (op
) != elem_type
)
3254 size
= TREE_INT_CST_LOW (TYPE_SIZE (elem_type
));
3255 n
= TREE_INT_CST_LOW (op1
) / size
;
3258 idx
= TREE_INT_CST_LOW (op2
) / size
;
3260 if (code
== VEC_PERM_EXPR
)
3262 tree p
, m
, index
, tem
;
3264 m
= gimple_assign_rhs3 (def_stmt
);
3265 if (TREE_CODE (m
) != VECTOR_CST
)
3267 nelts
= VECTOR_CST_NELTS (m
);
3268 idx
= TREE_INT_CST_LOW (VECTOR_CST_ELT (m
, idx
));
3272 p
= gimple_assign_rhs1 (def_stmt
);
3276 p
= gimple_assign_rhs2 (def_stmt
);
3279 index
= build_int_cst (TREE_TYPE (TREE_TYPE (m
)), idx
* size
);
3280 tem
= build3 (BIT_FIELD_REF
, TREE_TYPE (op
),
3281 unshare_expr (p
), op1
, index
);
3282 gimple_assign_set_rhs1 (stmt
, tem
);
3284 update_stmt (gsi_stmt (*gsi
));
3291 /* Determine whether applying the 2 permutations (mask1 then mask2)
3292 gives back one of the input. */
3295 is_combined_permutation_identity (tree mask1
, tree mask2
)
3298 unsigned int nelts
, i
, j
;
3299 bool maybe_identity1
= true;
3300 bool maybe_identity2
= true;
3302 gcc_checking_assert (TREE_CODE (mask1
) == VECTOR_CST
3303 && TREE_CODE (mask2
) == VECTOR_CST
);
3304 mask
= fold_ternary (VEC_PERM_EXPR
, TREE_TYPE (mask1
), mask1
, mask1
, mask2
);
3305 gcc_assert (TREE_CODE (mask
) == VECTOR_CST
);
3307 nelts
= VECTOR_CST_NELTS (mask
);
3308 for (i
= 0; i
< nelts
; i
++)
3310 tree val
= VECTOR_CST_ELT (mask
, i
);
3311 gcc_assert (TREE_CODE (val
) == INTEGER_CST
);
3312 j
= TREE_INT_CST_LOW (val
) & (2 * nelts
- 1);
3314 maybe_identity2
= false;
3315 else if (j
== i
+ nelts
)
3316 maybe_identity1
= false;
3320 return maybe_identity1
? 1 : maybe_identity2
? 2 : 0;
3323 /* Combine a shuffle with its arguments. Returns 1 if there were any
3324 changes made, 2 if cfg-cleanup needs to run. Else it returns 0. */
3327 simplify_permutation (gimple_stmt_iterator
*gsi
)
3329 gimple stmt
= gsi_stmt (*gsi
);
3331 tree op0
, op1
, op2
, op3
, arg0
, arg1
;
3332 enum tree_code code
;
3333 bool single_use_op0
= false;
3335 gcc_checking_assert (gimple_assign_rhs_code (stmt
) == VEC_PERM_EXPR
);
3337 op0
= gimple_assign_rhs1 (stmt
);
3338 op1
= gimple_assign_rhs2 (stmt
);
3339 op2
= gimple_assign_rhs3 (stmt
);
3341 if (TREE_CODE (op2
) != VECTOR_CST
)
3344 if (TREE_CODE (op0
) == VECTOR_CST
)
3349 else if (TREE_CODE (op0
) == SSA_NAME
)
3351 def_stmt
= get_prop_source_stmt (op0
, false, &single_use_op0
);
3352 if (!def_stmt
|| !can_propagate_from (def_stmt
))
3355 code
= gimple_assign_rhs_code (def_stmt
);
3356 arg0
= gimple_assign_rhs1 (def_stmt
);
3361 /* Two consecutive shuffles. */
3362 if (code
== VEC_PERM_EXPR
)
3369 op3
= gimple_assign_rhs3 (def_stmt
);
3370 if (TREE_CODE (op3
) != VECTOR_CST
)
3372 ident
= is_combined_permutation_identity (op3
, op2
);
3375 orig
= (ident
== 1) ? gimple_assign_rhs1 (def_stmt
)
3376 : gimple_assign_rhs2 (def_stmt
);
3377 gimple_assign_set_rhs1 (stmt
, unshare_expr (orig
));
3378 gimple_assign_set_rhs_code (stmt
, TREE_CODE (orig
));
3379 gimple_set_num_ops (stmt
, 2);
3381 return remove_prop_source_from_use (op0
) ? 2 : 1;
3384 /* Shuffle of a constructor. */
3385 else if (code
== CONSTRUCTOR
|| code
== VECTOR_CST
)
3391 if (TREE_CODE (op0
) == SSA_NAME
&& !single_use_op0
)
3394 if (TREE_CODE (op1
) == VECTOR_CST
)
3396 else if (TREE_CODE (op1
) == SSA_NAME
)
3398 enum tree_code code2
;
3400 gimple def_stmt2
= get_prop_source_stmt (op1
, true, NULL
);
3401 if (!def_stmt2
|| !can_propagate_from (def_stmt2
))
3404 code2
= gimple_assign_rhs_code (def_stmt2
);
3405 if (code2
!= CONSTRUCTOR
&& code2
!= VECTOR_CST
)
3407 arg1
= gimple_assign_rhs1 (def_stmt2
);
3414 /* Already used twice in this statement. */
3415 if (TREE_CODE (op0
) == SSA_NAME
&& num_imm_uses (op0
) > 2)
3419 opt
= fold_ternary (VEC_PERM_EXPR
, TREE_TYPE (op0
), arg0
, arg1
, op2
);
3421 || (TREE_CODE (opt
) != CONSTRUCTOR
&& TREE_CODE (opt
) != VECTOR_CST
))
3423 gimple_assign_set_rhs_from_tree (gsi
, opt
);
3424 update_stmt (gsi_stmt (*gsi
));
3425 if (TREE_CODE (op0
) == SSA_NAME
)
3426 ret
= remove_prop_source_from_use (op0
);
3427 if (op0
!= op1
&& TREE_CODE (op1
) == SSA_NAME
)
3428 ret
|= remove_prop_source_from_use (op1
);
3435 /* Recognize a VEC_PERM_EXPR. Returns true if there were any changes. */
3438 simplify_vector_constructor (gimple_stmt_iterator
*gsi
)
3440 gimple stmt
= gsi_stmt (*gsi
);
3442 tree op
, op2
, orig
, type
, elem_type
;
3443 unsigned elem_size
, nelts
, i
;
3444 enum tree_code code
;
3445 constructor_elt
*elt
;
3449 gcc_checking_assert (gimple_assign_rhs_code (stmt
) == CONSTRUCTOR
);
3451 op
= gimple_assign_rhs1 (stmt
);
3452 type
= TREE_TYPE (op
);
3453 gcc_checking_assert (TREE_CODE (type
) == VECTOR_TYPE
);
3455 nelts
= TYPE_VECTOR_SUBPARTS (type
);
3456 elem_type
= TREE_TYPE (type
);
3457 elem_size
= TREE_INT_CST_LOW (TYPE_SIZE (elem_type
));
3459 sel
= XALLOCAVEC (unsigned char, nelts
);
3462 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (op
), i
, elt
)
3469 if (TREE_CODE (elt
->value
) != SSA_NAME
)
3471 def_stmt
= get_prop_source_stmt (elt
->value
, false, NULL
);
3474 code
= gimple_assign_rhs_code (def_stmt
);
3475 if (code
!= BIT_FIELD_REF
)
3477 op1
= gimple_assign_rhs1 (def_stmt
);
3478 ref
= TREE_OPERAND (op1
, 0);
3486 if (TREE_CODE (ref
) != SSA_NAME
)
3488 if (!useless_type_conversion_p (type
, TREE_TYPE (ref
)))
3492 if (TREE_INT_CST_LOW (TREE_OPERAND (op1
, 1)) != elem_size
)
3494 sel
[i
] = TREE_INT_CST_LOW (TREE_OPERAND (op1
, 2)) / elem_size
;
3495 if (sel
[i
] != i
) maybe_ident
= false;
3501 gimple_assign_set_rhs_from_tree (gsi
, orig
);
3504 tree mask_type
, *mask_elts
;
3506 if (!can_vec_perm_p (TYPE_MODE (type
), false, sel
))
3509 = build_vector_type (build_nonstandard_integer_type (elem_size
, 1),
3511 if (GET_MODE_CLASS (TYPE_MODE (mask_type
)) != MODE_VECTOR_INT
3512 || GET_MODE_SIZE (TYPE_MODE (mask_type
))
3513 != GET_MODE_SIZE (TYPE_MODE (type
)))
3515 mask_elts
= XALLOCAVEC (tree
, nelts
);
3516 for (i
= 0; i
< nelts
; i
++)
3517 mask_elts
[i
] = build_int_cst (TREE_TYPE (mask_type
), sel
[i
]);
3518 op2
= build_vector (mask_type
, mask_elts
);
3519 gimple_assign_set_rhs_with_ops_1 (gsi
, VEC_PERM_EXPR
, orig
, orig
, op2
);
3521 update_stmt (gsi_stmt (*gsi
));
3525 /* Simplify multiplications.
3526 Return true if a transformation applied, otherwise return false. */
3529 simplify_mult (gimple_stmt_iterator
*gsi
)
3531 gimple stmt
= gsi_stmt (*gsi
);
3532 tree arg1
= gimple_assign_rhs1 (stmt
);
3533 tree arg2
= gimple_assign_rhs2 (stmt
);
3535 if (TREE_CODE (arg1
) != SSA_NAME
)
3538 gimple def_stmt
= SSA_NAME_DEF_STMT (arg1
);
3539 if (!is_gimple_assign (def_stmt
))
3542 /* Look through a sign-changing conversion. */
3543 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
3545 if (TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (def_stmt
)))
3546 != TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
3547 || TREE_CODE (gimple_assign_rhs1 (def_stmt
)) != SSA_NAME
)
3549 def_stmt
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (def_stmt
));
3550 if (!is_gimple_assign (def_stmt
))
3554 if (gimple_assign_rhs_code (def_stmt
) == EXACT_DIV_EXPR
)
3556 if (operand_equal_p (gimple_assign_rhs2 (def_stmt
), arg2
, 0))
3558 tree res
= gimple_assign_rhs1 (def_stmt
);
3559 if (useless_type_conversion_p (TREE_TYPE (arg1
), TREE_TYPE (res
)))
3560 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (res
), res
,
3563 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, res
, NULL_TREE
);
3564 gcc_assert (gsi_stmt (*gsi
) == stmt
);
3572 /* Main entry point for the forward propagation and statement combine
3576 ssa_forward_propagate_and_combine (void)
3579 unsigned int todoflags
= 0;
3581 cfg_changed
= false;
3583 FOR_EACH_BB_FN (bb
, cfun
)
3585 gimple_stmt_iterator gsi
;
3587 /* Apply forward propagation to all stmts in the basic-block.
3588 Note we update GSI within the loop as necessary. */
3589 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
3591 gimple stmt
= gsi_stmt (gsi
);
3593 enum tree_code code
;
3595 if (!is_gimple_assign (stmt
))
3601 lhs
= gimple_assign_lhs (stmt
);
3602 rhs
= gimple_assign_rhs1 (stmt
);
3603 code
= gimple_assign_rhs_code (stmt
);
3604 if (TREE_CODE (lhs
) != SSA_NAME
3605 || has_zero_uses (lhs
))
3611 /* If this statement sets an SSA_NAME to an address,
3612 try to propagate the address into the uses of the SSA_NAME. */
3613 if (code
== ADDR_EXPR
3614 /* Handle pointer conversions on invariant addresses
3615 as well, as this is valid gimple. */
3616 || (CONVERT_EXPR_CODE_P (code
)
3617 && TREE_CODE (rhs
) == ADDR_EXPR
3618 && POINTER_TYPE_P (TREE_TYPE (lhs
))))
3620 tree base
= get_base_address (TREE_OPERAND (rhs
, 0));
3623 || decl_address_invariant_p (base
))
3624 && !stmt_references_abnormal_ssa_name (stmt
)
3625 && forward_propagate_addr_expr (lhs
, rhs
, true))
3627 release_defs (stmt
);
3628 gsi_remove (&gsi
, true);
3633 else if (code
== POINTER_PLUS_EXPR
)
3635 tree off
= gimple_assign_rhs2 (stmt
);
3636 if (TREE_CODE (off
) == INTEGER_CST
3637 && can_propagate_from (stmt
)
3638 && !simple_iv_increment_p (stmt
)
3639 /* ??? Better adjust the interface to that function
3640 instead of building new trees here. */
3641 && forward_propagate_addr_expr
3643 build1_loc (gimple_location (stmt
),
3644 ADDR_EXPR
, TREE_TYPE (rhs
),
3645 fold_build2 (MEM_REF
,
3646 TREE_TYPE (TREE_TYPE (rhs
)),
3648 fold_convert (ptr_type_node
,
3651 release_defs (stmt
);
3652 gsi_remove (&gsi
, true);
3654 else if (is_gimple_min_invariant (rhs
))
3656 /* Make sure to fold &a[0] + off_1 here. */
3657 fold_stmt_inplace (&gsi
);
3659 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
3665 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3667 if (forward_propagate_comparison (&gsi
))
3674 /* Combine stmts with the stmts defining their operands.
3675 Note we update GSI within the loop as necessary. */
3676 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);)
3678 gimple stmt
= gsi_stmt (gsi
);
3679 bool changed
= false;
3681 /* Mark stmt as potentially needing revisiting. */
3682 gimple_set_plf (stmt
, GF_PLF_1
, false);
3684 switch (gimple_code (stmt
))
3688 tree rhs1
= gimple_assign_rhs1 (stmt
);
3689 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3691 if ((code
== BIT_NOT_EXPR
3692 || code
== NEGATE_EXPR
)
3693 && TREE_CODE (rhs1
) == SSA_NAME
)
3694 changed
= simplify_not_neg_expr (&gsi
);
3695 else if (code
== COND_EXPR
3696 || code
== VEC_COND_EXPR
)
3698 /* In this case the entire COND_EXPR is in rhs1. */
3699 if (forward_propagate_into_cond (&gsi
)
3700 || combine_cond_exprs (&gsi
))
3703 stmt
= gsi_stmt (gsi
);
3706 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3709 did_something
= forward_propagate_into_comparison (&gsi
);
3710 if (did_something
== 2)
3712 changed
= did_something
!= 0;
3714 else if ((code
== PLUS_EXPR
3715 || code
== BIT_IOR_EXPR
3716 || code
== BIT_XOR_EXPR
)
3717 && simplify_rotate (&gsi
))
3719 else if (code
== BIT_AND_EXPR
3720 || code
== BIT_IOR_EXPR
3721 || code
== BIT_XOR_EXPR
)
3722 changed
= simplify_bitwise_binary (&gsi
);
3723 else if (code
== MULT_EXPR
)
3725 changed
= simplify_mult (&gsi
);
3727 && maybe_clean_or_replace_eh_stmt (stmt
, stmt
)
3728 && gimple_purge_dead_eh_edges (bb
))
3731 else if (code
== PLUS_EXPR
3732 || code
== MINUS_EXPR
)
3734 changed
= associate_plusminus (&gsi
);
3736 && maybe_clean_or_replace_eh_stmt (stmt
, stmt
)
3737 && gimple_purge_dead_eh_edges (bb
))
3740 else if (code
== POINTER_PLUS_EXPR
)
3741 changed
= associate_pointerplus (&gsi
);
3742 else if (CONVERT_EXPR_CODE_P (code
)
3743 || code
== FLOAT_EXPR
3744 || code
== FIX_TRUNC_EXPR
)
3746 int did_something
= combine_conversions (&gsi
);
3747 if (did_something
== 2)
3750 /* If we have a narrowing conversion to an integral
3751 type that is fed by a BIT_AND_EXPR, we might be
3752 able to remove the BIT_AND_EXPR if it merely
3753 masks off bits outside the final type (and nothing
3755 if (! did_something
)
3757 tree outer_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
3758 tree inner_type
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
3759 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
3760 && INTEGRAL_TYPE_P (outer_type
)
3761 && INTEGRAL_TYPE_P (inner_type
)
3762 && (TYPE_PRECISION (outer_type
)
3763 <= TYPE_PRECISION (inner_type
)))
3764 did_something
= simplify_conversion_from_bitmask (&gsi
);
3767 changed
= did_something
!= 0;
3769 else if (code
== VIEW_CONVERT_EXPR
)
3770 changed
= simplify_vce (&gsi
);
3771 else if (code
== VEC_PERM_EXPR
)
3773 int did_something
= simplify_permutation (&gsi
);
3774 if (did_something
== 2)
3776 changed
= did_something
!= 0;
3778 else if (code
== BIT_FIELD_REF
)
3779 changed
= simplify_bitfield_ref (&gsi
);
3780 else if (code
== CONSTRUCTOR
3781 && TREE_CODE (TREE_TYPE (rhs1
)) == VECTOR_TYPE
)
3782 changed
= simplify_vector_constructor (&gsi
);
3787 changed
= simplify_gimple_switch (stmt
);
3793 did_something
= forward_propagate_into_gimple_cond (stmt
);
3794 if (did_something
== 2)
3796 changed
= did_something
!= 0;
3802 tree callee
= gimple_call_fndecl (stmt
);
3803 if (callee
!= NULL_TREE
3804 && DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
3805 changed
= simplify_builtin_call (&gsi
, callee
);
3814 /* If the stmt changed then re-visit it and the statements
3815 inserted before it. */
3816 for (; !gsi_end_p (gsi
); gsi_prev (&gsi
))
3817 if (gimple_plf (gsi_stmt (gsi
), GF_PLF_1
))
3819 if (gsi_end_p (gsi
))
3820 gsi
= gsi_start_bb (bb
);
3826 /* Stmt no longer needs to be revisited. */
3827 gimple_set_plf (stmt
, GF_PLF_1
, true);
3834 todoflags
|= TODO_cleanup_cfg
;
3841 gate_forwprop (void)
3843 return flag_tree_forwprop
;
3848 const pass_data pass_data_forwprop
=
3850 GIMPLE_PASS
, /* type */
3851 "forwprop", /* name */
3852 OPTGROUP_NONE
, /* optinfo_flags */
3853 true, /* has_gate */
3854 true, /* has_execute */
3855 TV_TREE_FORWPROP
, /* tv_id */
3856 ( PROP_cfg
| PROP_ssa
), /* properties_required */
3857 0, /* properties_provided */
3858 0, /* properties_destroyed */
3859 0, /* todo_flags_start */
3860 ( TODO_update_ssa
| TODO_verify_ssa
), /* todo_flags_finish */
3863 class pass_forwprop
: public gimple_opt_pass
3866 pass_forwprop (gcc::context
*ctxt
)
3867 : gimple_opt_pass (pass_data_forwprop
, ctxt
)
3870 /* opt_pass methods: */
3871 opt_pass
* clone () { return new pass_forwprop (m_ctxt
); }
3872 bool gate () { return gate_forwprop (); }
3873 unsigned int execute () { return ssa_forward_propagate_and_combine (); }
3875 }; // class pass_forwprop
3880 make_pass_forwprop (gcc::context
*ctxt
)
3882 return new pass_forwprop (ctxt
);