[M3devel] code quality..
Tony Hosking
hosking at cs.purdue.edu
Mon Jan 21 17:01:52 CET 2008
Is this having the front-end handle return of structs or the backend?
On Jan 21, 2008, at 9:50 AM, Jay wrote:
> Unoptimized code quality is really bad,
> even compared to unoptimized C, even compared
> to gcc, and optimized code quality isn't great.
>
> I have this made up C code. I was verifying
> the struct return calling convention.
>
> typedef struct {
> unsigned a;
> unsigned b;
> unsigned c;
> } d;
>
> d F2(unsigned a, unsigned b, unsigned c)
> {
> d e;
> e.a = a * b * c;
> e.b = 123;
> e.c = a - b - c;
> return e;
> }
>
> and then the equivalent Modula-3:
>
> INTERFACE T;
>
> TYPE T1 = RECORD
> a,b,c : INTEGER
> END;
>
> PROCEDURE F2(a,b,c : INTEGER) : T1;
>
> END T.
>
> MODULE T;
>
> PROCEDURE F2(a,b,c : INTEGER) : T1 =
> VAR
> e : T1;
> BEGIN
> e.a := (a * b * c);
> e.b := 123;
> e.c := (a - b - c);
> RETURN e;
> END F2;
>
> BEGIN
> END T.
>
> Unoptimized Visual C++ 8.0:
>
> _F2:
> 00000000: 55 push ebp
> 00000001: 8B EC mov ebp,esp
> 00000003: 83 EC 0C sub esp,0Ch
> 00000006: 8B 45 0C mov eax,dword ptr [ebp+0Ch]
> 00000009: 0F AF 45 10 imul eax,dword ptr [ebp+10h]
> 0000000D: 0F AF 45 14 imul eax,dword ptr [ebp+14h]
> 00000011: 89 45 F4 mov dword ptr [ebp-0Ch],eax
> 00000014: C7 45 F8 7B 00 00 mov dword ptr [ebp-8],7Bh
> 00
> 0000001B: 8B 4D 0C mov ecx,dword ptr [ebp+0Ch]
> 0000001E: 2B 4D 10 sub ecx,dword ptr [ebp+10h]
> 00000021: 2B 4D 14 sub ecx,dword ptr [ebp+14h]
> 00000024: 89 4D FC mov dword ptr [ebp-4],ecx
> 00000027: 8B 55 08 mov edx,dword ptr [ebp+8]
> 0000002A: 8B 45 F4 mov eax,dword ptr [ebp-0Ch]
> 0000002D: 89 02 mov dword ptr [edx],eax
> 0000002F: 8B 4D F8 mov ecx,dword ptr [ebp-8]
> 00000032: 89 4A 04 mov dword ptr [edx+4],ecx
> 00000035: 8B 45 FC mov eax,dword ptr [ebp-4]
> 00000038: 89 42 08 mov dword ptr [edx+8],eax
> 0000003B: 8B 45 08 mov eax,dword ptr [ebp+8]
> 0000003E: 8B E5 mov esp,ebp
> 00000040: 5D pop ebp
> 00000041: C3 ret
>
> Visual C++ 8.0 with /O1
>
> It writes the results right into the output.
> _F2:
> 00000000: 55 push ebp
> 00000001: 8B EC mov ebp,esp
> 00000003: 8B 4D 0C mov ecx,dword ptr [ebp+0Ch]
> 00000006: 8B 45 08 mov eax,dword ptr [ebp+8]
> 00000009: 8B D1 mov edx,ecx
> 0000000B: 0F AF 55 10 imul edx,dword ptr [ebp+10h]
> 0000000F: 0F AF 55 14 imul edx,dword ptr [ebp+14h]
> 00000013: 2B 4D 10 sub ecx,dword ptr [ebp+10h]
> 00000016: 89 10 mov dword ptr [eax],edx
> 00000018: 2B 4D 14 sub ecx,dword ptr [ebp+14h]
> 0000001B: C7 40 04 7B 00 00 mov dword ptr [eax+4],7Bh
> 00
> 00000022: 89 48 08 mov dword ptr [eax+8],ecx
> 00000025: 5D pop ebp
> 00000026: C3 ret
>
> with /O2
>
> _F2:
> 00000000: 8B 4C 24 08 mov ecx,dword ptr [esp+8]
> 00000004: 8B 54 24 0C mov edx,dword ptr [esp+0Ch]
> 00000008: 8B 44 24 04 mov eax,dword ptr [esp+4]
> 0000000C: 56 push esi
> 0000000D: 8B 74 24 14 mov esi,dword ptr [esp+14h]
> 00000011: 57 push edi
> 00000012: 8B F9 mov edi,ecx
> 00000014: 0F AF FA imul edi,edx
> 00000017: 0F AF FE imul edi,esi
> 0000001A: 2B CA sub ecx,edx
> 0000001C: 89 38 mov dword ptr [eax],edi
> 0000001E: 2B CE sub ecx,esi
> 00000020: 5F pop edi
> 00000021: C7 40 04 7B 00 00 mov dword ptr [eax+4],7Bh
> 00
> 00000028: 89 48 08 mov dword ptr [eax+8],ecx
> 0000002B: 5E pop esi
> 0000002C: C3 ret
>
> gcc 3.4.5 unoptimized
>
> _F2:
> 00000000: 55 push ebp
> 00000001: 89 E5 mov ebp,esp
> 00000003: 83 EC 18 sub esp,18h
> 00000006: 8B 4D 08 mov ecx,dword ptr [ebp+8]
> 00000009: 8B 45 0C mov eax,dword ptr [ebp+0Ch]
> 0000000C: 0F AF 45 10 imul eax,dword ptr [ebp+10h]
> 00000010: 0F AF 45 14 imul eax,dword ptr [ebp+14h]
> 00000014: 89 45 E8 mov dword ptr [ebp-18h],eax
> 00000017: C7 45 EC 7B 00 00 mov dword ptr [ebp-14h],7Bh
> 00
> 0000001E: 8B 55 10 mov edx,dword ptr [ebp+10h]
> 00000021: 8B 45 0C mov eax,dword ptr [ebp+0Ch]
> 00000024: 29 D0 sub eax,edx
> 00000026: 2B 45 14 sub eax,dword ptr [ebp+14h]
> 00000029: 89 45 F0 mov dword ptr [ebp-10h],eax
> 0000002C: 8B 45 E8 mov eax,dword ptr [ebp-18h]
> 0000002F: 89 01 mov dword ptr [ecx],eax
> 00000031: 8B 45 EC mov eax,dword ptr [ebp-14h]
> 00000034: 89 41 04 mov dword ptr [ecx+4],eax
> 00000037: 8B 45 F0 mov eax,dword ptr [ebp-10h]
> 0000003A: 89 41 08 mov dword ptr [ecx+8],eax
> 0000003D: 89 C8 mov eax,ecx
> 0000003F: C9 leave
> 00000040: C2 04 00 ret 4
> 00000043: 90 nop
> 00000044: 90 nop
> ...more nops for padding...
>
> gcc -O1
>
> _F2:
> 00000000: 55 push ebp
> 00000001: 89 E5 mov ebp,esp
> 00000003: 83 EC 18 sub esp,18h
> 00000006: 89 5D F8 mov dword ptr [ebp-8],ebx
> 00000009: 89 75 FC mov dword ptr [ebp-4],esi
> 0000000C: 8B 45 08 mov eax,dword ptr [ebp+8]
> 0000000F: 8B 4D 0C mov ecx,dword ptr [ebp+0Ch]
> 00000012: 8B 5D 10 mov ebx,dword ptr [ebp+10h]
> 00000015: 8B 75 14 mov esi,dword ptr [ebp+14h]
> 00000018: 89 CA mov edx,ecx
> 0000001A: 0F AF D3 imul edx,ebx
> 0000001D: 0F AF D6 imul edx,esi
> 00000020: 29 D9 sub ecx,ebx
> 00000022: 29 F1 sub ecx,esi
> 00000024: 89 10 mov dword ptr [eax],edx
> 00000026: C7 40 04 7B 00 00 mov dword ptr [eax+4],7Bh
> 00
> 0000002D: 89 48 08 mov dword ptr [eax+8],ecx
> 00000030: 8B 5D F8 mov ebx,dword ptr [ebp-8]
> 00000033: 8B 75 FC mov esi,dword ptr [ebp-4]
> 00000036: 89 EC mov esp,ebp
> 00000038: 5D pop ebp
> 00000039: C2 04 00 ret 4
> 0000003C: 90 nop
> 0000003D: 90 nop
> 0000003E: 90 nop
> 0000003F: 90 nop
>
> I didn't investigate all the switches much and simple -O vs. -O1
> vs. -O2 vs. -o3
> all do the same.
>
> -O1 -fomit-frame-pointer netted:
>
> _F2:
> 00000000: 83 EC 1C sub esp,1Ch
> 00000003: 89 5C 24 14 mov dword ptr [esp+14h],ebx
> 00000007: 89 74 24 18 mov dword ptr [esp+18h],esi
> 0000000B: 8B 44 24 20 mov eax,dword ptr [esp+20h]
> 0000000F: 8B 4C 24 24 mov ecx,dword ptr [esp+24h]
> 00000013: 8B 5C 24 28 mov ebx,dword ptr [esp+28h]
> 00000017: 8B 74 24 2C mov esi,dword ptr [esp+2Ch]
> 0000001B: 89 CA mov edx,ecx
> 0000001D: 0F AF D3 imul edx,ebx
> 00000020: 0F AF D6 imul edx,esi
> 00000023: 29 D9 sub ecx,ebx
> 00000025: 29 F1 sub ecx,esi
> 00000027: 89 10 mov dword ptr [eax],edx
> 00000029: C7 40 04 7B 00 00 mov dword ptr [eax+4],7Bh
> 00
> 00000030: 89 48 08 mov dword ptr [eax+8],ecx
> 00000033: 8B 5C 24 14 mov ebx,dword ptr [esp+14h]
> 00000037: 8B 74 24 18 mov esi,dword ptr [esp+18h]
> 0000003B: 83 C4 1C add esp,1Ch
> 0000003E: C2 04 00 ret 4
> 00000041: 90 nop
> .. more nops ..
>
> Ok, ready, here goes the unoptimized Modula-3.
> Notice the duplicate copying of the return value and the *doubling*
> in code size.
>
> _T__F2:
> 00000000: 55 push ebp
> 00000001: 89 E5 mov ebp,esp
> 00000003: 57 push edi
> 00000004: 56 push esi
> 00000005: 83 EC 30 sub esp,30h
> 00000008: 8B 55 0C mov edx,dword ptr [ebp+0Ch]
> 0000000B: 8B 45 10 mov eax,dword ptr [ebp+10h]
> 0000000E: 0F AF D0 imul edx,eax
> 00000011: 8B 45 14 mov eax,dword ptr [ebp+14h]
> 00000014: 0F AF D0 imul edx,eax
> 00000017: 8B 45 D4 mov eax,dword ptr [ebp-2Ch]
> 0000001A: 83 E0 00 and eax,0
> 0000001D: 09 D0 or eax,edx
> 0000001F: 89 45 D4 mov dword ptr [ebp-2Ch],eax
> 00000022: 8B 45 D8 mov eax,dword ptr [ebp-28h]
> 00000025: 83 E0 00 and eax,0
> 00000028: 83 C8 7B or eax,7Bh
> 0000002B: 89 45 D8 mov dword ptr [ebp-28h],eax
> 0000002E: 8B 55 0C mov edx,dword ptr [ebp+0Ch]
> 00000031: 8B 45 10 mov eax,dword ptr [ebp+10h]
> 00000034: 29 C2 sub edx,eax
> 00000036: 8B 45 14 mov eax,dword ptr [ebp+14h]
> 00000039: 29 C2 sub edx,eax
> 0000003B: 8B 45 DC mov eax,dword ptr [ebp-24h]
> 0000003E: 83 E0 00 and eax,0
> 00000041: 09 D0 or eax,edx
> 00000043: 89 45 DC mov dword ptr [ebp-24h],eax
> 00000046: 8B 45 08 mov eax,dword ptr [ebp+8]
> 00000049: 89 C2 mov edx,eax
> 0000004B: 8D 45 D4 lea eax,[ebp-2Ch]
> 0000004E: 8D 7D EC lea edi,[ebp-14h]
> 00000051: 89 C6 mov esi,eax
> 00000053: FC cld
> 00000054: A5 movs dword ptr es:[edi],dword
> ptr [esi]
> 00000055: A5 movs dword ptr es:[edi],dword
> ptr [esi]
> 00000056: A5 movs dword ptr es:[edi],dword
> ptr [esi]
> 00000057: 89 D7 mov edi,edx
> 00000059: 8D 75 EC lea esi,[ebp-14h]
> 0000005C: FC cld
> 0000005D: A5 movs dword ptr es:[edi],dword
> ptr [esi]
> 0000005E: A5 movs dword ptr es:[edi],dword
> ptr [esi]
> 0000005F: A5 movs dword ptr es:[edi],dword
> ptr [esi]
> 00000060: 83 C4 30 add esp,30h
> 00000063: 5E pop esi
> 00000064: 5F pop edi
> 00000065: C9 leave
> 00000066: C3 ret
>
> optimized Modula-3 is better, saves the extra copy, but still not
> great:
>
> _T__F2:
> 00000000: 55 push ebp
> 00000001: 89 E5 mov ebp,esp
> 00000003: 57 push edi
> 00000004: 56 push esi
> 00000005: 53 push ebx
> 00000006: 83 EC 10 sub esp,10h
> 00000009: 8B 55 0C mov edx,dword ptr [ebp+0Ch]
> 0000000C: 8B 4D 10 mov ecx,dword ptr [ebp+10h]
> 0000000F: 8B 5D 14 mov ebx,dword ptr [ebp+14h]
> 00000012: 89 D0 mov eax,edx
> 00000014: 0F AF C1 imul eax,ecx
> 00000017: 0F AF C3 imul eax,ebx
> 0000001A: 8B 75 E8 mov esi,dword ptr [ebp-18h]
> 0000001D: 89 45 E8 mov dword ptr [ebp-18h],eax
> 00000020: 8B 45 EC mov eax,dword ptr [ebp-14h]
> 00000023: C7 45 EC 7B 00 00 mov dword ptr [ebp-14h],7Bh
> 00
> 0000002A: 29 CA sub edx,ecx
> 0000002C: 29 DA sub edx,ebx
>
> Huh what does this next instruction achieve?
> 0000002E: 8B 45 F0 mov eax,dword ptr [ebp-10h]
> 00000031: 89 55 F0 mov dword ptr [ebp-10h],edx
> 00000034: 8B 7D 08 mov edi,dword ptr [ebp+8]
> 00000037: 8D 75 E8 lea esi,[ebp-18h]
> 0000003A: FC cld
> 0000003B: A5 movs dword ptr es:[edi],dword
> ptr [esi]
> 0000003C: A5 movs dword ptr es:[edi],dword
> ptr [esi]
> 0000003D: A5 movs dword ptr es:[edi],dword
> ptr [esi]
> 0000003E: 83 C4 10 add esp,10h
> 00000041: 5B pop ebx
> 00000042: 5E pop esi
> 00000043: 5F pop edi
> 00000044: C9 leave
> 00000045: C3 ret
>
> and lastly the integrated backend, which runs much faster, and I
> believe has just one mode,
> somewhat optimized (but still lacks int64 support):
>
> _T__F2:
> 00000000: 55 push ebp
> 00000001: 8B EC mov ebp,esp
> 00000003: 81 EC 18 00 00 00 sub esp,18h
> 00000009: 53 push ebx
> 0000000A: 56 push esi
> 0000000B: 57 push edi
> 0000000C: 8B 5D 10 mov ebx,dword ptr [ebp+10h]
> 0000000F: 0F AF 5D 0C imul ebx,dword ptr [ebp+0Ch]
> 00000013: 0F AF 5D 14 imul ebx,dword ptr [ebp+14h]
> 00000017: 89 5D F4 mov dword ptr [ebp-0Ch],ebx
> 0000001A: C7 45 F8 7B 00 00 mov dword ptr [ebp-8],7Bh
> 00
> 00000021: 8B 55 0C mov edx,dword ptr [ebp+0Ch]
> 00000024: 2B 55 10 sub edx,dword ptr [ebp+10h]
> 00000027: 2B 55 14 sub edx,dword ptr [ebp+14h]
> 0000002A: 89 55 FC mov dword ptr [ebp-4],edx
> 0000002D: 8D 75 F4 lea esi,[ebp-0Ch]
> 00000030: 8B 7D 08 mov edi,dword ptr [ebp+8]
> 00000033: 8B 4E 00 mov ecx,dword ptr [esi]
> 00000036: 89 4F 00 mov dword ptr [edi],ecx
> 00000039: 8B 4E 04 mov ecx,dword ptr [esi+4]
> 0000003C: 89 4F 04 mov dword ptr [edi+4],ecx
> 0000003F: 8B 4E 08 mov ecx,dword ptr [esi+8]
> 00000042: 89 4F 08 mov dword ptr [edi+8],ecx
> 00000045: 8B C7 mov eax,edi
> 00000047: 5F pop edi
> 00000048: 5E pop esi
> 00000049: 5B pop ebx
> 0000004A: C9 leave
> 0000004B: C3 ret
>
> I haven't looked at all this in depth, but the doubling in size,
> the extra copy,
> that the integrated backend's code is fairly small...
>
> - Jay
>
> Shed those extra pounds with MSN and The Biggest Loser! Learn more.
More information about the M3devel
mailing list