17

I was having a look at the IL generated for a very simple method because I want to do a small bit of reflection emitting myself and I came across something that is mentioned in the comments in this question (but was not the question):Using Br_S OpCode to point to next instruction using Reflection.Emit.Label and nobody answered it and I am wondering about it. so...

If I have a method like this:

    public string Test()
    {            
        return "hello";
    }

and then I run ILDASM on it I see the IL is this:

.method public hidebysig instance string 
        Test() cil managed
{
  // Code size       11 (0xb)
  .maxstack  1
  .locals init ([0] string CS$1$0000)
  IL_0000:  nop
  IL_0001:  ldstr      "hello"
  IL_0006:  stloc.0
  IL_0007:  br.s       IL_0009
  IL_0009:  ldloc.0
  IL_000a:  ret
}

The part that I find curious is:

  IL_0007:  br.s       IL_0009
  IL_0009:  ldloc.0

The first line is doing an Unconditional Transfer to the second line. What is the reason for this operation, doesn't it do nothing?

EDIT

It seems my question was phrased badly as there is some confusion over what I wanted to know. The last sentence should maybe be something like this:

What is the reason that the compiler has output this unconditional transfer statement when it seems to be serving no purpose?

UPDATE

The suggestion that it was for a breakpoint made me think to try and compile this in Release mode and sure enough the part that I am interested in vanished and the IL became just this (which is why I jumped the gun and thought that the breakpoint answer was the reason):

.method public hidebysig instance string 
        Test() cil managed
{
  // Code size       6 (0x6)
  .maxstack  8
  IL_0000:  ldstr      "hello"
  IL_0005:  ret
} 

The question of "why is it there" still plays on my mind though - if it is not the way the compiler always works and it is not for some useful debugging reason (like having somewhere to place a breakpoint) why have it at all?

I guess the answer is probably: "just the way it has been made, no solid reason, and it doesn't really matter because the JIT will sort it all out nicely in the end."

I wish I'd not asked this now, this is going to ruin my acceptance percentage!! :-)

4
  • 1
    I don't know why the compiler does this and I suspect that the compiler behaves differently depending on the target (x86 vs. 64 bit...). The only ones who possibly could answer your question are very few people at Microsoft. I suspect though that these things like nop etc. get optimized away at runtime by the JIT... it might also be easier for example in case of try/catch blocks to already have such instructions in place without any harm...
    – Yahia
    Feb 3, 2012 at 8:05
  • @Yahia I'm sure Jon Skeet could answer this.
    – Rich
    Feb 3, 2012 at 15:04
  • @Rich perhaps... certainly Eric Lippert could give an insightful answer since he word at MS.
    – Yahia
    Feb 3, 2012 at 15:20
  • Your question was phrased perfectly. I found it because I am trying to debug some problems with woven code using Fody and the IL from LINQpad contained a similar br.s to the next instruction. Thanks, it (with Guffa's answer) gave me one less thing to puzzle over!
    – Andy Dent
    Aug 6, 2015 at 6:18

4 Answers 4

13

The first of the two instructions is part of the standard code for the return statement, the second instruction is part of the boilerplate code for the method.

The return statement puts the return value in a local variable, then it jumps to the exit point of the method:

IL_0001:  ldstr      "hello"
IL_0006:  stloc.0
IL_0007:  br.s       IL_0009

The boilerplate code of the method gets the return value from the local variable and then exits from the method:

IL_0009:  ldloc.0
IL_000a:  ret

In the IL code that the compiler creates, a method always have a single exit point. That's why the return statement jumps to that location instead of just exiting the function directly. The code for the return statement is always the same, so there is always a branch even if it jumps to the next instruction.

The compiler often produces IL code that looks inefficient, because the JIT compiler optimises the code. The compiler produces unoptimised, simple and predictable code which is easier for the JIT compiler to optimise.

10

One reason the NOP instruction is in debug builds it to enable you to set breakpoints.

VB.net internals, go down to debuging

Visual Basic .NET allows you to set breakpoints on non-executing lines of code such as End If, End Sub, and Dim statements. To facilitate this popular debugging technique, the compiler inserts nop instructions as placeholders for the non-executing lines of code (since non-executing lines are not translated into IL instructions). The nop instruction is a "no operation" instruction—it does not perform any meaningful work yet can consume a processing cycle.

6
  • I was compiling it in "debug" - I did not think to try switching the compilation mode - thank you! When I went to release mode the statements disappeared so I think you are probably correct in this instance.
    – kmp
    Feb 3, 2012 at 8:44
  • @user1039947: This doesn't answer your question at all, it answers the question why the nop instruction is in the code.
    – Guffa
    Feb 3, 2012 at 9:27
  • That was my question - but I think I phased it wrong and peer was the person who thinks closest to me! I was wanting to know why the compiler would put this operation in the IL that seems to serve no purpose (as it just jumps to the next line) - it serves a purpose if it is to allow you to put a break point on it though and it does go away when I go to release mode
    – kmp
    Feb 3, 2012 at 9:52
  • @user1039947: From your question, your edit, and your comment, it sound like you are asking about the br.s operation, not the nop operation. The br.s operation is not there to create a debugging point.
    – Guffa
    Feb 3, 2012 at 11:13
  • 1
    @Guffa 2 (because I ran out of characters!) It would suggest to me that the code is there for some purpose only valid in debugging and it is not just the way the compiler always does things, so the suggestion that it is for a breakpoint makes sense to my little brain - maybe there is an alternate reason why it exists?
    – kmp
    Feb 3, 2012 at 11:41
1

A quick look on the internet sugests that it might be the code for the return statement. It's suposed to return the code to the end of the function and it has to work from anywhere in the function body. I'm guessing it could be optimised but you didn't mention the optimization level of your code.

1

In the intermediate language, when you see the instruction "br", this indicates a branch. A branch instruction is a conditional instruction that will change what instruction is executed next based on the condition. In the C# language, things such as goto, while, for, break, and return may be implemented with variants of the branch instruction.

http://www.dotnetperls.com/il

1
  • 1
    That does not answer this question at all.
    – H H
    Feb 3, 2012 at 8:58

Your Answer

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

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