The following code,
int foo(int);
int bar(int);
int foobar(int i) {
int a = foo(i);
int b = bar(i);
return a == b ? a : b;
};
with GCC trunk is compiling to this assembly:
foobar(int):
push rbx
mov ebx, edi
call foo(int)
mov edi, ebx
pop rbx
jmp bar(int)
This TU has no clue what i
it will be given, and it has no clue what foo
and bar
will return, so it has no clue whether a == b
will be true
or false
. So it must
inspect the outputs of the two calls and somehow compare them to decide which one it shuld return.
But I can't see that in the assembly. What am I missing?
return a == b ? a : b;
is optimized toreturn b;
– Drainagea == b
, in this case it doesn't matter what you return, ora != b
, in this case you returnb
. Compiler has to callfoo
in case it has side effects, but it can safely optimise the condition toreturn b;
, since it satisfies both cases. – Septa>
instead of==
, I wouldn't even have asked the question. :D – Meandrous