Browse Source

Add 'InlineCompilerGeneratedVariables' transform, hopefully this is sufficient to replace the 'variable splitting' in the old decompiler.

pull/728/head
Daniel Grunwald 10 years ago
parent
commit
b10feca78b
  1. 4
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
  2. 1
      ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj
  3. 2
      ICSharpCode.Decompiler/IL/ControlFlow/LoopDetection.cs
  4. 44
      ICSharpCode.Decompiler/IL/Transforms/ILInlining.cs
  5. 86
      ICSharpCode.Decompiler/IL/Transforms/InlineCompilerGeneratedVariables.cs

4
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -49,9 +49,11 @@ namespace ICSharpCode.Decompiler.CSharp @@ -49,9 +49,11 @@ namespace ICSharpCode.Decompiler.CSharp
new IntroduceExitPoints(),
new ConditionDetection(),
new ILInlining(),
new InlineCompilerGeneratedVariables(),
new TransformingVisitor(),
new ExpressionTransforms(),
new TransformArrayInitializers()
new TransformArrayInitializers(),
new ILInlining()
};
List<IAstTransform> astTransforms = new List<IAstTransform> {

1
ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

@ -116,6 +116,7 @@ @@ -116,6 +116,7 @@
<Compile Include="IL\Transforms\IILTransform.cs" />
<Compile Include="IL\Transforms\ILInlining.cs" />
<Compile Include="IL\Transforms\ExpressionTransforms.cs" />
<Compile Include="IL\Transforms\InlineCompilerGeneratedVariables.cs" />
<Compile Include="IL\Transforms\TransformArrayInitializers.cs" />
<Compile Include="IL\Transforms\TransformingVisitor.cs" />
<Compile Include="CecilExtensions.cs" />

2
ICSharpCode.Decompiler/IL/ControlFlow/LoopDetection.cs

@ -32,7 +32,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow @@ -32,7 +32,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
/// * LoopDetection should run before other control flow structures are detected.
/// * Blocks should be basic blocks (not extended basic blocks) so that the natural loops
/// don't include more instructions than strictly necessary.
/// * (depending on future loop detection improvements:) Loop detection should run after the 'return block' is duplicated (OptimizingTransform).
/// * (depending on future loop detection improvements:) Loop detection should run after the 'return block' is duplicated (ControlFlowSimplification).
/// </remarks>
public class LoopDetection : IILTransform
{

44
ICSharpCode.Decompiler/IL/Transforms/ILInlining.cs

@ -93,9 +93,29 @@ namespace ICSharpCode.Decompiler.IL @@ -93,9 +93,29 @@ namespace ICSharpCode.Decompiler.IL
public bool InlineOneIfPossible(Block block, int pos, bool aggressive)
{
StLoc stloc = block.Instructions[pos] as StLoc;
if (stloc != null && stloc.Variable.Kind != VariableKind.PinnedLocal) {
if (stloc == null || stloc.Variable.Kind == VariableKind.PinnedLocal)
return false;
ILVariable v = stloc.Variable;
if (InlineIfPossible(v, stloc.Value, block.Instructions.ElementAtOrDefault(pos+1), aggressive)) {
// ensure the variable is accessed only a single time
if (v.StoreCount != 1)
return false;
if (v.LoadCount > 1 || v.LoadCount + v.AddressCount != 1)
return false;
return InlineOne(stloc, aggressive);
}
/// <summary>
/// Inlines the stloc instruction at block.Instructions[pos] into the next instruction.
///
/// Note that this method does not check whether 'v' has only one use;
/// the caller is expected to validate whether inlining 'v' has any effects on other uses of 'v'.
/// </summary>
public static bool InlineOne(StLoc stloc, bool aggressive)
{
ILVariable v = stloc.Variable;
Block block = (Block)stloc.Parent;
int pos = stloc.ChildIndex;
if (DoInline(v, stloc.Value, block.Instructions.ElementAtOrDefault(pos + 1), aggressive)) {
// Assign the ranges of the stloc instruction:
stloc.Value.AddILRange(stloc.ILRange);
// Remove the stloc instruction:
@ -117,21 +137,17 @@ namespace ICSharpCode.Decompiler.IL @@ -117,21 +137,17 @@ namespace ICSharpCode.Decompiler.IL
return true;
}
}
}
return false;
}
/// <summary>
/// Inlines 'expr' into 'next', if possible.
///
/// Note that this method does not check whether 'v' has only one use;
/// the caller is expected to validate whether inlining 'v' has any effects on other uses of 'v'.
/// </summary>
bool InlineIfPossible(ILVariable v, ILInstruction inlinedExpression, ILInstruction next, bool aggressive)
static bool DoInline(ILVariable v, ILInstruction inlinedExpression, ILInstruction next, bool aggressive)
{
// ensure the variable is accessed only a single time
if (v.StoreCount != 1)
return false;
if (v.LoadCount > 1 || v.LoadCount + v.AddressCount != 1)
return false;
ILInstruction loadInst;
if (FindLoadInNext(next, v, inlinedExpression, out loadInst) == true) {
if (loadInst.OpCode == OpCode.LdLoca) {
@ -255,7 +271,7 @@ namespace ICSharpCode.Decompiler.IL @@ -255,7 +271,7 @@ namespace ICSharpCode.Decompiler.IL
/// <param name="next">The next top-level expression</param>
/// <param name="loadInst">The load within 'next'</param>
/// <param name="inlinedExpression">The expression being inlined</param>
bool NonAggressiveInlineInto(ILInstruction next, ILInstruction loadInst, ILInstruction inlinedExpression)
static bool NonAggressiveInlineInto(ILInstruction next, ILInstruction loadInst, ILInstruction inlinedExpression)
{
Debug.Assert(loadInst.IsDescendantOf(next));
@ -278,7 +294,7 @@ namespace ICSharpCode.Decompiler.IL @@ -278,7 +294,7 @@ namespace ICSharpCode.Decompiler.IL
/// <summary>
/// Gets whether 'expressionBeingMoved' can be inlined into 'expr'.
/// </summary>
public bool CanInlineInto(ILInstruction expr, ILVariable v, ILInstruction expressionBeingMoved)
public static bool CanInlineInto(ILInstruction expr, ILVariable v, ILInstruction expressionBeingMoved)
{
ILInstruction loadInst;
return FindLoadInNext(expr, v, expressionBeingMoved, out loadInst) == true;
@ -288,7 +304,7 @@ namespace ICSharpCode.Decompiler.IL @@ -288,7 +304,7 @@ namespace ICSharpCode.Decompiler.IL
/// Finds the position to inline to.
/// </summary>
/// <returns>true = found; false = cannot continue search; null = not found</returns>
bool? FindLoadInNext(ILInstruction expr, ILVariable v, ILInstruction expressionBeingMoved, out ILInstruction loadInst)
static bool? FindLoadInNext(ILInstruction expr, ILVariable v, ILInstruction expressionBeingMoved, out ILInstruction loadInst)
{
loadInst = null;
if (expr == null)
@ -316,7 +332,7 @@ namespace ICSharpCode.Decompiler.IL @@ -316,7 +332,7 @@ namespace ICSharpCode.Decompiler.IL
/// <summary>
/// Determines whether it is safe to move 'expressionBeingMoved' past 'expr'
/// </summary>
bool IsSafeForInlineOver(ILInstruction expr, ILInstruction expressionBeingMoved)
static bool IsSafeForInlineOver(ILInstruction expr, ILInstruction expressionBeingMoved)
{
ILVariable v;
if (expr.MatchLdLoc(out v) && v.AddressCount == 0) {

86
ICSharpCode.Decompiler/IL/Transforms/InlineCompilerGeneratedVariables.cs

@ -0,0 +1,86 @@ @@ -0,0 +1,86 @@
// Copyright (c) 2014 Daniel Grunwald
//
// 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.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace ICSharpCode.Decompiler.IL.Transforms
{
/// <summary>
/// Inline compiler-generated variables.
/// Unlike normal inlining that acts on stack variables which have only one use,
/// this transform will also perform inlining if the variables are re-used.
/// </summary>
/// <remarks>
/// Should run after ControlFlowSimplification duplicates the return blocks.
/// Should run after a regular round of inlining, so that stack variables
/// don't prevent us from determining whether a variable is likely to be compiler-generated.
/// </remarks>
public class InlineCompilerGeneratedVariables : IILTransform
{
public void Run(ILFunction function, ILTransformContext context)
{
List<StLoc> storesToInline = new List<StLoc>();
List<StLoc> deadStores = new List<StLoc>();
foreach (var g in function.Descendants.OfType<StLoc>().GroupBy(inst => inst.Variable)) {
storesToInline.Clear();
deadStores.Clear();
if (CanInlineVariable(g.Key, g, storesToInline, deadStores)) {
foreach (var stloc in storesToInline) {
ILInlining.InlineOne(stloc, aggressive: false);
}
foreach (var stloc in deadStores) {
if (SemanticHelper.IsPure(stloc.Flags)) {
((Block)stloc.Parent).Instructions.RemoveAt(stloc.ChildIndex);
}
}
}
}
}
bool CanInlineVariable(ILVariable v, IEnumerable<StLoc> stores, /*out*/ List<StLoc> storesToInline, /*out*/ List<StLoc> deadStores)
{
if (v.Kind != VariableKind.Local)
return false;
Debug.Assert(v.StoreCount == stores.Count());
// We expect there to be one store for every load,
// and potentially also some dead stores.
if (v.StoreCount < v.LoadCount || v.AddressCount != 0)
return false;
int loadsAccountedFor = 0;
foreach (var stloc in stores) {
Block block = stloc.Parent as Block;
if (block == null)
return false;
ILInstruction next = block.Instructions.ElementAtOrDefault(stloc.ChildIndex + 1);
if (next == null)
continue; // store at end of block might still be a dead store
if (ILInlining.CanInlineInto(next, v, stloc.Value)) {
loadsAccountedFor++;
storesToInline.Add(stloc);
} else {
// a dead store (but only if this method returns true)
deadStores.Add(stloc);
}
}
return v.LoadCount == loadsAccountedFor;
}
}
}
Loading…
Cancel
Save