1 /* Forward propagation of expressions for single use variables.
2 Copyright (C) 2004-2013 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"
31 #include "gimple-iterator.h"
32 #include "gimplify-me.h"
33 #include "gimple-ssa.h"
35 #include "tree-phinodes.h"
36 #include "ssa-iterators.h"
37 #include "stringpool.h"
38 #include "tree-ssanames.h"
41 #include "tree-pass.h"
42 #include "langhooks.h"
47 #include "tree-ssa-propagate.h"
48 #include "tree-ssa-dom.h"
50 /* This pass propagates the RHS of assignment statements into use
51 sites of the LHS of the assignment. It's basically a specialized
52 form of tree combination. It is hoped all of this can disappear
53 when we have a generalized tree combiner.
55 One class of common cases we handle is forward propagating a single use
56 variable into a COND_EXPR.
60 if (x) goto ... else goto ...
62 Will be transformed into:
65 if (a COND b) goto ... else goto ...
67 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
69 Or (assuming c1 and c2 are constants):
73 if (x EQ/NEQ c2) goto ... else goto ...
75 Will be transformed into:
78 if (a EQ/NEQ (c2 - c1)) goto ... else goto ...
80 Similarly for x = a - c1.
86 if (x) goto ... else goto ...
88 Will be transformed into:
91 if (a == 0) goto ... else goto ...
93 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
94 For these cases, we propagate A into all, possibly more than one,
95 COND_EXPRs that use X.
101 if (x) goto ... else goto ...
103 Will be transformed into:
106 if (a != 0) goto ... else goto ...
108 (Assuming a is an integral type and x is a boolean or x is an
109 integral and a is a boolean.)
111 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
112 For these cases, we propagate A into all, possibly more than one,
113 COND_EXPRs that use X.
115 In addition to eliminating the variable and the statement which assigns
116 a value to the variable, we may be able to later thread the jump without
117 adding insane complexity in the dominator optimizer.
119 Also note these transformations can cascade. We handle this by having
120 a worklist of COND_EXPR statements to examine. As we make a change to
121 a statement, we put it back on the worklist to examine on the next
122 iteration of the main loop.
124 A second class of propagation opportunities arises for ADDR_EXPR
135 ptr = (type1*)&type2var;
138 Will get turned into (if type1 and type2 are the same size
139 and neither have volatile on them):
140 res = VIEW_CONVERT_EXPR<type1>(type2var)
145 ptr2 = ptr + <constant>;
149 ptr2 = &x[constant/elementsize];
154 offset = index * element_size;
155 offset_p = (pointer) offset;
156 ptr2 = ptr + offset_p
158 Will get turned into:
166 Provided that decl has known alignment >= 2, will get turned into
170 We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to
171 allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent
174 This will (of course) be extended as other needs arise. */
176 static bool forward_propagate_addr_expr (tree
, tree
, bool);
178 /* Set to true if we delete dead edges during the optimization. */
179 static bool cfg_changed
;
181 static tree
rhs_to_tree (tree type
, gimple stmt
);
183 /* Get the next statement we can propagate NAME's value into skipping
184 trivial copies. Returns the statement that is suitable as a
185 propagation destination or NULL_TREE if there is no such one.
186 This only returns destinations in a single-use chain. FINAL_NAME_P
187 if non-NULL is written to the ssa name that represents the use. */
190 get_prop_dest_stmt (tree name
, tree
*final_name_p
)
196 /* If name has multiple uses, bail out. */
197 if (!single_imm_use (name
, &use
, &use_stmt
))
200 /* If this is not a trivial copy, we found it. */
201 if (!gimple_assign_ssa_name_copy_p (use_stmt
)
202 || gimple_assign_rhs1 (use_stmt
) != name
)
205 /* Continue searching uses of the copy destination. */
206 name
= gimple_assign_lhs (use_stmt
);
210 *final_name_p
= name
;
215 /* Get the statement we can propagate from into NAME skipping
216 trivial copies. Returns the statement which defines the
217 propagation source or NULL_TREE if there is no such one.
218 If SINGLE_USE_ONLY is set considers only sources which have
219 a single use chain up to NAME. If SINGLE_USE_P is non-null,
220 it is set to whether the chain to NAME is a single use chain
221 or not. SINGLE_USE_P is not written to if SINGLE_USE_ONLY is set. */
224 get_prop_source_stmt (tree name
, bool single_use_only
, bool *single_use_p
)
226 bool single_use
= true;
229 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
231 if (!has_single_use (name
))
238 /* If name is defined by a PHI node or is the default def, bail out. */
239 if (!is_gimple_assign (def_stmt
))
242 /* If def_stmt is a simple copy, continue looking. */
243 if (gimple_assign_rhs_code (def_stmt
) == SSA_NAME
)
244 name
= gimple_assign_rhs1 (def_stmt
);
247 if (!single_use_only
&& single_use_p
)
248 *single_use_p
= single_use
;
255 /* Checks if the destination ssa name in DEF_STMT can be used as
256 propagation source. Returns true if so, otherwise false. */
259 can_propagate_from (gimple def_stmt
)
261 gcc_assert (is_gimple_assign (def_stmt
));
263 /* If the rhs has side-effects we cannot propagate from it. */
264 if (gimple_has_volatile_ops (def_stmt
))
267 /* If the rhs is a load we cannot propagate from it. */
268 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt
)) == tcc_reference
269 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt
)) == tcc_declaration
)
272 /* Constants can be always propagated. */
273 if (gimple_assign_single_p (def_stmt
)
274 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt
)))
277 /* We cannot propagate ssa names that occur in abnormal phi nodes. */
278 if (stmt_references_abnormal_ssa_name (def_stmt
))
281 /* If the definition is a conversion of a pointer to a function type,
282 then we can not apply optimizations as some targets require
283 function pointers to be canonicalized and in this case this
284 optimization could eliminate a necessary canonicalization. */
285 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
287 tree rhs
= gimple_assign_rhs1 (def_stmt
);
288 if (POINTER_TYPE_P (TREE_TYPE (rhs
))
289 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs
))) == FUNCTION_TYPE
)
296 /* Remove a chain of dead statements starting at the definition of
297 NAME. The chain is linked via the first operand of the defining statements.
298 If NAME was replaced in its only use then this function can be used
299 to clean up dead stmts. The function handles already released SSA
301 Returns true if cleanup-cfg has to run. */
304 remove_prop_source_from_use (tree name
)
306 gimple_stmt_iterator gsi
;
308 bool cfg_changed
= false;
313 if (SSA_NAME_IN_FREE_LIST (name
)
314 || SSA_NAME_IS_DEFAULT_DEF (name
)
315 || !has_zero_uses (name
))
318 stmt
= SSA_NAME_DEF_STMT (name
);
319 if (gimple_code (stmt
) == GIMPLE_PHI
320 || gimple_has_side_effects (stmt
))
323 bb
= gimple_bb (stmt
);
324 gsi
= gsi_for_stmt (stmt
);
325 unlink_stmt_vdef (stmt
);
326 if (gsi_remove (&gsi
, true))
327 cfg_changed
|= gimple_purge_dead_eh_edges (bb
);
330 name
= is_gimple_assign (stmt
) ? gimple_assign_rhs1 (stmt
) : NULL_TREE
;
331 } while (name
&& TREE_CODE (name
) == SSA_NAME
);
336 /* Return the rhs of a gimple_assign STMT in a form of a single tree,
337 converted to type TYPE.
339 This should disappear, but is needed so we can combine expressions and use
340 the fold() interfaces. Long term, we need to develop folding and combine
341 routines that deal with gimple exclusively . */
344 rhs_to_tree (tree type
, gimple stmt
)
346 location_t loc
= gimple_location (stmt
);
347 enum tree_code code
= gimple_assign_rhs_code (stmt
);
348 if (get_gimple_rhs_class (code
) == GIMPLE_TERNARY_RHS
)
349 return fold_build3_loc (loc
, code
, type
, gimple_assign_rhs1 (stmt
),
350 gimple_assign_rhs2 (stmt
),
351 gimple_assign_rhs3 (stmt
));
352 else if (get_gimple_rhs_class (code
) == GIMPLE_BINARY_RHS
)
353 return fold_build2_loc (loc
, code
, type
, gimple_assign_rhs1 (stmt
),
354 gimple_assign_rhs2 (stmt
));
355 else if (get_gimple_rhs_class (code
) == GIMPLE_UNARY_RHS
)
356 return build1 (code
, type
, gimple_assign_rhs1 (stmt
));
357 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
)
358 return gimple_assign_rhs1 (stmt
);
363 /* Combine OP0 CODE OP1 in the context of a COND_EXPR. Returns
364 the folded result in a form suitable for COND_EXPR_COND or
365 NULL_TREE, if there is no suitable simplified form. If
366 INVARIANT_ONLY is true only gimple_min_invariant results are
367 considered simplified. */
370 combine_cond_expr_cond (gimple stmt
, enum tree_code code
, tree type
,
371 tree op0
, tree op1
, bool invariant_only
)
375 gcc_assert (TREE_CODE_CLASS (code
) == tcc_comparison
);
377 fold_defer_overflow_warnings ();
378 t
= fold_binary_loc (gimple_location (stmt
), code
, type
, op0
, op1
);
381 fold_undefer_overflow_warnings (false, NULL
, 0);
385 /* Require that we got a boolean type out if we put one in. */
386 gcc_assert (TREE_CODE (TREE_TYPE (t
)) == TREE_CODE (type
));
388 /* Canonicalize the combined condition for use in a COND_EXPR. */
389 t
= canonicalize_cond_expr_cond (t
);
391 /* Bail out if we required an invariant but didn't get one. */
392 if (!t
|| (invariant_only
&& !is_gimple_min_invariant (t
)))
394 fold_undefer_overflow_warnings (false, NULL
, 0);
398 fold_undefer_overflow_warnings (!gimple_no_warning_p (stmt
), stmt
, 0);
403 /* Combine the comparison OP0 CODE OP1 at LOC with the defining statements
404 of its operand. Return a new comparison tree or NULL_TREE if there
405 were no simplifying combines. */
408 forward_propagate_into_comparison_1 (gimple stmt
,
409 enum tree_code code
, tree type
,
412 tree tmp
= NULL_TREE
;
413 tree rhs0
= NULL_TREE
, rhs1
= NULL_TREE
;
414 bool single_use0_p
= false, single_use1_p
= false;
416 /* For comparisons use the first operand, that is likely to
417 simplify comparisons against constants. */
418 if (TREE_CODE (op0
) == SSA_NAME
)
420 gimple def_stmt
= get_prop_source_stmt (op0
, false, &single_use0_p
);
421 if (def_stmt
&& can_propagate_from (def_stmt
))
423 rhs0
= rhs_to_tree (TREE_TYPE (op1
), def_stmt
);
424 tmp
= combine_cond_expr_cond (stmt
, code
, type
,
425 rhs0
, op1
, !single_use0_p
);
431 /* If that wasn't successful, try the second operand. */
432 if (TREE_CODE (op1
) == SSA_NAME
)
434 gimple def_stmt
= get_prop_source_stmt (op1
, false, &single_use1_p
);
435 if (def_stmt
&& can_propagate_from (def_stmt
))
437 rhs1
= rhs_to_tree (TREE_TYPE (op0
), def_stmt
);
438 tmp
= combine_cond_expr_cond (stmt
, code
, type
,
439 op0
, rhs1
, !single_use1_p
);
445 /* If that wasn't successful either, try both operands. */
446 if (rhs0
!= NULL_TREE
447 && rhs1
!= NULL_TREE
)
448 tmp
= combine_cond_expr_cond (stmt
, code
, type
,
450 !(single_use0_p
&& single_use1_p
));
455 /* Propagate from the ssa name definition statements of the assignment
456 from a comparison at *GSI into the conditional if that simplifies it.
457 Returns 1 if the stmt was modified and 2 if the CFG needs cleanup,
458 otherwise returns 0. */
461 forward_propagate_into_comparison (gimple_stmt_iterator
*gsi
)
463 gimple stmt
= gsi_stmt (*gsi
);
465 bool cfg_changed
= false;
466 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
467 tree rhs1
= gimple_assign_rhs1 (stmt
);
468 tree rhs2
= gimple_assign_rhs2 (stmt
);
470 /* Combine the comparison with defining statements. */
471 tmp
= forward_propagate_into_comparison_1 (stmt
,
472 gimple_assign_rhs_code (stmt
),
474 if (tmp
&& useless_type_conversion_p (type
, TREE_TYPE (tmp
)))
476 gimple_assign_set_rhs_from_tree (gsi
, tmp
);
478 update_stmt (gsi_stmt (*gsi
));
480 if (TREE_CODE (rhs1
) == SSA_NAME
)
481 cfg_changed
|= remove_prop_source_from_use (rhs1
);
482 if (TREE_CODE (rhs2
) == SSA_NAME
)
483 cfg_changed
|= remove_prop_source_from_use (rhs2
);
484 return cfg_changed
? 2 : 1;
490 /* Propagate from the ssa name definition statements of COND_EXPR
491 in GIMPLE_COND statement STMT into the conditional if that simplifies it.
492 Returns zero if no statement was changed, one if there were
493 changes and two if cfg_cleanup needs to run.
495 This must be kept in sync with forward_propagate_into_cond. */
498 forward_propagate_into_gimple_cond (gimple stmt
)
501 enum tree_code code
= gimple_cond_code (stmt
);
502 bool cfg_changed
= false;
503 tree rhs1
= gimple_cond_lhs (stmt
);
504 tree rhs2
= gimple_cond_rhs (stmt
);
506 /* We can do tree combining on SSA_NAME and comparison expressions. */
507 if (TREE_CODE_CLASS (gimple_cond_code (stmt
)) != tcc_comparison
)
510 tmp
= forward_propagate_into_comparison_1 (stmt
, code
,
515 if (dump_file
&& tmp
)
517 fprintf (dump_file
, " Replaced '");
518 print_gimple_expr (dump_file
, stmt
, 0, 0);
519 fprintf (dump_file
, "' with '");
520 print_generic_expr (dump_file
, tmp
, 0);
521 fprintf (dump_file
, "'\n");
524 gimple_cond_set_condition_from_tree (stmt
, unshare_expr (tmp
));
527 if (TREE_CODE (rhs1
) == SSA_NAME
)
528 cfg_changed
|= remove_prop_source_from_use (rhs1
);
529 if (TREE_CODE (rhs2
) == SSA_NAME
)
530 cfg_changed
|= remove_prop_source_from_use (rhs2
);
531 return (cfg_changed
|| is_gimple_min_invariant (tmp
)) ? 2 : 1;
534 /* Canonicalize _Bool == 0 and _Bool != 1 to _Bool != 0 by swapping edges. */
535 if ((TREE_CODE (TREE_TYPE (rhs1
)) == BOOLEAN_TYPE
536 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
537 && TYPE_PRECISION (TREE_TYPE (rhs1
)) == 1))
539 && integer_zerop (rhs2
))
541 && integer_onep (rhs2
))))
543 basic_block bb
= gimple_bb (stmt
);
544 gimple_cond_set_code (stmt
, NE_EXPR
);
545 gimple_cond_set_rhs (stmt
, build_zero_cst (TREE_TYPE (rhs1
)));
546 EDGE_SUCC (bb
, 0)->flags
^= (EDGE_TRUE_VALUE
|EDGE_FALSE_VALUE
);
547 EDGE_SUCC (bb
, 1)->flags
^= (EDGE_TRUE_VALUE
|EDGE_FALSE_VALUE
);
555 /* Propagate from the ssa name definition statements of COND_EXPR
556 in the rhs of statement STMT into the conditional if that simplifies it.
557 Returns true zero if the stmt was changed. */
560 forward_propagate_into_cond (gimple_stmt_iterator
*gsi_p
)
562 gimple stmt
= gsi_stmt (*gsi_p
);
563 tree tmp
= NULL_TREE
;
564 tree cond
= gimple_assign_rhs1 (stmt
);
565 enum tree_code code
= gimple_assign_rhs_code (stmt
);
568 /* We can do tree combining on SSA_NAME and comparison expressions. */
569 if (COMPARISON_CLASS_P (cond
))
570 tmp
= forward_propagate_into_comparison_1 (stmt
, TREE_CODE (cond
),
572 TREE_OPERAND (cond
, 0),
573 TREE_OPERAND (cond
, 1));
574 else if (TREE_CODE (cond
) == SSA_NAME
)
576 enum tree_code def_code
;
578 gimple def_stmt
= get_prop_source_stmt (name
, true, NULL
);
579 if (!def_stmt
|| !can_propagate_from (def_stmt
))
582 def_code
= gimple_assign_rhs_code (def_stmt
);
583 if (TREE_CODE_CLASS (def_code
) == tcc_comparison
)
584 tmp
= fold_build2_loc (gimple_location (def_stmt
),
587 gimple_assign_rhs1 (def_stmt
),
588 gimple_assign_rhs2 (def_stmt
));
589 else if (code
== COND_EXPR
590 && ((def_code
== BIT_NOT_EXPR
591 && TYPE_PRECISION (TREE_TYPE (cond
)) == 1)
592 || (def_code
== BIT_XOR_EXPR
593 && integer_onep (gimple_assign_rhs2 (def_stmt
)))))
595 tmp
= gimple_assign_rhs1 (def_stmt
);
601 && is_gimple_condexpr (tmp
))
603 if (dump_file
&& tmp
)
605 fprintf (dump_file
, " Replaced '");
606 print_generic_expr (dump_file
, cond
, 0);
607 fprintf (dump_file
, "' with '");
608 print_generic_expr (dump_file
, tmp
, 0);
609 fprintf (dump_file
, "'\n");
612 if ((code
== VEC_COND_EXPR
) ? integer_all_onesp (tmp
)
613 : integer_onep (tmp
))
614 gimple_assign_set_rhs_from_tree (gsi_p
, gimple_assign_rhs2 (stmt
));
615 else if (integer_zerop (tmp
))
616 gimple_assign_set_rhs_from_tree (gsi_p
, gimple_assign_rhs3 (stmt
));
619 gimple_assign_set_rhs1 (stmt
, unshare_expr (tmp
));
622 tree t
= gimple_assign_rhs2 (stmt
);
623 gimple_assign_set_rhs2 (stmt
, gimple_assign_rhs3 (stmt
));
624 gimple_assign_set_rhs3 (stmt
, t
);
627 stmt
= gsi_stmt (*gsi_p
);
636 /* Propagate from the ssa name definition statements of COND_EXPR
637 values in the rhs of statement STMT into the conditional arms
638 if that simplifies it.
639 Returns true if the stmt was changed. */
642 combine_cond_exprs (gimple_stmt_iterator
*gsi_p
)
644 gimple stmt
= gsi_stmt (*gsi_p
);
645 tree cond
, val1
, val2
;
646 bool changed
= false;
648 cond
= gimple_assign_rhs1 (stmt
);
649 val1
= gimple_assign_rhs2 (stmt
);
650 if (TREE_CODE (val1
) == SSA_NAME
)
652 gimple def_stmt
= SSA_NAME_DEF_STMT (val1
);
653 if (is_gimple_assign (def_stmt
)
654 && gimple_assign_rhs_code (def_stmt
) == gimple_assign_rhs_code (stmt
)
655 && operand_equal_p (gimple_assign_rhs1 (def_stmt
), cond
, 0))
657 val1
= unshare_expr (gimple_assign_rhs2 (def_stmt
));
658 gimple_assign_set_rhs2 (stmt
, val1
);
662 val2
= gimple_assign_rhs3 (stmt
);
663 if (TREE_CODE (val2
) == SSA_NAME
)
665 gimple def_stmt
= SSA_NAME_DEF_STMT (val2
);
666 if (is_gimple_assign (def_stmt
)
667 && gimple_assign_rhs_code (def_stmt
) == gimple_assign_rhs_code (stmt
)
668 && operand_equal_p (gimple_assign_rhs1 (def_stmt
), cond
, 0))
670 val2
= unshare_expr (gimple_assign_rhs3 (def_stmt
));
671 gimple_assign_set_rhs3 (stmt
, val2
);
675 if (operand_equal_p (val1
, val2
, 0))
677 gimple_assign_set_rhs_from_tree (gsi_p
, val1
);
678 stmt
= gsi_stmt (*gsi_p
);
688 /* We've just substituted an ADDR_EXPR into stmt. Update all the
689 relevant data structures to match. */
692 tidy_after_forward_propagate_addr (gimple stmt
)
694 /* We may have turned a trapping insn into a non-trapping insn. */
695 if (maybe_clean_or_replace_eh_stmt (stmt
, stmt
)
696 && gimple_purge_dead_eh_edges (gimple_bb (stmt
)))
699 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) == ADDR_EXPR
)
700 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt
));
703 /* NAME is a SSA_NAME representing DEF_RHS which is of the form
704 ADDR_EXPR <whatever>.
706 Try to forward propagate the ADDR_EXPR into the use USE_STMT.
707 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
708 node or for recovery of array indexing from pointer arithmetic.
710 Return true if the propagation was successful (the propagation can
711 be not totally successful, yet things may have been changed). */
714 forward_propagate_addr_expr_1 (tree name
, tree def_rhs
,
715 gimple_stmt_iterator
*use_stmt_gsi
,
718 tree lhs
, rhs
, rhs2
, array_ref
;
719 gimple use_stmt
= gsi_stmt (*use_stmt_gsi
);
720 enum tree_code rhs_code
;
723 gcc_assert (TREE_CODE (def_rhs
) == ADDR_EXPR
);
725 lhs
= gimple_assign_lhs (use_stmt
);
726 rhs_code
= gimple_assign_rhs_code (use_stmt
);
727 rhs
= gimple_assign_rhs1 (use_stmt
);
729 /* Do not perform copy-propagation but recurse through copy chains. */
730 if (TREE_CODE (lhs
) == SSA_NAME
731 && rhs_code
== SSA_NAME
)
732 return forward_propagate_addr_expr (lhs
, def_rhs
, single_use_p
);
734 /* The use statement could be a conversion. Recurse to the uses of the
735 lhs as copyprop does not copy through pointer to integer to pointer
736 conversions and FRE does not catch all cases either.
737 Treat the case of a single-use name and
738 a conversion to def_rhs type separate, though. */
739 if (TREE_CODE (lhs
) == SSA_NAME
740 && CONVERT_EXPR_CODE_P (rhs_code
))
742 /* If there is a point in a conversion chain where the types match
743 so we can remove a conversion re-materialize the address here
746 && useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (def_rhs
)))
748 gimple_assign_set_rhs1 (use_stmt
, unshare_expr (def_rhs
));
749 gimple_assign_set_rhs_code (use_stmt
, TREE_CODE (def_rhs
));
753 /* Else recurse if the conversion preserves the address value. */
754 if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
755 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
756 && (TYPE_PRECISION (TREE_TYPE (lhs
))
757 >= TYPE_PRECISION (TREE_TYPE (def_rhs
))))
758 return forward_propagate_addr_expr (lhs
, def_rhs
, single_use_p
);
763 /* If this isn't a conversion chain from this on we only can propagate
764 into compatible pointer contexts. */
765 if (!types_compatible_p (TREE_TYPE (name
), TREE_TYPE (def_rhs
)))
768 /* Propagate through constant pointer adjustments. */
769 if (TREE_CODE (lhs
) == SSA_NAME
770 && rhs_code
== POINTER_PLUS_EXPR
772 && TREE_CODE (gimple_assign_rhs2 (use_stmt
)) == INTEGER_CST
)
775 /* As we come here with non-invariant addresses in def_rhs we need
776 to make sure we can build a valid constant offsetted address
777 for further propagation. Simply rely on fold building that
778 and check after the fact. */
779 new_def_rhs
= fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (rhs
)),
781 fold_convert (ptr_type_node
,
782 gimple_assign_rhs2 (use_stmt
)));
783 if (TREE_CODE (new_def_rhs
) == MEM_REF
784 && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs
, 0)))
786 new_def_rhs
= build_fold_addr_expr_with_type (new_def_rhs
,
789 /* Recurse. If we could propagate into all uses of lhs do not
790 bother to replace into the current use but just pretend we did. */
791 if (TREE_CODE (new_def_rhs
) == ADDR_EXPR
792 && forward_propagate_addr_expr (lhs
, new_def_rhs
, single_use_p
))
795 if (useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (new_def_rhs
)))
796 gimple_assign_set_rhs_with_ops (use_stmt_gsi
, TREE_CODE (new_def_rhs
),
797 new_def_rhs
, NULL_TREE
);
798 else if (is_gimple_min_invariant (new_def_rhs
))
799 gimple_assign_set_rhs_with_ops (use_stmt_gsi
, NOP_EXPR
,
800 new_def_rhs
, NULL_TREE
);
803 gcc_assert (gsi_stmt (*use_stmt_gsi
) == use_stmt
);
804 update_stmt (use_stmt
);
808 /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
809 ADDR_EXPR will not appear on the LHS. */
810 tree
*lhsp
= gimple_assign_lhs_ptr (use_stmt
);
811 while (handled_component_p (*lhsp
))
812 lhsp
= &TREE_OPERAND (*lhsp
, 0);
815 /* Now see if the LHS node is a MEM_REF using NAME. If so,
816 propagate the ADDR_EXPR into the use of NAME and fold the result. */
817 if (TREE_CODE (lhs
) == MEM_REF
818 && TREE_OPERAND (lhs
, 0) == name
)
821 HOST_WIDE_INT def_rhs_offset
;
822 /* If the address is invariant we can always fold it. */
823 if ((def_rhs_base
= get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs
, 0),
826 double_int off
= mem_ref_offset (lhs
);
828 off
+= double_int::from_shwi (def_rhs_offset
);
829 if (TREE_CODE (def_rhs_base
) == MEM_REF
)
831 off
+= mem_ref_offset (def_rhs_base
);
832 new_ptr
= TREE_OPERAND (def_rhs_base
, 0);
835 new_ptr
= build_fold_addr_expr (def_rhs_base
);
836 TREE_OPERAND (lhs
, 0) = new_ptr
;
837 TREE_OPERAND (lhs
, 1)
838 = double_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs
, 1)), off
);
839 tidy_after_forward_propagate_addr (use_stmt
);
840 /* Continue propagating into the RHS if this was not the only use. */
844 /* If the LHS is a plain dereference and the value type is the same as
845 that of the pointed-to type of the address we can put the
846 dereferenced address on the LHS preserving the original alias-type. */
847 else if (integer_zerop (TREE_OPERAND (lhs
, 1))
848 && ((gimple_assign_lhs (use_stmt
) == lhs
849 && useless_type_conversion_p
850 (TREE_TYPE (TREE_OPERAND (def_rhs
, 0)),
851 TREE_TYPE (gimple_assign_rhs1 (use_stmt
))))
852 || types_compatible_p (TREE_TYPE (lhs
),
853 TREE_TYPE (TREE_OPERAND (def_rhs
, 0))))
854 /* Don't forward anything into clobber stmts if it would result
855 in the lhs no longer being a MEM_REF. */
856 && (!gimple_clobber_p (use_stmt
)
857 || TREE_CODE (TREE_OPERAND (def_rhs
, 0)) == MEM_REF
))
859 tree
*def_rhs_basep
= &TREE_OPERAND (def_rhs
, 0);
860 tree new_offset
, new_base
, saved
, new_lhs
;
861 while (handled_component_p (*def_rhs_basep
))
862 def_rhs_basep
= &TREE_OPERAND (*def_rhs_basep
, 0);
863 saved
= *def_rhs_basep
;
864 if (TREE_CODE (*def_rhs_basep
) == MEM_REF
)
866 new_base
= TREE_OPERAND (*def_rhs_basep
, 0);
867 new_offset
= fold_convert (TREE_TYPE (TREE_OPERAND (lhs
, 1)),
868 TREE_OPERAND (*def_rhs_basep
, 1));
872 new_base
= build_fold_addr_expr (*def_rhs_basep
);
873 new_offset
= TREE_OPERAND (lhs
, 1);
875 *def_rhs_basep
= build2 (MEM_REF
, TREE_TYPE (*def_rhs_basep
),
876 new_base
, new_offset
);
877 TREE_THIS_VOLATILE (*def_rhs_basep
) = TREE_THIS_VOLATILE (lhs
);
878 TREE_SIDE_EFFECTS (*def_rhs_basep
) = TREE_SIDE_EFFECTS (lhs
);
879 TREE_THIS_NOTRAP (*def_rhs_basep
) = TREE_THIS_NOTRAP (lhs
);
880 new_lhs
= unshare_expr (TREE_OPERAND (def_rhs
, 0));
882 TREE_THIS_VOLATILE (new_lhs
) = TREE_THIS_VOLATILE (lhs
);
883 TREE_SIDE_EFFECTS (new_lhs
) = TREE_SIDE_EFFECTS (lhs
);
884 *def_rhs_basep
= saved
;
885 tidy_after_forward_propagate_addr (use_stmt
);
886 /* Continue propagating into the RHS if this was not the
892 /* We can have a struct assignment dereferencing our name twice.
893 Note that we didn't propagate into the lhs to not falsely
894 claim we did when propagating into the rhs. */
898 /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
899 nodes from the RHS. */
900 tree
*rhsp
= gimple_assign_rhs1_ptr (use_stmt
);
901 if (TREE_CODE (*rhsp
) == ADDR_EXPR
)
902 rhsp
= &TREE_OPERAND (*rhsp
, 0);
903 while (handled_component_p (*rhsp
))
904 rhsp
= &TREE_OPERAND (*rhsp
, 0);
907 /* Now see if the RHS node is a MEM_REF using NAME. If so,
908 propagate the ADDR_EXPR into the use of NAME and fold the result. */
909 if (TREE_CODE (rhs
) == MEM_REF
910 && TREE_OPERAND (rhs
, 0) == name
)
913 HOST_WIDE_INT def_rhs_offset
;
914 if ((def_rhs_base
= get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs
, 0),
917 double_int off
= mem_ref_offset (rhs
);
919 off
+= double_int::from_shwi (def_rhs_offset
);
920 if (TREE_CODE (def_rhs_base
) == MEM_REF
)
922 off
+= mem_ref_offset (def_rhs_base
);
923 new_ptr
= TREE_OPERAND (def_rhs_base
, 0);
926 new_ptr
= build_fold_addr_expr (def_rhs_base
);
927 TREE_OPERAND (rhs
, 0) = new_ptr
;
928 TREE_OPERAND (rhs
, 1)
929 = double_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs
, 1)), off
);
930 fold_stmt_inplace (use_stmt_gsi
);
931 tidy_after_forward_propagate_addr (use_stmt
);
934 /* If the RHS is a plain dereference and the value type is the same as
935 that of the pointed-to type of the address we can put the
936 dereferenced address on the RHS preserving the original alias-type. */
937 else if (integer_zerop (TREE_OPERAND (rhs
, 1))
938 && ((gimple_assign_rhs1 (use_stmt
) == rhs
939 && useless_type_conversion_p
940 (TREE_TYPE (gimple_assign_lhs (use_stmt
)),
941 TREE_TYPE (TREE_OPERAND (def_rhs
, 0))))
942 || types_compatible_p (TREE_TYPE (rhs
),
943 TREE_TYPE (TREE_OPERAND (def_rhs
, 0)))))
945 tree
*def_rhs_basep
= &TREE_OPERAND (def_rhs
, 0);
946 tree new_offset
, new_base
, saved
, new_rhs
;
947 while (handled_component_p (*def_rhs_basep
))
948 def_rhs_basep
= &TREE_OPERAND (*def_rhs_basep
, 0);
949 saved
= *def_rhs_basep
;
950 if (TREE_CODE (*def_rhs_basep
) == MEM_REF
)
952 new_base
= TREE_OPERAND (*def_rhs_basep
, 0);
953 new_offset
= fold_convert (TREE_TYPE (TREE_OPERAND (rhs
, 1)),
954 TREE_OPERAND (*def_rhs_basep
, 1));
958 new_base
= build_fold_addr_expr (*def_rhs_basep
);
959 new_offset
= TREE_OPERAND (rhs
, 1);
961 *def_rhs_basep
= build2 (MEM_REF
, TREE_TYPE (*def_rhs_basep
),
962 new_base
, new_offset
);
963 TREE_THIS_VOLATILE (*def_rhs_basep
) = TREE_THIS_VOLATILE (rhs
);
964 TREE_SIDE_EFFECTS (*def_rhs_basep
) = TREE_SIDE_EFFECTS (rhs
);
965 TREE_THIS_NOTRAP (*def_rhs_basep
) = TREE_THIS_NOTRAP (rhs
);
966 new_rhs
= unshare_expr (TREE_OPERAND (def_rhs
, 0));
968 TREE_THIS_VOLATILE (new_rhs
) = TREE_THIS_VOLATILE (rhs
);
969 TREE_SIDE_EFFECTS (new_rhs
) = TREE_SIDE_EFFECTS (rhs
);
970 *def_rhs_basep
= saved
;
971 fold_stmt_inplace (use_stmt_gsi
);
972 tidy_after_forward_propagate_addr (use_stmt
);
977 /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there
979 if (gimple_assign_rhs_code (use_stmt
) != POINTER_PLUS_EXPR
980 || gimple_assign_rhs1 (use_stmt
) != name
)
983 /* The remaining cases are all for turning pointer arithmetic into
984 array indexing. They only apply when we have the address of
985 element zero in an array. If that is not the case then there
987 array_ref
= TREE_OPERAND (def_rhs
, 0);
988 if ((TREE_CODE (array_ref
) != ARRAY_REF
989 || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref
, 0))) != ARRAY_TYPE
990 || TREE_CODE (TREE_OPERAND (array_ref
, 1)) != INTEGER_CST
)
991 && TREE_CODE (TREE_TYPE (array_ref
)) != ARRAY_TYPE
)
994 rhs2
= gimple_assign_rhs2 (use_stmt
);
995 /* Optimize &x[C1] p+ C2 to &x p+ C3 with C3 = C1 * element_size + C2. */
996 if (TREE_CODE (rhs2
) == INTEGER_CST
)
998 tree new_rhs
= build1_loc (gimple_location (use_stmt
),
999 ADDR_EXPR
, TREE_TYPE (def_rhs
),
1000 fold_build2 (MEM_REF
,
1001 TREE_TYPE (TREE_TYPE (def_rhs
)),
1002 unshare_expr (def_rhs
),
1003 fold_convert (ptr_type_node
,
1005 gimple_assign_set_rhs_from_tree (use_stmt_gsi
, new_rhs
);
1006 use_stmt
= gsi_stmt (*use_stmt_gsi
);
1007 update_stmt (use_stmt
);
1008 tidy_after_forward_propagate_addr (use_stmt
);
1015 /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
1017 Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
1018 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
1019 node or for recovery of array indexing from pointer arithmetic.
1021 PARENT_SINGLE_USE_P tells if, when in a recursive invocation, NAME was
1022 the single use in the previous invocation. Pass true when calling
1025 Returns true, if all uses have been propagated into. */
1028 forward_propagate_addr_expr (tree name
, tree rhs
, bool parent_single_use_p
)
1030 imm_use_iterator iter
;
1033 bool single_use_p
= parent_single_use_p
&& has_single_use (name
);
1035 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, name
)
1040 /* If the use is not in a simple assignment statement, then
1041 there is nothing we can do. */
1042 if (!is_gimple_assign (use_stmt
))
1044 if (!is_gimple_debug (use_stmt
))
1049 gimple_stmt_iterator gsi
= gsi_for_stmt (use_stmt
);
1050 result
= forward_propagate_addr_expr_1 (name
, rhs
, &gsi
,
1052 /* If the use has moved to a different statement adjust
1053 the update machinery for the old statement too. */
1054 if (use_stmt
!= gsi_stmt (gsi
))
1056 update_stmt (use_stmt
);
1057 use_stmt
= gsi_stmt (gsi
);
1059 update_stmt (use_stmt
);
1062 /* Remove intermediate now unused copy and conversion chains. */
1063 use_rhs
= gimple_assign_rhs1 (use_stmt
);
1065 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
1066 && TREE_CODE (use_rhs
) == SSA_NAME
1067 && has_zero_uses (gimple_assign_lhs (use_stmt
)))
1069 gimple_stmt_iterator gsi
= gsi_for_stmt (use_stmt
);
1070 release_defs (use_stmt
);
1071 gsi_remove (&gsi
, true);
1075 return all
&& has_zero_uses (name
);
1079 /* Forward propagate the comparison defined in *DEFGSI like
1080 cond_1 = x CMP y to uses of the form
1084 Returns true if stmt is now unused. Advance DEFGSI to the next
1088 forward_propagate_comparison (gimple_stmt_iterator
*defgsi
)
1090 gimple stmt
= gsi_stmt (*defgsi
);
1091 tree name
= gimple_assign_lhs (stmt
);
1093 tree tmp
= NULL_TREE
;
1094 gimple_stmt_iterator gsi
;
1095 enum tree_code code
;
1098 /* Don't propagate ssa names that occur in abnormal phis. */
1099 if ((TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
1100 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt
)))
1101 || (TREE_CODE (gimple_assign_rhs2 (stmt
)) == SSA_NAME
1102 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs2 (stmt
))))
1105 /* Do not un-cse comparisons. But propagate through copies. */
1106 use_stmt
= get_prop_dest_stmt (name
, &name
);
1108 || !is_gimple_assign (use_stmt
))
1111 code
= gimple_assign_rhs_code (use_stmt
);
1112 lhs
= gimple_assign_lhs (use_stmt
);
1113 if (!INTEGRAL_TYPE_P (TREE_TYPE (lhs
)))
1116 /* We can propagate the condition into a statement that
1117 computes the logical negation of the comparison result. */
1118 if ((code
== BIT_NOT_EXPR
1119 && TYPE_PRECISION (TREE_TYPE (lhs
)) == 1)
1120 || (code
== BIT_XOR_EXPR
1121 && integer_onep (gimple_assign_rhs2 (use_stmt
))))
1123 tree type
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
1124 bool nans
= HONOR_NANS (TYPE_MODE (type
));
1125 enum tree_code inv_code
;
1126 inv_code
= invert_tree_comparison (gimple_assign_rhs_code (stmt
), nans
);
1127 if (inv_code
== ERROR_MARK
)
1130 tmp
= build2 (inv_code
, TREE_TYPE (lhs
), gimple_assign_rhs1 (stmt
),
1131 gimple_assign_rhs2 (stmt
));
1136 gsi
= gsi_for_stmt (use_stmt
);
1137 gimple_assign_set_rhs_from_tree (&gsi
, unshare_expr (tmp
));
1138 use_stmt
= gsi_stmt (gsi
);
1139 update_stmt (use_stmt
);
1141 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1143 fprintf (dump_file
, " Replaced '");
1144 print_gimple_expr (dump_file
, stmt
, 0, dump_flags
);
1145 fprintf (dump_file
, "' with '");
1146 print_gimple_expr (dump_file
, use_stmt
, 0, dump_flags
);
1147 fprintf (dump_file
, "'\n");
1150 /* When we remove stmt now the iterator defgsi goes off it's current
1151 sequence, hence advance it now. */
1154 /* Remove defining statements. */
1155 return remove_prop_source_from_use (name
);
1163 /* GSI_P points to a statement which performs a narrowing integral
1166 Look for cases like:
1176 If T is narrower than X's type and C merely masks off bits outside
1177 of (T) and nothing else.
1179 Normally we'd let DCE remove the dead statement. But no DCE runs
1180 after the last forwprop/combine pass, so we remove the obviously
1181 dead code ourselves.
1183 Return TRUE if a change was made, FALSE otherwise. */
1186 simplify_conversion_from_bitmask (gimple_stmt_iterator
*gsi_p
)
1188 gimple stmt
= gsi_stmt (*gsi_p
);
1189 gimple rhs_def_stmt
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
1191 /* See if the input for the conversion was set via a BIT_AND_EXPR and
1192 the only use of the BIT_AND_EXPR result is the conversion. */
1193 if (is_gimple_assign (rhs_def_stmt
)
1194 && gimple_assign_rhs_code (rhs_def_stmt
) == BIT_AND_EXPR
1195 && has_single_use (gimple_assign_lhs (rhs_def_stmt
)))
1197 tree rhs_def_operand1
= gimple_assign_rhs1 (rhs_def_stmt
);
1198 tree rhs_def_operand2
= gimple_assign_rhs2 (rhs_def_stmt
);
1199 tree lhs_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1201 /* Now verify suitability of the BIT_AND_EXPR's operands.
1202 The first must be an SSA_NAME that we can propagate and the
1203 second must be an integer constant that masks out all the
1204 bits outside the final result's type, but nothing else. */
1205 if (TREE_CODE (rhs_def_operand1
) == SSA_NAME
1206 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand1
)
1207 && TREE_CODE (rhs_def_operand2
) == INTEGER_CST
1208 && operand_equal_p (rhs_def_operand2
,
1209 build_low_bits_mask (TREE_TYPE (rhs_def_operand2
),
1210 TYPE_PRECISION (lhs_type
)),
1213 /* This is an optimizable case. Replace the source operand
1214 in the conversion with the first source operand of the
1216 gimple_assign_set_rhs1 (stmt
, rhs_def_operand1
);
1217 stmt
= gsi_stmt (*gsi_p
);
1220 /* There is no DCE after the last forwprop pass. It's
1221 easy to clean up the first order effects here. */
1222 gimple_stmt_iterator si
;
1223 si
= gsi_for_stmt (rhs_def_stmt
);
1224 gsi_remove (&si
, true);
1225 release_defs (rhs_def_stmt
);
1234 /* If we have lhs = ~x (STMT), look and see if earlier we had x = ~y.
1235 If so, we can change STMT into lhs = y which can later be copy
1236 propagated. Similarly for negation.
1238 This could trivially be formulated as a forward propagation
1239 to immediate uses. However, we already had an implementation
1240 from DOM which used backward propagation via the use-def links.
1242 It turns out that backward propagation is actually faster as
1243 there's less work to do for each NOT/NEG expression we find.
1244 Backwards propagation needs to look at the statement in a single
1245 backlink. Forward propagation needs to look at potentially more
1246 than one forward link.
1248 Returns true when the statement was changed. */
1251 simplify_not_neg_expr (gimple_stmt_iterator
*gsi_p
)
1253 gimple stmt
= gsi_stmt (*gsi_p
);
1254 tree rhs
= gimple_assign_rhs1 (stmt
);
1255 gimple rhs_def_stmt
= SSA_NAME_DEF_STMT (rhs
);
1257 /* See if the RHS_DEF_STMT has the same form as our statement. */
1258 if (is_gimple_assign (rhs_def_stmt
)
1259 && gimple_assign_rhs_code (rhs_def_stmt
) == gimple_assign_rhs_code (stmt
))
1261 tree rhs_def_operand
= gimple_assign_rhs1 (rhs_def_stmt
);
1263 /* Verify that RHS_DEF_OPERAND is a suitable SSA_NAME. */
1264 if (TREE_CODE (rhs_def_operand
) == SSA_NAME
1265 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand
))
1267 gimple_assign_set_rhs_from_tree (gsi_p
, rhs_def_operand
);
1268 stmt
= gsi_stmt (*gsi_p
);
1277 /* Helper function for simplify_gimple_switch. Remove case labels that
1278 have values outside the range of the new type. */
1281 simplify_gimple_switch_label_vec (gimple stmt
, tree index_type
)
1283 unsigned int branch_num
= gimple_switch_num_labels (stmt
);
1284 auto_vec
<tree
> labels (branch_num
);
1285 unsigned int i
, len
;
1287 /* Collect the existing case labels in a VEC, and preprocess it as if
1288 we are gimplifying a GENERIC SWITCH_EXPR. */
1289 for (i
= 1; i
< branch_num
; i
++)
1290 labels
.quick_push (gimple_switch_label (stmt
, i
));
1291 preprocess_case_label_vec_for_gimple (labels
, index_type
, NULL
);
1293 /* If any labels were removed, replace the existing case labels
1294 in the GIMPLE_SWITCH statement with the correct ones.
1295 Note that the type updates were done in-place on the case labels,
1296 so we only have to replace the case labels in the GIMPLE_SWITCH
1297 if the number of labels changed. */
1298 len
= labels
.length ();
1299 if (len
< branch_num
- 1)
1301 bitmap target_blocks
;
1305 /* Corner case: *all* case labels have been removed as being
1306 out-of-range for INDEX_TYPE. Push one label and let the
1307 CFG cleanups deal with this further. */
1312 label
= CASE_LABEL (gimple_switch_default_label (stmt
));
1313 elt
= build_case_label (build_int_cst (index_type
, 0), NULL
, label
);
1314 labels
.quick_push (elt
);
1318 for (i
= 0; i
< labels
.length (); i
++)
1319 gimple_switch_set_label (stmt
, i
+ 1, labels
[i
]);
1320 for (i
++ ; i
< branch_num
; i
++)
1321 gimple_switch_set_label (stmt
, i
, NULL_TREE
);
1322 gimple_switch_set_num_labels (stmt
, len
+ 1);
1324 /* Cleanup any edges that are now dead. */
1325 target_blocks
= BITMAP_ALLOC (NULL
);
1326 for (i
= 0; i
< gimple_switch_num_labels (stmt
); i
++)
1328 tree elt
= gimple_switch_label (stmt
, i
);
1329 basic_block target
= label_to_block (CASE_LABEL (elt
));
1330 bitmap_set_bit (target_blocks
, target
->index
);
1332 for (ei
= ei_start (gimple_bb (stmt
)->succs
); (e
= ei_safe_edge (ei
)); )
1334 if (! bitmap_bit_p (target_blocks
, e
->dest
->index
))
1338 free_dominance_info (CDI_DOMINATORS
);
1343 BITMAP_FREE (target_blocks
);
1347 /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
1348 the condition which we may be able to optimize better. */
1351 simplify_gimple_switch (gimple stmt
)
1353 tree cond
= gimple_switch_index (stmt
);
1357 /* The optimization that we really care about is removing unnecessary
1358 casts. That will let us do much better in propagating the inferred
1359 constant at the switch target. */
1360 if (TREE_CODE (cond
) == SSA_NAME
)
1362 def_stmt
= SSA_NAME_DEF_STMT (cond
);
1363 if (is_gimple_assign (def_stmt
))
1365 if (gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
)
1370 def
= gimple_assign_rhs1 (def_stmt
);
1372 to
= TREE_TYPE (cond
);
1373 ti
= TREE_TYPE (def
);
1375 /* If we have an extension that preserves value, then we
1376 can copy the source value into the switch. */
1378 need_precision
= TYPE_PRECISION (ti
);
1380 if (! INTEGRAL_TYPE_P (ti
))
1382 else if (TYPE_UNSIGNED (to
) && !TYPE_UNSIGNED (ti
))
1384 else if (!TYPE_UNSIGNED (to
) && TYPE_UNSIGNED (ti
))
1385 need_precision
+= 1;
1386 if (TYPE_PRECISION (to
) < need_precision
)
1391 gimple_switch_set_index (stmt
, def
);
1392 simplify_gimple_switch_label_vec (stmt
, ti
);
1403 /* For pointers p2 and p1 return p2 - p1 if the
1404 difference is known and constant, otherwise return NULL. */
1407 constant_pointer_difference (tree p1
, tree p2
)
1410 #define CPD_ITERATIONS 5
1411 tree exps
[2][CPD_ITERATIONS
];
1412 tree offs
[2][CPD_ITERATIONS
];
1415 for (i
= 0; i
< 2; i
++)
1417 tree p
= i
? p1
: p2
;
1418 tree off
= size_zero_node
;
1420 enum tree_code code
;
1422 /* For each of p1 and p2 we need to iterate at least
1423 twice, to handle ADDR_EXPR directly in p1/p2,
1424 SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc.
1425 on definition's stmt RHS. Iterate a few extra times. */
1429 if (!POINTER_TYPE_P (TREE_TYPE (p
)))
1431 if (TREE_CODE (p
) == ADDR_EXPR
)
1433 tree q
= TREE_OPERAND (p
, 0);
1434 HOST_WIDE_INT offset
;
1435 tree base
= get_addr_base_and_unit_offset (q
, &offset
);
1440 off
= size_binop (PLUS_EXPR
, off
, size_int (offset
));
1442 if (TREE_CODE (q
) == MEM_REF
1443 && TREE_CODE (TREE_OPERAND (q
, 0)) == SSA_NAME
)
1445 p
= TREE_OPERAND (q
, 0);
1446 off
= size_binop (PLUS_EXPR
, off
,
1447 double_int_to_tree (sizetype
,
1448 mem_ref_offset (q
)));
1457 if (TREE_CODE (p
) != SSA_NAME
)
1461 if (j
== CPD_ITERATIONS
)
1463 stmt
= SSA_NAME_DEF_STMT (p
);
1464 if (!is_gimple_assign (stmt
) || gimple_assign_lhs (stmt
) != p
)
1466 code
= gimple_assign_rhs_code (stmt
);
1467 if (code
== POINTER_PLUS_EXPR
)
1469 if (TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
1471 off
= size_binop (PLUS_EXPR
, off
, gimple_assign_rhs2 (stmt
));
1472 p
= gimple_assign_rhs1 (stmt
);
1474 else if (code
== ADDR_EXPR
|| code
== NOP_EXPR
)
1475 p
= gimple_assign_rhs1 (stmt
);
1483 for (i
= 0; i
< cnt
[0]; i
++)
1484 for (j
= 0; j
< cnt
[1]; j
++)
1485 if (exps
[0][i
] == exps
[1][j
])
1486 return size_binop (MINUS_EXPR
, offs
[0][i
], offs
[1][j
]);
1491 /* *GSI_P is a GIMPLE_CALL to a builtin function.
1493 memcpy (p, "abcd", 4);
1494 memset (p + 4, ' ', 3);
1496 memcpy (p, "abcd ", 7);
1497 call if the latter can be stored by pieces during expansion. */
1500 simplify_builtin_call (gimple_stmt_iterator
*gsi_p
, tree callee2
)
1502 gimple stmt1
, stmt2
= gsi_stmt (*gsi_p
);
1503 tree vuse
= gimple_vuse (stmt2
);
1506 stmt1
= SSA_NAME_DEF_STMT (vuse
);
1508 switch (DECL_FUNCTION_CODE (callee2
))
1510 case BUILT_IN_MEMSET
:
1511 if (gimple_call_num_args (stmt2
) != 3
1512 || gimple_call_lhs (stmt2
)
1514 || BITS_PER_UNIT
!= 8)
1519 tree ptr1
, src1
, str1
, off1
, len1
, lhs1
;
1520 tree ptr2
= gimple_call_arg (stmt2
, 0);
1521 tree val2
= gimple_call_arg (stmt2
, 1);
1522 tree len2
= gimple_call_arg (stmt2
, 2);
1523 tree diff
, vdef
, new_str_cst
;
1525 unsigned int ptr1_align
;
1526 unsigned HOST_WIDE_INT src_len
;
1528 use_operand_p use_p
;
1530 if (!tree_fits_shwi_p (val2
)
1531 || !tree_fits_uhwi_p (len2
))
1533 if (is_gimple_call (stmt1
))
1535 /* If first stmt is a call, it needs to be memcpy
1536 or mempcpy, with string literal as second argument and
1538 callee1
= gimple_call_fndecl (stmt1
);
1539 if (callee1
== NULL_TREE
1540 || DECL_BUILT_IN_CLASS (callee1
) != BUILT_IN_NORMAL
1541 || gimple_call_num_args (stmt1
) != 3)
1543 if (DECL_FUNCTION_CODE (callee1
) != BUILT_IN_MEMCPY
1544 && DECL_FUNCTION_CODE (callee1
) != BUILT_IN_MEMPCPY
)
1546 ptr1
= gimple_call_arg (stmt1
, 0);
1547 src1
= gimple_call_arg (stmt1
, 1);
1548 len1
= gimple_call_arg (stmt1
, 2);
1549 lhs1
= gimple_call_lhs (stmt1
);
1550 if (!tree_fits_uhwi_p (len1
))
1552 str1
= string_constant (src1
, &off1
);
1553 if (str1
== NULL_TREE
)
1555 if (!tree_fits_uhwi_p (off1
)
1556 || compare_tree_int (off1
, TREE_STRING_LENGTH (str1
) - 1) > 0
1557 || compare_tree_int (len1
, TREE_STRING_LENGTH (str1
)
1558 - tree_to_uhwi (off1
)) > 0
1559 || TREE_CODE (TREE_TYPE (str1
)) != ARRAY_TYPE
1560 || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1
)))
1561 != TYPE_MODE (char_type_node
))
1564 else if (gimple_assign_single_p (stmt1
))
1566 /* Otherwise look for length 1 memcpy optimized into
1568 ptr1
= gimple_assign_lhs (stmt1
);
1569 src1
= gimple_assign_rhs1 (stmt1
);
1570 if (TREE_CODE (ptr1
) != MEM_REF
1571 || TYPE_MODE (TREE_TYPE (ptr1
)) != TYPE_MODE (char_type_node
)
1572 || !tree_fits_shwi_p (src1
))
1574 ptr1
= build_fold_addr_expr (ptr1
);
1575 callee1
= NULL_TREE
;
1576 len1
= size_one_node
;
1578 off1
= size_zero_node
;
1584 diff
= constant_pointer_difference (ptr1
, ptr2
);
1585 if (diff
== NULL
&& lhs1
!= NULL
)
1587 diff
= constant_pointer_difference (lhs1
, ptr2
);
1588 if (DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
1590 diff
= size_binop (PLUS_EXPR
, diff
,
1591 fold_convert (sizetype
, len1
));
1593 /* If the difference between the second and first destination pointer
1594 is not constant, or is bigger than memcpy length, bail out. */
1596 || !tree_fits_uhwi_p (diff
)
1597 || tree_int_cst_lt (len1
, diff
))
1600 /* Use maximum of difference plus memset length and memcpy length
1601 as the new memcpy length, if it is too big, bail out. */
1602 src_len
= tree_to_uhwi (diff
);
1603 src_len
+= tree_to_uhwi (len2
);
1604 if (src_len
< tree_to_uhwi (len1
))
1605 src_len
= tree_to_uhwi (len1
);
1609 /* If mempcpy value is used elsewhere, bail out, as mempcpy
1610 with bigger length will return different result. */
1611 if (lhs1
!= NULL_TREE
1612 && DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
1613 && (TREE_CODE (lhs1
) != SSA_NAME
1614 || !single_imm_use (lhs1
, &use_p
, &use_stmt
)
1615 || use_stmt
!= stmt2
))
1618 /* If anything reads memory in between memcpy and memset
1619 call, the modified memcpy call might change it. */
1620 vdef
= gimple_vdef (stmt1
);
1622 && (!single_imm_use (vdef
, &use_p
, &use_stmt
)
1623 || use_stmt
!= stmt2
))
1626 ptr1_align
= get_pointer_alignment (ptr1
);
1627 /* Construct the new source string literal. */
1628 src_buf
= XALLOCAVEC (char, src_len
+ 1);
1631 TREE_STRING_POINTER (str1
) + tree_to_uhwi (off1
),
1632 tree_to_uhwi (len1
));
1634 src_buf
[0] = tree_to_shwi (src1
);
1635 memset (src_buf
+ tree_to_uhwi (diff
),
1636 tree_to_shwi (val2
), tree_to_uhwi (len2
));
1637 src_buf
[src_len
] = '\0';
1638 /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
1639 handle embedded '\0's. */
1640 if (strlen (src_buf
) != src_len
)
1642 rtl_profile_for_bb (gimple_bb (stmt2
));
1643 /* If the new memcpy wouldn't be emitted by storing the literal
1644 by pieces, this optimization might enlarge .rodata too much,
1645 as commonly used string literals couldn't be shared any
1647 if (!can_store_by_pieces (src_len
,
1648 builtin_strncpy_read_str
,
1649 src_buf
, ptr1_align
, false))
1652 new_str_cst
= build_string_literal (src_len
, src_buf
);
1655 /* If STMT1 is a mem{,p}cpy call, adjust it and remove
1657 if (lhs1
&& DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
)
1658 gimple_call_set_lhs (stmt1
, NULL_TREE
);
1659 gimple_call_set_arg (stmt1
, 1, new_str_cst
);
1660 gimple_call_set_arg (stmt1
, 2,
1661 build_int_cst (TREE_TYPE (len1
), src_len
));
1662 update_stmt (stmt1
);
1663 unlink_stmt_vdef (stmt2
);
1664 gsi_remove (gsi_p
, true);
1665 release_defs (stmt2
);
1666 if (lhs1
&& DECL_FUNCTION_CODE (callee1
) == BUILT_IN_MEMPCPY
)
1667 release_ssa_name (lhs1
);
1672 /* Otherwise, if STMT1 is length 1 memcpy optimized into
1673 assignment, remove STMT1 and change memset call into
1675 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt1
);
1677 if (!is_gimple_val (ptr1
))
1678 ptr1
= force_gimple_operand_gsi (gsi_p
, ptr1
, true, NULL_TREE
,
1679 true, GSI_SAME_STMT
);
1680 gimple_call_set_fndecl (stmt2
,
1681 builtin_decl_explicit (BUILT_IN_MEMCPY
));
1682 gimple_call_set_arg (stmt2
, 0, ptr1
);
1683 gimple_call_set_arg (stmt2
, 1, new_str_cst
);
1684 gimple_call_set_arg (stmt2
, 2,
1685 build_int_cst (TREE_TYPE (len2
), src_len
));
1686 unlink_stmt_vdef (stmt1
);
1687 gsi_remove (&gsi
, true);
1688 release_defs (stmt1
);
1689 update_stmt (stmt2
);
1700 /* Checks if expression has type of one-bit precision, or is a known
1701 truth-valued expression. */
1703 truth_valued_ssa_name (tree name
)
1706 tree type
= TREE_TYPE (name
);
1708 if (!INTEGRAL_TYPE_P (type
))
1710 /* Don't check here for BOOLEAN_TYPE as the precision isn't
1711 necessarily one and so ~X is not equal to !X. */
1712 if (TYPE_PRECISION (type
) == 1)
1714 def
= SSA_NAME_DEF_STMT (name
);
1715 if (is_gimple_assign (def
))
1716 return truth_value_p (gimple_assign_rhs_code (def
));
1720 /* Helper routine for simplify_bitwise_binary_1 function.
1721 Return for the SSA name NAME the expression X if it mets condition
1722 NAME = !X. Otherwise return NULL_TREE.
1723 Detected patterns for NAME = !X are:
1724 !X and X == 0 for X with integral type.
1725 X ^ 1, X != 1,or ~X for X with integral type with precision of one. */
1727 lookup_logical_inverted_value (tree name
)
1730 enum tree_code code
;
1733 /* If name has none-intergal type, or isn't a SSA_NAME, then
1735 if (TREE_CODE (name
) != SSA_NAME
1736 || !INTEGRAL_TYPE_P (TREE_TYPE (name
)))
1738 def
= SSA_NAME_DEF_STMT (name
);
1739 if (!is_gimple_assign (def
))
1742 code
= gimple_assign_rhs_code (def
);
1743 op1
= gimple_assign_rhs1 (def
);
1746 /* Get for EQ_EXPR or BIT_XOR_EXPR operation the second operand.
1747 If CODE isn't an EQ_EXPR, BIT_XOR_EXPR, or BIT_NOT_EXPR, then return. */
1748 if (code
== EQ_EXPR
|| code
== NE_EXPR
1749 || code
== BIT_XOR_EXPR
)
1750 op2
= gimple_assign_rhs2 (def
);
1755 if (truth_valued_ssa_name (name
))
1759 /* Check if we have X == 0 and X has an integral type. */
1760 if (!INTEGRAL_TYPE_P (TREE_TYPE (op1
)))
1762 if (integer_zerop (op2
))
1766 /* Check if we have X != 1 and X is a truth-valued. */
1767 if (!INTEGRAL_TYPE_P (TREE_TYPE (op1
)))
1769 if (integer_onep (op2
) && truth_valued_ssa_name (op1
))
1773 /* Check if we have X ^ 1 and X is truth valued. */
1774 if (integer_onep (op2
) && truth_valued_ssa_name (op1
))
1784 /* Optimize ARG1 CODE ARG2 to a constant for bitwise binary
1785 operations CODE, if one operand has the logically inverted
1786 value of the other. */
1788 simplify_bitwise_binary_1 (enum tree_code code
, tree type
,
1789 tree arg1
, tree arg2
)
1793 /* If CODE isn't a bitwise binary operation, return NULL_TREE. */
1794 if (code
!= BIT_AND_EXPR
&& code
!= BIT_IOR_EXPR
1795 && code
!= BIT_XOR_EXPR
)
1798 /* First check if operands ARG1 and ARG2 are equal. If so
1799 return NULL_TREE as this optimization is handled fold_stmt. */
1802 /* See if we have in arguments logical-not patterns. */
1803 if (((anot
= lookup_logical_inverted_value (arg1
)) == NULL_TREE
1805 && ((anot
= lookup_logical_inverted_value (arg2
)) == NULL_TREE
1810 if (code
== BIT_AND_EXPR
)
1811 return fold_convert (type
, integer_zero_node
);
1812 /* X | !X -> 1 and X ^ !X -> 1, if X is truth-valued. */
1813 if (truth_valued_ssa_name (anot
))
1814 return fold_convert (type
, integer_one_node
);
1816 /* ??? Otherwise result is (X != 0 ? X : 1). not handled. */
1820 /* Given a ssa_name in NAME see if it was defined by an assignment and
1821 set CODE to be the code and ARG1 to the first operand on the rhs and ARG2
1822 to the second operand on the rhs. */
1825 defcodefor_name (tree name
, enum tree_code
*code
, tree
*arg1
, tree
*arg2
)
1828 enum tree_code code1
;
1832 enum gimple_rhs_class grhs_class
;
1834 code1
= TREE_CODE (name
);
1837 grhs_class
= get_gimple_rhs_class (code1
);
1839 if (code1
== SSA_NAME
)
1841 def
= SSA_NAME_DEF_STMT (name
);
1843 if (def
&& is_gimple_assign (def
)
1844 && can_propagate_from (def
))
1846 code1
= gimple_assign_rhs_code (def
);
1847 arg11
= gimple_assign_rhs1 (def
);
1848 arg21
= gimple_assign_rhs2 (def
);
1849 arg31
= gimple_assign_rhs2 (def
);
1852 else if (grhs_class
== GIMPLE_TERNARY_RHS
1853 || GIMPLE_BINARY_RHS
1855 || GIMPLE_SINGLE_RHS
)
1856 extract_ops_from_tree_1 (name
, &code1
, &arg11
, &arg21
, &arg31
);
1862 /* Ignore arg3 currently. */
1865 /* Return true if a conversion of an operand from type FROM to type TO
1866 should be applied after performing the operation instead. */
1869 hoist_conversion_for_bitop_p (tree to
, tree from
)
1871 /* That's a good idea if the conversion widens the operand, thus
1872 after hoisting the conversion the operation will be narrower. */
1873 if (TYPE_PRECISION (from
) < TYPE_PRECISION (to
))
1876 /* It's also a good idea if the conversion is to a non-integer mode. */
1877 if (GET_MODE_CLASS (TYPE_MODE (to
)) != MODE_INT
)
1880 /* Or if the precision of TO is not the same as the precision
1882 if (TYPE_PRECISION (to
) != GET_MODE_PRECISION (TYPE_MODE (to
)))
1888 /* GSI points to a statement of the form
1890 result = OP0 CODE OP1
1892 Where OP0 and OP1 are single bit SSA_NAMEs and CODE is either
1893 BIT_AND_EXPR or BIT_IOR_EXPR.
1895 If OP0 is fed by a bitwise negation of another single bit SSA_NAME,
1896 then we can simplify the two statements into a single LT_EXPR or LE_EXPR
1897 when code is BIT_AND_EXPR and BIT_IOR_EXPR respectively.
1899 If a simplification is made, return TRUE, else return FALSE. */
1901 simplify_bitwise_binary_boolean (gimple_stmt_iterator
*gsi
,
1902 enum tree_code code
,
1905 gimple op0_def_stmt
= SSA_NAME_DEF_STMT (op0
);
1907 if (!is_gimple_assign (op0_def_stmt
)
1908 || (gimple_assign_rhs_code (op0_def_stmt
) != BIT_NOT_EXPR
))
1911 tree x
= gimple_assign_rhs1 (op0_def_stmt
);
1912 if (TREE_CODE (x
) == SSA_NAME
1913 && INTEGRAL_TYPE_P (TREE_TYPE (x
))
1914 && TYPE_PRECISION (TREE_TYPE (x
)) == 1
1915 && TYPE_UNSIGNED (TREE_TYPE (x
)) == TYPE_UNSIGNED (TREE_TYPE (op1
)))
1917 enum tree_code newcode
;
1919 gimple stmt
= gsi_stmt (*gsi
);
1920 gimple_assign_set_rhs1 (stmt
, x
);
1921 gimple_assign_set_rhs2 (stmt
, op1
);
1922 if (code
== BIT_AND_EXPR
)
1923 newcode
= TYPE_UNSIGNED (TREE_TYPE (x
)) ? LT_EXPR
: GT_EXPR
;
1925 newcode
= TYPE_UNSIGNED (TREE_TYPE (x
)) ? LE_EXPR
: GE_EXPR
;
1926 gimple_assign_set_rhs_code (stmt
, newcode
);
1934 /* Simplify bitwise binary operations.
1935 Return true if a transformation applied, otherwise return false. */
1938 simplify_bitwise_binary (gimple_stmt_iterator
*gsi
)
1940 gimple stmt
= gsi_stmt (*gsi
);
1941 tree arg1
= gimple_assign_rhs1 (stmt
);
1942 tree arg2
= gimple_assign_rhs2 (stmt
);
1943 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1945 tree def1_arg1
, def1_arg2
, def2_arg1
, def2_arg2
;
1946 enum tree_code def1_code
, def2_code
;
1948 defcodefor_name (arg1
, &def1_code
, &def1_arg1
, &def1_arg2
);
1949 defcodefor_name (arg2
, &def2_code
, &def2_arg1
, &def2_arg2
);
1951 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1953 if (TREE_CODE (arg2
) == INTEGER_CST
1954 && CONVERT_EXPR_CODE_P (def1_code
)
1955 && hoist_conversion_for_bitop_p (TREE_TYPE (arg1
), TREE_TYPE (def1_arg1
))
1956 && INTEGRAL_TYPE_P (TREE_TYPE (def1_arg1
))
1957 && int_fits_type_p (arg2
, TREE_TYPE (def1_arg1
)))
1960 tree tem
= make_ssa_name (TREE_TYPE (def1_arg1
), NULL
);
1962 gimple_build_assign_with_ops (code
, tem
, def1_arg1
,
1963 fold_convert_loc (gimple_location (stmt
),
1964 TREE_TYPE (def1_arg1
),
1966 gimple_set_location (newop
, gimple_location (stmt
));
1967 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
1968 gimple_assign_set_rhs_with_ops_1 (gsi
, NOP_EXPR
,
1969 tem
, NULL_TREE
, NULL_TREE
);
1970 update_stmt (gsi_stmt (*gsi
));
1974 /* For bitwise binary operations apply operand conversions to the
1975 binary operation result instead of to the operands. This allows
1976 to combine successive conversions and bitwise binary operations. */
1977 if (CONVERT_EXPR_CODE_P (def1_code
)
1978 && CONVERT_EXPR_CODE_P (def2_code
)
1979 && types_compatible_p (TREE_TYPE (def1_arg1
), TREE_TYPE (def2_arg1
))
1980 && hoist_conversion_for_bitop_p (TREE_TYPE (arg1
), TREE_TYPE (def1_arg1
)))
1983 tree tem
= make_ssa_name (TREE_TYPE (def1_arg1
), NULL
);
1984 newop
= gimple_build_assign_with_ops (code
, tem
, def1_arg1
, def2_arg1
);
1985 gimple_set_location (newop
, gimple_location (stmt
));
1986 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
1987 gimple_assign_set_rhs_with_ops_1 (gsi
, NOP_EXPR
,
1988 tem
, NULL_TREE
, NULL_TREE
);
1989 update_stmt (gsi_stmt (*gsi
));
1994 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
1995 if (def1_code
== def2_code
1996 && def1_code
== BIT_AND_EXPR
1997 && operand_equal_for_phi_arg_p (def1_arg2
,
2003 tree inner
= fold_build2 (code
, TREE_TYPE (arg2
), a
, c
);
2004 /* If A OP0 C (this usually means C is the same as A) is 0
2005 then fold it down correctly. */
2006 if (integer_zerop (inner
))
2008 gimple_assign_set_rhs_from_tree (gsi
, inner
);
2012 /* If A OP0 C (this usually means C is the same as A) is a ssa_name
2013 then fold it down correctly. */
2014 else if (TREE_CODE (inner
) == SSA_NAME
)
2016 tree outer
= fold_build2 (def1_code
, TREE_TYPE (inner
),
2018 gimple_assign_set_rhs_from_tree (gsi
, outer
);
2026 tem
= make_ssa_name (TREE_TYPE (arg2
), NULL
);
2027 newop
= gimple_build_assign_with_ops (code
, tem
, a
, c
);
2028 gimple_set_location (newop
, gimple_location (stmt
));
2029 /* Make sure to re-process the new stmt as it's walking upwards. */
2030 gsi_insert_before (gsi
, newop
, GSI_NEW_STMT
);
2031 gimple_assign_set_rhs1 (stmt
, tem
);
2032 gimple_assign_set_rhs2 (stmt
, b
);
2033 gimple_assign_set_rhs_code (stmt
, def1_code
);
2039 /* (a | CST1) & CST2 -> (a & CST2) | (CST1 & CST2). */
2040 if (code
== BIT_AND_EXPR
2041 && def1_code
== BIT_IOR_EXPR
2042 && CONSTANT_CLASS_P (arg2
)
2043 && CONSTANT_CLASS_P (def1_arg2
))
2045 tree cst
= fold_build2 (BIT_AND_EXPR
, TREE_TYPE (arg2
),
2049 if (integer_zerop (cst
))
2051 gimple_assign_set_rhs1 (stmt
, def1_arg1
);
2055 tem
= make_ssa_name (TREE_TYPE (arg2
), NULL
);
2056 newop
= gimple_build_assign_with_ops (BIT_AND_EXPR
,
2057 tem
, def1_arg1
, arg2
);
2058 gimple_set_location (newop
, gimple_location (stmt
));
2059 /* Make sure to re-process the new stmt as it's walking upwards. */
2060 gsi_insert_before (gsi
, newop
, GSI_NEW_STMT
);
2061 gimple_assign_set_rhs1 (stmt
, tem
);
2062 gimple_assign_set_rhs2 (stmt
, cst
);
2063 gimple_assign_set_rhs_code (stmt
, BIT_IOR_EXPR
);
2068 /* Combine successive equal operations with constants. */
2069 if ((code
== BIT_AND_EXPR
2070 || code
== BIT_IOR_EXPR
2071 || code
== BIT_XOR_EXPR
)
2072 && def1_code
== code
2073 && CONSTANT_CLASS_P (arg2
)
2074 && CONSTANT_CLASS_P (def1_arg2
))
2076 tree cst
= fold_build2 (code
, TREE_TYPE (arg2
),
2078 gimple_assign_set_rhs1 (stmt
, def1_arg1
);
2079 gimple_assign_set_rhs2 (stmt
, cst
);
2084 /* Canonicalize X ^ ~0 to ~X. */
2085 if (code
== BIT_XOR_EXPR
2086 && integer_all_onesp (arg2
))
2088 gimple_assign_set_rhs_with_ops (gsi
, BIT_NOT_EXPR
, arg1
, NULL_TREE
);
2089 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2094 /* Try simple folding for X op !X, and X op X. */
2095 res
= simplify_bitwise_binary_1 (code
, TREE_TYPE (arg1
), arg1
, arg2
);
2096 if (res
!= NULL_TREE
)
2098 gimple_assign_set_rhs_from_tree (gsi
, res
);
2099 update_stmt (gsi_stmt (*gsi
));
2103 if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
)
2105 enum tree_code ocode
= code
== BIT_AND_EXPR
? BIT_IOR_EXPR
: BIT_AND_EXPR
;
2106 if (def1_code
== ocode
)
2109 enum tree_code coden
;
2111 /* ( X | Y) & X -> X */
2112 /* ( X & Y) | X -> X */
2116 gimple_assign_set_rhs_from_tree (gsi
, x
);
2117 update_stmt (gsi_stmt (*gsi
));
2121 defcodefor_name (def1_arg1
, &coden
, &a1
, &a2
);
2122 /* (~X | Y) & X -> X & Y */
2123 /* (~X & Y) | X -> X | Y */
2124 if (coden
== BIT_NOT_EXPR
&& a1
== x
)
2126 gimple_assign_set_rhs_with_ops (gsi
, code
,
2128 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2132 defcodefor_name (def1_arg2
, &coden
, &a1
, &a2
);
2133 /* (Y | ~X) & X -> X & Y */
2134 /* (Y & ~X) | X -> X | Y */
2135 if (coden
== BIT_NOT_EXPR
&& a1
== x
)
2137 gimple_assign_set_rhs_with_ops (gsi
, code
,
2139 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2144 if (def2_code
== ocode
)
2146 enum tree_code coden
;
2149 /* X & ( X | Y) -> X */
2150 /* X | ( X & Y) -> X */
2154 gimple_assign_set_rhs_from_tree (gsi
, x
);
2155 update_stmt (gsi_stmt (*gsi
));
2158 defcodefor_name (def2_arg1
, &coden
, &a1
, NULL
);
2159 /* (~X | Y) & X -> X & Y */
2160 /* (~X & Y) | X -> X | Y */
2161 if (coden
== BIT_NOT_EXPR
&& a1
== x
)
2163 gimple_assign_set_rhs_with_ops (gsi
, code
,
2165 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2169 defcodefor_name (def2_arg2
, &coden
, &a1
, NULL
);
2170 /* (Y | ~X) & X -> X & Y */
2171 /* (Y & ~X) | X -> X | Y */
2172 if (coden
== BIT_NOT_EXPR
&& a1
== x
)
2174 gimple_assign_set_rhs_with_ops (gsi
, code
,
2176 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2182 /* If arg1 and arg2 are booleans (or any single bit type)
2183 then try to simplify:
2190 But only do this if our result feeds into a comparison as
2191 this transformation is not always a win, particularly on
2192 targets with and-not instructions. */
2193 if (TREE_CODE (arg1
) == SSA_NAME
2194 && TREE_CODE (arg2
) == SSA_NAME
2195 && INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
2196 && TYPE_PRECISION (TREE_TYPE (arg1
)) == 1
2197 && TYPE_PRECISION (TREE_TYPE (arg2
)) == 1
2198 && (TYPE_UNSIGNED (TREE_TYPE (arg1
))
2199 == TYPE_UNSIGNED (TREE_TYPE (arg2
))))
2201 use_operand_p use_p
;
2204 if (single_imm_use (gimple_assign_lhs (stmt
), &use_p
, &use_stmt
))
2206 if (gimple_code (use_stmt
) == GIMPLE_COND
2207 && gimple_cond_lhs (use_stmt
) == gimple_assign_lhs (stmt
)
2208 && integer_zerop (gimple_cond_rhs (use_stmt
))
2209 && gimple_cond_code (use_stmt
) == NE_EXPR
)
2211 if (simplify_bitwise_binary_boolean (gsi
, code
, arg1
, arg2
))
2213 if (simplify_bitwise_binary_boolean (gsi
, code
, arg2
, arg1
))
2223 /* Recognize rotation patterns. Return true if a transformation
2224 applied, otherwise return false.
2226 We are looking for X with unsigned type T with bitsize B, OP being
2227 +, | or ^, some type T2 wider than T and
2228 (X << CNT1) OP (X >> CNT2) iff CNT1 + CNT2 == B
2229 ((T) ((T2) X << CNT1)) OP ((T) ((T2) X >> CNT2)) iff CNT1 + CNT2 == B
2230 (X << Y) OP (X >> (B - Y))
2231 (X << (int) Y) OP (X >> (int) (B - Y))
2232 ((T) ((T2) X << Y)) OP ((T) ((T2) X >> (B - Y)))
2233 ((T) ((T2) X << (int) Y)) OP ((T) ((T2) X >> (int) (B - Y)))
2234 (X << Y) | (X >> ((-Y) & (B - 1)))
2235 (X << (int) Y) | (X >> (int) ((-Y) & (B - 1)))
2236 ((T) ((T2) X << Y)) | ((T) ((T2) X >> ((-Y) & (B - 1))))
2237 ((T) ((T2) X << (int) Y)) | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
2239 and transform these into:
2243 Note, in the patterns with T2 type, the type of OP operands
2244 might be even a signed type, but should have precision B. */
2247 simplify_rotate (gimple_stmt_iterator
*gsi
)
2249 gimple stmt
= gsi_stmt (*gsi
);
2250 tree arg
[2], rtype
, rotcnt
= NULL_TREE
;
2251 tree def_arg1
[2], def_arg2
[2];
2252 enum tree_code def_code
[2];
2255 bool swapped_p
= false;
2258 arg
[0] = gimple_assign_rhs1 (stmt
);
2259 arg
[1] = gimple_assign_rhs2 (stmt
);
2260 rtype
= TREE_TYPE (arg
[0]);
2262 /* Only create rotates in complete modes. Other cases are not
2263 expanded properly. */
2264 if (!INTEGRAL_TYPE_P (rtype
)
2265 || TYPE_PRECISION (rtype
) != GET_MODE_PRECISION (TYPE_MODE (rtype
)))
2268 for (i
= 0; i
< 2; i
++)
2269 defcodefor_name (arg
[i
], &def_code
[i
], &def_arg1
[i
], &def_arg2
[i
]);
2271 /* Look through narrowing conversions. */
2272 if (CONVERT_EXPR_CODE_P (def_code
[0])
2273 && CONVERT_EXPR_CODE_P (def_code
[1])
2274 && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1
[0]))
2275 && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1
[1]))
2276 && TYPE_PRECISION (TREE_TYPE (def_arg1
[0]))
2277 == TYPE_PRECISION (TREE_TYPE (def_arg1
[1]))
2278 && TYPE_PRECISION (TREE_TYPE (def_arg1
[0])) > TYPE_PRECISION (rtype
)
2279 && has_single_use (arg
[0])
2280 && has_single_use (arg
[1]))
2282 for (i
= 0; i
< 2; i
++)
2284 arg
[i
] = def_arg1
[i
];
2285 defcodefor_name (arg
[i
], &def_code
[i
], &def_arg1
[i
], &def_arg2
[i
]);
2289 /* One operand has to be LSHIFT_EXPR and one RSHIFT_EXPR. */
2290 for (i
= 0; i
< 2; i
++)
2291 if (def_code
[i
] != LSHIFT_EXPR
&& def_code
[i
] != RSHIFT_EXPR
)
2293 else if (!has_single_use (arg
[i
]))
2295 if (def_code
[0] == def_code
[1])
2298 /* If we've looked through narrowing conversions before, look through
2299 widening conversions from unsigned type with the same precision
2301 if (TYPE_PRECISION (TREE_TYPE (def_arg1
[0])) != TYPE_PRECISION (rtype
))
2302 for (i
= 0; i
< 2; i
++)
2305 enum tree_code code
;
2306 defcodefor_name (def_arg1
[i
], &code
, &tem
, NULL
);
2307 if (!CONVERT_EXPR_CODE_P (code
)
2308 || !INTEGRAL_TYPE_P (TREE_TYPE (tem
))
2309 || TYPE_PRECISION (TREE_TYPE (tem
)) != TYPE_PRECISION (rtype
))
2313 /* Both shifts have to use the same first operand. */
2314 if (TREE_CODE (def_arg1
[0]) != SSA_NAME
|| def_arg1
[0] != def_arg1
[1])
2316 if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1
[0])))
2319 /* CNT1 + CNT2 == B case above. */
2320 if (tree_fits_uhwi_p (def_arg2
[0])
2321 && tree_fits_uhwi_p (def_arg2
[1])
2322 && tree_to_uhwi (def_arg2
[0])
2323 + tree_to_uhwi (def_arg2
[1]) == TYPE_PRECISION (rtype
))
2324 rotcnt
= def_arg2
[0];
2325 else if (TREE_CODE (def_arg2
[0]) != SSA_NAME
2326 || TREE_CODE (def_arg2
[1]) != SSA_NAME
)
2330 tree cdef_arg1
[2], cdef_arg2
[2], def_arg2_alt
[2];
2331 enum tree_code cdef_code
[2];
2332 /* Look through conversion of the shift count argument.
2333 The C/C++ FE cast any shift count argument to integer_type_node.
2334 The only problem might be if the shift count type maximum value
2335 is equal or smaller than number of bits in rtype. */
2336 for (i
= 0; i
< 2; i
++)
2338 def_arg2_alt
[i
] = def_arg2
[i
];
2339 defcodefor_name (def_arg2
[i
], &cdef_code
[i
],
2340 &cdef_arg1
[i
], &cdef_arg2
[i
]);
2341 if (CONVERT_EXPR_CODE_P (cdef_code
[i
])
2342 && INTEGRAL_TYPE_P (TREE_TYPE (cdef_arg1
[i
]))
2343 && TYPE_PRECISION (TREE_TYPE (cdef_arg1
[i
]))
2344 > floor_log2 (TYPE_PRECISION (rtype
))
2345 && TYPE_PRECISION (TREE_TYPE (cdef_arg1
[i
]))
2346 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (cdef_arg1
[i
]))))
2348 def_arg2_alt
[i
] = cdef_arg1
[i
];
2349 defcodefor_name (def_arg2_alt
[i
], &cdef_code
[i
],
2350 &cdef_arg1
[i
], &cdef_arg2
[i
]);
2353 for (i
= 0; i
< 2; i
++)
2354 /* Check for one shift count being Y and the other B - Y,
2355 with optional casts. */
2356 if (cdef_code
[i
] == MINUS_EXPR
2357 && tree_fits_shwi_p (cdef_arg1
[i
])
2358 && tree_to_shwi (cdef_arg1
[i
]) == TYPE_PRECISION (rtype
)
2359 && TREE_CODE (cdef_arg2
[i
]) == SSA_NAME
)
2362 enum tree_code code
;
2364 if (cdef_arg2
[i
] == def_arg2
[1 - i
]
2365 || cdef_arg2
[i
] == def_arg2_alt
[1 - i
])
2367 rotcnt
= cdef_arg2
[i
];
2370 defcodefor_name (cdef_arg2
[i
], &code
, &tem
, NULL
);
2371 if (CONVERT_EXPR_CODE_P (code
)
2372 && INTEGRAL_TYPE_P (TREE_TYPE (tem
))
2373 && TYPE_PRECISION (TREE_TYPE (tem
))
2374 > floor_log2 (TYPE_PRECISION (rtype
))
2375 && TYPE_PRECISION (TREE_TYPE (tem
))
2376 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (tem
)))
2377 && (tem
== def_arg2
[1 - i
]
2378 || tem
== def_arg2_alt
[1 - i
]))
2384 /* The above sequence isn't safe for Y being 0,
2385 because then one of the shifts triggers undefined behavior.
2386 This alternative is safe even for rotation count of 0.
2387 One shift count is Y and the other (-Y) & (B - 1). */
2388 else if (cdef_code
[i
] == BIT_AND_EXPR
2389 && tree_fits_shwi_p (cdef_arg2
[i
])
2390 && tree_to_shwi (cdef_arg2
[i
])
2391 == TYPE_PRECISION (rtype
) - 1
2392 && TREE_CODE (cdef_arg1
[i
]) == SSA_NAME
2393 && gimple_assign_rhs_code (stmt
) == BIT_IOR_EXPR
)
2396 enum tree_code code
;
2398 defcodefor_name (cdef_arg1
[i
], &code
, &tem
, NULL
);
2399 if (CONVERT_EXPR_CODE_P (code
)
2400 && INTEGRAL_TYPE_P (TREE_TYPE (tem
))
2401 && TYPE_PRECISION (TREE_TYPE (tem
))
2402 > floor_log2 (TYPE_PRECISION (rtype
))
2403 && TYPE_PRECISION (TREE_TYPE (tem
))
2404 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (tem
))))
2405 defcodefor_name (tem
, &code
, &tem
, NULL
);
2407 if (code
== NEGATE_EXPR
)
2409 if (tem
== def_arg2
[1 - i
] || tem
== def_arg2_alt
[1 - i
])
2414 defcodefor_name (tem
, &code
, &tem
, NULL
);
2415 if (CONVERT_EXPR_CODE_P (code
)
2416 && INTEGRAL_TYPE_P (TREE_TYPE (tem
))
2417 && TYPE_PRECISION (TREE_TYPE (tem
))
2418 > floor_log2 (TYPE_PRECISION (rtype
))
2419 && TYPE_PRECISION (TREE_TYPE (tem
))
2420 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (tem
)))
2421 && (tem
== def_arg2
[1 - i
]
2422 || tem
== def_arg2_alt
[1 - i
]))
2429 if (rotcnt
== NULL_TREE
)
2434 if (!useless_type_conversion_p (TREE_TYPE (def_arg2
[0]),
2435 TREE_TYPE (rotcnt
)))
2437 g
= gimple_build_assign_with_ops (NOP_EXPR
,
2438 make_ssa_name (TREE_TYPE (def_arg2
[0]),
2441 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
2442 rotcnt
= gimple_assign_lhs (g
);
2444 lhs
= gimple_assign_lhs (stmt
);
2445 if (!useless_type_conversion_p (rtype
, TREE_TYPE (def_arg1
[0])))
2446 lhs
= make_ssa_name (TREE_TYPE (def_arg1
[0]), NULL
);
2447 g
= gimple_build_assign_with_ops (((def_code
[0] == LSHIFT_EXPR
) ^ swapped_p
)
2448 ? LROTATE_EXPR
: RROTATE_EXPR
,
2449 lhs
, def_arg1
[0], rotcnt
);
2450 if (!useless_type_conversion_p (rtype
, TREE_TYPE (def_arg1
[0])))
2452 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
2453 g
= gimple_build_assign_with_ops (NOP_EXPR
, gimple_assign_lhs (stmt
),
2456 gsi_replace (gsi
, g
, false);
2460 /* Perform re-associations of the plus or minus statement STMT that are
2461 always permitted. Returns true if the CFG was changed. */
2464 associate_plusminus (gimple_stmt_iterator
*gsi
)
2466 gimple stmt
= gsi_stmt (*gsi
);
2467 tree rhs1
= gimple_assign_rhs1 (stmt
);
2468 tree rhs2
= gimple_assign_rhs2 (stmt
);
2469 enum tree_code code
= gimple_assign_rhs_code (stmt
);
2472 /* We can't reassociate at all for saturating types. */
2473 if (TYPE_SATURATING (TREE_TYPE (rhs1
)))
2476 /* First contract negates. */
2481 /* A +- (-B) -> A -+ B. */
2482 if (TREE_CODE (rhs2
) == SSA_NAME
)
2484 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs2
);
2485 if (is_gimple_assign (def_stmt
)
2486 && gimple_assign_rhs_code (def_stmt
) == NEGATE_EXPR
2487 && can_propagate_from (def_stmt
))
2489 code
= (code
== MINUS_EXPR
) ? PLUS_EXPR
: MINUS_EXPR
;
2490 gimple_assign_set_rhs_code (stmt
, code
);
2491 rhs2
= gimple_assign_rhs1 (def_stmt
);
2492 gimple_assign_set_rhs2 (stmt
, rhs2
);
2493 gimple_set_modified (stmt
, true);
2498 /* (-A) + B -> B - A. */
2499 if (TREE_CODE (rhs1
) == SSA_NAME
2500 && code
== PLUS_EXPR
)
2502 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs1
);
2503 if (is_gimple_assign (def_stmt
)
2504 && gimple_assign_rhs_code (def_stmt
) == NEGATE_EXPR
2505 && can_propagate_from (def_stmt
))
2508 gimple_assign_set_rhs_code (stmt
, code
);
2510 gimple_assign_set_rhs1 (stmt
, rhs1
);
2511 rhs2
= gimple_assign_rhs1 (def_stmt
);
2512 gimple_assign_set_rhs2 (stmt
, rhs2
);
2513 gimple_set_modified (stmt
, true);
2520 /* We can't reassociate floating-point or fixed-point plus or minus
2521 because of saturation to +-Inf. */
2522 if (FLOAT_TYPE_P (TREE_TYPE (rhs1
))
2523 || FIXED_POINT_TYPE_P (TREE_TYPE (rhs1
)))
2526 /* Second match patterns that allow contracting a plus-minus pair
2527 irrespective of overflow issues.
2529 (A +- B) - A -> +- B
2531 (CST +- A) +- CST -> CST +- A
2532 (A +- CST) +- CST -> A +- CST
2535 A - (A +- B) -> -+ B
2536 A +- (B +- A) -> +- B
2537 CST +- (CST +- A) -> CST +- A
2538 CST +- (A +- CST) -> CST +- A
2541 via commutating the addition and contracting operations to zero
2542 by reassociation. */
2544 if (TREE_CODE (rhs1
) == SSA_NAME
)
2546 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs1
);
2547 if (is_gimple_assign (def_stmt
) && can_propagate_from (def_stmt
))
2549 enum tree_code def_code
= gimple_assign_rhs_code (def_stmt
);
2550 if (def_code
== PLUS_EXPR
2551 || def_code
== MINUS_EXPR
)
2553 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2554 tree def_rhs2
= gimple_assign_rhs2 (def_stmt
);
2555 if (operand_equal_p (def_rhs1
, rhs2
, 0)
2556 && code
== MINUS_EXPR
)
2558 /* (A +- B) - A -> +- B. */
2559 code
= ((def_code
== PLUS_EXPR
)
2560 ? TREE_CODE (def_rhs2
) : NEGATE_EXPR
);
2563 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2564 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2565 gimple_set_modified (stmt
, true);
2567 else if (operand_equal_p (def_rhs2
, rhs2
, 0)
2568 && code
!= def_code
)
2570 /* (A +- B) -+ B -> A. */
2571 code
= TREE_CODE (def_rhs1
);
2574 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2575 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2576 gimple_set_modified (stmt
, true);
2578 else if (CONSTANT_CLASS_P (rhs2
)
2579 && CONSTANT_CLASS_P (def_rhs1
))
2581 /* (CST +- A) +- CST -> CST +- A. */
2582 tree cst
= fold_binary (code
, TREE_TYPE (rhs1
),
2584 if (cst
&& !TREE_OVERFLOW (cst
))
2587 gimple_assign_set_rhs_code (stmt
, code
);
2589 gimple_assign_set_rhs1 (stmt
, rhs1
);
2591 gimple_assign_set_rhs2 (stmt
, rhs2
);
2592 gimple_set_modified (stmt
, true);
2595 else if (CONSTANT_CLASS_P (rhs2
)
2596 && CONSTANT_CLASS_P (def_rhs2
))
2598 /* (A +- CST) +- CST -> A +- CST. */
2599 enum tree_code mix
= (code
== def_code
)
2600 ? PLUS_EXPR
: MINUS_EXPR
;
2601 tree cst
= fold_binary (mix
, TREE_TYPE (rhs1
),
2603 if (cst
&& !TREE_OVERFLOW (cst
))
2606 gimple_assign_set_rhs_code (stmt
, code
);
2608 gimple_assign_set_rhs1 (stmt
, rhs1
);
2610 gimple_assign_set_rhs2 (stmt
, rhs2
);
2611 gimple_set_modified (stmt
, true);
2615 else if (def_code
== BIT_NOT_EXPR
&& code
== PLUS_EXPR
)
2617 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2618 if (operand_equal_p (def_rhs1
, rhs2
, 0))
2621 rhs1
= build_all_ones_cst (TREE_TYPE (rhs2
));
2623 code
= TREE_CODE (rhs1
);
2624 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2625 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2626 gimple_set_modified (stmt
, true);
2628 else if ((TREE_CODE (TREE_TYPE (rhs2
)) != COMPLEX_TYPE
2629 && integer_onep (rhs2
))
2630 || (TREE_CODE (rhs2
) == COMPLEX_CST
2631 && integer_onep (TREE_REALPART (rhs2
))
2632 && integer_onep (TREE_IMAGPART (rhs2
))))
2638 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2639 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2640 gimple_set_modified (stmt
, true);
2646 if (rhs2
&& TREE_CODE (rhs2
) == SSA_NAME
)
2648 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs2
);
2649 if (is_gimple_assign (def_stmt
) && can_propagate_from (def_stmt
))
2651 enum tree_code def_code
= gimple_assign_rhs_code (def_stmt
);
2652 if (def_code
== PLUS_EXPR
2653 || def_code
== MINUS_EXPR
)
2655 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2656 tree def_rhs2
= gimple_assign_rhs2 (def_stmt
);
2657 if (operand_equal_p (def_rhs1
, rhs1
, 0)
2658 && code
== MINUS_EXPR
)
2660 /* A - (A +- B) -> -+ B. */
2661 code
= ((def_code
== PLUS_EXPR
)
2662 ? NEGATE_EXPR
: TREE_CODE (def_rhs2
));
2665 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2666 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2667 gimple_set_modified (stmt
, true);
2669 else if (operand_equal_p (def_rhs2
, rhs1
, 0)
2670 && code
!= def_code
)
2672 /* A +- (B +- A) -> +- B. */
2673 code
= ((code
== PLUS_EXPR
)
2674 ? TREE_CODE (def_rhs1
) : NEGATE_EXPR
);
2677 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2678 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2679 gimple_set_modified (stmt
, true);
2681 else if (CONSTANT_CLASS_P (rhs1
)
2682 && CONSTANT_CLASS_P (def_rhs1
))
2684 /* CST +- (CST +- A) -> CST +- A. */
2685 tree cst
= fold_binary (code
, TREE_TYPE (rhs2
),
2687 if (cst
&& !TREE_OVERFLOW (cst
))
2689 code
= (code
== def_code
? PLUS_EXPR
: MINUS_EXPR
);
2690 gimple_assign_set_rhs_code (stmt
, code
);
2692 gimple_assign_set_rhs1 (stmt
, rhs1
);
2694 gimple_assign_set_rhs2 (stmt
, rhs2
);
2695 gimple_set_modified (stmt
, true);
2698 else if (CONSTANT_CLASS_P (rhs1
)
2699 && CONSTANT_CLASS_P (def_rhs2
))
2701 /* CST +- (A +- CST) -> CST +- A. */
2702 tree cst
= fold_binary (def_code
== code
2703 ? PLUS_EXPR
: MINUS_EXPR
,
2706 if (cst
&& !TREE_OVERFLOW (cst
))
2709 gimple_assign_set_rhs1 (stmt
, rhs1
);
2711 gimple_assign_set_rhs2 (stmt
, rhs2
);
2712 gimple_set_modified (stmt
, true);
2716 else if (def_code
== BIT_NOT_EXPR
)
2718 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2719 if (code
== PLUS_EXPR
2720 && operand_equal_p (def_rhs1
, rhs1
, 0))
2723 rhs1
= build_all_ones_cst (TREE_TYPE (rhs1
));
2725 code
= TREE_CODE (rhs1
);
2726 gimple_assign_set_rhs_with_ops (gsi
, code
, rhs1
, NULL_TREE
);
2727 gcc_assert (gsi_stmt (*gsi
) == stmt
);
2728 gimple_set_modified (stmt
, true);
2735 if (gimple_modified_p (stmt
))
2737 fold_stmt_inplace (gsi
);
2739 if (maybe_clean_or_replace_eh_stmt (stmt
, stmt
)
2740 && gimple_purge_dead_eh_edges (gimple_bb (stmt
)))
2747 /* Associate operands of a POINTER_PLUS_EXPR assignmen at *GSI. Returns
2748 true if anything changed, false otherwise. */
2751 associate_pointerplus (gimple_stmt_iterator
*gsi
)
2753 gimple stmt
= gsi_stmt (*gsi
);
2755 tree ptr
, rhs
, algn
;
2758 tem = (sizetype) ptr;
2762 and produce the simpler and easier to analyze with respect to alignment
2763 ... = ptr & ~algn; */
2764 ptr
= gimple_assign_rhs1 (stmt
);
2765 rhs
= gimple_assign_rhs2 (stmt
);
2766 if (TREE_CODE (rhs
) != SSA_NAME
)
2768 def_stmt
= SSA_NAME_DEF_STMT (rhs
);
2769 if (!is_gimple_assign (def_stmt
)
2770 || gimple_assign_rhs_code (def_stmt
) != NEGATE_EXPR
)
2772 rhs
= gimple_assign_rhs1 (def_stmt
);
2773 if (TREE_CODE (rhs
) != SSA_NAME
)
2775 def_stmt
= SSA_NAME_DEF_STMT (rhs
);
2776 if (!is_gimple_assign (def_stmt
)
2777 || gimple_assign_rhs_code (def_stmt
) != BIT_AND_EXPR
)
2779 rhs
= gimple_assign_rhs1 (def_stmt
);
2780 algn
= gimple_assign_rhs2 (def_stmt
);
2781 if (TREE_CODE (rhs
) != SSA_NAME
2782 || TREE_CODE (algn
) != INTEGER_CST
)
2784 def_stmt
= SSA_NAME_DEF_STMT (rhs
);
2785 if (!is_gimple_assign (def_stmt
)
2786 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
2788 if (gimple_assign_rhs1 (def_stmt
) != ptr
)
2791 algn
= double_int_to_tree (TREE_TYPE (ptr
), ~tree_to_double_int (algn
));
2792 gimple_assign_set_rhs_with_ops (gsi
, BIT_AND_EXPR
, ptr
, algn
);
2793 fold_stmt_inplace (gsi
);
2799 /* Combine two conversions in a row for the second conversion at *GSI.
2800 Returns 1 if there were any changes made, 2 if cfg-cleanup needs to
2801 run. Else it returns 0. */
2804 combine_conversions (gimple_stmt_iterator
*gsi
)
2806 gimple stmt
= gsi_stmt (*gsi
);
2809 enum tree_code code
= gimple_assign_rhs_code (stmt
);
2810 enum tree_code code2
;
2812 gcc_checking_assert (CONVERT_EXPR_CODE_P (code
)
2813 || code
== FLOAT_EXPR
2814 || code
== FIX_TRUNC_EXPR
);
2816 lhs
= gimple_assign_lhs (stmt
);
2817 op0
= gimple_assign_rhs1 (stmt
);
2818 if (useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
)))
2820 gimple_assign_set_rhs_code (stmt
, TREE_CODE (op0
));
2824 if (TREE_CODE (op0
) != SSA_NAME
)
2827 def_stmt
= SSA_NAME_DEF_STMT (op0
);
2828 if (!is_gimple_assign (def_stmt
))
2831 code2
= gimple_assign_rhs_code (def_stmt
);
2833 if (CONVERT_EXPR_CODE_P (code2
) || code2
== FLOAT_EXPR
)
2835 tree defop0
= gimple_assign_rhs1 (def_stmt
);
2836 tree type
= TREE_TYPE (lhs
);
2837 tree inside_type
= TREE_TYPE (defop0
);
2838 tree inter_type
= TREE_TYPE (op0
);
2839 int inside_int
= INTEGRAL_TYPE_P (inside_type
);
2840 int inside_ptr
= POINTER_TYPE_P (inside_type
);
2841 int inside_float
= FLOAT_TYPE_P (inside_type
);
2842 int inside_vec
= TREE_CODE (inside_type
) == VECTOR_TYPE
;
2843 unsigned int inside_prec
= TYPE_PRECISION (inside_type
);
2844 int inside_unsignedp
= TYPE_UNSIGNED (inside_type
);
2845 int inter_int
= INTEGRAL_TYPE_P (inter_type
);
2846 int inter_ptr
= POINTER_TYPE_P (inter_type
);
2847 int inter_float
= FLOAT_TYPE_P (inter_type
);
2848 int inter_vec
= TREE_CODE (inter_type
) == VECTOR_TYPE
;
2849 unsigned int inter_prec
= TYPE_PRECISION (inter_type
);
2850 int inter_unsignedp
= TYPE_UNSIGNED (inter_type
);
2851 int final_int
= INTEGRAL_TYPE_P (type
);
2852 int final_ptr
= POINTER_TYPE_P (type
);
2853 int final_float
= FLOAT_TYPE_P (type
);
2854 int final_vec
= TREE_CODE (type
) == VECTOR_TYPE
;
2855 unsigned int final_prec
= TYPE_PRECISION (type
);
2856 int final_unsignedp
= TYPE_UNSIGNED (type
);
2858 /* Don't propagate ssa names that occur in abnormal phis. */
2859 if (TREE_CODE (defop0
) == SSA_NAME
2860 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (defop0
))
2863 /* In addition to the cases of two conversions in a row
2864 handled below, if we are converting something to its own
2865 type via an object of identical or wider precision, neither
2866 conversion is needed. */
2867 if (useless_type_conversion_p (type
, inside_type
)
2868 && (((inter_int
|| inter_ptr
) && final_int
)
2869 || (inter_float
&& final_float
))
2870 && inter_prec
>= final_prec
)
2872 gimple_assign_set_rhs1 (stmt
, unshare_expr (defop0
));
2873 gimple_assign_set_rhs_code (stmt
, TREE_CODE (defop0
));
2875 return remove_prop_source_from_use (op0
) ? 2 : 1;
2878 /* Likewise, if the intermediate and initial types are either both
2879 float or both integer, we don't need the middle conversion if the
2880 former is wider than the latter and doesn't change the signedness
2881 (for integers). Avoid this if the final type is a pointer since
2882 then we sometimes need the middle conversion. Likewise if the
2883 final type has a precision not equal to the size of its mode. */
2884 if (((inter_int
&& inside_int
)
2885 || (inter_float
&& inside_float
)
2886 || (inter_vec
&& inside_vec
))
2887 && inter_prec
>= inside_prec
2888 && (inter_float
|| inter_vec
2889 || inter_unsignedp
== inside_unsignedp
)
2890 && ! (final_prec
!= GET_MODE_PRECISION (TYPE_MODE (type
))
2891 && TYPE_MODE (type
) == TYPE_MODE (inter_type
))
2893 && (! final_vec
|| inter_prec
== inside_prec
))
2895 gimple_assign_set_rhs1 (stmt
, defop0
);
2897 return remove_prop_source_from_use (op0
) ? 2 : 1;
2900 /* If we have a sign-extension of a zero-extended value, we can
2901 replace that by a single zero-extension. Likewise if the
2902 final conversion does not change precision we can drop the
2903 intermediate conversion. */
2904 if (inside_int
&& inter_int
&& final_int
2905 && ((inside_prec
< inter_prec
&& inter_prec
< final_prec
2906 && inside_unsignedp
&& !inter_unsignedp
)
2907 || final_prec
== inter_prec
))
2909 gimple_assign_set_rhs1 (stmt
, defop0
);
2911 return remove_prop_source_from_use (op0
) ? 2 : 1;
2914 /* Two conversions in a row are not needed unless:
2915 - some conversion is floating-point (overstrict for now), or
2916 - some conversion is a vector (overstrict for now), or
2917 - the intermediate type is narrower than both initial and
2919 - the intermediate type and innermost type differ in signedness,
2920 and the outermost type is wider than the intermediate, or
2921 - the initial type is a pointer type and the precisions of the
2922 intermediate and final types differ, or
2923 - the final type is a pointer type and the precisions of the
2924 initial and intermediate types differ. */
2925 if (! inside_float
&& ! inter_float
&& ! final_float
2926 && ! inside_vec
&& ! inter_vec
&& ! final_vec
2927 && (inter_prec
>= inside_prec
|| inter_prec
>= final_prec
)
2928 && ! (inside_int
&& inter_int
2929 && inter_unsignedp
!= inside_unsignedp
2930 && inter_prec
< final_prec
)
2931 && ((inter_unsignedp
&& inter_prec
> inside_prec
)
2932 == (final_unsignedp
&& final_prec
> inter_prec
))
2933 && ! (inside_ptr
&& inter_prec
!= final_prec
)
2934 && ! (final_ptr
&& inside_prec
!= inter_prec
)
2935 && ! (final_prec
!= GET_MODE_PRECISION (TYPE_MODE (type
))
2936 && TYPE_MODE (type
) == TYPE_MODE (inter_type
)))
2938 gimple_assign_set_rhs1 (stmt
, defop0
);
2940 return remove_prop_source_from_use (op0
) ? 2 : 1;
2943 /* A truncation to an unsigned type should be canonicalized as
2944 bitwise and of a mask. */
2945 if (final_int
&& inter_int
&& inside_int
2946 && final_prec
== inside_prec
2947 && final_prec
> inter_prec
2951 tem
= fold_build2 (BIT_AND_EXPR
, inside_type
,
2954 (inside_type
, double_int::mask (inter_prec
)));
2955 if (!useless_type_conversion_p (type
, inside_type
))
2957 tem
= force_gimple_operand_gsi (gsi
, tem
, true, NULL_TREE
, true,
2959 gimple_assign_set_rhs1 (stmt
, tem
);
2962 gimple_assign_set_rhs_from_tree (gsi
, tem
);
2963 update_stmt (gsi_stmt (*gsi
));
2967 /* If we are converting an integer to a floating-point that can
2968 represent it exactly and back to an integer, we can skip the
2969 floating-point conversion. */
2970 if (inside_int
&& inter_float
&& final_int
&&
2971 (unsigned) significand_size (TYPE_MODE (inter_type
))
2972 >= inside_prec
- !inside_unsignedp
)
2974 if (useless_type_conversion_p (type
, inside_type
))
2976 gimple_assign_set_rhs1 (stmt
, unshare_expr (defop0
));
2977 gimple_assign_set_rhs_code (stmt
, TREE_CODE (defop0
));
2979 return remove_prop_source_from_use (op0
) ? 2 : 1;
2983 gimple_assign_set_rhs1 (stmt
, defop0
);
2984 gimple_assign_set_rhs_code (stmt
, CONVERT_EXPR
);
2986 return remove_prop_source_from_use (op0
) ? 2 : 1;
2994 /* Combine VIEW_CONVERT_EXPRs with their defining statement. */
2997 simplify_vce (gimple_stmt_iterator
*gsi
)
2999 gimple stmt
= gsi_stmt (*gsi
);
3000 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
3002 /* Drop useless VIEW_CONVERT_EXPRs. */
3003 tree op
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
3004 if (useless_type_conversion_p (type
, TREE_TYPE (op
)))
3006 gimple_assign_set_rhs1 (stmt
, op
);
3011 if (TREE_CODE (op
) != SSA_NAME
)
3014 gimple def_stmt
= SSA_NAME_DEF_STMT (op
);
3015 if (!is_gimple_assign (def_stmt
))
3018 tree def_op
= gimple_assign_rhs1 (def_stmt
);
3019 switch (gimple_assign_rhs_code (def_stmt
))
3022 /* Strip integral conversions that do not change the precision. */
3023 if ((INTEGRAL_TYPE_P (TREE_TYPE (op
))
3024 || POINTER_TYPE_P (TREE_TYPE (op
)))
3025 && (INTEGRAL_TYPE_P (TREE_TYPE (def_op
))
3026 || POINTER_TYPE_P (TREE_TYPE (def_op
)))
3027 && (TYPE_PRECISION (TREE_TYPE (op
))
3028 == TYPE_PRECISION (TREE_TYPE (def_op
))))
3030 TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0) = def_op
;
3036 case VIEW_CONVERT_EXPR
:
3037 /* Series of VIEW_CONVERT_EXPRs on register operands can
3039 if (TREE_CODE (TREE_OPERAND (def_op
, 0)) == SSA_NAME
)
3041 if (useless_type_conversion_p (type
,
3042 TREE_TYPE (TREE_OPERAND (def_op
, 0))))
3043 gimple_assign_set_rhs1 (stmt
, TREE_OPERAND (def_op
, 0));
3045 TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0)
3046 = TREE_OPERAND (def_op
, 0);
3057 /* Combine an element access with a shuffle. Returns true if there were
3058 any changes made, else it returns false. */
3061 simplify_bitfield_ref (gimple_stmt_iterator
*gsi
)
3063 gimple stmt
= gsi_stmt (*gsi
);
3065 tree op
, op0
, op1
, op2
;
3067 unsigned idx
, n
, size
;
3068 enum tree_code code
;
3070 op
= gimple_assign_rhs1 (stmt
);
3071 gcc_checking_assert (TREE_CODE (op
) == BIT_FIELD_REF
);
3073 op0
= TREE_OPERAND (op
, 0);
3074 if (TREE_CODE (op0
) != SSA_NAME
3075 || TREE_CODE (TREE_TYPE (op0
)) != VECTOR_TYPE
)
3078 def_stmt
= get_prop_source_stmt (op0
, false, NULL
);
3079 if (!def_stmt
|| !can_propagate_from (def_stmt
))
3082 op1
= TREE_OPERAND (op
, 1);
3083 op2
= TREE_OPERAND (op
, 2);
3084 code
= gimple_assign_rhs_code (def_stmt
);
3086 if (code
== CONSTRUCTOR
)
3088 tree tem
= fold_ternary (BIT_FIELD_REF
, TREE_TYPE (op
),
3089 gimple_assign_rhs1 (def_stmt
), op1
, op2
);
3090 if (!tem
|| !valid_gimple_rhs_p (tem
))
3092 gimple_assign_set_rhs_from_tree (gsi
, tem
);
3093 update_stmt (gsi_stmt (*gsi
));
3097 elem_type
= TREE_TYPE (TREE_TYPE (op0
));
3098 if (TREE_TYPE (op
) != elem_type
)
3101 size
= TREE_INT_CST_LOW (TYPE_SIZE (elem_type
));
3102 n
= TREE_INT_CST_LOW (op1
) / size
;
3105 idx
= TREE_INT_CST_LOW (op2
) / size
;
3107 if (code
== VEC_PERM_EXPR
)
3109 tree p
, m
, index
, tem
;
3111 m
= gimple_assign_rhs3 (def_stmt
);
3112 if (TREE_CODE (m
) != VECTOR_CST
)
3114 nelts
= VECTOR_CST_NELTS (m
);
3115 idx
= TREE_INT_CST_LOW (VECTOR_CST_ELT (m
, idx
));
3119 p
= gimple_assign_rhs1 (def_stmt
);
3123 p
= gimple_assign_rhs2 (def_stmt
);
3126 index
= build_int_cst (TREE_TYPE (TREE_TYPE (m
)), idx
* size
);
3127 tem
= build3 (BIT_FIELD_REF
, TREE_TYPE (op
),
3128 unshare_expr (p
), op1
, index
);
3129 gimple_assign_set_rhs1 (stmt
, tem
);
3131 update_stmt (gsi_stmt (*gsi
));
3138 /* Determine whether applying the 2 permutations (mask1 then mask2)
3139 gives back one of the input. */
3142 is_combined_permutation_identity (tree mask1
, tree mask2
)
3145 unsigned int nelts
, i
, j
;
3146 bool maybe_identity1
= true;
3147 bool maybe_identity2
= true;
3149 gcc_checking_assert (TREE_CODE (mask1
) == VECTOR_CST
3150 && TREE_CODE (mask2
) == VECTOR_CST
);
3151 mask
= fold_ternary (VEC_PERM_EXPR
, TREE_TYPE (mask1
), mask1
, mask1
, mask2
);
3152 gcc_assert (TREE_CODE (mask
) == VECTOR_CST
);
3154 nelts
= VECTOR_CST_NELTS (mask
);
3155 for (i
= 0; i
< nelts
; i
++)
3157 tree val
= VECTOR_CST_ELT (mask
, i
);
3158 gcc_assert (TREE_CODE (val
) == INTEGER_CST
);
3159 j
= TREE_INT_CST_LOW (val
) & (2 * nelts
- 1);
3161 maybe_identity2
= false;
3162 else if (j
== i
+ nelts
)
3163 maybe_identity1
= false;
3167 return maybe_identity1
? 1 : maybe_identity2
? 2 : 0;
3170 /* Combine a shuffle with its arguments. Returns 1 if there were any
3171 changes made, 2 if cfg-cleanup needs to run. Else it returns 0. */
3174 simplify_permutation (gimple_stmt_iterator
*gsi
)
3176 gimple stmt
= gsi_stmt (*gsi
);
3178 tree op0
, op1
, op2
, op3
, arg0
, arg1
;
3179 enum tree_code code
;
3180 bool single_use_op0
= false;
3182 gcc_checking_assert (gimple_assign_rhs_code (stmt
) == VEC_PERM_EXPR
);
3184 op0
= gimple_assign_rhs1 (stmt
);
3185 op1
= gimple_assign_rhs2 (stmt
);
3186 op2
= gimple_assign_rhs3 (stmt
);
3188 if (TREE_CODE (op2
) != VECTOR_CST
)
3191 if (TREE_CODE (op0
) == VECTOR_CST
)
3196 else if (TREE_CODE (op0
) == SSA_NAME
)
3198 def_stmt
= get_prop_source_stmt (op0
, false, &single_use_op0
);
3199 if (!def_stmt
|| !can_propagate_from (def_stmt
))
3202 code
= gimple_assign_rhs_code (def_stmt
);
3203 arg0
= gimple_assign_rhs1 (def_stmt
);
3208 /* Two consecutive shuffles. */
3209 if (code
== VEC_PERM_EXPR
)
3216 op3
= gimple_assign_rhs3 (def_stmt
);
3217 if (TREE_CODE (op3
) != VECTOR_CST
)
3219 ident
= is_combined_permutation_identity (op3
, op2
);
3222 orig
= (ident
== 1) ? gimple_assign_rhs1 (def_stmt
)
3223 : gimple_assign_rhs2 (def_stmt
);
3224 gimple_assign_set_rhs1 (stmt
, unshare_expr (orig
));
3225 gimple_assign_set_rhs_code (stmt
, TREE_CODE (orig
));
3226 gimple_set_num_ops (stmt
, 2);
3228 return remove_prop_source_from_use (op0
) ? 2 : 1;
3231 /* Shuffle of a constructor. */
3232 else if (code
== CONSTRUCTOR
|| code
== VECTOR_CST
)
3238 if (TREE_CODE (op0
) == SSA_NAME
&& !single_use_op0
)
3241 if (TREE_CODE (op1
) == VECTOR_CST
)
3243 else if (TREE_CODE (op1
) == SSA_NAME
)
3245 enum tree_code code2
;
3247 gimple def_stmt2
= get_prop_source_stmt (op1
, true, NULL
);
3248 if (!def_stmt2
|| !can_propagate_from (def_stmt2
))
3251 code2
= gimple_assign_rhs_code (def_stmt2
);
3252 if (code2
!= CONSTRUCTOR
&& code2
!= VECTOR_CST
)
3254 arg1
= gimple_assign_rhs1 (def_stmt2
);
3261 /* Already used twice in this statement. */
3262 if (TREE_CODE (op0
) == SSA_NAME
&& num_imm_uses (op0
) > 2)
3266 opt
= fold_ternary (VEC_PERM_EXPR
, TREE_TYPE (op0
), arg0
, arg1
, op2
);
3268 || (TREE_CODE (opt
) != CONSTRUCTOR
&& TREE_CODE (opt
) != VECTOR_CST
))
3270 gimple_assign_set_rhs_from_tree (gsi
, opt
);
3271 update_stmt (gsi_stmt (*gsi
));
3272 if (TREE_CODE (op0
) == SSA_NAME
)
3273 ret
= remove_prop_source_from_use (op0
);
3274 if (op0
!= op1
&& TREE_CODE (op1
) == SSA_NAME
)
3275 ret
|= remove_prop_source_from_use (op1
);
3282 /* Recognize a VEC_PERM_EXPR. Returns true if there were any changes. */
3285 simplify_vector_constructor (gimple_stmt_iterator
*gsi
)
3287 gimple stmt
= gsi_stmt (*gsi
);
3289 tree op
, op2
, orig
, type
, elem_type
;
3290 unsigned elem_size
, nelts
, i
;
3291 enum tree_code code
;
3292 constructor_elt
*elt
;
3296 gcc_checking_assert (gimple_assign_rhs_code (stmt
) == CONSTRUCTOR
);
3298 op
= gimple_assign_rhs1 (stmt
);
3299 type
= TREE_TYPE (op
);
3300 gcc_checking_assert (TREE_CODE (type
) == VECTOR_TYPE
);
3302 nelts
= TYPE_VECTOR_SUBPARTS (type
);
3303 elem_type
= TREE_TYPE (type
);
3304 elem_size
= TREE_INT_CST_LOW (TYPE_SIZE (elem_type
));
3306 sel
= XALLOCAVEC (unsigned char, nelts
);
3309 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (op
), i
, elt
)
3316 if (TREE_CODE (elt
->value
) != SSA_NAME
)
3318 def_stmt
= get_prop_source_stmt (elt
->value
, false, NULL
);
3321 code
= gimple_assign_rhs_code (def_stmt
);
3322 if (code
!= BIT_FIELD_REF
)
3324 op1
= gimple_assign_rhs1 (def_stmt
);
3325 ref
= TREE_OPERAND (op1
, 0);
3333 if (TREE_CODE (ref
) != SSA_NAME
)
3335 if (!useless_type_conversion_p (type
, TREE_TYPE (ref
)))
3339 if (TREE_INT_CST_LOW (TREE_OPERAND (op1
, 1)) != elem_size
)
3341 sel
[i
] = TREE_INT_CST_LOW (TREE_OPERAND (op1
, 2)) / elem_size
;
3342 if (sel
[i
] != i
) maybe_ident
= false;
3348 gimple_assign_set_rhs_from_tree (gsi
, orig
);
3351 tree mask_type
, *mask_elts
;
3353 if (!can_vec_perm_p (TYPE_MODE (type
), false, sel
))
3356 = build_vector_type (build_nonstandard_integer_type (elem_size
, 1),
3358 if (GET_MODE_CLASS (TYPE_MODE (mask_type
)) != MODE_VECTOR_INT
3359 || GET_MODE_SIZE (TYPE_MODE (mask_type
))
3360 != GET_MODE_SIZE (TYPE_MODE (type
)))
3362 mask_elts
= XALLOCAVEC (tree
, nelts
);
3363 for (i
= 0; i
< nelts
; i
++)
3364 mask_elts
[i
] = build_int_cst (TREE_TYPE (mask_type
), sel
[i
]);
3365 op2
= build_vector (mask_type
, mask_elts
);
3366 gimple_assign_set_rhs_with_ops_1 (gsi
, VEC_PERM_EXPR
, orig
, orig
, op2
);
3368 update_stmt (gsi_stmt (*gsi
));
3372 /* Main entry point for the forward propagation and statement combine
3376 ssa_forward_propagate_and_combine (void)
3379 unsigned int todoflags
= 0;
3381 cfg_changed
= false;
3385 gimple_stmt_iterator gsi
;
3387 /* Apply forward propagation to all stmts in the basic-block.
3388 Note we update GSI within the loop as necessary. */
3389 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
3391 gimple stmt
= gsi_stmt (gsi
);
3393 enum tree_code code
;
3395 if (!is_gimple_assign (stmt
))
3401 lhs
= gimple_assign_lhs (stmt
);
3402 rhs
= gimple_assign_rhs1 (stmt
);
3403 code
= gimple_assign_rhs_code (stmt
);
3404 if (TREE_CODE (lhs
) != SSA_NAME
3405 || has_zero_uses (lhs
))
3411 /* If this statement sets an SSA_NAME to an address,
3412 try to propagate the address into the uses of the SSA_NAME. */
3413 if (code
== ADDR_EXPR
3414 /* Handle pointer conversions on invariant addresses
3415 as well, as this is valid gimple. */
3416 || (CONVERT_EXPR_CODE_P (code
)
3417 && TREE_CODE (rhs
) == ADDR_EXPR
3418 && POINTER_TYPE_P (TREE_TYPE (lhs
))))
3420 tree base
= get_base_address (TREE_OPERAND (rhs
, 0));
3423 || decl_address_invariant_p (base
))
3424 && !stmt_references_abnormal_ssa_name (stmt
)
3425 && forward_propagate_addr_expr (lhs
, rhs
, true))
3427 release_defs (stmt
);
3428 gsi_remove (&gsi
, true);
3433 else if (code
== POINTER_PLUS_EXPR
)
3435 tree off
= gimple_assign_rhs2 (stmt
);
3436 if (TREE_CODE (off
) == INTEGER_CST
3437 && can_propagate_from (stmt
)
3438 && !simple_iv_increment_p (stmt
)
3439 /* ??? Better adjust the interface to that function
3440 instead of building new trees here. */
3441 && forward_propagate_addr_expr
3443 build1_loc (gimple_location (stmt
),
3444 ADDR_EXPR
, TREE_TYPE (rhs
),
3445 fold_build2 (MEM_REF
,
3446 TREE_TYPE (TREE_TYPE (rhs
)),
3448 fold_convert (ptr_type_node
,
3451 release_defs (stmt
);
3452 gsi_remove (&gsi
, true);
3454 else if (is_gimple_min_invariant (rhs
))
3456 /* Make sure to fold &a[0] + off_1 here. */
3457 fold_stmt_inplace (&gsi
);
3459 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
3465 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3467 if (forward_propagate_comparison (&gsi
))
3474 /* Combine stmts with the stmts defining their operands.
3475 Note we update GSI within the loop as necessary. */
3476 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);)
3478 gimple stmt
= gsi_stmt (gsi
);
3479 bool changed
= false;
3481 /* Mark stmt as potentially needing revisiting. */
3482 gimple_set_plf (stmt
, GF_PLF_1
, false);
3484 switch (gimple_code (stmt
))
3488 tree rhs1
= gimple_assign_rhs1 (stmt
);
3489 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3491 if ((code
== BIT_NOT_EXPR
3492 || code
== NEGATE_EXPR
)
3493 && TREE_CODE (rhs1
) == SSA_NAME
)
3494 changed
= simplify_not_neg_expr (&gsi
);
3495 else if (code
== COND_EXPR
3496 || code
== VEC_COND_EXPR
)
3498 /* In this case the entire COND_EXPR is in rhs1. */
3499 if (forward_propagate_into_cond (&gsi
)
3500 || combine_cond_exprs (&gsi
))
3503 stmt
= gsi_stmt (gsi
);
3506 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3509 did_something
= forward_propagate_into_comparison (&gsi
);
3510 if (did_something
== 2)
3512 changed
= did_something
!= 0;
3514 else if ((code
== PLUS_EXPR
3515 || code
== BIT_IOR_EXPR
3516 || code
== BIT_XOR_EXPR
)
3517 && simplify_rotate (&gsi
))
3519 else if (code
== BIT_AND_EXPR
3520 || code
== BIT_IOR_EXPR
3521 || code
== BIT_XOR_EXPR
)
3522 changed
= simplify_bitwise_binary (&gsi
);
3523 else if (code
== PLUS_EXPR
3524 || code
== MINUS_EXPR
)
3525 changed
= associate_plusminus (&gsi
);
3526 else if (code
== POINTER_PLUS_EXPR
)
3527 changed
= associate_pointerplus (&gsi
);
3528 else if (CONVERT_EXPR_CODE_P (code
)
3529 || code
== FLOAT_EXPR
3530 || code
== FIX_TRUNC_EXPR
)
3532 int did_something
= combine_conversions (&gsi
);
3533 if (did_something
== 2)
3536 /* If we have a narrowing conversion to an integral
3537 type that is fed by a BIT_AND_EXPR, we might be
3538 able to remove the BIT_AND_EXPR if it merely
3539 masks off bits outside the final type (and nothing
3541 if (! did_something
)
3543 tree outer_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
3544 tree inner_type
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
3545 if (INTEGRAL_TYPE_P (outer_type
)
3546 && INTEGRAL_TYPE_P (inner_type
)
3547 && (TYPE_PRECISION (outer_type
)
3548 <= TYPE_PRECISION (inner_type
)))
3549 did_something
= simplify_conversion_from_bitmask (&gsi
);
3552 changed
= did_something
!= 0;
3554 else if (code
== VIEW_CONVERT_EXPR
)
3555 changed
= simplify_vce (&gsi
);
3556 else if (code
== VEC_PERM_EXPR
)
3558 int did_something
= simplify_permutation (&gsi
);
3559 if (did_something
== 2)
3561 changed
= did_something
!= 0;
3563 else if (code
== BIT_FIELD_REF
)
3564 changed
= simplify_bitfield_ref (&gsi
);
3565 else if (code
== CONSTRUCTOR
3566 && TREE_CODE (TREE_TYPE (rhs1
)) == VECTOR_TYPE
)
3567 changed
= simplify_vector_constructor (&gsi
);
3572 changed
= simplify_gimple_switch (stmt
);
3578 did_something
= forward_propagate_into_gimple_cond (stmt
);
3579 if (did_something
== 2)
3581 changed
= did_something
!= 0;
3587 tree callee
= gimple_call_fndecl (stmt
);
3588 if (callee
!= NULL_TREE
3589 && DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
3590 changed
= simplify_builtin_call (&gsi
, callee
);
3599 /* If the stmt changed then re-visit it and the statements
3600 inserted before it. */
3601 for (; !gsi_end_p (gsi
); gsi_prev (&gsi
))
3602 if (gimple_plf (gsi_stmt (gsi
), GF_PLF_1
))
3604 if (gsi_end_p (gsi
))
3605 gsi
= gsi_start_bb (bb
);
3611 /* Stmt no longer needs to be revisited. */
3612 gimple_set_plf (stmt
, GF_PLF_1
, true);
3619 todoflags
|= TODO_cleanup_cfg
;
3626 gate_forwprop (void)
3628 return flag_tree_forwprop
;
3633 const pass_data pass_data_forwprop
=
3635 GIMPLE_PASS
, /* type */
3636 "forwprop", /* name */
3637 OPTGROUP_NONE
, /* optinfo_flags */
3638 true, /* has_gate */
3639 true, /* has_execute */
3640 TV_TREE_FORWPROP
, /* tv_id */
3641 ( PROP_cfg
| PROP_ssa
), /* properties_required */
3642 0, /* properties_provided */
3643 0, /* properties_destroyed */
3644 0, /* todo_flags_start */
3645 ( TODO_update_ssa
| TODO_verify_ssa
), /* todo_flags_finish */
3648 class pass_forwprop
: public gimple_opt_pass
3651 pass_forwprop (gcc::context
*ctxt
)
3652 : gimple_opt_pass (pass_data_forwprop
, ctxt
)
3655 /* opt_pass methods: */
3656 opt_pass
* clone () { return new pass_forwprop (m_ctxt
); }
3657 bool gate () { return gate_forwprop (); }
3658 unsigned int execute () { return ssa_forward_propagate_and_combine (); }
3660 }; // class pass_forwprop
3665 make_pass_forwprop (gcc::context
*ctxt
)
3667 return new pass_forwprop (ctxt
);