public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jeff Law <law@redhat.com>
To: Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org>,
	gcc Patches <gcc-patches@gcc.gnu.org>,
	Jan Hubicka <hubicka@ucw.cz>, Richard Biener <rguenther@suse.de>
Subject: Re: [RFC] propagate malloc attribute in ipa-pure-const pass
Date: Tue, 16 May 2017 01:14:00 -0000	[thread overview]
Message-ID: <dc281aa2-94fa-cb8e-f754-c8cffd790c9a@redhat.com> (raw)
In-Reply-To: <CAAgBjMkaTCAvwO6AVKu5C6_-TqpuQtTcs0EqYHqogDhru3juYg@mail.gmail.com>

On 05/15/2017 04:39 AM, Prathamesh Kulkarni wrote:
> Hi,
> I have attached a bare-bones prototype patch that propagates malloc attribute in
> ipa-pure-const. As far as I understand, from the doc a function could
> be annotated
> with malloc attribute if it returns a pointer to a newly allocated
> memory block (uninitialized or zeroed) and the pointer does not alias
> any other valid pointer ?
> 
> * Analysis
> The analysis used by the patch in malloc_candidate_p is too conservative,
> and I would be grateful for suggestions on improving it.
> Currently it only checks if:
> 1) The function returns a value of pointer type.
> 2) SSA_NAME_DEF_STMT (return_value) is either a function call or a phi, and
>      SSA_NAME_DEF_STMT(each element of phi) is function call.
> 3) The return-value has immediate uses only within comparisons (gcond
> or gassign) and return_stmt (and likewise a phi arg has immediate use only
> within comparison or the phi stmt).
ISTM the return value *must* be either the result of a call to a 
function with the malloc attribute or NULL.  It can't be a mix of 
malloc'd objects or something else (such as a passed-in object).

> 
> malloc_candidate_p would determine f to be a candidate for malloc
> attribute since it returns the return-value of it's callee
> (__builtin_malloc) and the return value is used only within comparison
> and return_stmt.
> 
> However due to the imprecise analysis it misses this case:
> char  **g(size_t n)
> {
>    char **p = malloc (sizeof(char *) * 10);
>    for (int i = 0; i < 10; i++)
>      p[i] = malloc (sizeof(char) * n);
>    return p;
> }
> I suppose g() could be annotated with malloc here ?
I think that's up to us to decide.  So the question becomes what makes 
the most sense from a user and optimization standpoint.


I would start relatively conservatively and look to add further analysis 
to detect more malloc functions over time.  You've already identified 
comparisons as valid uses, but ISTM the pointer could be passed as an 
argument, stored into memory and all kinds of other things.

You'll probably want instrumentation to log cases where something looked 
like a potential candidate, but had to be rejected for some reason.

Jeff

  reply	other threads:[~2017-05-16  1:12 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-15 10:56 Prathamesh Kulkarni
2017-05-16  1:14 ` Jeff Law [this message]
2017-05-16 11:48 ` Richard Biener
2017-05-17 21:22 ` Martin Sebor
2017-05-18  7:07   ` Richard Biener
2017-05-19 13:18     ` Jan Hubicka
2017-05-19 13:34 ` Jan Hubicka
2017-05-23 13:48   ` Prathamesh Kulkarni
2017-07-31 18:23     ` Prathamesh Kulkarni
2017-08-08  4:21       ` Prathamesh Kulkarni
2017-08-17 12:55         ` Prathamesh Kulkarni
2017-09-01  2:39           ` Prathamesh Kulkarni
2017-09-15 12:19             ` Prathamesh Kulkarni
2017-09-25 18:13               ` Prathamesh Kulkarni
2017-09-26  0:24                 ` Jan Hubicka
2017-09-27  1:11                   ` Prathamesh Kulkarni
2017-09-29 19:28                     ` Jan Hubicka
2017-10-06  2:16                       ` Prathamesh Kulkarni
2017-10-06 13:04                         ` Jan Hubicka
2017-10-07  1:46                           ` Prathamesh Kulkarni
2017-10-07 19:35                             ` Jan Hubicka
2017-10-07 22:17                               ` Prathamesh Kulkarni
2017-10-13 23:34                                 ` Prathamesh Kulkarni
2017-10-23  9:37                                   ` Prathamesh Kulkarni
2017-10-24 10:57                                   ` Jan Hubicka
2017-10-25 11:18                                     ` Prathamesh Kulkarni
2017-10-25 15:26                                       ` Jan Hubicka
2017-10-27 10:52                                         ` Prathamesh Kulkarni
2017-10-27 12:20                                           ` Jan Hubicka
2017-10-27 12:44                                           ` Jan Hubicka
2017-10-27 13:00                                             ` Richard Biener

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=dc281aa2-94fa-cb8e-f754-c8cffd790c9a@redhat.com \
    --to=law@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=hubicka@ucw.cz \
    --cc=prathamesh.kulkarni@linaro.org \
    --cc=rguenther@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).