http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57303
--- Comment #7 from rguenther at suse dot de <rguenther at suse dot de> --- On Mon, 20 May 2013, glisse at gcc dot gnu.org wrote: > 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; > + } The obvious place would be dead store elimination. But beware that the above, if not _literally_ being *p = *p can be changing the effective type of the memory location and thus might be not dead in terms of type-based aliasing rules (which basically means that we need to do sth more clever than just removing the store).