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:



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...
Posted on 2005-01-30 06:25:35 by lifewire
Did you compile in release mode or turn on optimizations?
Posted on 2005-01-30 06:45:04 by ThoughtCriminal
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.
Posted on 2005-01-30 18:48:06 by tenkey
VC7.1 Generates this code for me:



; 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.
Posted on 2005-01-31 03:30:15 by death
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:



...
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 ;)
Posted on 2005-02-01 05:31:32 by lifewire