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



>>   a) having no effects except the return value
>>   b) return value depends only on the parameters
>>   c) return value depends on global variables
>>
>> Condition a) is too strong for removal of duplicate calls, since it
>> eliminates considering memo functions as pure.

> Yes, but I think that's okay. There are cases where duplicate calls
> to a non-pure memoizing function could be removed too, but how should
> the compiler determine which side-effects can be ignored?
> All we say is that if condition a) is fulfilled, we are on the safe
> side.

There are two different issues going on here confusing things:

1. The defined semantics
2. What we actually do

For #1, Robert is correct...
>   a) having no effects except the return value

Is too strong of a statement... the problem is that it's really hard to
make an accurate summary of what things really are.  Perhaps "Always
returns the same value for a particular set of input parameters" coupled
with "makes no changes to global state that would break the program if
they didn't happen" is close enough.  Effectively we just need to get
across the idea that a pure function _can_ be eliminated, and doing so
should not affect the semantics of the program.

This seems nice and simple, but #2 confuses things.  As I understand it,
GCC automatically trys to prove functions pure.  Obviously it will not be
able to figure out the structure of a memoizing function in general, so it
would punt on these functions, thus not automatically marking it pure.

The programmer, however, can manually add the pure attribute, so the
actual _notion_ of being pure is still intact, even if the actual
implementation isn't "smart enough" (and there isn't really any reasonable
way to make it so).

-Chris

http://www.nondot.org/~sabre/os/
http://www.nondot.org/~sabre/Projects/


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