4

It is fairly common knowledge that the most powerful tool in a compilers tool-belt is the inlining of functions into their call sites. But what about doing the reverse? If so, is it done? And when? For example given:

void foo(int x)
{
  auto y = bar(x);
  baz(y);
}

void bop()
{
  int x;
  auto y = bar(x);
  baz(y);
}

Does it ever make sense for the compiler to abstract this out to

void qux(int x)
{
  auto y = bar(x);
  baz(y);
}

void foo(int x)
{
  qux(x);
}

void bop()
{
  int x;
  qux(x);
}
5
  • 4
    Most compilers have an option to optimize to reduce binary size instead of optimizing for speed or memory usage. They could in theory apply this approach if it helps. Compilers can make any changes they want to your code that doesn't change the behavior of the program, and since the two examples shown have equivalent behavior there is nothing preventing a compiler from making that transformation. See The as-if rule. Commented Feb 24, 2022 at 15:28
  • I've never seen this done. It is probably allowed under the as-if rule, but adding a function call is most likely a performance loss, so an optimizer would not do that. Commented Feb 24, 2022 at 15:28
  • 1
    I can't see why they should. Optimizers are meant to reduce instruction count or to generate more efficient instructions. "Outlining" as in this example only adds additional instructions and an indirection which requires modifying the stack and a jump, for no reason. The only situation it could be helpful would be if the compiler recognized many separate code snippets in the program as being the same and put them into a function (and thus reducing code size) but that is very rare to ever happen and immensely complex. Commented Feb 24, 2022 at 15:29
  • There are compiler that perform this kind of outlining on the instruction level when optimizing for low code size. Commented Feb 24, 2022 at 16:20
  • The compiler is not required to inline anything - even the inline keyword is considered a hint that the compiler is permitted to ignore. I remember documentation for one older compiler that listed criteria for a function to be "too complicated to inline" (and one of those criteria was containing conditional statements), and another that treated inlining as an optimisation so would never inline when compiling without optimisation. There is nothing stopping modern compilers from doing similar things (although, presumably, the basis for "too complicated to inline" will have evolved). Commented Feb 24, 2022 at 23:30

2 Answers 2

7

Yes, for example LLVM has a MachineOutliner optimization pass.

Sign up to request clarification or add additional context in comments.

Comments

4

Outlining makes sense even without repeated code, when the outlined section is [[unlikely]]. The function call is a loss, but unlikely, while on the other hand more likely code can fit in cache.

Compilers might also assume that an exception is unlikely, and outline the catch.

3 Comments

> when the outlined section is [[unlikely]] This is the main motivation for my question actually. I have a C-style library returning error-codes where the error-checking must be abstracted to a macro not a function-call (which can be manually out-lined by the programmer). I'm hoping the compiler is smart enough to not generate the same instructions at each of the thousands of callsites...
@JacobFaib You can look at the assembly to know for sure. If it's literally the same instructions, they should all jump to a common block (but, probably not to a separate function). It's pretty normal to have a no-inline cold function for fallback rare error handling (i.e. coldcc in llvm), but you opt into that with human knowledge beyond the compiler.
Yeah, although it is kind of tough to look at the assembly for this :(. In the cases where I care about these operations being hoisted, compiler optimizations jumble things up to where I can't make heads or tails of things. Each macro uses __LINE__, __FILE__, and __func__ so not identical steps but more or less identical procedure. Hence why the compiler hoisting it into a function would be best.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.