Negative optimization in C# compiler?

G

Gianluca

..method public hidebysig newslot virtual final instance object
GetValue(object target) cil managed
{
// Code Size: 16 byte(s)
.maxstack 1
.locals (
object obj1)
L_0000: ldarg.1
L_0001: castclass WindowsApplication2.CMyClass
L_0006: ldfld string WindowsApplication2.CMyClass::sName
L_000b: stloc.0
L_000c: br.s L_000e
L_000e: ldloc.0
L_000f: ret
}


This is a simple GetValue() { return value } method. What's the point of
L_000c branching to the next instruction at L_000e?
 
W

Willy Denoyette [MVP]

Gianluca said:
.method public hidebysig newslot virtual final instance object
GetValue(object target) cil managed
{
// Code Size: 16 byte(s)
.maxstack 1
.locals (
object obj1)
L_0000: ldarg.1
L_0001: castclass WindowsApplication2.CMyClass
L_0006: ldfld string WindowsApplication2.CMyClass::sName
L_000b: stloc.0
L_000c: br.s L_000e
L_000e: ldloc.0
L_000f: ret
}


This is a simple GetValue() { return value } method. What's the point of
L_000c branching to the next instruction at L_000e?

Doesn't look like an optimized build, did you compile this using /optimize+
?.

Willy.
 
W

Willy Denoyette [MVP]

Could you post your code and the version of the CLR you run this on?
The IL source you posted comes from Reflector, I prefer ILDasm....

Here is what I get ...

..method public hidebysig virtual final instance object
GetValue(object target) cil managed
{
// Code size 12 (0xc)
.maxstack 1
IL_0000: ldarg.1
IL_0001: castclass Willys.MyClass
IL_0006: ldfld string Willys.MyClass::sName
IL_000b: ret
} // end of method T::GetValue

from ...

public sealed override object GetValue(object target)
{
return ((MyClass)target).sName;
}

Willy.
 
G

Gianluca

Yes, this is what I get too with /optimze. Otherwise the dummy branch is
there:

..method public hidebysig instance object
GetValue(object target,
object 'value') cil managed
{
// Code size 16 (0x10)
.maxstack 1
.locals init (object V_0)
IL_0000: ldarg.1
IL_0001: castclass WindowsApplication2.Form1
IL_0006: ldfld string WindowsApplication2.Form1::sName
IL_000b: stloc.0
IL_000c: br.s IL_000e
IL_000e: ldloc.0
IL_000f: ret
} // end of method Test::GetValue


from:
public object GetValue(object target, object value)
{
return ((Form1)target).sName;
}

(version 1.1.4322.573)

I also have a strange problem with the Emit method, I'll explain in the next
post.
 
W

Willy Denoyette [MVP]

Gianluca said:
Yes, this is what I get too with /optimze. Otherwise the dummy branch is
there:

.method public hidebysig instance object
GetValue(object target,
object 'value') cil managed
{
// Code size 16 (0x10)
.maxstack 1
.locals init (object V_0)
IL_0000: ldarg.1
IL_0001: castclass WindowsApplication2.Form1
IL_0006: ldfld string WindowsApplication2.Form1::sName
IL_000b: stloc.0
IL_000c: br.s IL_000e
IL_000e: ldloc.0
IL_000f: ret
} // end of method Test::GetValue


from:
public object GetValue(object target, object value)
{
return ((Form1)target).sName;
}

(version 1.1.4322.573)

I also have a strange problem with the Emit method, I'll explain in the
next
post.

The generated IL isn't optimized unless you specify /optimize.
Why do you consider this as a problem?

Willy.
 
G

Gianluca

I don't consider it a big problem. But I don't consider optimization
not-adding a non-existent branch. In any case, I don't mean to start a
discussion about this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top