This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: pure and const functions


On Fri, 26 Apr 2002, Chris Lattner wrote:

> Then the definition has to be fixed.  :)

*nod*

> The whole point of this discussion is to enable transformations like:
>
> foo()+foo() -> 2*foo() and
>
> fib(10000) -> /* noop */
>
> right?  In this case, _no_ global state may be access or updated, whether
> it be a side effect causing syscall or an update of a buffer.

This is the point of the "const" modifier used in the original post.

Here I am speaking in the context of the original post (I agree that the
use of "pure function" is bewildering in this thread)

pure functions may change global state and may depend of their arguments.
const functions may depend of their aguments only.

What I am after is that we use the definitions in the gcc manual

> `pure'
>      Many functions have no effects except the return value and their
>      return value depends only on the parameters and/or global
>      variables.  Such a function can be subject to common subexpression
>      elimination and loop optimization just as an arithmetic operator
>      would be.  These functions should be declared with the attribute
>      `pure'.

and

> `const'
>      Many functions do not examine any values except their arguments,
>      and have no effects except the return value.  Basically this is
>      just slightly more strict class than the `pure' attribute above,
>      since function is not allowed to read global memory.
>
>      Note that a function that has pointer arguments and examines the
>      data pointed to must _not_ be declared `const'.  Likewise, a
>      function that calls a non-`const' function usually must not be
>      `const'.  It does not make sense for a `const' function to return
>      `void'.

rather than the botched ones specified in the original post.

/Violently agreeing with most of the people around here.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]