# GCC Inlining **Function inlining** replaces a function call with its body, eliminating call overhead and enabling better optimization across the boundary. GCC inlines automatically at `-O2` and above, but you can guide it with hints and flags. ```bash gcc -O3 -o app app.c # automatic inlining gcc -O3 -finline-functions -o app app.c # aggressive inlining ``` At `-O2` and `-O3`, GCC inlines small functions automatically. At `-O1`, inlining is minimal. `-finline-functions` at any level enables more aggressive inlining (may increase code size). **Hints in code:** ```c static inline int add(int a, int b) { return a + b; } ``` `inline` is a hint to the compiler "consider inlining this function." It's not a guarantee—GCC makes the final decision based on cost-benefit analysis. `static` limits the function to one translation unit, allowing the compiler to inline freely. **Disabling inlining:** ```bash gcc -fno-inline -o app app.c # disable all inlining gcc -fno-inline-functions -o app app.c # disable function inlining only ``` Useful for debugging or measuring the impact of inlining on performance. **Reporting inlining decisions:** ```bash gcc -O3 -Winline -o app app.c ``` `-Winline` warns about functions marked `inline` that GCC didn't inline (because they're too large or too complex). Helps identify bottlenecks. **Example:** ```c inline int expensive(int x) { // complex code, large function for (int i = 0; i < 1000; i++) { x += i; } return x; } int main() { return expensive(5); // inlining this call? } ``` With `gcc -O3 -Winline`, you'd see a warning: the function is large, so GCC doesn't inline it despite the hint. **Cost-benefit:** GCC inlines when the benefit (eliminating call overhead, enabling cross-function optimizations) outweighs the cost (increased code size, worse cache behavior). For hot functions called from many places, inlining one call is different from inlining all calls. **Link-time inlining:** With `-flto` (link-time optimization), GCC can inline across translation units—functions defined in one `.c` file can be inlined into another. This enables more aggressive inlining than single-file compilation allows. **Performance impact:** Inlining can dramatically improve performance for small, frequently-called functions (5-20% speedup on tight loops). For large functions, inlining increases code size and can hurt cache performance. Measure to be sure.