public void ConstantPropagation() { string text = @" if 1 { x = 2; y = 3; } else { x = 3; y = 2; } z = x + y; "; SyntaxNode root = ParserWrap.Parse(text); Graph graph = new Graph( BasicBlocksGenerator.CreateBasicBlocks( ThreeAddressCodeGenerator.CreateAndVisit(root).Program)); var constantPropagationIterative = IterativeAlgorithm.Apply(graph, new ConstantsPropagationParameters()); var constantPropagationMOP = MeetOverPaths.Apply(graph, new ConstantsPropagationParameters()); var it = constantPropagationIterative.Out.Select( pair => $"{pair.Key}: {string.Join(", ", pair.Value.Select(ex => ex.ToString()))}"); foreach (var outInfo in it) { Trace.WriteLine(outInfo); } var mop = constantPropagationMOP.Select( pair => $"{pair.Key}: {string.Join(", ", pair.Value.Select(ex => ex.ToString()))}"); Trace.WriteLine("===="); foreach (var outInfo in mop) { Trace.WriteLine(outInfo); } Assert.IsFalse(constantPropagationIterative.In.OrderBy(kvp => kvp.Key). Zip(constantPropagationMOP.OrderBy(kvp => kvp.Key), (v1, v2) => v1.Key == v2.Key && v1.Value.OrderBy(kvp => kvp.Key).SequenceEqual(v2.Value.OrderBy(kvp => kvp.Key))).All(x => x)); }
public void AvailableExpression() { string programText = @" a = 4; b = 4; c = a + b; if 1 a = 3; else b = 2; print(c); "; SyntaxNode root = ParserWrap.Parse(programText); var threeAddressCode = ThreeAddressCodeGenerator.CreateAndVisit(root).Program; var basicBlocks = BasicBlocksGenerator.CreateBasicBlocks(threeAddressCode); Graph g = new Graph(basicBlocks); var availableExprsIterative = IterativeAlgorithm.Apply(g, new AvailableExpressionsCalculator(g)); var availableExprsMOP = MeetOverPaths.Apply(g, new AvailableExpressionsCalculator(g)); var it = availableExprsIterative.Out.Select( pair => $"{pair.Key}: {string.Join(", ", pair.Value.Select(ex => ex.ToString()))}"); foreach (var outInfo in it) { Trace.WriteLine(outInfo); } var mop = availableExprsMOP.Select( pair => $"{pair.Key}: {string.Join(", ", pair.Value.Select(ex => ex.ToString()))}"); Trace.WriteLine("===="); foreach (var outInfo in mop) { Trace.WriteLine(outInfo); } Assert.IsTrue(availableExprsIterative.Out.OrderBy(kvp => kvp.Key). Zip(availableExprsMOP.OrderBy(kvp => kvp.Key), (v1, v2) => v1.Key == v2.Key && v1.Value.SetEquals(v2.Value)).All(x => x)); }
public void DeadAliveVariables() { string text = @" a = 2; b = 3; 1: c = a + b; 2: a = 3; b = 4; 3: c = a; "; SyntaxNode root = ParserWrap.Parse(text); Graph graph = new Graph( BasicBlocksGenerator.CreateBasicBlocks( ThreeAddressCodeGenerator.CreateAndVisit(root).Program)); var deadAliveVarsIterative = IterativeAlgorithm.Apply(graph, new DeadAliveIterativeAlgorithmParameters()); var deadAliveVarsMOP = MeetOverPaths.Apply(graph, new DeadAliveIterativeAlgorithmParameters()); var it = deadAliveVarsIterative.In.Select( pair => $"{pair.Key}: {string.Join(", ", pair.Value.Select(ex => ex.ToString()))}"); foreach (var outInfo in it) { Trace.WriteLine(outInfo); } var mop = deadAliveVarsMOP.Select( pair => $"{pair.Key}: {string.Join(", ", pair.Value.Select(ex => ex.ToString()))}"); Trace.WriteLine("===="); foreach (var outInfo in mop) { Trace.WriteLine(outInfo); } Assert.IsTrue(deadAliveVarsIterative.In.OrderBy(kvp => kvp.Key). Zip(deadAliveVarsMOP.OrderBy(kvp => kvp.Key), (v1, v2) => v1.Key == v2.Key && v1.Value.SetEquals(v2.Value)).All(x => x)); }