Unknown compiler "optimizations" make no sense
I was looking at the disassembly of some code I compiled with Visual Studio 2008, and I see some weird "optimizations" litter throughout the code that don't quite make sense when functions are called and parameters are passed. For example, the following code outputs the following disassembly:
Code:
int version;
int result = canParse(code, &version);`
Disassembly:
003CE9FA push eax ; version
003CE9FB push ecx ; code
003CE9FC mov ecx, [esp+50h+code] ; AbcParser *
003CEA00 mov eax, esp
003CEA02 mov [eax], ecx
003CEA04 call avmplus::AbcParser::canParse(avmplus::ScriptBuffer,int *)
In this case, push ecx
makes some space on the stac开发者_运维问答k that is then overwritten by [esp+50h+code]
.
Why does the compiler do this?
It doesn't save space. (It would take less room to have mov ecx, [esp+50h+code]; push ecx
.) It doesn't save time, as far as I know. (Wouldn't executing the two instructions I just mentioned be faster?)
Also, both ecx
and eax
are overwritten when used within canParse
.
As @Radek Pro-Grammer suggested in his comment, even if there are more instructions which seem useless, the code might run in less clock cycles. Nowadays processors are much more complicated, with pipelining, prefetch queue, caching, superscalar design etc, so optimizations can be quite.. daring. :)
精彩评论