If I understand it correctly, code compiled with optimization following these steps:
Generating IR and optimizing it, e.g., dead code elimination
Generating machine-specific code
Performing target-specific optimizations, e.g., choosing specific strcmp implementation
Link time optimization
Now, I only want optimizations in step 1, but not all other steps. Is that possible by feeding some compilation/linking flags to clang?
FYI, I’m doing some instrumentation on LLVM IR level, and eventually compile and run the binary. I want the binary to be as similar as possible to IR as possible, and don’t want any optimization happens after IR compilation to mess up this structure.
In fact now I am dealing with optimized IR compiled with -O3. But I’m not sure whether some optimization during codegen and backend would restructure the code (e.g. function inlining), and I am looking for a way to check/disable it.
I just read about optnone can be used as a function attribute. The doc says:
The optnone attribute suppresses essentially all optimizations on a function or method
So would it be possible to first optmize in frontend and then give specific functions optnone? Will backend and codegen respect this attribute? The doc is unclear about this part though.
The frontend doesn’t really do optimizations at all. I believe backend and codegen mostly respect optnone, although I remember there were some things that couldn’t be turned off (caused correctness problems). The doc is unclear because there isn’t a clear dividing line between things that respect it and things that do not.