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
toy == -1
. - The compiler can reduce the expression
(y + 1) - 1
toy
(assuming wraparound overflow semantics) - The expressions
y
andy+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 the benefits of a college education is, to show the boy its little avail.”
—Ralph Waldo Emerson (18031882)
“It is with benefits as with injuries in this respect, that we do not so much weigh the accidental good or evil they do us, as that which they were designed to do us.That is, we consider no part of them so much as their intention.”
—Laurence Sterne (17131768)
“I do seriously believe that if we can measure among the States the benefits resulting from the preservation of the Union, the rebellious States have the larger share. It destroyed an institution that was their destruction. It opened the way for a commercial life that, if they will only embrace it and face the light, means to them a development that shall rival the best attainments of the greatest of our States.”
—Benjamin Harrison (18331901)