Search code examples
coptimizationembeddedinlineiar

optimization of static function referenced once


I am writing embedded code for MSP430, using the IAR compiler at the highest optimization level (speed or size does not change anything).

I define a function as static, then reference it only once, in the same file. Since the function has internal linkage, and is used exactly once, I expected the optimizer to perform inline expansion. I can see no reason not to.

The function is short, it results in 16 words of machine code. It is called from an ISR. Adding the inline keyword makes the function inline, but the optimizer seems to need the hint. Having it inline saves two push/pops to the stack, one calla and one reta.

Am I right to expect inline expansion to be performed (even without the inline keyword), or am I missing something?


Edit: a few more tests showed that the inline expansion was dependent on the size of the function, and that the threshold was quite low. It seems to be around 15 or 16 words of machine code. Above that and the optimizer does not expand if not given the keyword.

I still don't see why it wouldn't (readability shouldn't be the concern of an optimizer, should it?), but I understand that IAR only can answer this.


Solution

  • I'm using an IAR ARM compiler version that's a few years old (v5.2); how much of this might apply to the MSP430 compiler I have no idea.

    The IAR ARM compiler inlines static functions that aren't explicitly marked inline for me using any of the 'high' optimization settings, -Oh (balanced), -Ohs (speed), or -Ohz (size) - including static functions with some measure of complexity (loops for example).

    Of course, I imagine that there are some static functions which aren't inlined, but a quick check indicates that the IAR compiler is performing this optimization in general.

    So, I'd expect the compiler to inline your static function - but if you want to depend on these optimization, I think you'd need to examine the output (as you did). Of course, what optimizations and how they're applied are completely compiler dependent, so only IAR can really answer the question of whether the optimization 'should' occur (or defend why they might decide that it shouldn't). You might want to talk with IAR if you think they aren't performing this optimization appropriately. They might be able to give you a pointer as to why it's not happening in this particular case.