public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <richard.guenther@gmail.com>
To: Jeff Law <law@redhat.com>
Cc: gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: Handle strncpy in tree-ssa-dse.c
Date: Mon, 22 Jul 2019 08:07:00 -0000	[thread overview]
Message-ID: <CAFiYyc00F5a-k+DBHB_8QxzjDiG3ffqYrB+vyW=GBqUB=NnAnA@mail.gmail.com> (raw)
In-Reply-To: <cb770c02-a88c-4aa3-b834-a64e175ae1f4@redhat.com>

On Fri, Jul 19, 2019 at 7:04 PM Jeff Law <law@redhat.com> wrote:
>
>
> While looking at BZ 80576 I realized a few things.
>
> First for STRNCPY we know the exact count of bytes written and we can
> treat it just like MEMCPY and others, both in terms of removing/trimming
> them and in terms of using them to allow removal of other stores.
>
> This patch adds support for those routines in DSE.  We test that
> subsequent statements can make those calls dead and vice versa and that
> we can trim from the head or tail appropriately.
>
> While writing that code I also stumbled over a blob of code that I think
> I copied from tree-ssa-alias.c that isn't necessary.  In the relevant
> code the byte count is always found in the same place.  There's no need
> to check the number of operands to the call to figure out where the
> count would be.  So that little blob of code is simplified ever so slightly.
>
> Finally, while writing the tests for strncpy I stumbled over a case that
> we're still not handling well.
>
> In particular something like this:
>
>
>
> void h (char *s)
> {
>   extern char a[8];
>   __builtin_memset (a, 0, sizeof a);
>   __builtin_strncpy (a, s, sizeof a);
>   frob (a);
> }
>
> In this case ref_maybe_used_by_stmt_p returns true for the "a" array at
> the strncpy call.  AFAICT that appears to happen because  "a" and "s"
> could alias each other.
>
> strncpy is documented as not allowing overlap between the source and
> destination objects.  So in theory we could consider them not aliasing
> for this call.  I haven't implemented this, but I've got some ideas
> here.

But it does allow things like strncpy (&a[0], &a[n+1], n) for example?

I think this kind of specialities should be handled in
ref_maybe_used_by_call_p_1
directly, but I'm not exactly sure how - it's probably another case of
a missing must-alias query, with that we could do

  /* If REF overlaps with the strncpy destination then the source argument
     may not alias it.  */
  if (refs_must_alias_p (ref_for_strncpy_dest, ref))
    return false;

hmm, OTOH for REF covering &a[n/2] to &a[3*n/2] (half overlapping
source and destination) and the above strncpy we have a must-alias
(not kill!) of REF but also are using it.

So it's not so easy and would cover only very specific cases.

>  Anyway, I've included an xfailed test for this case in this patch.
>
> Bootstrapped and regression tested on x86_64, ppc64, ppc64le, aarch64 &
> sparc64.  Installing on the trunk momentarily.
>
> We could in theory handle stpncpy too, we just have to be more careful
> with its return value.
>
> Jeff

  reply	other threads:[~2019-07-22  8:02 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-19 18:24 Jeff Law
2019-07-22  8:07 ` Richard Biener [this message]
2019-07-22 15:11   ` Jeff Law
2019-07-22 15:46     ` Martin Sebor
2019-07-22 23:26       ` Jeff Law

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='CAFiYyc00F5a-k+DBHB_8QxzjDiG3ffqYrB+vyW=GBqUB=NnAnA@mail.gmail.com' \
    --to=richard.guenther@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=law@redhat.com \
    /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).