public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* tail calls in const functions?
@ 2000-03-23  6:24 Jan Hubicka
  2000-03-23  6:29 ` Jakub Jelinek
  2000-03-24  2:22 ` Richard Henderson
  0 siblings, 2 replies; 7+ messages in thread
From: Jan Hubicka @ 2000-03-23  6:24 UTC (permalink / raw)
  To: egcs

Hi
The tail call optimization makes function to modify the stack, that
belongs to the caller.  Is this valid for const function?
IMO gcc has right to optimize out:
int test()
{
return c(1) + d(1);
}
the second store of 1, in case c is const function (it don't do that currently).
When tail call optimization is done for c, the stack is clobbered.

So perhaps it will be neccesary to disable tail call for const functions.

Honza

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: tail calls in const functions?
  2000-03-23  6:24 tail calls in const functions? Jan Hubicka
@ 2000-03-23  6:29 ` Jakub Jelinek
  2000-03-23  6:43   ` Jan Hubicka
  2000-03-24  2:22 ` Richard Henderson
  1 sibling, 1 reply; 7+ messages in thread
From: Jakub Jelinek @ 2000-03-23  6:29 UTC (permalink / raw)
  To: Jan Hubicka; +Cc: egcs

On Thu, Mar 23, 2000 at 03:24:31PM +0100, Jan Hubicka wrote:
> Hi
> The tail call optimization makes function to modify the stack, that
> belongs to the caller.  Is this valid for const function?
> IMO gcc has right to optimize out:
> int test()
> {
> return c(1) + d(1);
> }
> the second store of 1, in case c is const function (it don't do that currently).

This is actually a wrong example, because none of these functions can be
a tail call (the parent routine has to sum the two returned values up).

Cheers,
    Jakub
___________________________________________________________________
Jakub Jelinek | jakub@redhat.com | http://sunsite.mff.cuni.cz/~jj
Linux version 2.3.99-pre2 on a sparc64 machine (1343.49 BogoMips)
___________________________________________________________________

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: tail calls in const functions?
  2000-03-23  6:29 ` Jakub Jelinek
@ 2000-03-23  6:43   ` Jan Hubicka
  2000-03-23  9:09     ` Jeffrey A Law
  0 siblings, 1 reply; 7+ messages in thread
From: Jan Hubicka @ 2000-03-23  6:43 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: egcs

> On Thu, Mar 23, 2000 at 03:24:31PM +0100, Jan Hubicka wrote:
> > Hi
> > The tail call optimization makes function to modify the stack, that
> > belongs to the caller.  Is this valid for const function?
> > IMO gcc has right to optimize out:
> > int test()
> > {
> > return c(1) + d(1);
> > }
> > the second store of 1, in case c is const function (it don't do that currently).
> 
> This is actually a wrong example, because none of these functions can be
> a tail call (the parent routine has to sum the two returned values up).
The problem is not in function test, but in the function c.
Imagine function c like this one:

int
__attribute__ ((const))
c(int a)
{
  return e(a+1);
}

The c (after tail-call optimizations done) will clobber the stack
created by function test.

Honza
> 
> Cheers,
>     Jakub
> ___________________________________________________________________
> Jakub Jelinek | jakub@redhat.com | http://sunsite.mff.cuni.cz/~jj
> Linux version 2.3.99-pre2 on a sparc64 machine (1343.49 BogoMips)
> ___________________________________________________________________

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: tail calls in const functions?
  2000-03-23  6:43   ` Jan Hubicka
@ 2000-03-23  9:09     ` Jeffrey A Law
  2000-03-23 14:20       ` Jan Hubicka
  0 siblings, 1 reply; 7+ messages in thread
From: Jeffrey A Law @ 2000-03-23  9:09 UTC (permalink / raw)
  To: Jan Hubicka; +Cc: Jakub Jelinek, egcs

  In message < 20000323154300.D2277@atrey.karlin.mff.cuni.cz >you write:
  > > On Thu, Mar 23, 2000 at 03:24:31PM +0100, Jan Hubicka wrote:
  > > > Hi
  > > > The tail call optimization makes function to modify the stack, that
  > > > belongs to the caller.  Is this valid for const function?
I would think so -- it's only going to diddle in the parameter area.

One way to think about a const function is does it read/write *state* that
is needed across invocations of the function or which are significant for
the behavior of other code in the program.

In this case we're modifying stack slots that will not be read again anyway;
think of those slots as pass-by-value parameters.  We can stomp on the contents
of those slots all we want since we know their value will never be used again.

jeff

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: tail calls in const functions?
  2000-03-23  9:09     ` Jeffrey A Law
@ 2000-03-23 14:20       ` Jan Hubicka
  2000-03-23 14:47         ` Jeffrey A Law
  0 siblings, 1 reply; 7+ messages in thread
From: Jan Hubicka @ 2000-03-23 14:20 UTC (permalink / raw)
  To: Jeffrey A Law; +Cc: Jakub Jelinek, egcs

> 
>   In message < 20000323154300.D2277@atrey.karlin.mff.cuni.cz >you write:
>   > > On Thu, Mar 23, 2000 at 03:24:31PM +0100, Jan Hubicka wrote:
>   > > > Hi
>   > > > The tail call optimization makes function to modify the stack, that
>   > > > belongs to the caller.  Is this valid for const function?
> I would think so -- it's only going to diddle in the parameter area.
> 
> One way to think about a const function is does it read/write *state* that
> is needed across invocations of the function or which are significant for
> the behavior of other code in the program.
> 
> In this case we're modifying stack slots that will not be read again anyway;
> think of those slots as pass-by-value parameters.  We can stomp on the contents
> of those slots all we want since we know their value will never be used again.
OK.
I was bringing this issue mainly because I was unsure whther we want to read it
again or not.  In the example I gave the caller was storing value "1" twice to
the same position once for first function, later for the second.  I was unsure
whther we would not want to implement later pass that will eliminate the second
sture.

With const fuctions modifying their arguemnt area we will have to hack
this future pass for such special case.

Honza

> jeff

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: tail calls in const functions?
  2000-03-23 14:20       ` Jan Hubicka
@ 2000-03-23 14:47         ` Jeffrey A Law
  0 siblings, 0 replies; 7+ messages in thread
From: Jeffrey A Law @ 2000-03-23 14:47 UTC (permalink / raw)
  To: Jan Hubicka; +Cc: Jakub Jelinek, egcs

  In message < 20000323232003.B5316@atrey.karlin.mff.cuni.cz >you write:
  > OK.
  > I was bringing this issue mainly because I was unsure whther we want to rea
  > d it
  > again or not.  In the example I gave the caller was storing value "1" twice
  >  to
  > the same position once for first function, later for the second.  I was uns
  > ure
  > whther we would not want to implement later pass that will eliminate the se
  > cond
  > sture.
  > 
  > With const fuctions modifying their arguemnt area we will have to hack
  > this future pass for such special case.
We must always consider the argument area clobbered by a call, even to a
const function.

jeff

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: tail calls in const functions?
  2000-03-23  6:24 tail calls in const functions? Jan Hubicka
  2000-03-23  6:29 ` Jakub Jelinek
@ 2000-03-24  2:22 ` Richard Henderson
  1 sibling, 0 replies; 7+ messages in thread
From: Richard Henderson @ 2000-03-24  2:22 UTC (permalink / raw)
  To: Jan Hubicka; +Cc: egcs

On Thu, Mar 23, 2000 at 03:24:31PM +0100, Jan Hubicka wrote:
> The tail call optimization makes function to modify the stack, that
> belongs to the caller.  Is this valid for const function?

It doesn't belong to the caller.  Not any more than
call-clobbered registers do.

> IMO gcc has right to optimize out:
> int test()
> {
> return c(1) + d(1);
> }
> the second store of 1, in case c is const function

I don't agree.


r~

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2000-03-24  2:22 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-03-23  6:24 tail calls in const functions? Jan Hubicka
2000-03-23  6:29 ` Jakub Jelinek
2000-03-23  6:43   ` Jan Hubicka
2000-03-23  9:09     ` Jeffrey A Law
2000-03-23 14:20       ` Jan Hubicka
2000-03-23 14:47         ` Jeffrey A Law
2000-03-24  2:22 ` Richard Henderson

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).