I need to be able to look at the assembly output of a specific function using GCC. I use -S, but sometimes if the function is inlined no seperate assembly code for the function is outputted. I don't want to prevent the inlining alltogether, because I'm also doing performance benchmarks, and I want inlining for that.
I am aware of the option -fkeep-inline-functions, but sadly this applies to every function, and I don't want to bloat my assembly output file with thousands of lines of assembly code for the standard library.
I did find out that if you take the address of the function it will not be optimized away from the assembly output. Knowing this I hacked the following macro together:
#define DONT_OPT_OUT_CONCAT1(a, b) DONT_OPT_OUT_CONCAT2(a, b)
#define DONT_OPT_OUT_CONCAT2(a, b) a ## b
#define DONT_OPT_OUT(x) \
int DONT_OPT_OUT_CONCAT1(DONT_OPT_OUT_BLACK_HOLE, __COUNTER__) = (&x, 0);
Usage:
int f(int x) { return x*x; }
DONT_OPT_OUT(f);
Now, this works, but it has two problems:
- It pollutes the global namespace with throwaway variables that may or may not get optimized out.
- It does not work for overloaded functions.
Is there any better way?