Browse files

Recombine variables after ILAst optimizations so that there is only a…

… single C# variable for a single IL variable.
  • Loading branch information...
1 parent 6a0d365 commit d430ee056bf0487ee8b0efdfd9407b8bb87b9b01 @dgrunwald dgrunwald committed Mar 20, 2011
Showing with 24 additions and 1 deletion.
  1. +24 −1 ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs
View
25 ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs
@@ -38,6 +38,7 @@ public enum ILAstOptimizationStep
InlineVariables3,
CachedDelegateInitialization,
IntroduceFixedStatements,
+ RecombineVariables,
TypeInference2,
RemoveRedundantCode3,
None
@@ -180,6 +181,9 @@ public void Optimize(DecompilerContext context, ILBlock method, ILAstOptimizatio
}
}
+ if (abortBeforeStep == ILAstOptimizationStep.RecombineVariables) return;
+ RecombineVariables(method);
+
if (abortBeforeStep == ILAstOptimizationStep.TypeInference2) return;
TypeAnalysis.Reset(method);
TypeAnalysis.Run(context, method);
@@ -458,6 +462,25 @@ void ReduceIfNesting(ILNode node)
}
}
+ void RecombineVariables(ILBlock method)
+ {
+ // Recombine variables that were split when the ILAst was created
+ // This ensures that a single IL variable is a single C# variable (gets assigned only one name)
+ // The DeclareVariables transformation might then split up the C# variable again if it is used indendently in two separate scopes.
+ Dictionary<VariableDefinition, ILVariable> dict = new Dictionary<VariableDefinition, ILVariable>();
+ foreach (ILExpression expr in method.GetSelfAndChildrenRecursive<ILExpression>()) {
+ ILVariable v = expr.Operand as ILVariable;
+ if (v != null && v.OriginalVariable != null) {
+ ILVariable combinedVariable;
+ if (!dict.TryGetValue(v.OriginalVariable, out combinedVariable)) {
+ dict.Add(v.OriginalVariable, v);
+ combinedVariable = v;
+ }
+ expr.Operand = combinedVariable;
+ }
+ }
+ }
+
void ReportUnassignedILRanges(ILBlock method)
{
var unassigned = ILRange.Invert(method.GetSelfAndChildrenRecursive<ILExpression>().SelectMany(e => e.ILRanges), context.CurrentMethod.Body.CodeSize).ToList();
@@ -511,7 +534,7 @@ public static bool IsUnconditionalControlFlow(this ILNode node)
}
/// <summary>
- /// The expression has no effect on the program and can be removed
+ /// The expression has no effect on the program and can be removed
/// if its return value is not needed.
/// </summary>
public static bool HasNoSideEffects(this ILExpression expr)

0 comments on commit d430ee0

Please sign in to comment.