Jacob Faib
Jacob Faib

Reputation: 1130

Do Compilers Un-Inline?

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);
}

Upvotes: 4

Views: 250

Answers (2)

MSalters
MSalters

Reputation: 180145

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.

Upvotes: 4

yugr
yugr

Reputation: 21954

Yes, for example LLVM has a MachineOutliner optimization pass.

Upvotes: 7

Related Questions