-6

Say I have a function:

void foo ( int bar ){
    return;
}

will this compile to a nop instruction?

Funkyguy
  • 628
  • 2
  • 10
  • 31

2 Answers2

6

It's not specified what should be done by the c++ standard. But I'd say any decent compiler would optimize out such code and not even leave a nop instruction.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
2

Modern compilers emit nop-like instructions for some purpose - commonly for alignment (on many architectures branches are executed faster if the target is aligned to - say - 16 bytes) or to wait for some operations to complete (IIRC on IA64 you must wait some number of cycles before using the result of some opcodes, so either you have something else to do or you have to emit nops).

Now, either we are in this case or most probably no nop will be emitted. For a function like this you can expect the regular stack frame setup followed by the cleanup for non optimized builds, or a plain ret (plus the stack cleanup code if mandated by the calling convention) in optimized ones.

Even better, if the compiler is smart enough and the definition of the function is known when compiling the call site (so, this is a static function, an inline one or link time code generation is enabled) the call may be omitted altogether, and there may remain no trace of the function code in the final executable.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299