public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <richard.guenther@gmail.com>
To: gcc@gcc.gnu.org,Peter Bergner <bergner@vnet.ibm.com>,Jeff Law
	<law@redhat.com>
Cc: GCC <gcc@gcc.gnu.org>,Vladimir N Makarov
	<vmakarov@redhat.com>,Pat Haugen <pthaugen@linux.vnet.ibm.com>
Subject: Re: Why does IRA force all pseudos live across a setjmp call to be spilled?
Date: Sun, 04 Mar 2018 08:57:00 -0000	[thread overview]
Message-ID: <0F51D9FD-7F6C-453E-80FB-BE782B2E3C90@gmail.com> (raw)
In-Reply-To: <3f6c4ed0-faaa-bfe1-3db0-527555572ab0@vnet.ibm.com>

On March 4, 2018 1:30:39 AM GMT+01:00, Peter Bergner <bergner@vnet.ibm.com> wrote:
>On 3/3/18 5:47 PM, Peter Bergner wrote:
>> On 3/3/18 10:29 AM, Jeff Law wrote:
>>> Here's the comment from regstat.c:
>>>
>>>               /* We have a problem with any pseudoreg that lives
>>>                  across the setjmp.  ANSI says that if a user
>variable
>>>                  does not change in value between the setjmp and the
>>>                  longjmp, then the longjmp preserves it.  This
>>>                  includes longjmp from a place where the pseudo
>>>                  appears dead.  (In principle, the value still
>exists
>>>                  if it is in scope.)  If the pseudo goes in a hard
>>>                  reg, some other value may occupy that hard reg
>where
>>>                  this pseudo is dead, thus clobbering the pseudo.
>>>                  Conclusion: such a pseudo must not go in a hard
>>>                  reg.  */
>> 
>> I can't argue with anything in that comment, other than the
>conclusion. :-)
>> It's not the compiler's job to implement the setjmp/longjmp
>save/restore.
>> Maybe Kenny was working around a problem with some target's buggy
>setjmp
>> and spilling everything "fixed" it?
>
>The only observable difference I can see between a variable that has
>been
>spilled to memory versus one that is assigned to a non-volatile hard
>reg
>is if it is modified between the setjmp and the longjmp.  In the case
>where the variable is spilled to memory, the "new" updated value is the
>value you _may_ see on the return from setjmp (the return caused by the
>call to longjmp), whereas if it is assigned to a non-volatile register,
>then you _will_ see the "old" value that was saved by the setjmp call.
>I say _may_ see above, because there are cases were we might not store
>the "new" updated value to memory, even if we've spilled the pseudo.
>Examples would be spill code optimization, or the variable has been
>broken into separate live ranges/pseudos. etc. etc.  I guess I can even
>think of cases where we could see both "old" and "new" values of a
>variable.  Think of a variable that has been spilled/split like below:
>
>  a = <old value> [start of live range, a assigned to non-volatile reg]
>    spill store a
>    ...
>    setjmp()
>    ...
>1)  ... = ... a ...     [end of live range]
>    ...                 [a not assigned to a reg in this region]
>    spill load a        [start of live range]
>2)  ... = ... a ...     [end of live range]
>    ...
>    if (...)
>       a = <new value>  [start of live range]
>3)     spill store a    [end of live range]
>    ...                 [a not assigned to a reg in this region]
>    longjmp()
>
>
>On return from setjmp (the return caused by the call to longjmp),
>the use of "a" at "1)" will use the non-volatile hard register
>that was saved by the initial call to setjmp, so it will see the
>"old" value of "a".  However, since the use of "a" at "2)" loads
>the value from memory, it will use the "new" value stored by
>the spill load at "3)"!
>
>That said, the comment above only talks about variables that do not
>change between the setjmp and the longjmp and in that case, you will
>see the same "old" value (which is the only value, since it wasn't
>modified) regardless of whether it was spilled or not.
>
>What does ANSI (or any spec) say about what should happen to variables
>that are modified between the setjmp and longjmp calls?  Maybe all bets
>are off, given the example above, since even spilling a variable live
>across a setjmp can still lead to strange behavior unless you don't
>allow spill/split optimization and I don't think we'd want that at all.

I think posix says you have to mark such variables volatile. So I fully agree with your analysis of why setjmp isn't special for RA. It would be only making non-conforming code work by accident. 

Richard. 


>Peter

  reply	other threads:[~2018-03-04  8:57 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-02 19:45 Peter Bergner
2018-03-02 19:55 ` Alexander Monakov
2018-03-02 21:26 ` Jeff Law
2018-03-03  3:28   ` Peter Bergner
2018-03-03 16:29     ` Jeff Law
2018-03-03 23:47       ` Peter Bergner
2018-03-04  0:30         ` Peter Bergner
2018-03-04  8:57           ` Richard Biener [this message]
2018-03-04  9:11             ` Andreas Schwab
2018-03-05  2:12             ` Jeff Law
2018-03-04 13:57         ` Eric Botcazou
2018-03-04 16:40           ` Peter Bergner
2018-03-04 17:39             ` Andreas Schwab
2018-03-05  0:16           ` Segher Boessenkool
2018-03-05  7:01             ` Eric Botcazou
2018-03-05  7:09               ` Eric Botcazou
2018-03-05 15:33               ` Segher Boessenkool
2018-03-05 16:18                 ` Eric Botcazou
2018-03-06 16:20                 ` Peter Bergner
2018-03-03  3:21 ` Vladimir Makarov

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=0F51D9FD-7F6C-453E-80FB-BE782B2E3C90@gmail.com \
    --to=richard.guenther@gmail.com \
    --cc=bergner@vnet.ibm.com \
    --cc=gcc@gcc.gnu.org \
    --cc=law@redhat.com \
    --cc=pthaugen@linux.vnet.ibm.com \
    --cc=vmakarov@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).