The copmiler is also free to do so for non-library functions, within
the bounds set by the as-if rule. So if the defintion of mystrlen()
is
adequetely visible to the compiler (and assuming the obvious
implementation of that function), the compiler is free to reduce
"a=mystrlen("somestring")" to "a=9".
In fact, this sort of optimization is quite common with inlined
functions.
With inlined functions, it's fair to _assume_ your compiler will do such
optimizations; if it doesn't, it's time to upgrade. Better compilers
have "inter-procedural optimization" or "whole-program optimization",
which allow them to do the same thing with non-inline functions within
the same translation unit or even across translation units -- provided,
of course, they can prove the "as if" rule applies.
<OT>The GCC folks have recently started playing with IPO within the same
translation unit, but it's still in it infancy and only seems to work on
inline and static functions so far; many commercial compilers have had
IPO for a while. A few have WPO, but typically that means the "compile"
stage emits some intermediate representation and actual compilation and
most optimization is delayed until the "link" stage. The GCC folks
haven't made any noises they're considering that route, but five years
from now I wouldn't be surprised to see it.</OT>
S