On Sat, Apr 16, 2016 at 11:32 PM, Mikhail Maltsev <malts...@gmail.com> wrote: > Hi, all! > > Currently GCC can optimize away the following dead store: > > void test(char *x) > { > *x = 1; > free(x); > } > > but not this one (Clang handles both cases): > > void test(char *x) > { > *x = 1; > delete x; > } > > The attached patch fixes this by introducing a new __attribute__((free)). I > first tried to add new built-ins for each version of operator delete (there > are > four of them), but it looked a little clumsy, and would require some special > handling for warning about taking address of built-in function. > > Is such approach (i.e. adding a new attribute) OK? Bootstrapped and regtested > on > x86_64-pc-linux-gnu.
Enlarging tree_function_decl is bad. I think as you made this a decl attribute it won't work for virtual delete methods which are called indirectly. Passes should get at the info via flags_from_decl_or_type () and a new ECF_FREE. Richard. > -- > Regards, > Mikhail Maltsev > > gcc/c/ChangeLog: > > 2016-04-16 Mikhail Maltsev <malts...@gmail.com> > > * c-decl.c (merge_decls): Handle free_flag. > > gcc/ChangeLog: > > 2016-04-16 Mikhail Maltsev <malts...@gmail.com> > > * builtin-attrs.def: Add attribute free. > * builtins.def (free): Add attribute free. > * doc/extend.texi: Document attribute free. > * gtm-builtins.def (_ITM_free): Add attribute free. > * tree-core.h (struct tree_function_decl): Add free_flag. > * tree-ssa-alias.c (ref_maybe_used_by_call_p_1): Handle free_flag. > (call_may_clobber_ref_p_1): Likewise. > (stmt_kills_ref_p): Likewise. > * tree-streamer-in.c (unpack_ts_function_decl_value_fields): Likewise. > * tree-streamer-out.c (pack_ts_function_decl_value_fields): Likewise. > * tree.h (DECL_IS_FREE): New accessor macro. > > gcc/testsuite/ChangeLog: > > 2016-04-16 Mikhail Maltsev <malts...@gmail.com> > > * g++.dg/opt/op-delete-dse.C: New test. > * gcc.dg/attr-free.c: New test. > > gcc/c-family/ChangeLog: > > 2016-04-16 Mikhail Maltsev <malts...@gmail.com> > > > > * c-common.c (handle_free_attribute): New function. > > > > > > gcc/cp/ChangeLog: > > > > > > 2016-04-16 Mikhail Maltsev <malts...@gmail.com> > > > > > > * decl.c (cxx_init_decl_processing): Set flag_free for operator > delete.