While doing my day job, I was stepping through some JIT-compiled x86 code using the VS.NET debugger. It got me interested in the differences in generated code between code that was compiled without and with optimizations. So I decided to write this entry to record what I saw.
Here’s the simplest possible method that I could think of:
static int Add( int x, int y )
{
return x + y;
}
The first thing to examine are the differences between the CIL that is generated by the CSC compiler. When you compile the code without using the /optimize switch, this is the CIL that was generated:
.method private hidebysig static int32 SafeAdd(int32 x,
int32 y) cil managed
{
// Code size 8 (0x8)
.maxstack 2
.locals ([0] int32 CS$00000003$00000000)
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: add
IL_0003: stloc.0
IL_0004: br.s IL_0006
IL_0006: ldloc.0
IL_0007: ret
} // end of method Class1::SafeAdd
When you compile this code using the /optimize switch, this is the CIL that I got:
.method private hidebysig static int32 SafeAdd(int32 x,
int32 y) cil managed
{
// Code size 4 (0x4)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: add
IL_0003: ret
} // end of method Class1::SafeAdd
It’s interesting that the “debug” build created an extra local variable to hold the intermediate results of the calculation. This is presumably done so that a debugger can let you step over the addition and see the result of that calculation. There is also the redundant br.s instruction that does nothing more than do an unconditional branch to the next line of code. Apparently this is done so that you can set a breakpoint on either the return x + y; line or the terminating curly brace.
When I examined the “debug” build using the VS.NET debugger in disassembly mode, this is the x86 instruction stream that I saw:
00000000 push ebp ; setup stack frame
00000001 mov ebp,esp
00000003 sub esp,0Ch
00000006 push edi ; save edi
00000007 push esi ; save esi
00000008 push ebx ; save ebx
00000009 mov ebx,ecx ; x is in ecx
0000000b mov esi,edx ; y is in edx
0000000d xor edi,edi ; zero edi
0000000f lea eax,[ebx+esi] ; add x and y, result in eax
00000012 mov edi,eax ; store result in edi
00000014 jmp 00000016
00000016 mov eax,edi ; move result to eax
00000018 pop ebx ; restore ebx
00000019 pop esi ; restore esi
0000001a pop edi ; restore edi
0000001b mov esp,ebp ; cleanup stack frame
0000001d pop ebp
0000001e ret
There are a number of interesting observations that we can make about this code. First, we can clearly see the fastcall calling convention in use: the ecx and edx registers hold the x and y parameters respectively. We can see that the return value of the method is returned in the eax register. We also see that space is being reserved on the stack for two parameters + a temporary variable even though x86 registers are used for all three values!
It’s interesting to note that the edi register is being used as a temporary variable in this case.
When I examined the optimized build using the VS.NET debugger in disassembly mode, this is the x86 instruction stream that I saw:
00000000 push ebp ; setup stack frame
00000001 mov ebp,esp
00000003 sub esp,8
00000006 push edi ; save edi
00000007 push esi ; save esi
00000008 mov edi,ecx ; x is in ecx
0000000a mov esi,edx ; y is in edx
0000000c lea eax,[edi+esi] ; add x and y, result in eax
0000000f pop esi ; restore esi
00000010 pop edi ; restore edi
00000011 mov esp,ebp ; clean up stack frame
00000013 pop ebp
00000014 ret
In this case, we can see that no code was generated for the temporary variable, and the redundant jmp instruction has been eliminated. We also see that space is being reserved on the stack only for two parameters as the temporary variable is no longer used. There is no need to save the ebx register in this case since the edi register is no longer being used as a temporary variable.
I’m actually somewhat surprised that the sub esp, x instruction is even generated when it is pretty clear that the stack frame is never used due to the enregistration of the parameters and the variables. This looks like a healthy candidate for future optimization of small functions that never use the stack frame.