Suppose my code has the following function:
inline int foo() {
bar();
int y = baz();
return y;
}
and suppose that bar()
and baz()
have side-effects.
If I write:
int z = foo();
printf("z is %d\n", z);
then obviously both bar()
and baz()
have to be executed. But if I write:
foo();
and not use the return value, the only reason to call baz()
is for its side effects.
Is there a way I can tell my compiler (for any of: GCC, clang, MSVC, Intel) something like "you may ignore the side effects of baz()
for purposes of deciding whether to optimize it away"? Or "you may ignore the side effects of this instruction for purposes of deciding whether to optimize it away"?
This question is phrased for C and C++ but could be relevant for all sorts of procedural languages. Let's make it C++ because that's what I use the most right now.
Note:
bar()
,baz()
are not under my control.- The signature of
foo()
may not be altered. Of course I could use some proxy and applybaz()
lazily, if I so wanted; or just write a different function for the case of not usingy
. That's not what I'm asking.
As an extension to standard C and C++, gcc and clang support the
pure
function attribute which informs the compiler that the function has no side effects and unneeded calls can be deleted. If you declare a function with this attribute, the compiler will believe you, even if you are lying.This prints no output. Note that
clang
will give a warning that you are ignoring the return value of apure
function.(Peculiarly, when you do something similar in C, gcc optimizes out the call with
-O0
, but leaves it in with-O2
!)The definition of the function does not need to be in scope for this to work, and you can re-declare a function with this attribute even if a previous declaration is already in scope. So it can also be used for library functions. Example.
Of course, lying to the compiler is always at your own risk, and conceivably this could have potential ill effects that I haven't thought of. It may be a useful hack, if you are willing to carefully scrutinize the generated code to make sure it is really doing what you want, but I wouldn't trust it too far.