Abhineet
Abhineet

Reputation: 6617

How will i know whether inline function is actually replaced at the place where it is called or not?

I know that inline function are either replaced where it is called or behave as a normal function.

But how will I know whether inline function is actually replaced at the place where it is called or not as decision of treating inline function as inline is at the compile time?

Upvotes: 50

Views: 23016

Answers (10)

Bogi
Bogi

Reputation: 2598

There is a way to determine if a function is inline programmatically, without looking at the assembly code. This answer is taken from here.

Say you want to check if a specific call is inlined. You would go about like this. Compiler inlines functions, but for those functions that are exported (and almost all function are exported) it needs to maintain a non-inlined addressable function code that can be called from the outside world.

To check if your function my_function is inlined, you need to compare the my_function function pointer (which is not inlined) to the current value of the PC. Here is how I did it in my environment (GCC 7, x86_64):

void * __attribute__((noinline)) get_pc () { return _builtin_return_address(0); }

void my_function() {
    void* pc = get_pc();
    asm volatile("": : :"memory");
    printf("Function pointer = %p, current pc = %p\n", &my_function, pc);
}
void main() {
    my_function();
}

If a function is not inlined, difference between the current value of the PC and value of the function pointer should small, otherwise it will be larger. On my system, when my_function is not inlined I get the following output:

Function pointer = 0x55fc17902500, pc = 0x55fc1790257b

If the function is inlined, I get:

Function pointer = 0x55ddcffc6560, pc = 0x55ddcffc4c6a

For the non-inlined version difference is 0x7b and for the inlined version difference is 0x181f.

Upvotes: 2

pkthapa
pkthapa

Reputation: 1071

The compiler does not make a function inline if the function returns an address.

Upvotes: -1

intiyaz ahammad shaik
intiyaz ahammad shaik

Reputation: 168

Above answer are very mush useful, I am just adding some point which we keep in our mind while writing inline function.

Remember, inlining is only a request to the compiler, not a command. Compiler can ignore the request for inlining. Compiler may not perform inlining in such circumstances like:

1) If a function contains a loop. (for, while, do-while)

2) If a function contains static variables.

3) If a function is recursive.

4) If a function return type is other than void, and the return statement doesn’t exist in function body.

5) If a function contains switch or goto statement.

Complete info: https://www.geeksforgeeks.org/inline-functions-cpp/

Upvotes: -1

李鹏程
李鹏程

Reputation: 81

  1. see the size of object files, they are different between inlined and not inlined
  2. use nm "obj_file" | grep "fun_name", they are also different
  3. gcc -Winline -O1
  4. compare with assembly code

Upvotes: 0

Vlad Didenko
Vlad Didenko

Reputation: 4771

If you need to make sure that function is inlined and OK to go with proprietary extension in MS VC++, check out the __forceinline declarator. The compiler will either inline the function or, if it falls into the list of documented special cases, you will get a warning - so you will know the inlining status.

Not endorsing it in any way.

Upvotes: 9

ABu
ABu

Reputation: 12259

With gdb, if you cannot call to a function, one of its possible meanings is the function is inline. Flipping the reasoning, if you can call a function inside gdb, means the function is not marked inline.

Upvotes: 6

Alok Save
Alok Save

Reputation: 206518

Programatically at run-time, You cannot.
And the truth of the matter is: You don't need to know

An compiler can choose to inline functions that are not marked inline or ignore functions marked explicitly inline, it is completely the wish(read wisdom) of the compiler & You should trust the compiler do its job judiciously. Most of the mainstream compilers will do their job nicely.

If your question is purely from a academic point of view then there are a couple of options available:


Analyze generated Assembly Code:

You can check the assembly code to check if the function code is inlined at point of calling.

How to generate the assembly code?

For gcc:
Use the -S switch while compilation.
For ex:

g++ -S FileName.cpp

The generated assembly code is created as file FileName.s.

For MSVC:
Use the /FA Switch from command line.

In the generated assembly code lookup if there is a call assembly instruction for the particular function.


Use Compiler specific Warnings and Diagnostics:

Some compilers will emit a warning if they fail to comply an inline function request.
For example, in gcc, the -Winline command option will emit a warning if the compiler does not inline a function that was declared inline.

Check the GCC documentation for more detail:

-Winline

Warn if a function that is declared as inline cannot be inlined. Even with this option, the compiler does not warn about failures to inline functions declared in system headers.

The compiler uses a variety of heuristics to determine whether or not to inline a function. For example, the compiler takes into account the size of the function being inlined and the amount of inlining that has already been done in the current function. Therefore, seemingly insignificant changes in the source program can cause the warnings produced by -Winline to appear or disappear.

Upvotes: 73

ks1322
ks1322

Reputation: 35716

You can use tools for listing symbols from object files such as nm on Linux. If the function was inlined, it will not be listed in nm output - it became part of some other function. Also you will not be able to put breakpoint on this function by name in debugger.

Upvotes: 9

saurabh jindal
saurabh jindal

Reputation: 121

The decision to inline or not a function is made by compiler. And since it is made by compiler, so YES, it can be made at compile time only.

So, if you can see the assembly code by using -S option (with gcc -S produces assembly code), you can see whether your function has been inlined or not.

Upvotes: 3

Luchian Grigore
Luchian Grigore

Reputation: 258578

Check the generated code. If the function is expanded, you'll see its body, as opposed to a call or similar instruction.

Upvotes: 13

Related Questions