This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: malloc cannot alias preexisting pointers
On 5/11/19 5:33 PM, Marc Glisse wrote:
Hello,
this patch lets gcc know that if a pointer existed before the call to
malloc, the result of malloc cannot alias it. This is a bit ad hoc and
fragile. A small improvement would be, when the 2 statements are in the
same bb but in the wrong order, to check if there is any statement in
between that might prevent from reordering them. But that's more
complicated, and the patch as it is already does help.
I.e., given the description of attribute malloc:
the pointer P returned by the function cannot alias any other
pointer valid when the function returns, and moreover no pointers
to valid objects occur in any storage addressed by P.
doesn't the same guarantee hold for other functions declared with
the attribute (so that the same optimization could be applied to
them as well)?
Martin
I expect people may not like the call to a function from
tree-ssa-loop-niter too much, but it is convenient. And if someone
improves it, they will likely have to rewrite something not quite
equivalent to stmt_dominates_stmt_p.
The testcase uses libstdc++ quite a bit. I thought about putting it in
the libstdc++ testsuite, but it does not know scan-tree-dump, and in the
assembly it may be too late, memcpy may get expanded to something
target-specific. So it is in g++.dg. I could write a more artificial
testcase, but the behavior of std::vector is really what I want to test...
Bootstrap+regtest on x86_64-pc-linux-gnu.
2019-05-13 Marc Glisse <marc.glisse@inria.fr>
gcc/
* tree-ssa-loop-niter.c (stmt_dominates_stmt_p): Handle NULL
basic block.
* tree-ssa-alias.c: Include tree-ssa-loop-niter.h.
(ptr_derefs_may_alias_p): Detect malloc and an older pointer.
gcc/testsuite/
* g++.dg/tree-ssa/ldist-2.C: New file.