|
|
@ -152,8 +152,8 @@ namespace ICSharpCode.Decompiler.IL |
|
|
|
ILInstruction loadInst; |
|
|
|
ILInstruction loadInst; |
|
|
|
if (FindLoadInNext(next, v, inlinedExpression, out loadInst) == true) { |
|
|
|
if (FindLoadInNext(next, v, inlinedExpression, out loadInst) == true) { |
|
|
|
if (loadInst.OpCode == OpCode.LdLoca) { |
|
|
|
if (loadInst.OpCode == OpCode.LdLoca) { |
|
|
|
//if (!IsGeneratedValueTypeTemporary((ILInstruction)next, loadInst, v, inlinedExpression))
|
|
|
|
if (!IsGeneratedValueTypeTemporary(next, loadInst.Parent, loadInst.ChildIndex, v)) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
Debug.Assert(loadInst.OpCode == OpCode.LdLoc); |
|
|
|
Debug.Assert(loadInst.OpCode == OpCode.LdLoc); |
|
|
|
if (!aggressive && v.Kind != VariableKind.StackSlot && !NonAggressiveInlineInto(next, loadInst, inlinedExpression)) |
|
|
|
if (!aggressive && v.Kind != VariableKind.StackSlot && !NonAggressiveInlineInto(next, loadInst, inlinedExpression)) |
|
|
@ -174,7 +174,7 @@ namespace ICSharpCode.Decompiler.IL |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
/* |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Is this a temporary variable generated by the C# compiler for instance method calls on value type values
|
|
|
|
/// Is this a temporary variable generated by the C# compiler for instance method calls on value type values
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
@ -182,89 +182,25 @@ namespace ICSharpCode.Decompiler.IL |
|
|
|
/// <param name="parent">The direct parent of the load within 'next'</param>
|
|
|
|
/// <param name="parent">The direct parent of the load within 'next'</param>
|
|
|
|
/// <param name="pos">Index of the load within 'parent'</param>
|
|
|
|
/// <param name="pos">Index of the load within 'parent'</param>
|
|
|
|
/// <param name="v">The variable being inlined.</param>
|
|
|
|
/// <param name="v">The variable being inlined.</param>
|
|
|
|
/// <param name="inlinedExpression">The expression being inlined</param>
|
|
|
|
static bool IsGeneratedValueTypeTemporary(ILInstruction next, ILInstruction parent, int pos, ILVariable v) |
|
|
|
bool IsGeneratedValueTypeTemporary(ILInstruction next, ILInstruction parent, int pos, ILVariable v, ILInstruction inlinedExpression) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
if (pos == 0 && v.Type != null && v.Type.IsValueType) { |
|
|
|
if (pos == 0 && v.Type != null && v.Type.IsReferenceType == false) { |
|
|
|
// Inlining a value type variable is allowed only if the resulting code will maintain the semantics
|
|
|
|
|
|
|
|
// that the method is operating on a copy.
|
|
|
|
|
|
|
|
// Thus, we have to disallow inlining of other locals, fields, array elements, dereferenced pointers
|
|
|
|
|
|
|
|
switch (inlinedExpression.Code) { |
|
|
|
|
|
|
|
case ILCode.Ldloc: |
|
|
|
|
|
|
|
case ILCode.Stloc: |
|
|
|
|
|
|
|
case ILCode.CompoundAssignment: |
|
|
|
|
|
|
|
case ILCode.Ldelem_Any: |
|
|
|
|
|
|
|
case ILCode.Ldelem_I: |
|
|
|
|
|
|
|
case ILCode.Ldelem_I1: |
|
|
|
|
|
|
|
case ILCode.Ldelem_I2: |
|
|
|
|
|
|
|
case ILCode.Ldelem_I4: |
|
|
|
|
|
|
|
case ILCode.Ldelem_I8: |
|
|
|
|
|
|
|
case ILCode.Ldelem_R4: |
|
|
|
|
|
|
|
case ILCode.Ldelem_R8: |
|
|
|
|
|
|
|
case ILCode.Ldelem_Ref: |
|
|
|
|
|
|
|
case ILCode.Ldelem_U1: |
|
|
|
|
|
|
|
case ILCode.Ldelem_U2: |
|
|
|
|
|
|
|
case ILCode.Ldelem_U4: |
|
|
|
|
|
|
|
case ILCode.Ldobj: |
|
|
|
|
|
|
|
case ILCode.Ldind_Ref: |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
case ILCode.Ldfld: |
|
|
|
|
|
|
|
case ILCode.Stfld: |
|
|
|
|
|
|
|
case ILCode.Ldsfld: |
|
|
|
|
|
|
|
case ILCode.Stsfld: |
|
|
|
|
|
|
|
// allow inlining field access only if it's a readonly field
|
|
|
|
|
|
|
|
FieldDefinition f = ((FieldReference)inlinedExpression.Operand).Resolve(); |
|
|
|
|
|
|
|
if (!(f != null && f.IsInitOnly)) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ILCode.Call: |
|
|
|
|
|
|
|
case ILCode.CallGetter: |
|
|
|
|
|
|
|
// inlining runs both before and after IntroducePropertyAccessInstructions,
|
|
|
|
|
|
|
|
// so we have to handle both 'call' and 'callgetter'
|
|
|
|
|
|
|
|
MethodReference mr = (MethodReference)inlinedExpression.Operand; |
|
|
|
|
|
|
|
// ensure that it's not an multi-dimensional array getter
|
|
|
|
|
|
|
|
if (mr.DeclaringType is ArrayType) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
goto case ILCode.Callvirt; |
|
|
|
|
|
|
|
case ILCode.Callvirt: |
|
|
|
|
|
|
|
case ILCode.CallvirtGetter: |
|
|
|
|
|
|
|
// don't inline foreach loop variables:
|
|
|
|
|
|
|
|
mr = (MethodReference)inlinedExpression.Operand; |
|
|
|
|
|
|
|
if (mr.Name == "get_Current" && mr.HasThis) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ILCode.Castclass: |
|
|
|
|
|
|
|
case ILCode.Unbox_Any: |
|
|
|
|
|
|
|
// These are valid, but might occur as part of a foreach loop variable.
|
|
|
|
|
|
|
|
ILInstruction arg = inlinedExpression.Arguments[0]; |
|
|
|
|
|
|
|
if (arg.Code == ILCode.CallGetter || arg.Code == ILCode.CallvirtGetter || arg.Code == ILCode.Call || arg.Code == ILCode.Callvirt) { |
|
|
|
|
|
|
|
mr = (MethodReference)arg.Operand; |
|
|
|
|
|
|
|
if (mr.Name == "get_Current" && mr.HasThis) |
|
|
|
|
|
|
|
return false; // looks like a foreach loop variable, so don't inline it
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// inline the compiler-generated variable that are used when accessing a member on a value type:
|
|
|
|
// inline the compiler-generated variable that are used when accessing a member on a value type:
|
|
|
|
switch (parent.Code) { |
|
|
|
switch (parent.OpCode) { |
|
|
|
case ILCode.Call: |
|
|
|
case OpCode.Call: |
|
|
|
case ILCode.CallGetter: |
|
|
|
return !((Call)parent).Method.IsStatic; |
|
|
|
case ILCode.CallSetter: |
|
|
|
case OpCode.CallVirt: |
|
|
|
case ILCode.Callvirt: |
|
|
|
return !((CallVirt)parent).Method.IsStatic; |
|
|
|
case ILCode.CallvirtGetter: |
|
|
|
case OpCode.StFld: |
|
|
|
case ILCode.CallvirtSetter: |
|
|
|
case OpCode.LdFld: |
|
|
|
MethodReference mr = (MethodReference)parent.Operand; |
|
|
|
case OpCode.LdFlda: |
|
|
|
return mr.HasThis; |
|
|
|
// TODO : Reimplement Await
|
|
|
|
case ILCode.Stfld: |
|
|
|
//case OpCode.Await:
|
|
|
|
case ILCode.Ldfld: |
|
|
|
|
|
|
|
case ILCode.Ldflda: |
|
|
|
|
|
|
|
case ILCode.Await: |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Determines whether a variable should be inlined in non-aggressive mode, even though it is not a generated variable.
|
|
|
|
/// Determines whether a variable should be inlined in non-aggressive mode, even though it is not a generated variable.
|
|
|
@ -354,78 +290,5 @@ namespace ICSharpCode.Decompiler.IL |
|
|
|
} |
|
|
|
} |
|
|
|
return SemanticHelper.MayReorder(expressionBeingMoved.Flags, expr.Flags); |
|
|
|
return SemanticHelper.MayReorder(expressionBeingMoved.Flags, expr.Flags); |
|
|
|
} |
|
|
|
} |
|
|
|
/* |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Runs a very simple form of copy propagation.
|
|
|
|
|
|
|
|
/// Copy propagation is used in two cases:
|
|
|
|
|
|
|
|
/// 1) assignments from arguments to local variables
|
|
|
|
|
|
|
|
/// If the target variable is assigned to only once (so always is that argument) and the argument is never changed (no ldarga/starg),
|
|
|
|
|
|
|
|
/// then we can replace the variable with the argument.
|
|
|
|
|
|
|
|
/// 2) assignments of address-loading instructions to local variables
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
public void CopyPropagation() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
foreach (ILBlock block in method.GetSelfAndChildrenRecursive<ILBlock>()) { |
|
|
|
|
|
|
|
for (int i = 0; i < block.Body.Count; i++) { |
|
|
|
|
|
|
|
ILVariable v; |
|
|
|
|
|
|
|
ILInstruction copiedExpr; |
|
|
|
|
|
|
|
if (block.Body[i].Match(ILCode.Stloc, out v, out copiedExpr) |
|
|
|
|
|
|
|
&& !v.IsParameter && numStloc.GetOrDefault(v) == 1 && numLdloca.GetOrDefault(v) == 0 |
|
|
|
|
|
|
|
&& CanPerformCopyPropagation(copiedExpr, v)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// un-inline the arguments of the ldArg instruction
|
|
|
|
|
|
|
|
ILVariable[] uninlinedArgs = new ILVariable[copiedExpr.Arguments.Count]; |
|
|
|
|
|
|
|
for (int j = 0; j < uninlinedArgs.Length; j++) { |
|
|
|
|
|
|
|
uninlinedArgs[j] = new ILVariable { IsGenerated = true, Name = v.Name + "_cp_" + j }; |
|
|
|
|
|
|
|
block.Body.Insert(i++, new ILInstruction(ILCode.Stloc, uninlinedArgs[j], copiedExpr.Arguments[j])); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// perform copy propagation:
|
|
|
|
|
|
|
|
foreach (var expr in method.GetSelfAndChildrenRecursive<ILInstruction>()) { |
|
|
|
|
|
|
|
if (expr.Code == ILCode.Ldloc && expr.Operand == v) { |
|
|
|
|
|
|
|
expr.Code = copiedExpr.Code; |
|
|
|
|
|
|
|
expr.Operand = copiedExpr.Operand; |
|
|
|
|
|
|
|
for (int j = 0; j < uninlinedArgs.Length; j++) { |
|
|
|
|
|
|
|
expr.Arguments.Add(new ILInstruction(ILCode.Ldloc, uninlinedArgs[j])); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
block.Body.RemoveAt(i); |
|
|
|
|
|
|
|
if (uninlinedArgs.Length > 0) { |
|
|
|
|
|
|
|
// if we un-inlined stuff; we need to update the usage counters
|
|
|
|
|
|
|
|
AnalyzeMethod(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
InlineInto(block.Body, i, aggressive: false); // maybe inlining gets possible after the removal of block.Body[i]
|
|
|
|
|
|
|
|
i -= uninlinedArgs.Length + 1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CanPerformCopyPropagation(ILInstruction expr, ILVariable copyVariable) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
switch (expr.Code) { |
|
|
|
|
|
|
|
case ILCode.Ldloca: |
|
|
|
|
|
|
|
case ILCode.Ldelema: |
|
|
|
|
|
|
|
case ILCode.Ldflda: |
|
|
|
|
|
|
|
case ILCode.Ldsflda: |
|
|
|
|
|
|
|
// All address-loading instructions always return the same value for a given operand/argument combination,
|
|
|
|
|
|
|
|
// so they can be safely copied.
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
case ILCode.Ldloc: |
|
|
|
|
|
|
|
ILVariable v = (ILVariable)expr.Operand; |
|
|
|
|
|
|
|
if (v.IsParameter) { |
|
|
|
|
|
|
|
// Parameters can be copied only if they aren't assigned to (directly or indirectly via ldarga)
|
|
|
|
|
|
|
|
return numLdloca.GetOrDefault(v) == 0 && numStloc.GetOrDefault(v) == 0; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// Variables are be copied only if both they and the target copy variable are generated,
|
|
|
|
|
|
|
|
// and if the variable has only a single assignment
|
|
|
|
|
|
|
|
return v.IsGenerated && copyVariable.IsGenerated && numLdloca.GetOrDefault(v) == 0 && numStloc.GetOrDefault(v) == 1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}*/ |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|