This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [RFC][PATCH] Extend DCE to remove unnecessary new/delete-pairs
- From: Trevor Saunders <tbsaunde at tbsaunde dot org>
- To: Martin Sebor <msebor at gmail dot com>
- Cc: Jakub Jelinek <jakub at redhat dot com>, Dominik Inführ <dominik dot infuehr at theobroma-systems dot com>, Richard Biener <richard dot guenther at gmail dot com>, GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Mon, 4 Dec 2017 07:20:10 -0500
- Subject: Re: [RFC][PATCH] Extend DCE to remove unnecessary new/delete-pairs
- Authentication-results: sourceware.org; auth=none
- References: <8305B5F4-2A96-4698-8C2E-3255658B5C12@theobroma-systems.com> <CAFiYyc2nZ4vSGa5d_ni0km2kwUtyd9+BScrKzxKdbhZV=t6d1g@mail.gmail.com> <20171122103742.GN14653@tucnak> <BC60F078-9257-4E4F-8D94-7C41F7C7B802@theobroma-systems.com> <ec89be92-a232-5e34-e482-493b9babb65b@gmail.com> <20171129083045.GX2353@tucnak> <f6e7d0ed-4709-5e66-5bec-16d4e7c61ac0@gmail.com>
On Wed, Nov 29, 2017 at 08:56:44AM -0700, Martin Sebor wrote:
> On 11/29/2017 01:30 AM, Jakub Jelinek wrote:
> > On Tue, Nov 28, 2017 at 09:11:00PM -0700, Martin Sebor wrote:
> > > On 11/27/2017 02:22 AM, Dominik Inführ wrote:
> > > > Thanks for all the reviews! I’ve revised the patch, the operator_delete_flag is now stored in tree_decl_with_vis (there already seem to be some FUNCTION_DECL-flags in there). I’ve also added the option -fallocation-dce to disable this optimization. It bootstraps and no regressions on aarch64 and x86_64.
> > > >
> > > It's great to be able to eliminate pairs of these calls. For
> > > unpaired calls, though, I think it would be even more useful to
> > > also issue a warning. Otherwise the elimination will mask bugs
> >
> > ?? I hope you're only talking about allocation where the returned
> > pointer can't leak elsewhere, doing allocation in one function
> > (e.g. constructor, or whatever other function) and deallocation in some
> > other one is so common such a warning would be not just useless, but
> > harmful with almost all occurrences being false positives.
> >
> > Warning on malloc/standard operator new or malloc/realloc-like function
> > when the return pointer can't escape the current function is reasonable.
>
> Yes, warn for leaks, or for calls to delete/free with no matching
> new/malloc (when they can be detected).
>
> From the test case included in the patch, warn on the first two
> of the following three functions:
>
> +++ b/gcc/testsuite/g++.dg/cpp1y/new1.C
> @@ -0,0 +1,65 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-cddce-details" } */
> +
> +#include <stdlib.h>
> +
> +void
> +new_without_use() {
> + int *x = new int;
> +}
> +
> +void
> +new_array_without_use() {
> + int *x = new int[5];
> +}
> +
> +void
> +new_primitive() {
> + int *x = new int;
> + delete x;
> +}
>
> An obvious extension to such a checker would then be to also detect
> possible invalid deallocations, as in:
>
> void f (unsigned n)
> {
> void *p = n < 256 ? alloca (n) : malloc (n);
> // ...
> free (p);
> }
>
> David Malcolm was working on something like that earlier this year
> so he might have some thoughts on this as well.
I also sent https://gcc.gnu.org/ml/gcc-patches/2017-05/msg00491.html a
while back, sorry I haven't gotten to improving it yet though its gcc 9
stuff at this point I guess.
thanks
Trev
>
> Martin