http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57303
--- Comment #6 from Marc Glisse <glisse at gcc dot gnu.org> --- I wonder if, in addition to fixing the sink pass, we should add an optimization like the following (it passes bootstrap+testsuite, but I am not so sure where it should go and what it should look like exactly). --- gimple-fold.c (revision 199093) +++ gimple-fold.c (working copy) @@ -1174,20 +1174,27 @@ fold_stmt_1 (gimple_stmt_iterator *gsi, if ((commutative_tree_code (subcode) || commutative_ternary_tree_code (subcode)) && tree_swap_operands_p (gimple_assign_rhs1 (stmt), gimple_assign_rhs2 (stmt), false)) { tree tem = gimple_assign_rhs1 (stmt); gimple_assign_set_rhs1 (stmt, gimple_assign_rhs2 (stmt)); gimple_assign_set_rhs2 (stmt, tem); changed = true; } + /* Remove *p = *p. */ + if (!inplace && TREE_CODE_CLASS (subcode) == tcc_reference + && operand_equal_p (lhs, gimple_assign_rhs1 (stmt), 0)) + { + gsi_remove (gsi, true); + return true; + } new_rhs = fold_gimple_assign (gsi); if (new_rhs && !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (new_rhs))) new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs); if (new_rhs && (!inplace || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops)) { gimple_assign_set_rhs_from_tree (gsi, new_rhs);