7

Suppose I have the following code:

public class Class1
{
    private Class2 obj;

    public void MethodA()
    {
        var class2 = new Class2();
        class2.PropertyI = 2;
        obj = MethodB(class2);
    }

    public Class2 MethodB(Class2 class2)
    {
        return class2;
    }
}

public class Class2
{
    public int PropertyI { get; set; }
}

The generated IL code from compiling with Visual Studio 2010 as a .NET 2.0 assembly is the following:

.method public hidebysig instance void MethodA() cil managed
{
    .maxstack 3
    .locals init (
        [0] class ClassLibrary1.Class2 class2)
    L_0000: nop 
    L_0001: newobj instance void ClassLibrary1.Class2::.ctor()
    L_0006: stloc.0 
    L_0007: ldloc.0 
    L_0008: ldc.i4.2 
    L_0009: callvirt instance void ClassLibrary1.Class2::set_PropertyI(int32)
    L_000e: nop 
    L_000f: ldarg.0 
    L_0010: ldarg.0 
    L_0011: ldloc.0 
    L_0012: call instance class ClassLibrary1.Class2 ClassLibrary1.Class1::MethodB(class ClassLibrary1.Class2)
    L_0017: stfld class ClassLibrary1.Class2 ClassLibrary1.Class1::obj
    L_001c: ret 
}

.method public hidebysig instance class ClassLibrary1.Class2 MethodB(class ClassLibrary1.Class2 class2) cil managed
{
    .maxstack 1
    .locals init (
        [0] class ClassLibrary1.Class2 CS$1$0000)
    L_0000: nop 
    L_0001: ldarg.1 
    L_0002: stloc.0 
    L_0003: br.s L_0005
    L_0005: ldloc.0 
    L_0006: ret 
}

My questions are the following:

  1. In MethodA, why is there not a nop code between L_0006 and L_0007?
    • Since L_0001 to L_0006 are distinct from L_0007 to L_0009, why is there no nop opcode?
  2. In MethodB, why is L_0003 necessary?
2
  • 1
    Are you compiling in Release? Because I've heard that at method headers, the debug compilation has NOP codes.
    – Ray
    Mar 22, 2013 at 22:30
  • I am compiling in Debug.
    – cm007
    Mar 22, 2013 at 22:30

2 Answers 2

10

The C# compiler emits a NOP instruction at a curly brace. Which makes it a lot easier to set breakpoints in your code. The debugger only permits setting a breakpoint on code and a curly brace doesn't normally produce any code. So this is just a simple debugging aid, these NOPs won't get generated in the release build.

The BR.S instruction is a minor flaw in the compiler, it doesn't have a peephole optimizer to get rid of these kind of extraneous instructions. In general, it is not the job of the C# compiler to optimize code, that's done by the jitter. Which will readily and easily remove the instruction.

3
  • I'm pretty sure the branch will be removed in Release mode. It is common that such garbage is produced by early compiler passes and removing it is trivial.
    – usr
    Mar 22, 2013 at 22:50
  • My question was more along the lines of, why is there a nop opcode at L_000e but not between L_0006 and L_0007? I see that a nop opcode is needed at L_0000 for setting a breakpoint at the curly brace beginning the method, but why at L_000e and not between L_0006 and L_0007?
    – cm007
    Mar 23, 2013 at 2:00
  • 2
    There's no curly brace at L_0006. L_000e is unnecessary the same way the BR.S was unnecessary. It really doesn't matter, this doesn't have to be flawless. Bugs get fixed when they need to be fixed, not because they exist. Getting rid of spurious code gen from a recursive decent parser can be difficult and fixing it isn't always worth the risk of breaking it. If you want to chase down the reason then you can study the source code of the C# compiler from the SSCLI20 distribution, csharp/sccomp subdirectory. Mar 23, 2013 at 2:21
1

All of what you see is because you're compiling in Debug mode. The redundant jumps and nops are disabled optimization passes as well as debugging support (I believe).

Compile in Release mode.

1
  • Thanks. Compiling in Release mode gave MethodB just the two instructions ldarg.1 ret.
    – cm007
    Mar 23, 2013 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.