17

I have the following piece of code:

#include <stdio.h>

typedef struct {
    bool some_var;
} model_t;

const model_t model = {
    true
};

void bla(const model_t *m) {
    if (m->some_var) {
        printf("Some var is true!\n");
    }
    else {
        printf("Some var is false!\n");
    }
}

int main() {
    bla(&model);
}

I'd imagine that the compiler has all the information required to eliminate the else clause in the bla() function. The only code path that calls the function comes from main, and it takes in const model_t, so it should be able to figure out that that code path is not being used. However:

Without inline

With GCC 12.2 we see that the second part is linked in.

If I inline the function this goes away though:

With inline

What am I missing here? And is there some way I can make the compiler do some smarter work? This happens in both C and C++ with -O3 and -Os.

8
  • 15
    The compiler cannot optimize the else path away as the object file might be linked against any other code. This would be different if the function would be static.
    – MrTux
    Aug 29, 2022 at 9:40
  • 4
    If you make bla static, compiler may optimize it. Aug 29, 2022 at 9:42
  • 9
    The compiler did inline the bla call in main and optimized the else branch away there. It just has to emit the function in case another translation unit uses it. (Compiler explorer is showing you the output after compilation before linking.) Aug 29, 2022 at 9:44
  • 4
    @Zakk What are you referring to? That bla was inlined? The image in the question shows the assembly output from the compiler. In the emitted main function there is no call to bla, just a call to puts with its argument loaded unconditionally from .LC0 which is the string in the first branch of the if. So the branching was optimized away. Aug 29, 2022 at 13:19
  • 2
    Your images should be godbolt.org full links, not just to the image on imgur! Code shouldn't be posted as images in the first place, though. Aug 30, 2022 at 14:57

3 Answers 3

33

The compiler does eliminate the else path in the inlined function in main. You're confusing the global function that is not called anyway and will be discarded by the linker eventually.

If you use the -fwhole-program flag to let the compiler know that no other file is going to be linked, that unused segment is discarded:

[See online]

Enter image description here

Additionally, you use static or inline keywords to achieve something similar.

27

The compiler cannot optimize the else path away as the object file might be linked against any other code. This would be different if the function would be static or you use whole program optimization.

1
  • 8
    If the function was declared static it would also simply not be emitted (same as with inline). All call sites are inlined anyway. The emitted function in the question was never used. Aug 29, 2022 at 9:47
8

The only code path that calls the function comes from main

GCC can't know that unless you tell it so with -fwhole-program or maybe -flto (link-time optimization). Otherwise it has to assume that some static constructor in another compilation unit could call it. (Including possibly in a shared library, but another .cpp that you link with could do it.) e.g.

// another .cpp
typedef struct {  bool some_var; } model_t;
void bla(const model_t *m);              // declare the things from the other .cpp

int foo() {
    model_t model = {false};
    bla(&model);
    return 1;
}
int some_global = foo();  // C++ only: non-constant static initializer.

Example on Godbolt with these lines in the same compilation unit as main, showing that it outputs both Some var is false! and then Some var is true!, without having changed the code for main.

ISO C doesn't have easy ways to get init code executed, but GNU C (and GCC specifically) have ways to get code run at startup, not called by main. This works even for shared libraries.


With -fwhole-program, the appropriate optimization would be simply not emitting a definition for it at all, as it's already inlined into the call-site in main. Like with inline (In C++, a promise that any other caller in another compilation unit can see its own definition of the function) or static (private to this compilation unit).

Inside main, it has optimized away the branch after constant propagation. If you ran the program, no branch would actually execute; nothing calls the stand-alone definition of the function.


The stand-alone definition of the function doesn't know that the only possible value for m is &model. If you put that inside the function, then it could optimize like you're expecting.

Only -fPIC would force the compiler to consider the possibility of symbol-interposition so the definition of const model_t model isn't the one that is in effect after (dynamic) linking. But you're compiling code for an executable not a library. (You can disable symbol-interposition for a global variable by giving it "hidden" visibility, __attribute__((visibility("hidden"))), or use -fvisibility=hidden to make that the default).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.