mirror of https://github.com/icsharpcode/ILSpy.git
27 changed files with 677 additions and 47 deletions
@ -0,0 +1,143 @@ |
|||||||
|
.assembly extern mscorlib |
||||||
|
{ |
||||||
|
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. |
||||||
|
.ver 4:0:0:0 |
||||||
|
} |
||||||
|
|
||||||
|
.assembly _ |
||||||
|
{ |
||||||
|
.hash algorithm 0x00008004 // SHA1 |
||||||
|
.ver 0:0:0:0 |
||||||
|
} |
||||||
|
|
||||||
|
.class public auto ansi beforefieldinit NonGenericConstrainedCallVirt |
||||||
|
extends [mscorlib]System.Object |
||||||
|
{ |
||||||
|
// Methods |
||||||
|
.method public hidebysig static |
||||||
|
void Main () cil managed |
||||||
|
{ |
||||||
|
// Method begins at RVA 0x2050 |
||||||
|
// Code size 16 (0x10) |
||||||
|
.entrypoint |
||||||
|
.maxstack 8 |
||||||
|
|
||||||
|
IL_0000: ldstr "A" |
||||||
|
IL_0005: newobj instance void C::.ctor(string) |
||||||
|
IL_000a: call void NonGenericConstrainedCallVirt::Foo(class C) |
||||||
|
IL_000f: ret |
||||||
|
} // end of method NonGenericConstrainedCallVirt::Main |
||||||
|
|
||||||
|
.method private hidebysig static |
||||||
|
void Foo ( |
||||||
|
class C arg |
||||||
|
) cil managed |
||||||
|
{ |
||||||
|
// Method begins at RVA 0x2064 |
||||||
|
// Code size 25 (0x19) |
||||||
|
.maxstack 8 |
||||||
|
|
||||||
|
IL_0000: ldarga arg |
||||||
|
IL_0004: ldarga arg |
||||||
|
IL_0008: call int32 NonGenericConstrainedCallVirt::Bar(class C&) |
||||||
|
IL_000d: constrained. C |
||||||
|
IL_0013: callvirt instance void C::Baz(int32) |
||||||
|
IL_0018: ret |
||||||
|
} // end of method NonGenericConstrainedCallVirt::Foo |
||||||
|
|
||||||
|
.method private hidebysig static |
||||||
|
int32 Bar ( |
||||||
|
class C& o |
||||||
|
) cil managed |
||||||
|
{ |
||||||
|
// Method begins at RVA 0x2080 |
||||||
|
// Code size 14 (0xe) |
||||||
|
.maxstack 8 |
||||||
|
|
||||||
|
IL_0000: ldarg.0 |
||||||
|
IL_0001: ldstr "B" |
||||||
|
IL_0006: newobj instance void C::.ctor(string) |
||||||
|
IL_000b: stind.ref |
||||||
|
IL_000c: ldc.i4.0 |
||||||
|
IL_000d: ret |
||||||
|
} // end of method NonGenericConstrainedCallVirt::Bar |
||||||
|
|
||||||
|
.method public hidebysig specialname rtspecialname |
||||||
|
instance void .ctor () cil managed |
||||||
|
{ |
||||||
|
// Method begins at RVA 0x2090 |
||||||
|
// Code size 7 (0x7) |
||||||
|
.maxstack 8 |
||||||
|
|
||||||
|
IL_0000: ldarg.0 |
||||||
|
IL_0001: call instance void [mscorlib]System.Object::.ctor() |
||||||
|
IL_0006: ret |
||||||
|
} // end of method NonGenericConstrainedCallVirt::.ctor |
||||||
|
|
||||||
|
} // end of class NonGenericConstrainedCallVirt |
||||||
|
|
||||||
|
.class public auto ansi beforefieldinit C |
||||||
|
extends [mscorlib]System.Object |
||||||
|
{ |
||||||
|
// Fields |
||||||
|
.field private initonly string '<Name>k__BackingField' |
||||||
|
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( |
||||||
|
01 00 00 00 |
||||||
|
) |
||||||
|
|
||||||
|
// Methods |
||||||
|
.method public hidebysig specialname rtspecialname |
||||||
|
instance void .ctor ( |
||||||
|
string n |
||||||
|
) cil managed |
||||||
|
{ |
||||||
|
// Method begins at RVA 0x2098 |
||||||
|
// Code size 14 (0xe) |
||||||
|
.maxstack 8 |
||||||
|
|
||||||
|
IL_0000: ldarg.0 |
||||||
|
IL_0001: call instance void [mscorlib]System.Object::.ctor() |
||||||
|
IL_0006: ldarg.0 |
||||||
|
IL_0007: ldarg.1 |
||||||
|
IL_0008: stfld string C::'<Name>k__BackingField' |
||||||
|
IL_000d: ret |
||||||
|
} // end of method C::.ctor |
||||||
|
|
||||||
|
.method public hidebysig |
||||||
|
instance void Baz ( |
||||||
|
int32 arg |
||||||
|
) cil managed |
||||||
|
{ |
||||||
|
// Method begins at RVA 0x20a8 |
||||||
|
// Code size 12 (0xc) |
||||||
|
.maxstack 8 |
||||||
|
|
||||||
|
IL_0000: ldarg.0 |
||||||
|
IL_0001: call instance string C::get_Name() |
||||||
|
IL_0006: call void [mscorlib]System.Console::WriteLine(string) |
||||||
|
IL_000b: ret |
||||||
|
} // end of method C::Baz |
||||||
|
|
||||||
|
.method public hidebysig specialname |
||||||
|
instance string get_Name () cil managed |
||||||
|
{ |
||||||
|
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( |
||||||
|
01 00 00 00 |
||||||
|
) |
||||||
|
// Method begins at RVA 0x20b8 |
||||||
|
// Code size 7 (0x7) |
||||||
|
.maxstack 8 |
||||||
|
|
||||||
|
IL_0000: ldarg.0 |
||||||
|
IL_0001: ldfld string C::'<Name>k__BackingField' |
||||||
|
IL_0006: ret |
||||||
|
} // end of method C::get_Name |
||||||
|
|
||||||
|
// Properties |
||||||
|
.property instance string Name() |
||||||
|
{ |
||||||
|
.get instance string C::get_Name() |
||||||
|
} |
||||||
|
|
||||||
|
} // end of class C |
||||||
|
|
@ -0,0 +1,145 @@ |
|||||||
|
// Copyright (c) 2025 Siegfried Pammer
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
#nullable enable |
||||||
|
|
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Linq; |
||||||
|
|
||||||
|
namespace ICSharpCode.Decompiler.IL.Transforms |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// [stloc address(...)]
|
||||||
|
/// stloc temp(default.value ``0)
|
||||||
|
/// if (comp.o(ldloc temp == ldnull)) Block IL_002a {
|
||||||
|
/// stloc temp(ldobj ``0(ldloc address))
|
||||||
|
/// stloc address(ldloca temp)
|
||||||
|
/// }
|
||||||
|
/// stloc V_i(expr_i)
|
||||||
|
/// ...(constrained[``0].callvirt Method(ldobj.if.ref ``0(ldloc address), ldloc V_i ...))...
|
||||||
|
///
|
||||||
|
///=>
|
||||||
|
///
|
||||||
|
/// [stloc address(...)]
|
||||||
|
/// stloc address(ldobj.if.ref(ldloc address))
|
||||||
|
/// stloc V_i(expr_i)
|
||||||
|
/// ...(constrained[``0].callvirt Method(ldobj.if.ref ``0(ldloc address), ldloc V_i ...))...
|
||||||
|
///
|
||||||
|
/// Then ldobj.if.ref in the call is redundant because any object reference was already loaded into an immutable temporary.
|
||||||
|
/// So we can removed and inlining of the arguments (V_i) becomes possible.
|
||||||
|
///
|
||||||
|
/// Finally the newly created ldobj.if.ref is inlined into the place where the old ldobj.if.ref was.
|
||||||
|
///
|
||||||
|
/// =>
|
||||||
|
///
|
||||||
|
/// [stloc address(...)]
|
||||||
|
/// ...(constrained[``0].callvirt Method(ldobj.if.ref ``0(ldloc address), expr_i ...))...
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
class RemoveUnconstrainedGenericReferenceTypeCheck : IStatementTransform |
||||||
|
{ |
||||||
|
void IStatementTransform.Run(Block block, int pos, StatementTransformContext context) |
||||||
|
{ |
||||||
|
int startPos = pos; |
||||||
|
// stloc temp(default.value ``0)
|
||||||
|
if (!block.Instructions[pos].MatchStLoc(out var temp, out var defaultValue)) |
||||||
|
return; |
||||||
|
if (!defaultValue.MatchDefaultValue(out var type)) |
||||||
|
return; |
||||||
|
if (temp.StoreCount != 2 || temp.LoadCount != 1 || temp.AddressCount != 1) |
||||||
|
return; |
||||||
|
pos++; |
||||||
|
// if (comp.o(ldloc temp == ldnull)) Block IL_002a {
|
||||||
|
// stloc temp(ldobj ``0(ldloc address))
|
||||||
|
// stloc address(ldloca temp)
|
||||||
|
// }
|
||||||
|
if (block.Instructions.ElementAtOrDefault(pos) is not IfInstruction ifInst) |
||||||
|
return; |
||||||
|
if (!ifInst.Condition.MatchCompEqualsNull(out var tempLoadForNullCheck)) |
||||||
|
return; |
||||||
|
if (!tempLoadForNullCheck.MatchLdLoc(temp)) |
||||||
|
return; |
||||||
|
if (ifInst.TrueInst is not Block dereferenceBlock) |
||||||
|
return; |
||||||
|
if (ifInst.FalseInst is not Nop) |
||||||
|
return; |
||||||
|
if (dereferenceBlock.Instructions is not [StLoc tempStore, StLoc addressReassign]) |
||||||
|
return; |
||||||
|
if (tempStore.Variable != temp) |
||||||
|
return; |
||||||
|
if (!tempStore.Value.MatchLdObj(out var addressLoadForLdObj, type)) |
||||||
|
return; |
||||||
|
if (!addressLoadForLdObj.MatchLdLoc(addressReassign.Variable)) |
||||||
|
return; |
||||||
|
if (!addressReassign.Value.MatchLdLoca(temp)) |
||||||
|
return; |
||||||
|
var address = addressReassign.Variable; |
||||||
|
if (address.StoreCount != 2 || address.LoadCount != 2 || address.AddressCount != 0) |
||||||
|
return; |
||||||
|
pos++; |
||||||
|
// pos now is the first store to V_i
|
||||||
|
// ...(constrained[``0].callvirt Method(ldobj.if.ref ``0(ldloc address), ldloc V_i ...))...
|
||||||
|
var callTarget = address.LoadInstructions.Single(l => addressLoadForLdObj != l); |
||||||
|
if (callTarget.Parent is not LdObjIfRef { Parent: CallVirt call } ldobjIfRef) |
||||||
|
return; |
||||||
|
if (call.Arguments.Count == 0 || call.Arguments[0] != ldobjIfRef || !type.Equals(call.ConstrainedTo)) |
||||||
|
return; |
||||||
|
ILInstruction containingStmt = call; |
||||||
|
while (containingStmt.Parent != block) |
||||||
|
{ |
||||||
|
if (containingStmt.Parent == null) |
||||||
|
return; |
||||||
|
containingStmt = containingStmt.Parent; |
||||||
|
} |
||||||
|
if (containingStmt.ChildIndex < pos) |
||||||
|
return; |
||||||
|
// check if we can inline all temporaries used in the call:
|
||||||
|
int temporaryInitIndex = containingStmt.ChildIndex - 1; |
||||||
|
List<(ILInstruction, ILInstruction)> replacements = new(); |
||||||
|
for (int argIndex = call.Arguments.Count - 1; argIndex > 0 && temporaryInitIndex >= pos; argIndex--) |
||||||
|
{ |
||||||
|
var argument = call.Arguments[argIndex]; |
||||||
|
switch (argument) |
||||||
|
{ |
||||||
|
case LdLoc load: |
||||||
|
if (block.Instructions[temporaryInitIndex].MatchStLoc(load.Variable, out var expr) && ILInlining.VariableCanBeUsedForInlining(load.Variable)) |
||||||
|
{ |
||||||
|
if (!ILInlining.CanMoveIntoCallVirt(expr, containingStmt, call, argument)) |
||||||
|
{ |
||||||
|
return; |
||||||
|
} |
||||||
|
replacements.Add((argument, expr)); |
||||||
|
temporaryInitIndex--; |
||||||
|
} |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
// all stores to V_i processed?
|
||||||
|
if (temporaryInitIndex != pos - 1) |
||||||
|
{ |
||||||
|
return; |
||||||
|
} |
||||||
|
context.Step("RemoveUnconstrainedGenericReferenceTypeCheck", block.Instructions[startPos]); |
||||||
|
foreach (var (argument, expr) in replacements) |
||||||
|
{ |
||||||
|
argument.ReplaceWith(expr); |
||||||
|
} |
||||||
|
block.Instructions.RemoveRange(startPos, containingStmt.ChildIndex - startPos); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue