1

How does compiler decide whether the function should be considered for inline or not?

Unlike linkage, there is no such thing as "inline" conflicts (declarations of same name may have inline or not).

For example, multiple declarations of functions can either have inline or not, such as:

inline static void test(); // declaration with inline

int main()
{
    extern void test(); // declaration without inline.
    test();
}  

static void test() // definition does not have inline
{
}

or

static void test(); // declaration without inline

int main()
{
    test();
}  

inline static void test() // but definition has inline!
{
}

In each case, does the test() gets inlined?

My real question is, how does compiler discover that the function should be inlined? Does it look at whether the most recent declaration bear the keyword inline or does it look at whether the definition has keyword inline or not?

I tried to search it in the standard, but I could not find any rules concerning this ambiguity. It looks like inline is defined very vague in the standard.

(I initially thought that there should be a "rule" that mandates once a function is declared inline, every declaration of same name has to be inline as well.)


EDIT: I am basically asking how the compiler solves the ambiguity of multiple same-name declarations which only some of them contain inline keyword.

inline static void test() // definition with inline keyword
{
}

int main()
{
    extern void test(); // declaration without inline
    test(); // Does compiler consider inlining or not?
}
SHH
  • 3,226
  • 1
  • 28
  • 41
  • 2
    Just don't worry about it. Leave `inline` out of your code and let the compiler do the worrying. – pmg Nov 16 '11 at 23:08
  • 1
    Read this: http://drdobbs.com/184403879# – Fred Larson Nov 16 '11 at 23:12
  • 1
    Compilers are free to inline what you didn't mark as inline, and will frequently _not_ inline what you told them to. Let the compiler make the call, just drop it as a hint for humans if you feel the need (_look, this function is efficient because it gets optimized away_). – sehe Nov 16 '11 at 23:29
  • 1
    I am not asking whether compilers will inline it or not. I am asking whether compilers will know my intention of inline in such ambiguous situations. This ambiguity should be defined in standard somewhere, but I can't seem to find it. – SHH Nov 16 '11 at 23:33
  • 2
    The relevant part of the standard is 6.7.4/6. In practice, a decent compiler isn't very interested in your "intention to inline", it will inline or not according to its own rules, that are quite possibly unaffected by whether the function is marked inline or not. The meaningful effects of marking a function inline are unrelated to inlining. Btw, in your first code snippet, `test` has internal linkage in first declaration and external linkage in the second declaration, which by 6.2.2/4 is ignored, the identifier still has internal linkage. – Steve Jessop Nov 17 '11 at 00:22
  • @Steve Jessop I am wondering what official standard says about it. I can only look at the draft, and it only mentions when all declarations have inline keyword in it without extern or static. According to http://www.greenend.org.uk/rjk/2003/03/inline.html, I think official standard (not draft) mentions the case where "A function where at least one declaration mentions inline, but where some declaration doesn't mention inline or does mention extern." Maybe I need to purchase C99 standard and take a look at it. – SHH Nov 17 '11 at 00:33
  • I'm using n1256, which is freely available, and is the last published draft of TC3. It discusses cases where some declarations are marked inline and others not. It also discusses `extern` - declaring an inline function with `extern` in file scope (but not function scope as in your first example) provides an external definition. Otherwise there's no external definition. – Steve Jessop Nov 17 '11 at 00:37
  • I am looking at n1256, and it only specifies two cases: 1. external linkage with every declarations bearing inline without extern: creates inline definition. 2. external linkage with extern or only some inline keywords: creates external definition. But it does not talk anything specific about static inline functions (mentions one line: "Any function with internal linkage can be an inline function. ". What about the case where static function is defined, and only static declaration has the keyword "inline"? (my example #1 on top). – SHH Nov 17 '11 at 00:54
  • But, I am getting to believe that if any one declaration bears "inline," (definition or not), then the compiler will look at the function as if it was declared inline. – SHH Nov 17 '11 at 01:00

1 Answers1

4

My real question is, how does compiler discover that the function should be inlined? Does it look at whether the most recent declaration bear the keyword inline or does it look at whether the definition has keyword inline or not?

Generally, compilers decide whether to inline a function based on (1) if the body of the function is in the header file and (2) rules of thumb specific to the compiler that determine if there's a net benefit to inline.

The inline keyword is meant to help, but much like the register keyword, it doesn't actually provide much when generating code. Instead, inline simply means "don't complain if you see this definition more than once (because I'm sticking it in a header file)."

That "don't complain" thing is why your code doesn't complain even though it probably should.

If you're going to use the keyword, don't put it on the forward declaration, and do put it on the definition (and do put the definition in the header):

void test();

/* ... other declarations ... */

/* (in the same file) */

inline void test() { printf("inlined!\n"; }

There's actually a reason to do this: you may not want to clutter your declarations with the implementations.

Max Lybbert
  • 19,717
  • 4
  • 46
  • 69
  • 2
    Rule #1:if the body of the function is in the header file. This rule makes absolutely no sense. Isn't anything in the header file simply inserted into source code? – SHH Nov 16 '11 at 23:10
  • 1
    @SHH I don't think modern compilers that perform [link-time optimization](https://secure.wikimedia.org/wikipedia/en/wiki/Link-time_optimization) require the function to be defined in the header, but it used to be the case that the compiler had to be able to *see* the function body at the callsite in order to be able to perform inlining – Praetorian Nov 16 '11 at 23:13
  • @SHH: yes, the contents header files are dumped into whatever .c file they are included in and then the .c files are turned into object code. Putting the body of the function in the header then means that the body of the function is available to the compiler when it's actually compiling, and therefore available to inline. The alternative would be putting the body into a .c file, compiling that to a .o file and **not** having the body of the function around when the compiler is compiling (but instead having the .o file to link to when the linker is linking). – Max Lybbert Nov 17 '11 at 08:28
  • Praetorian mentioned link-time optimization which I hadn't considered when I wrote the question. But, yes, linkers are now sophisticated enough that they can inline your code even without direct access to your source code. – Max Lybbert Nov 17 '11 at 08:30