|
|
|
@ -44,22 +44,24 @@ namespace ICSharpCode.Decompiler.IL.Transforms
@@ -44,22 +44,24 @@ namespace ICSharpCode.Decompiler.IL.Transforms
|
|
|
|
|
// This is necessary to remove useless stores generated by some compilers, e.g., the F# compiler.
|
|
|
|
|
// In yield return + async, the C# compiler tends to store null/default(T) to variables
|
|
|
|
|
// when the variable goes out of scope.
|
|
|
|
|
var variableQueue = new Queue<ILVariable>(function.Variables); |
|
|
|
|
while (variableQueue.Count > 0) { |
|
|
|
|
var v = variableQueue.Dequeue(); |
|
|
|
|
if (v.Kind != VariableKind.Local && v.Kind != VariableKind.StackSlot) |
|
|
|
|
continue; |
|
|
|
|
if (v.LoadCount != 0 || v.AddressCount != 0) |
|
|
|
|
continue; |
|
|
|
|
foreach (var stloc in v.StoreInstructions.OfType<StLoc>().ToArray()) { |
|
|
|
|
if (stloc.Parent is Block block) { |
|
|
|
|
if (SemanticHelper.IsPure(stloc.Value.Flags)) { |
|
|
|
|
block.Instructions.Remove(stloc); |
|
|
|
|
} else { |
|
|
|
|
stloc.ReplaceWith(stloc.Value); |
|
|
|
|
} |
|
|
|
|
if (stloc.Value is LdLoc ldloc) { |
|
|
|
|
variableQueue.Enqueue(ldloc.Variable); |
|
|
|
|
if (function.IsAsync || function.IsIterator || context.Settings.RemoveDeadCode) { |
|
|
|
|
var variableQueue = new Queue<ILVariable>(function.Variables); |
|
|
|
|
while (variableQueue.Count > 0) { |
|
|
|
|
var v = variableQueue.Dequeue(); |
|
|
|
|
if (v.Kind != VariableKind.Local && v.Kind != VariableKind.StackSlot) |
|
|
|
|
continue; |
|
|
|
|
if (v.LoadCount != 0 || v.AddressCount != 0) |
|
|
|
|
continue; |
|
|
|
|
foreach (var stloc in v.StoreInstructions.OfType<StLoc>().ToArray()) { |
|
|
|
|
if (stloc.Parent is Block block) { |
|
|
|
|
if (SemanticHelper.IsPure(stloc.Value.Flags)) { |
|
|
|
|
block.Instructions.Remove(stloc); |
|
|
|
|
} else { |
|
|
|
|
stloc.ReplaceWith(stloc.Value); |
|
|
|
|
} |
|
|
|
|
if (stloc.Value is LdLoc ldloc) { |
|
|
|
|
variableQueue.Enqueue(ldloc.Variable); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|