Hi, I was just playing with the microsoft c++ compiler from visual studio 7, and had this test routine, a kind of useless one, but i was just interested in what the compiler could make of it:
to my surprise, the resulting x86 code became this:
imho can even a machine see very easy that this code would be faster and smaller:
(an inc would be even smaller, but iirc is an add faster nowadays?)
but well, my point of this thread: why does the compiler do this? there must be a reason i think, it isn't just a glitch, as the developers worked for years on it to optimize it...
typedef struct
{
char a[70];
int x;
} idiotstruct;
void crap(idiotstruct* k, int n)
{
k[n].x++;
}
to my surprise, the resulting x86 code became this:
push ebp
mov ebp,esp
mov eax,[ebp][0C]
imul eax,eax,04C ;"L"
mov ecx,[ebp][08]
mov edx,[ecx][eax][48]
add edx,001 ;"?"
mov eax,[ebp][0C]
imul eax,eax,04C ;"L"
mov ecx,[ebp][08]
mov [ecx][eax][48],edx
pop ebp
retn
imho can even a machine see very easy that this code would be faster and smaller:
push ebp
mov ebp,esp
mov eax,[ebp][0C]
imul eax,eax,04C ;"L"
mov ecx,[ebp][08]
add dword [ecx][eax][48],1
pop ebp
retn
(an inc would be even smaller, but iirc is an add faster nowadays?)
but well, my point of this thread: why does the compiler do this? there must be a reason i think, it isn't just a glitch, as the developers worked for years on it to optimize it...
Did you compile in release mode or turn on optimizations?
but well, my point of this thread: why does the compiler do this? there must be a reason i think, it isn't just a glitch, as the developers worked for years on it to optimize it...
High performance optimizing compilers can start by generating simple code. They can also skip the optimization step(s) for faster compiles.
VC7.1 Generates this code for me:
This is with /Ox switch. Be aware that there are many more optimization-related switches.
; 9 : k[n].x++;
mov eax, DWORD PTR _n$[esp-4]
mov ecx, DWORD PTR _k$[esp-4]
imul eax, 76 ; 0000004cH
lea eax, DWORD PTR [eax+ecx+72]
inc DWORD PTR [eax]
; 10 : }
ret 0
This is with /Ox switch. Be aware that there are many more optimization-related switches.
Yeah, okay, I see: you were right about the optimization switch. But even then, without optimization, I expected it to fix something trivial as this. But well, I was wrong, thanks ;)
but even then:
i'd rather say
though ;)
but even then:
...
imul eax, 76 ; 0000004cH
lea eax, DWORD PTR [eax+ecx+72]
inc DWORD PTR [eax]
...
i'd rather say
...
imul eax, 76 ; 0000004cH
inc DWORD PTR [eax+ecx+72]
...
though ;)