public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Roger Sayle <roger@eyesopen.com>
To: Geert Bosch <bosch@gnat.com>
Cc: "Novillo, Diego" <dnovillo@redhat.com>,
	Kenneth Zadeck <zadeck@naturalbridge.com>,
	GCC Mailing List <gcc@gcc.gnu.org>,
	"Berlin, Daniel" <dberlin@dberlin.org>
Subject: Re: a question about const and pure functions.
Date: Thu, 11 Nov 2004 17:45:00 -0000	[thread overview]
Message-ID: <Pine.LNX.4.44.0411110855200.31420-100000@www.eyesopen.com> (raw)
In-Reply-To: <1E9C1A46-33FA-11D9-B415-000A959A128E@gnat.com>


On Thu, 11 Nov 2004, Geert Bosch wrote:
> I find the definition in the Ada standard (ISO/IEC 8652:1995)
> very useful in practice.
> ...
>                          _Implementation Permissions_
>
>   18. If a library unit is declared pure, then the implementation is
>       permitted to omit a call on a library-level subprogram of the
>       library unit if the results are not needed after the call...
>       This permission applies even if the subprogram
>       produces other side effects when called.


The critical distinction is between whether the attributes of a function
are determined by the compiler, or are asserted by the programmer.  GCC
already makes use of Ada's implementation permission #18 above.
Consider:

int foo() __attribute__(const)
{
  printf("Hello world!\n");
  global++;
  return 0;
}

here the programmer is *declaring* (note the wording in the Ada standard)
that this function may be considered const, and therefore optimized away
if the return value is not used, or CSE'd blah, blah, blah!  Of course,
Ada's implementation permission #18, doesn't apply if the definition of
foo isn't explicitly *declared* foo.  Instead the remaining implementation
permissions described what constitutes a "safe code transformation" based
upon observable behaviour.

The other implementation permissions allow that calls to

int foo()
{
  return 0;
}

may be eliminated if the result isn't used, but for example prohibit
the compiler eliminating calls to

int foo()
{
  printf("hello world!\n");
  return 0;
}

where foo has not been explicitly declared "pure".  Hence pragmatically
Ada's notion of pure is actually derived from the aspects of the remaining
implementation permissions that allow a function to be eliminated under
the same conditions as being explicitly declared "pure".


Note it is the distinction between "declared" behaviour and "actual"
behaviour that allows us to optimize away calls to "sqrt" with -ffast-math
even though sqrt(-1.0) has observable side-effects.  Some language
front-ends are required to check whether the "const" and/or "pure"
attribute are actually honored by the code, others don't.  In GCC's
middle-end, however, these definitions are honored as always "true".

Roger
--

  reply	other threads:[~2004-11-11 17:20 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-11-10 19:14 Kenneth Zadeck
2004-11-10 23:35 ` Geoffrey Keating
2004-11-11 15:04   ` Kenneth Zadeck
2004-11-11 15:07     ` Roger Sayle
2004-11-11 15:17       ` Daniel Berlin
2004-11-11 15:17       ` Daniel Berlin
2004-11-11 17:20       ` Geert Bosch
2004-11-11 17:45         ` Roger Sayle [this message]
2004-11-11 19:04       ` Tom Tromey
2004-11-11 19:17         ` Roger Sayle
2004-11-11 19:31           ` Tom Tromey
2004-11-11 19:38             ` Roger Sayle
2004-11-11 20:15               ` Diego Novillo
2004-11-11 21:40     ` Geoffrey Keating
2004-11-12 14:50       ` Kenneth Zadeck
2004-11-12 23:19       ` Geert Bosch
2004-11-25 13:22     ` Jan Hubicka
2004-11-11 15:09 Richard Kenner
2004-11-11 15:26 ` Steven Bosscher
2004-11-11 15:45 Richard Kenner

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=Pine.LNX.4.44.0411110855200.31420-100000@www.eyesopen.com \
    --to=roger@eyesopen.com \
    --cc=bosch@gnat.com \
    --cc=dberlin@dberlin.org \
    --cc=dnovillo@redhat.com \
    --cc=gcc@gcc.gnu.org \
    --cc=zadeck@naturalbridge.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).