public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Richard Biener <richard.guenther@gmail.com>
Cc: Martin Sebor <msebor@gmail.com>, gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] handle "invisible" reference in -Wdangling-pointer (PR104436)
Date: Wed, 9 Feb 2022 10:00:08 -0500	[thread overview]
Message-ID: <f0d3ce55-6351-0c60-5d5f-fae2f2f443ec@redhat.com> (raw)
In-Reply-To: <CAFiYyc2TEtFLM_VEmCTeb-BchqeHi5=4_FPpEnYaFPNLw7x4LQ@mail.gmail.com>

On 2/9/22 03:30, Richard Biener wrote:
> On Tue, Feb 8, 2022 at 11:38 PM Jason Merrill via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
>>
>> On 2/8/22 16:59, Martin Sebor wrote:
>>> Transforming a by-value arguments to by-reference as GCC does for some
>>> class types can trigger -Wdangling-pointer when the argument is used
>>> to store the address of a local variable.  Since the stored value is
>>> not accessible in the caller the warning is a false positive.
>>>
>>> The attached patch handles this case by excluding PARM_DECLs with
>>> the DECL_BY_REFERENCE bit set from consideration.
>>>
>>> While testing the patch I noticed some instances of the warning are
>>> uninitentionally duplicated as the pass runs more than once.  To avoid
>>> that, I also introduce warning suppression into the handler for this
>>> instance of the warning.  (There might still be others.)
>>
>> The second test should verify that we do warn about returning 't' from a
>> function; we don't want to ignore the DECL_BY_REFERENCE RESULT_DECL.
>>
>>> +       tree var = SSA_NAME_VAR (lhs_ref.ref);
>>> +       if (DECL_BY_REFERENCE (var))
> 
> I think you need to test var && TREE_CODE (var) == PARM_DECL here since
> for DECL_BY_REFERENCE RESULT_DECL we _do_ escape to the caller.  Also
> SSA_NAME_VAR var might be NULL.
> 
>>> +         /* Avoid by-value arguments transformed into by-reference.  */
>>> +         continue;
>>
>> I wonder if we can we express this property of invisiref parms somewhere
>> more general?  I imagine optimizations would find it useful as well.
>> Could pointer_query somehow treat the reference as pointing to a
>> function-local object?
> 
> I think points-to analysis got this correct when the reference was marked
> restrict but now it also fails at this, making DSE fail to eliminate the
> store in
> 
> struct A { A(); ~A(); int *p; };
> 
> void foo (struct A a, int *p)
> {
>    a.p = p;
> }

Well, that's conservatively correct; since we don't know the definition 
of ~A, we don't know whether it copies p somewhere, e.g.

int *global_p;
A::~A() { global_p = p; }

in which case eliminating the store would be an invalid optimization, 
just as it would be if 'a' were a local variable.

>> I previously tried to express this by marking the reference as
>> 'restrict', but that was wrong
>> (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97474).


  reply	other threads:[~2022-02-09 15:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-08 21:59 Martin Sebor
2022-02-08 22:37 ` Jason Merrill
2022-02-09  8:30   ` Richard Biener
2022-02-09 15:00     ` Jason Merrill [this message]
2022-02-10 23:04   ` Martin Sebor
2022-03-01 23:14     ` PING " Martin Sebor
2022-03-09 13:17     ` Richard Biener
2022-03-16 15:47       ` Martin Sebor

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=f0d3ce55-6351-0c60-5d5f-fae2f2f443ec@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=msebor@gmail.com \
    --cc=richard.guenther@gmail.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).