In C, if I have a function call that looks like
// main.c ... do_work_on_object(object, arg1, arg2); ... // object.c void do_work_on_object(struct object_t *object, int arg1, int arg2) { if(object == NULL) { return; } // do lots of work }
then the compiler will generate a lot of things in main.o to save the state, transfer parameters (hopefully in the register in this case) and restore the state.
However, during the connection, you will notice that arg1 and arg2 are not used in the return trip, so cleaning up and restoring state can be shorted out. Do linkers typically do these things automatically, or will connection time optimization (LTO) need to be enabled to get this working?
(Yes, I could test the disassembled code, but I'm interested in the behavior of compilers and linkers in general and on several architectures, so we hope to learn from the experience of others.)
Assuming profiling shows that this function call is worth optimizing, should you expect the following code to be noticeably faster (for example, without the need for LTO)?
// main.c ... if(object != NULL) { do_work_on_object(object, arg1, arg2); } ... // object.c void do_work_on_object(struct object_t *object, int arg1, int arg2) { assert(object != NULL) // generates no code in release build // do lots of work }
source share