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:

    One of your biggest jobs as a parent of multiples is no bigger than simply talking to your children individually and requiring that they respond to you individually as well. The benefits of this kind of communication can be enormous, in terms of the relationship you develop with each child, in terms of their language development, and eventually in terms of their sense of individuality, too.
    Pamela Patrick Novotny (20th century)

    One of the benefits of a college education is, to show the boy its little avail.
    Ralph Waldo Emerson (1803–1882)

    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)