This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to remove unused code to minimise the memory on IAR

I'm using IAR  MSP430 workbench, I need to minimise the memory. So I kept HIGH Balanced optimisation in release mode. I created list of output file to get each file memory information. But it is not removing unused codes while calculating code size.

If I comment it off , decreasing code size otherwise not. How to remove unused code on compilation to minimise the memory usage?

Please give input on the same.

  • By default, the linker will include all code in a compilation unit, or none. So if you want parts only included if required (referenced), so put them into separate .c files.
    You may change this default behavior by telling the compiler to put all functions into separate segments, and tell the linker to drop segments without reference to it. However, this prevents global optimizations.
    Alternatively, you can use conditional compilation, to control which code parts are included (and compiled at all) depending on a config header file or by system defines.
    The ‘debug mode’, for example, defines a global symbol __DEBUG. You can include debug code with #ifdef __DEBUG, so it only gets compiled in debug mode, but not in release mode.
    Keep in mind that this may change your code behavior, so tested code from debug mode may break in release mode due to changed code size and code timing.

  • Jens-Michael Gross said:

    By default, the linker will include all code in a compilation unit, or none. So if you want parts only included if required (referenced), so put them into separate .c files.

    Jens-Michael,

    This should not be necessary. The IAR linker XLINK will include only those functions actually used by the application.

    To find out the memory usage of an application, I would recommend looking at the linker map file. You can also use it to find out why a particular piece of code has been included by checking which function or variable referred to it.

        -- Anders Lindgren, IAR Systems, Author of the IAR compiler for MSP430

  • Good to know. However, this approach prevents re-using the trailing part of a function, even if identical for all. Or other global optimization features. Each function needs to be treated as stand-alone (as it could well be alone after linking).
    Well, normally, project code shouldn’t contain unused functions anyway. If they aren’t used, they don’t belong to the project :)
    Libraries are a different thing.

    Referring to the linker map file is a good suggestion.

    Re-reading the original question, I’m no longer sure whether the “problem” is with the linker at all. The compiler does not know which functions are used or not, so “each file memory information” will of course contain all code that got compiled. Only the final linker output will tell which of it was included and which was discarded during link.

  • Jens-Michael Gross said:

    Good to know. However, this approach prevents re-using the trailing part of a function, even if identical for all. Or other global optimization features. Each function needs to be treated as stand-alone (as it could well be alone after linking).

    Well, we do perform cross-function optimizations like tail merging and cross calling on all functions in a compilation unit, based on the assumption that most often all functions are used. Admittedly, if that isn't the case you could end up with a couple of extra jumps or calls. Of course, if that is a problem, the user can always split the source files, to prevent the optimizations to take place.

        -- Anders Lindgren, IAR Systems

**Attention** This is a public forum