Inline Expansion - Benefits

Benefits

Inline expansion itself is an optimization, since it eliminates overhead from calls, but it is much more important as an enabling transformation. That is, once the compiler expands a function body in the context of its call site—often with arguments that may be fixed constants -- it may be able to do a variety of transformations that were not possible before. For example, a conditional branch may turn out to be always true or always false at this particular call site. This in turn may enable dead code elimination, loop-invariant code motion, or induction variable elimination.

In the C example in the previous section, optimization opportunities abound. The compiler may follow this sequence of steps:

  • The temp += 0 statements in the lines marked (1), (2) and (3) do nothing. The compiler can remove them.
  • The condition 0 == 0 is always true, so the compiler can replace the line marked (2) with the consequent, temp += 0 (which does nothing).
  • The compiler can rewrite the condition y+1 == 0 to y == -1.
  • The compiler can reduce the expression (y + 1) - 1 to y (assuming wraparound overflow semantics)
  • The expressions y and y+1 cannot both equal zero. This lets the compiler eliminate one test.

The new function looks like:

int f(int y) { if (y == 0) return y; /* or return 0 */ else if (y == -1) return y - 1; /* or return -2 */ else return y + y - 1; }

Read more about this topic:  Inline Expansion

Famous quotes containing the word benefits:

    In America the young are always ready to give to those who are older than themselves the full benefits of their inexperience.
    Oscar Wilde (1854–1900)

    While greedy good-doers, beneficent beasts of prey,
    Swarm over their lives enforcing benefits ...
    Robert Frost (1874–1963)

    Through all opposition the personal benefits of the reform [dress] [bracketed word in original] have compensated; but had it been mainly sacrifice, the thought of working for the amelioration of women and the elevation of humanity would still have been the beacon-star guiding me on amid all discouragements.
    Susan Pecker Fowler (1823–1911)