public void ForLoopRenameIgnoreDeclaredVariables() { var c1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s1 = new StatementForLoop(c1, new ValSimple("5", typeof(int))); var a1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var v1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); s1.Add(new StatementAssign(a1, new ValSimple("10", typeof(int)))); s1.Add(new StatementAssign(a1, new ValSimple($"{a1.RawValue}+{v1.RawValue}", typeof(int), new IDeclaredParameter[] { a1, v1 }))); s1.Add(v1); var c2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s2 = new StatementForLoop(c2, new ValSimple("5", typeof(int))); var a2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var v2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); s2.Add(new StatementAssign(a2, new ValSimple("10", typeof(int)))); s2.Add(new StatementAssign(a2, new ValSimple($"{a2.RawValue}+{v2.RawValue}", typeof(int), new IDeclaredParameter[] { a2, v2 }))); s2.Add(v2); var r = s1.RequiredForEquivalence(s2); // Since only loop variable matters here, we don't care, and we can do the rename. Assert.IsTrue(r.Item1, "Should be equivalent"); var renames = r.Item2.ToArray(); Assert.AreEqual(1, renames.Length, "# of renames"); }
public void DependentAndResultVariablesWithDecl() { var counter = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s = new StatementForLoop(counter, new ValSimple("5", typeof(int))); var result = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); s.Add(result); var assign = new StatementAssign(result, new ValSimple($"{result.RawValue}+{counter.RawValue}", typeof(int), new IDeclaredParameter[] { counter, result })); s.Add(assign); Assert.AreEqual(0, s.DependentVariables.Count(), "# of dependent variables"); Assert.AreEqual(0, s.ResultVariables.Count(), "# of result variables"); }
public void TestCombineNestedForLoopOnOneSide() { var base1 = new StatementInlineBlock(); var limit = new LINQToTTreeLib.Variables.ValSimple("5", typeof(int)); var loopP1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var loop1 = new StatementForLoop(loopP1, limit); base1.Add(loop1); var base2 = new StatementInlineBlock(); var limit2 = new LINQToTTreeLib.Variables.ValSimple("5", typeof(int)); var loopP12 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var loop12 = new StatementForLoop(loopP12, limit); base2.Add(loop12); var loopP22 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var loop22 = new StatementForLoop(loopP22, limit); loop12.Add(loop22); var r = base1.TryCombineStatement(base2, new dummyOpt()); Assert.IsTrue(r, "combination should work"); Assert.AreEqual(base1, loop1.Parent, "loop 1 parent"); Assert.AreEqual(loop1, loop22.Parent, "Loop 2 parent"); }
public void ForLoopRenameTwiceAsSource() { var c1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s1 = new StatementForLoop(c1, new ValSimple("5", typeof(int))); var a1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var v1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); s1.Add(new StatementAssign(a1, new ValSimple("10", typeof(int)))); s1.Add(new StatementAssign(a1, new ValSimple($"{a1.RawValue}+{a1.RawValue}", typeof(int), new IDeclaredParameter[] { a1 }))); var c2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s2 = new StatementForLoop(c1, new ValSimple("6", typeof(int))); var a2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var v2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); s2.Add(new StatementAssign(a2, new ValSimple("10", typeof(int)))); s2.Add(new StatementAssign(a2, new ValSimple($"{a2.RawValue}+{v2.RawValue}", typeof(int), new IDeclaredParameter[] { a2, v2 }))); var r = s1.RequiredForEquivalence(s2); // Since only loop variable matters here, we don't care, and we can do the rename. Assert.IsFalse(r.Item1, "Should be equivalent"); }
public void DeclaredVariablesLocalOnly() { var outter = new StatementInlineBlock(); outter.Add(DeclarableParameter.CreateDeclarableParameterExpression(typeof(int))); var s = new StatementForLoop(DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)), new ValSimple("5", typeof(int))); s.Add(DeclarableParameter.CreateDeclarableParameterExpression(typeof(int))); outter.Add(s); Assert.AreEqual(2, s.DeclaredVariables.Count()); Assert.AreEqual(3, s.AllDeclaredVariables.Count()); }
public void ForLoopDifferentNumberOfStatements() { var c1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s1 = new StatementForLoop(c1, new ValSimple("5", typeof(int))); var a1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); s1.Add(new StatementAssign(a1, new ValSimple("10", typeof(int)))); var c2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s2 = new StatementForLoop(c1, new ValSimple("6", typeof(int))); var r = s1.RequiredForEquivalence(s2); // Since only loop variable matters here, we don't care, and we can do the rename. Assert.IsFalse(r.Item1, "Can't be equivalent"); }
public void TestLiftSimpleStatementInFunction() { var v = new StatementInlineBlock(); var loopP = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var loop = new StatementForLoop(loopP, new LINQToTTreeLib.Variables.ValSimple("10", typeof(int))); v.Add(loop); loop.Add(new StatementWithNoSideEffects()); var f = QMFunctions.QMFuncUtils.GenerateFunction(); f.SetCodeBody(v); var gc = new GeneratedCode(); gc.Add(new StatementSimpleStatement("int i = 10;")); gc.Add(f); StatementLifter.Optimize(gc); Assert.AreEqual(1, gc.Functions.Count(), "# of functions after lifting"); var firstStatement = gc.Functions.First().StatementBlock.Statements.First(); Assert.IsInstanceOfType(firstStatement, typeof(StatementWithNoSideEffects), "first statement"); }
public void ForLoopGivenRenameRequestStatements() { var c1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s1 = new StatementForLoop(c1, new ValSimple("5", typeof(int))); var a1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); s1.Add(new StatementAssign(a1, new ValSimple("10", typeof(int)))); var c2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s2 = new StatementForLoop(c2, new ValSimple("5", typeof(int))); var a2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); s2.Add(new StatementAssign(a2, new ValSimple("10", typeof(int)))); var r = s1.RequiredForEquivalence(s2, new Tuple <string, string>[] { new Tuple <string, string>(a2.RawValue, a1.RawValue) }); // Since only loop variable matters here, we don't care, and we can do the rename. Assert.IsTrue(r.Item1, "Should be equivalent"); var renames = r.Item2.ToArray(); Assert.AreEqual(0, renames.Length, "# of renames"); }
public void TestCombineNestedForLoop() { var limit = new LINQToTTreeLib.Variables.ValSimple("5", typeof(int)); var loopP1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var loop1 = new StatementForLoop(loopP1, limit); var loopP2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var loop2 = new StatementForLoop(loopP2, limit); loop1.Add(loop2); var limit2 = new LINQToTTreeLib.Variables.ValSimple("5", typeof(int)); var loopP12 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var loop12 = new StatementForLoop(loopP12, limit); var loopP22 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var loop22 = new StatementForLoop(loopP22, limit); loop12.Add(loop22); var r = loop1.TryCombineStatement(loop12, new dummyOpt()); Assert.IsTrue(r, "combination should work"); Assert.IsNull(loop1.Parent, "loop 1 parent"); Assert.AreEqual(loop1, loop2.Parent, "Loop 2 parent"); }
public void ForLoopRenameTwiceAsSource() { var c1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s1 = new StatementForLoop(c1, new ValSimple("5", typeof(int))); var a1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var v1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); s1.Add(new StatementAssign(a1, new ValSimple("10", typeof(int)))); s1.Add(new StatementAssign(a1, new ValSimple($"{a1.RawValue}+{a1.RawValue}", typeof(int), new IDeclaredParameter[] { a1 }))); var c2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s2 = new StatementForLoop(c1, new ValSimple("6", typeof(int))); var a2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var v2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); s2.Add(new StatementAssign(a2, new ValSimple("10", typeof(int)))); s2.Add(new StatementAssign(a2, new ValSimple($"{a2.RawValue}+{v2.RawValue}", typeof(int), new IDeclaredParameter[] { a2, v2 }))); var r = s1.RequiredForEquivalence(s2); // Since only loop variable matters here, we don't care, and we can do the rename. Assert.IsFalse(r.Item1, "Should be equivalent"); }
public void ForLoopRenameIgnoreDeclaredVariables() { var c1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s1 = new StatementForLoop(c1, new ValSimple("5", typeof(int))); var a1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var v1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); s1.Add(new StatementAssign(a1, new ValSimple("10", typeof(int)))); s1.Add(new StatementAssign(a1, new ValSimple($"{a1.RawValue}+{v1.RawValue}", typeof(int), new IDeclaredParameter[] { a1, v1 }))); s1.Add(v1); var c2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s2 = new StatementForLoop(c2, new ValSimple("5", typeof(int))); var a2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var v2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); s2.Add(new StatementAssign(a2, new ValSimple("10", typeof(int)))); s2.Add(new StatementAssign(a2, new ValSimple($"{a2.RawValue}+{v2.RawValue}", typeof(int), new IDeclaredParameter[] { a2, v2 }))); s2.Add(v2); var r = s1.RequiredForEquivalence(s2); // Since only loop variable matters here, we don't care, and we can do the rename. Assert.IsTrue(r.Item1, "Should be equivalent"); var renames = r.Item2.ToArray(); Assert.AreEqual(1, renames.Length, "# of renames"); }
public void ForLoopGivenRenameRequestStatements() { var c1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s1 = new StatementForLoop(c1, new ValSimple("5", typeof(int))); var a1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); s1.Add(new StatementAssign(a1, new ValSimple("10", typeof(int)))); var c2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s2 = new StatementForLoop(c2, new ValSimple("5", typeof(int))); var a2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); s2.Add(new StatementAssign(a2, new ValSimple("10", typeof(int)))); var r = s1.RequiredForEquivalence(s2, new Tuple<string, string>[] { new Tuple<string, string>(a2.RawValue, a1.RawValue) }); // Since only loop variable matters here, we don't care, and we can do the rename. Assert.IsTrue(r.Item1, "Should be equivalent"); var renames = r.Item2.ToArray(); Assert.AreEqual(0, renames.Length, "# of renames"); }
public void ForLoopDifferentNumberOfStatements() { var c1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s1 = new StatementForLoop(c1, new ValSimple("5", typeof(int))); var a1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); s1.Add(new StatementAssign(a1, new ValSimple("10", typeof(int)))); var c2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s2 = new StatementForLoop(c1, new ValSimple("6", typeof(int))); var r = s1.RequiredForEquivalence(s2); // Since only loop variable matters here, we don't care, and we can do the rename. Assert.IsFalse(r.Item1, "Can't be equivalent"); }
public void DeclaredVariablesLocalOnly() { var outter = new StatementInlineBlock(); outter.Add(DeclarableParameter.CreateDeclarableParameterExpression(typeof(int))); var s = new StatementForLoop(DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)), new ValSimple("5", typeof(int))); s.Add(DeclarableParameter.CreateDeclarableParameterExpression(typeof(int))); outter.Add(s); Assert.AreEqual(2, s.DeclaredVariables.Count()); Assert.AreEqual(3, s.AllDeclaredVariables.Count()); }
public void DependentAndResultVariablesWithDecl() { var counter = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var s = new StatementForLoop(counter, new ValSimple("5", typeof(int))); var result = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); s.Add(result); var assign = new StatementAssign(result, new ValSimple($"{result.RawValue}+{counter.RawValue}", typeof(int), new IDeclaredParameter[] { counter, result })); s.Add(assign); Assert.AreEqual(0, s.DependentVariables.Count(), "# of dependent variables"); Assert.AreEqual(0, s.ResultVariables.Count(), "# of result variables"); }
public void LiftLoopInvarient() { var v = new GeneratedCode(); var limit = new LINQToTTreeLib.Variables.ValSimple("5", typeof(int)); var loopP1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var loop1 = new StatementForLoop(loopP1, limit); v.Add(loop1); var p2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var assign1 = new StatementAssign(p2, new ValSimple("f", typeof(int))); loop1.Add(p2); loop1.Add(assign1); Console.WriteLine("Unoptimized:"); v.DumpCodeToConsole(); StatementLifter.Optimize(v); Console.WriteLine(""); Console.WriteLine("Optimized:"); v.DumpCodeToConsole(); Assert.AreEqual(0, loop1.Statements.Count()); }