summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Build.Tasks
diff options
context:
space:
mode:
authorStephane Delcroix <stephane@delcroix.org>2016-12-13 15:41:41 (GMT)
committerGitHub <noreply@github.com>2016-12-13 15:41:41 (GMT)
commit4625f6976a71fdad89c19803b9ac44cebc8f5b69 (patch)
treeb35672e5afe315325cc60954970a744d9483cfd3 /Xamarin.Forms.Build.Tasks
parentdb727808a82844fce8d20ce7817907c7ed6d34a4 (diff)
downloadxamarin-forms-4625f6976a71fdad89c19803b9ac44cebc8f5b69.zip
xamarin-forms-4625f6976a71fdad89c19803b9ac44cebc8f5b69.tar.gz
xamarin-forms-4625f6976a71fdad89c19803b9ac44cebc8f5b69.tar.bz2
[XamlC] remove unused variables (#620)
Diffstat (limited to 'Xamarin.Forms.Build.Tasks')
-rw-r--r--Xamarin.Forms.Build.Tasks/MethodBodyExtensions.cs35
1 files changed, 34 insertions, 1 deletions
diff --git a/Xamarin.Forms.Build.Tasks/MethodBodyExtensions.cs b/Xamarin.Forms.Build.Tasks/MethodBodyExtensions.cs
index 380cb59..7db321f 100644
--- a/Xamarin.Forms.Build.Tasks/MethodBodyExtensions.cs
+++ b/Xamarin.Forms.Build.Tasks/MethodBodyExtensions.cs
@@ -1,6 +1,8 @@
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
using System;
+using System.Linq;
+using System.Collections.Generic;
namespace Xamarin.Forms.Build.Tasks
{
@@ -13,6 +15,7 @@ namespace Xamarin.Forms.Build.Tasks
self.OptimizeLongs();
self.OptimizeStLdLoc();
+ self.RemoveUnusedLocals();
self.OptimizeMacros();
}
@@ -25,7 +28,6 @@ namespace Xamarin.Forms.Build.Tasks
//this can be removed if/when https://github.com/jbevain/cecil/pull/307 is released in a nuget we consume
static void OptimizeLongs(this MethodBody self)
{
- var method = self.Method;
for (var i = 0; i < self.Instructions.Count; i++) {
var instruction = self.Instructions[i];
if (instruction.OpCode.Code != Code.Ldc_I8)
@@ -56,5 +58,36 @@ namespace Xamarin.Forms.Build.Tasks
ExpandMacro(next, OpCodes.Stloc, vardef);
}
}
+
+ static void RemoveUnusedLocals(this MethodBody self)
+ {
+ //Count ldloc for each variable
+ var ldlocUsed = new List<VariableDefinition>();
+ foreach (var instruction in self.Instructions) {
+ if (instruction.OpCode.Code != Code.Ldloc)
+ continue;
+ var varDef = (VariableDefinition)instruction.Operand;
+ if (!ldlocUsed.Contains(varDef))
+ ldlocUsed.Add(varDef);
+ }
+
+ foreach (var varDef in self.Variables.ToArray()) {
+ if (ldlocUsed.Contains(varDef))
+ continue;
+
+ //find the Stloc instruction
+ var instruction = (from instr in self.Instructions where instr.OpCode.Code == Code.Stloc && instr.Operand == varDef select instr).First();
+
+ //remove dup/stloc
+ if (instruction.Previous.OpCode.Code != Code.Dup)
+ break;
+
+ self.Instructions.Remove(instruction.Previous);
+ self.Instructions.Remove(instruction);
+
+ //and remove the variable
+ self.Variables.Remove(varDef);
+ }
+ }
}
} \ No newline at end of file