Function Inlining by GCC

Inline function is an optimisation technique used by the GCC compiler. The compiler will try to insert the complete body of the function wherever the function is used. This will eliminate the time overhead when the function is called each time. The details about this insertion can be viwed by looking at the machine code.

Consider the following example:

We can get the assembly code by doing the compilation upto second phase.

On viewing the assembly code we could see the function call, sqr.

In this, the value 10 is pushed to to the register esp. On calling the function the esp address value will be decremented by 4 and the control jumps to the function. The square is evaluated there and stored in the register eax.

The method will create a time overhead of calling the function. Using the optimization option -O3 of gcc we can do the function inlining.

After the optimisation if we look at the assembly code, we can see that there is no function call. In that place we can see the value obtained by evaluating the function.

This example is an example of the extreme case of function inlining. In this the compiler knows that the function sqr will take a parameter which in this case is a constant. There is no need of evaluating the function value at runtime. The compiler evaluates the function at compile time itself and use that value at runtime eliminating the overhead of calling function at runtime. If the parameter was a variable, the compiler would have inserted the function body there.

The limit on the number of lines strictly depend upon the gcc heuristics.


About Odol Shinu

I've completed my B Tech in Information Technology in 2010 from Government Engineering College Sreekrishnapuram Palakkad under Calicut University.

Posted on October 19, 2010, in C Language. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: