Вопрос

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.

Это было полезно?

Решение

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.

Другие советы

The compiler is not required to inline code (even if explicitly marked inline), and optimisers vary in sophistication and strategy. So this is really a question for your compiler vendor (consulting the documentation may also help).

If the function is particularly large, the compiler may have made the decision that the avoidance of a function call overhead was insignificant in the overall scheme.

Your compiler may have a _force_inline keyword or similar, which would be regarded and a directive rather than a suggestion (as inline typically is on most compilers).

One possible argument for not in-lining, is to maintain deterministic performance under maintenance. If a later second reference caused it to no longer be in-lined, your code execution speed may change in ways that would be detrimental to your application's performance requirements, while remaining in-lined might significantly impact code size.

[edit] Reading the documentation, on your compiler the directive #pragma inline=forced is required immediately before the function definition, otherwise in-lining is dependent upon the optimisers heuristics. Even when forced, in-lining will not occur at low or no optimisation.

According to an IAR employee in this thread IAR Size optimization & inline functions:

Officially, it's only supported if the compiler sees the definition in a header file or in the same source file. However, as an experiment we've tried out something called "multifile compilation", where you can feed several C files to the compiler at the same time, allowing it to inline between compilation units. Note that this is purely experimental, and the IDE has no support for it.

...

Personally, should I write an application today, I would not use the --mfc option. Instead I would place the function I would like to inline in header files, must like you do when you write C++ class definitions with inlined methods.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top