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: [PATCH] Make attribute functions pure


On Friday 23 July 2004 13:42, Richard Kenner wrote:
> [Moved from gcc-patches to gcc.]
>
>     > I don't see how.  It's usually accepted that pure functions can be
>     > memoized. Can you give an example of the sort of miscompilation
>     > you're worried about?
>
>     $ cat impure.c
>
> Sorry, I obviously must not have been clear. I perfectly well understand
> that pure functions can't have side effects of the sort you show.
>
> But what was I asking for are examples of miscompilations caused by
> *memoization* in pure functions.

Ah, sorry.

> To be clear, by "memoization" I mean the storing of the result that the
> function returns so that it can return the same value in a later invokation
> with the same parameters.
>
> In other words, suppose I have a version of sqrt that stores its last input
> and output in a global variable and checks to see if its next input was the
> same as its last input and, if so, returns the last output.

sqrt is actually a const function, although similar reasonings apply.

> Does doing that make it impure?  I don't see how and would like an example
> of how that can cause miscompilations.

As you describe it, no. However if you store the inputs, outputs and *any 
global variables it or its children look at* in *static local* variables, 
that should be ok.

Paul


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