This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: Make tree-ssa-strlen.c handle partial unterminated strings
- From: Jakub Jelinek <jakub at redhat dot com>
- To: gcc-patches at gcc dot gnu dot org, richard dot sandiford at linaro dot org
- Date: Fri, 5 May 2017 18:01:53 +0200
- Subject: Re: Make tree-ssa-strlen.c handle partial unterminated strings
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=jakub at redhat dot com
- Dkim-filter: OpenDKIM Filter v2.11.0 mx1.redhat.com E24724AE9A
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com E24724AE9A
- References: <87efw3sebf.fsf@linaro.org>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Fri, May 05, 2017 at 01:01:08PM +0100, Richard Sandiford wrote:
> tree-ssa-strlen.c looks for cases in which a string is built up using
> operations like:
>
> memcpy (a, "foo", 4);
> memcpy (a + 3, "bar", 4);
> int x = strlen (a);
>
> As a side-effect, it optimises the non-final memcpys so that they don't
> include the nul terminator.
>
> However, after removing some "& ~0x1"s from tree-ssa-dse.c, the DSE pass
> does this optimisation itself (because it can tell that later memcpys
> overwrite the terminators). The strlen pass wasn't able to handle these
> pre-optimised calls in the same way as the unoptimised ones.
>
> This patch adds support for tracking unterminated strings.
I'm not sure I like the terminology (terminated vs. !terminated), I wonder
if it wouldn't be better to add next to length field minimum_length field,
length would be what it is now, tree representing the string length,
while minimum_length would be just a guarantee that strlen (ptr) >=
minimum_length, i.e. that in the first minimum_length bytes (best would be
to guarantee that it is just a constant if non-NULL) are non-zero.
It shouldn't be handled just by non-zero terminated memcpy, but e.g. even if
you e.g. construct it byte by byte, etc.
a[0] = 'a';
a[1] = 'b';
a[2] = 'c';
a[3] = 'd';
a[4] = '\0';
x = strlen (a);
etc., or
strcpy (a, "abcdefg");
strcpy (a + 8, "hijk");
a[7] = 'q';
x = strlen (a);
or say by storing 4 non-zero bytes at a time...
Jakub