public void CodeWithoutCommonSubexpressionsDueToDifferentMethods() { var source = "var x = Method1(1); var y = Method2(1); var z = y * 1;"; var code = TestCodeFactory.CreateThreeAddressCode(source); var cfg = ControlFlowGraphFactory.Create(code); var optimized = CommonSubexpressionElimination.Optimize(code, cfg); Assert.AreEqual(code, optimized.Code); Assert.IsFalse(optimized.Changed); }
public void EmptyCode() { var source = ""; var code = TestCodeFactory.CreateThreeAddressCode(source); var cfg = ControlFlowGraphFactory.Create(code); var optimized = CommonSubexpressionElimination.Optimize(code, cfg); Assert.AreEqual(code, optimized.Code); Assert.IsFalse(optimized.Changed); }
public void CommonSubexpressionsWithMultipleInvocationsOfTheSameMethodWithDistinctArguments() { var source = @"var result = Method(x) + Method(y);"; var code = TestCodeFactory.CreateThreeAddressCode(source); var cfg = ControlFlowGraphFactory.Create(code); var optimized = CommonSubexpressionElimination.Optimize(code, cfg); Assert.AreEqual(code, optimized.Code); Assert.IsFalse(optimized.Changed); }
public void CodeWithSameInvocationsButIntermediateValueChange() { var source = @" var x = 5; var a = Method(x); x = 2; var b = Method(x);"; var code = TestCodeFactory.CreateThreeAddressCode(source); var cfg = ControlFlowGraphFactory.Create(code); var optimized = CommonSubexpressionElimination.Optimize(code, cfg); Assert.AreEqual(code, optimized.Code); Assert.IsFalse(optimized.Changed); }
public void SelfAssignmentNegatesOptimization() { var source = @" var x = 5; var y = 3; x = x + y; var a = x + y;"; var code = TestCodeFactory.CreateThreeAddressCode(source); var cfg = ControlFlowGraphFactory.Create(code); var optimized = CommonSubexpressionElimination.Optimize(code, cfg); Assert.AreEqual(code, optimized.Code); Assert.IsFalse(optimized.Changed); }
public void CodeWithSameInvocationsButInLoop() { var source = @" var x = 5; var y = 3; var a = Method(x); for(var i = 0; i < 10; ++i) { x = Method(x); }"; var code = TestCodeFactory.CreateThreeAddressCode(source); var cfg = ControlFlowGraphFactory.Create(code); var optimized = CommonSubexpressionElimination.Optimize(code, cfg); Assert.AreEqual(code, optimized.Code); Assert.IsFalse(optimized.Changed); }
public void CodeWithSameExpressionsButBranchedValueChange() { var source = @" var x = 5; var y = 3; var a = y + x; if(x > y) { x = 2; } var b = y + x;"; var code = TestCodeFactory.CreateThreeAddressCode(source); var cfg = ControlFlowGraphFactory.Create(code); var optimized = CommonSubexpressionElimination.Optimize(code, cfg); Assert.AreEqual(code, optimized.Code); Assert.IsFalse(optimized.Changed); }
public void CommonSubexpressionsInSingleMethodInvocation() { var source = @"Method(x + 1, x + 1);"; var code = TestCodeFactory.CreateThreeAddressCode(source); var cfg = ControlFlowGraphFactory.Create(code); var optimized = CommonSubexpressionElimination.Optimize(code, cfg); Assert.AreNotEqual(code, optimized.Code); Assert.IsTrue(optimized.Changed); var expected = @" DECL $temp_0 $temp_0 = x + 1 DECL $temp_1 $temp_1 = $temp_0 INVOKE Method($temp_0, $temp_1)"; Assert.AreEqual(expected.Trim(), CodeStringifier.Generate(optimized.Code)); }
public void CodeWithSingleCommonInvocation() { var source = "var x = 3; var y = Method(x); var z = Method(x);"; var code = TestCodeFactory.CreateThreeAddressCode(source); var cfg = ControlFlowGraphFactory.Create(code); var optimized = CommonSubexpressionElimination.Optimize(code, cfg); Assert.AreNotEqual(code, optimized.Code); Assert.IsTrue(optimized.Changed); var expected = @" DECL x x = 3 DECL y y = Method(x) DECL z z = y"; Assert.AreEqual(expected.Trim(), CodeStringifier.Generate(optimized.Code)); }
public void CommonSubexpressionsWithMultipleInvocationsOfTheSameMethodWithTheSameArguments() { var source = @"var result = Method(x) + Method(x);"; var code = TestCodeFactory.CreateThreeAddressCode(source); var cfg = ControlFlowGraphFactory.Create(code); var optimized = CommonSubexpressionElimination.Optimize(code, cfg); Assert.AreNotEqual(code, optimized.Code); Assert.IsTrue(optimized.Changed); var expected = @" DECL result DECL $temp_0 $temp_0 = Method(x) DECL $temp_1 $temp_1 = $temp_0 result = $temp_0 + $temp_1"; Assert.AreEqual(expected.Trim(), CodeStringifier.Generate(optimized.Code)); }
public void CodeWithCommonInvocationArgumentExpressions() { var source = @" var a = Method[x + 1]; Method[x + 1] = 4;"; var code = TestCodeFactory.CreateThreeAddressCode(source); var cfg = ControlFlowGraphFactory.Create(code); var optimized = CommonSubexpressionElimination.Optimize(code, cfg); Assert.AreNotEqual(code, optimized.Code); Assert.IsTrue(optimized.Changed); var expected = @" DECL a DECL $temp_0 $temp_0 = x + 1 a = Method[$temp_0] DECL $temp_1 $temp_1 = $temp_0 Method[$temp_1] = 4"; Assert.AreEqual(expected.Trim(), CodeStringifier.Generate(optimized.Code)); }
public void CommonSubexpressionsInMultipleMethodInvocations() { var source = @"var result = Method1(x + 1) + Method2(x + 1);"; var code = TestCodeFactory.CreateThreeAddressCode(source); var cfg = ControlFlowGraphFactory.Create(code); var optimized = CommonSubexpressionElimination.Optimize(code, cfg); Assert.AreNotEqual(code, optimized.Code); Assert.IsTrue(optimized.Changed); var expected = @" DECL result DECL $temp_0 $temp_0 = x + 1 DECL $temp_1 $temp_1 = Method1($temp_0) DECL $temp_2 $temp_2 = $temp_0 DECL $temp_3 $temp_3 = Method2($temp_2) result = $temp_1 + $temp_3"; Assert.AreEqual(expected.Trim(), CodeStringifier.Generate(optimized.Code)); }