gcc: freestanding option with higher optimisation results in larger code

216 views Asked by At

When compiling with higher optimisation flags (eg: -O3), gcc will assemble code like this:

extern unsigned char g_wibble[200];

int test(void) 
{
    int i;
    for (i = 0; i < sizeof(g_wibble); i++)
        g_wibble[i] = 0;

    return 0;
}

in a manner that uses memset() rather than a local loop. This goes awry with the addition of the -ffreestanding flag. As described in this answer, gcc cannot rely on any semantic considerations so it cannot assume that memset has the same meaning in this environment as it usually does (this seems at odds with the documentation which says that gcc expects functions like memset to be provided in a freestanding environment. The result is that gcc goes slightly mad and appears to inline its entire internal view of memset into the code.

Testing with godbolt.org and using arm gcc 7.2.1 (none) and compiler flags -O2 -mthumb -mcpu=cortex-m0plus -ffreestanding, I get 14 lines of output. Changing to -O3 creates 62 lines of output, a considerably less efficient approach.

It's worth considering your optimisation options closely when considering a freestanding environment! Bear in mind that -O3 does just mean "optimise more" but optimise more for performance, no matter the code size or compilation time impact.

Yes, this is a statement rather than a question; however I could ask if this is expected gcc behaviour or is it possibly an optimisation bug? The behaviour doesn't quite sync with the gcc documentation.

0

There are 0 answers