From: "Vaivaswatha Nagaraj" <vn@compilertree.com>
To: "Hal Finkel" <hfinkel@anl.gov>
Cc: "LLVM Dev" <llvm-dev@lists.llvm.org>, "James Molloy" <james@jamesmolloy.co.uk>
Sent: Thursday, December 3, 2015 9:20:16 AM
Subject: Re: [llvm-dev] Function attributes for LibFunc and its impact on GlobalsAA
Hi Hal,
>malloc hooks are an interesting point, but those are not standard,
>not commonly used
I very much agree with this.
>readonly is a more-interesting question, because, in practice, this
>will currently work. It works, however, for the wrong reason
Rather than read-only, could we mark malloc/free etc with
onlyAccessesArgMem()? GlobalsAA would just need a simple check to
ignore such functions (along with read-only which it already is
checking for) during propagation along the call graph. As a
reference, I'm attaching a prototype patch (This patch is on
release37 unfortunately, but is applicable verbatim to the latest
svn version).
The semantics here are not quite right. These functions don't only access memory based on their pointer arguments, but also other global state. It just happens to be that this global state is not something with which any accesses in user code can alias.
For example, in your patch, you're adding argmemonly to printf. This is not right:
void foo(char * restrict s1, char * restrict s2) {
printf(s1);
printf(s2);
}
If printf is argmemonly, then we could interchange the two printf calls. For malloc this is still a problem, in the following sense, if we have:
p1 = malloc(really_big);
...
free(p1);
p2 = malloc(really_big);
...
free(p2);
allowing a transformation into:
p1 = malloc(really_big);
p2 = malloc(really_big);
...
free(p1); free(p2);
could be problematic.
I understand what you're trying to do, but I think you need to introduce a new attribute to do it. We don't currently have one that fits. You kind of want something like argmem_and_inaccessible_state_only (bikeshedding aside). I'm in favor of adding such an attribute, it seems like it could be applied to many things. I suggest you precisely define the semantics you want in a new RFC, give some examples of the functions to which it might apply, and discuss where/how it will be used by analysis/transformations.
-Hal