public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* side-effect-free function
@ 2021-03-09 15:05 Rasmus Villemoes
  2021-03-09 17:41 ` Martin Sebor
  0 siblings, 1 reply; 2+ messages in thread
From: Rasmus Villemoes @ 2021-03-09 15:05 UTC (permalink / raw)
  To: gcc

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-03-09 17:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-09 15:05 side-effect-free function Rasmus Villemoes
2021-03-09 17:41 ` Martin Sebor

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).