side-effect-free function

Rasmus Villemoes rasmus.villemoes@prevas.dk
Tue Mar 9 15:05:15 GMT 2021


Hi,

Consider some function now() which returns some kind of "current
timestamp" as a simple scalar. It could be a wrapper for
clock_gettime(CLOCK_MONOTONIC) which converts the timespec value to
nanoseconds, or in the linux kernel one of the ktime_get* family.

Then consider code like

start = now();
do_something();
end = now();
debug("something took %lu\n", end - start);

If debug() is a macro that expands to nothing (or an if(0) statement),
the now() calls are actually redundant. But AFAIU one can't mark now()
as pure, since gcc must not assume it returns the same value when
do_something() provably doesn't touch global memory.

Is there some way to specify that a function doesn't have any side
effects, but may return a different value each time it is called? I.e.,
if its return value is not used, it can be elided completely, but
consecutive calls can not be assumed to return the same value.

Rasmus


More information about the Gcc mailing list