public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jeff Law <law@redhat.com>
To: Jakub Jelinek <jakub@redhat.com>
Cc: Segher Boessenkool <segher@kernel.crashing.org>,
	       Richard Biener <rguenther@suse.de>,
	       Eric Botcazou <ebotcazou@adacore.com>,
	gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] Reenable CSE of non-volatile inline asm (PR rtl-optimization/63637)
Date: Tue, 13 Jan 2015 22:28:00 -0000	[thread overview]
Message-ID: <54B59964.7070707@redhat.com> (raw)
In-Reply-To: <20150113201322.GJ1405@tucnak.redhat.com>

On 01/13/15 13:13, Jakub Jelinek wrote:
> On Tue, Jan 13, 2015 at 12:45:27PM -0700, Jeff Law wrote:
>> On 01/13/15 09:38, Segher Boessenkool wrote:
>>> On Tue, Jan 13, 2015 at 05:18:19PM +0100, Jakub Jelinek wrote:
>>>> 3) on request from Richard (which Segher on IRC argues against), "memory"
>>>>     clobber also prevents CSE;
>>>
>>> As extend.texi used to say:
>>>
>>> "
>>> If your assembler instructions access memory in an unpredictable
>>> fashion, add @samp{memory} to the list of clobbered registers.  This
>>> causes GCC to not keep memory values cached in registers across the
>>> assembler instruction and not optimize stores or loads to that memory.
>>> You also should add the @code{volatile} keyword if the memory
>>> affected is not listed in the inputs or outputs of the @code{asm}, as
>>> the @samp{memory} clobber does not count as a side-effect of the
>>> @code{asm}.
>>> "
>>>
>>> so a "memory" clobber in a non-volatile asm should not prevent CSE.
>> My reading of that paragraph is somewhat different.
>>
>> The key here is the memory clobber affects optimization of instructions
>> around the asm while the volatile specifier affects the optimization of the
>> ASM itself.
>>
>> A memory clobber must inhibit CSE of memory references on either side of the
>> asm because the asm must be assumed to read or write memory in unpredictable
>> ways.
>>
>> The volatile specifier tells the compiler that the asm itself must be
>> preserved, even if dataflow shows the outputs as not used.
>
> That is not necessarily in conflict.
Possibly not :-)  This stuff isn't trivial and as well meaning as folks 
trying to update the docs have been, it's possible subtle issues like 
these have been missed, even in the review process.

I know that for me it's easier to reason about code changes like this 
than their associated documentation :-)


> My reading of Jeff's comment is that in
> int a;
> int
> foo (void)
> {
>    int b, c, d, e;
>    b = a;
>    asm ("..." : "=r" (c) : : "memory");
>    d = a;
>    asm ("..." : "=r" (e) : : "memory");
>    return b + d + 2 * (c + e);
> }
> we are not allowed to CSE d = a; into d = b;
Precisely.  At least that's how I read things and it makes sense to the 
part of my brain that used to split time between kernel & GCC 
development in a previous life.

In effect the "memory" clobber is an aggregation of the read, write, 
clobbers dataflow for memory (and imprecise as it hits all memory).


.  CSE invalidate_from_clobbers
> should ensure that already, even when we don't do anything special about
> "memory" clobber in the patch.
OK.


   Another thing is if there is a store
> in between the two non-volatile asms with "memory" clobber, here I'm not
> sure if with the alternate patch we'd treat the "memory" clobber as use of
> everything previously stored into memory (in this regard the posted version
> is safe).
I woudln't be terribly surprised if DSE isn't safe in this regard.  I 
don't recall CSE doing any kind of dead store elimination so it wouldn't 
likely care that the memory clobber implies a read as well.



> And finally there is the case of non-volatile asm with "memory" clobber with
> no memory stores in between the two - the posted (safer) patch will not
> allow to CSE the two, while in theory we could CSE them into just one asm.
I think we have to assume that CSEing them is wrong.  The first may set 
something in memory that is read by the second.

Thoughts?


Jeff



  reply	other threads:[~2015-01-13 22:17 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-13 16:22 Jakub Jelinek
2015-01-13 17:06 ` Segher Boessenkool
2015-01-13 20:02   ` Jeff Law
2015-01-13 20:29     ` Jakub Jelinek
2015-01-13 22:28       ` Jeff Law [this message]
2015-01-14  3:44         ` Segher Boessenkool
2015-01-14  6:52           ` Jeff Law
2015-01-14 15:40             ` Segher Boessenkool
2015-01-15  6:46               ` Jeff Law
2015-01-15  7:54                 ` Richard Biener
2015-01-15  8:40                   ` Jakub Jelinek
2015-01-15  8:43                     ` Richard Biener
2015-01-15  9:50                     ` Jakub Jelinek
2015-01-15 18:22                     ` Jeff Law
2015-01-23 21:39                     ` Richard Henderson
2015-01-23 22:53                       ` Segher Boessenkool
2015-01-23 23:12                         ` Jakub Jelinek
2015-01-24  7:23                           ` Segher Boessenkool
2015-01-24 14:39                             ` Richard Sandiford
2015-01-13 22:42     ` Segher Boessenkool
2015-01-14  0:40       ` Segher Boessenkool
2015-01-14  7:12 ` 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=54B59964.7070707@redhat.com \
    --to=law@redhat.com \
    --cc=ebotcazou@adacore.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=rguenther@suse.de \
    --cc=segher@kernel.crashing.org \
    /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).