Optimizer Configuration: Function and Loop Level
Let's learn how function inlining, loop unrolling, and loop vectorization can be configured in the optimizer.
Function inlining
As you will recall, compilers can be encouraged to inline some functions, either by defining a function inside a class declaration block or by explicitly using the inline
keyword:
struct X {void im_inlined(){ cout << "hi\n"; };void me_too();};inline void X::me_too() { cout << "bye\n"; };
It's up to the compiler to decide whether a function will be inlined. If inlining is enabled and the function is used in a single place (or is a relatively small function used in a few places), then inlining will most likely happen.
How function inlining works
It's a really curious optimization technique. It works by extracting the code from the function in question and putting it in all the places the function was called, replacing the original call and saving precious CPU cycles.
Let's consider the following example using the class we just defined:
int main() {X x;x.im_inlined();x.me_too();return 0;}
Without inlining, the code would execute in the main()
frame until a method call. Then, it would create a new frame for im_inlined()
, execute in a separate scope, and go back to the main()
frame. The same would happen for the me_too()
method.
However, when inlining takes place, the compiler will replace the calls, like so:
int main() {X x;cout << "hi\n";cout << "bye\n";return 0;}
This isn't an exact ...