public void AvailableExpressions()
        {
            var program   = @"
var a, b, c, d, x, e, g, y, zz, i;
2: a = x + y;
g = c + d;
3: zz = 1;
goto 1;
1: if (a < b)
    c = 1;
b = c + d;
goto 3;
e = zz + i;
";
            var graph     = GenCFG(program);
            var algorithm = new AvailableExpressions();

            _ = algorithm.Execute(graph);
            var iterationsFast = algorithm.Iterations;

            _ = algorithm.Execute(graph, false);
            var iterationsSlow = algorithm.Iterations;

            Assert.LessOrEqual(iterationsFast, iterationsSlow);
        }
        private List <(List <OneExpression>, List <OneExpression>)> GetActualInOutData(List <Instruction> TAC)
        {
            cfg = new ControlFlowGraph(BasicBlockLeader.DivideLeaderToLeader(TAC));
            availableExpressions    = new AvailableExpressions();
            resAvailableExpressions = availableExpressions.Execute(cfg);
            In  = new List <OneExpression>();
            Out = new List <OneExpression>();
            var actual = new List <(List <OneExpression>, List <OneExpression>)>();

            foreach (var block in resAvailableExpressions)
            {
                foreach (var expr in block.Value.In)
                {
                    In.Add(expr);
                }
                foreach (var expr in block.Value.Out)
                {
                    Out.Add(expr);
                }
                actual.Add((new List <OneExpression>(In), new List <OneExpression>(Out)));
                In.Clear();
                Out.Clear();
            }
            return(actual);
        }
        private IReadOnlyList <BasicBlock> GeneratorBasicBlockAfterOptimization(List <Instruction> TAC)
        {
            var cfg       = new ControlFlowGraph(BasicBlockLeader.DivideLeaderToLeader(TAC));
            var inOutData = new AvailableExpressions().Execute(cfg);

            AvailableExpressionsApplication.Execute(cfg, inOutData);
            return(cfg.GetCurrentBasicBlocks());
        }
        private static List <(IEnumerable <OneExpression>, IEnumerable <OneExpression>)> GetActualInOutData(string program)
        {
            var cfg = GenCFG(program);
            var resActiveVariable = new AvailableExpressions().Execute(cfg);
            var actual            = cfg.GetCurrentBasicBlocks()
                                    .Select(z => resActiveVariable[z])
                                    .Select(p => ((IEnumerable <OneExpression>)p.In, (IEnumerable <OneExpression>)p.Out))
                                    .ToList();

            return(actual);
        }
Exemplo n.º 5
0
        internal static (string, string) ApplyIterativeAlgorithm(ControlFlowGraph cfg, List <string> opts)
        {
            var strReturn = new StringBuilder();
            var strBefore = new StringBuilder();


            foreach (var b in cfg.GetCurrentBasicBlocks())
            {
                foreach (var inst in b.GetInstructions())
                {
                    strBefore.AppendLine(inst.ToString());
                }
                strBefore.AppendLine("----------");
            }

            foreach (var opt in opts)
            {
                switch (opt)
                {
                case "Доступные выражения":
                    var inout = new AvailableExpressions().Execute(cfg);
                    AvailableExpressionsApplication.Execute(cfg, inout);
                    break;

                case "Активные переменные":
                    cfg = LiveVariableAnalysisOptimization.LiveVariableDeleteDeadCode(cfg);
                    break;

                case "Достигающие определения":
                    var reachDef = new ReachingDefinitionsGlobal();
                    reachDef.DeleteDeadCode(cfg);
                    break;
                }
            }


            foreach (var b in cfg.GetCurrentBasicBlocks())
            {
                foreach (var inst in b.GetInstructions())
                {
                    strReturn.AppendLine(inst.ToString());
                }
                strReturn.AppendLine("----------");
            }

            return(strBefore.ToString(), strReturn.ToString());
        }
Exemplo n.º 6
0
        internal static (string, string) ApplyIterativeAlgorithm(ControlFlowGraph cfg, List <string> opts)
        {
            var strReturn = new StringBuilder();
            var strBefore = new StringBuilder();


            foreach (var b in cfg.GetCurrentBasicBlocks())
            {
                foreach (var inst in b.GetInstructions())
                {
                    strBefore.AppendLine(inst.ToString());
                }
                strBefore.AppendLine("----------");
            }

            foreach (var opt in opts)
            {
                switch (opt)
                {
                case "Доступные выражения":
                    var inout = new AvailableExpressions().Execute(cfg);
                    AvailableExpressionsApplication.Execute(cfg, inout);
                    break;

                default:
                    return(strBefore.ToString(), "Not realized yet");
                }
            }


            foreach (var b in cfg.GetCurrentBasicBlocks())
            {
                foreach (var inst in b.GetInstructions())
                {
                    strReturn.AppendLine(inst.ToString());
                }
                strReturn.AppendLine("----------");
            }

            return(strBefore.ToString(), strReturn.ToString());
        }
        public void NoOptimizationAfterAllOptimization()
        {
            var graph = BuildTACOptimizeCFG(@"var a, b, x, y, z, p, q, s;
x = y + z;
y = 1;
if (a < b)
{
    z = 2;
    p = y + z;
}
z = 1;
q = y + z;");
            var ae    = new AvailableExpressions().Execute(graph);

            AvailableExpressionsApplication.Execute(graph, ae);
            var expected = new List <string>()
            {
                "#in: noop",
                "#t1 = y + z",
                "x = #t1",
                "y = 1",
                "#t2 = a < b",
                "if #t2 goto L1",
                "goto L2",
                "L1: z = 2",
                "#t3 = y + 2",
                "p = #t3",
                "L2: z = 1",
                "#t4 = y + 1",
                "q = #t4",
                "#out: noop"
            };
            var actual = GetStringFromBlocks(graph.GetCurrentBasicBlocks());

            CollectionAssert.AreEqual(expected, actual);
        }
Exemplo n.º 8
0
        public void AvailableExpressions()
        {
            var cfg = GenCFG(@"
var a, b, c, d, x, e, g, y, zz, i;
2: a = x + y;
g = c + d;
3: zz = 1;
goto 1;
1: if (a < b)
    c = 1;
b = c + d;
goto 3;
e = zz + i;
");

            var resAvailableExpressions = new AvailableExpressions().Execute(cfg);
            var actual = cfg.GetCurrentBasicBlocks()
                         .Select(z => resAvailableExpressions[z])
                         .Select(p => ((IEnumerable <OneExpression>)p.In, (IEnumerable <OneExpression>)p.Out))
                         .ToList();

            var expected = new List <(IEnumerable <OneExpression> In, IEnumerable <OneExpression> Out)>()
            {
                // 0
                (In : new List <OneExpression>(),
                 Out : new List <OneExpression>()),

                // 1
                (In : new List <OneExpression>(),
                 Out : new List <OneExpression>()
                {
                    new OneExpression("PLUS", "x", "y"),
                    new OneExpression("PLUS", "c", "d")
                }),

                // 2
                (In : new List <OneExpression>()
                {
                    new OneExpression("PLUS", "x", "y"),
                    new OneExpression("PLUS", "c", "d")
                },
                 Out : new List <OneExpression>()
                {
                    new OneExpression("PLUS", "x", "y"),
                    new OneExpression("PLUS", "c", "d")
                }),

                // 3
                (In : new List <OneExpression>()
                {
                    new OneExpression("PLUS", "x", "y"),
                    new OneExpression("PLUS", "c", "d")
                },
                 Out : new List <OneExpression>()
                {
                    new OneExpression("EQGREATER", "a", "b"),
                    new OneExpression("PLUS", "x", "y"),
                    new OneExpression("PLUS", "c", "d")
                }),

                // 4
                (In : new List <OneExpression>()
                {
                    new OneExpression("EQGREATER", "a", "b"),
                    new OneExpression("PLUS", "x", "y"),
                    new OneExpression("PLUS", "c", "d")
                },
                 Out : new List <OneExpression>()
                {
                    new OneExpression("EQGREATER", "a", "b"),
                    new OneExpression("PLUS", "x", "y"),
                }),

                // 5
                (In : new List <OneExpression>()
                {
                    new OneExpression("EQGREATER", "a", "b"),
                    new OneExpression("PLUS", "x", "y"),
                },
                 Out : new List <OneExpression>()
                {
                    new OneExpression("PLUS", "c", "d"),
                    new OneExpression("PLUS", "x", "y")
                }),

                // 6
                (In : new List <OneExpression>(),
                 Out : new List <OneExpression>())
            };

            AssertSet(expected, actual);
        }
Exemplo n.º 9
0
        public void AvailableExpressionsTest()
        {
            var TAC = GenTAC(@"var a, b, c, d, x, u, e,g, y,zz,i; 
2: a = x + y;
g = c + d;
3: zz = 1;
goto 1;
1: if(a < b) 
    c = 1; 
b = c + d;
goto 3;
e = zz + i;"
                             );
            var expected = new List <(List <OneExpression>, List <OneExpression>)>()
            {
                (new List <OneExpression>(), new List <OneExpression>()),
                (new List <OneExpression>(), new List <OneExpression>()
                {
                    new OneExpression("PLUS", "x", "y"), new OneExpression("PLUS", "c", "d")
                }),

                (new List <OneExpression>()
                {
                    new OneExpression("PLUS", "x", "y"), new OneExpression("PLUS", "c", "d")
                },
                 new List <OneExpression>()
                {
                    new OneExpression("PLUS", "x", "y"), new OneExpression("PLUS", "c", "d")
                }),

                (new List <OneExpression>()
                {
                    new OneExpression("PLUS", "x", "y"), new OneExpression("PLUS", "c", "d")
                },
                 new List <OneExpression>()
                {
                    new OneExpression("LESS", "a", "b"),
                    new OneExpression("PLUS", "x", "y"), new OneExpression("PLUS", "c", "d")
                }),

                (new List <OneExpression>()
                {
                    new OneExpression("LESS", "a", "b"), new OneExpression("PLUS", "x", "y"), new OneExpression("PLUS", "c", "d")
                },
                 new List <OneExpression>()
                {
                    new OneExpression("LESS", "a", "b")
                    , new OneExpression("PLUS", "x", "y"), new OneExpression("PLUS", "c", "d")
                }
                ),

                (new List <OneExpression>()
                {
                    new OneExpression("LESS", "a", "b"), new OneExpression("PLUS", "x", "y"), new OneExpression("PLUS", "c", "d")
                },
                 new List <OneExpression>()
                {
                    new OneExpression("LESS", "a", "b"), new OneExpression("PLUS", "x", "y")
                }
                ),

                (new List <OneExpression>()
                {
                    new OneExpression("PLUS", "x", "y"), new OneExpression("LESS", "a", "b")
                },
                 new List <OneExpression>()
                {
                    new OneExpression("PLUS", "c", "d"), new OneExpression("PLUS", "x", "y")
                }
                ),
            };

            var cfg = new ControlFlowGraph(BasicBlockLeader.DivideLeaderToLeader(TAC));
            var availableExpressions    = new AvailableExpressions();
            var resAvailableExpressions = availableExpressions.Execute(cfg);
            var In     = new List <OneExpression>();
            var Out    = new List <OneExpression>();
            var actual = new List <(List <OneExpression>, List <OneExpression>)>();

            foreach (var block in resAvailableExpressions)
            {
                foreach (var expr in block.Value.In)
                {
                    In.Add(expr);
                }
                foreach (var expr in block.Value.Out)
                {
                    Out.Add(expr);
                }
                actual.Add((new List <OneExpression>(In), new List <OneExpression>(Out)));
                In.Clear();
                Out.Clear();
            }
            AssertSet(expected, actual);
        }
Exemplo n.º 10
0
        public void AvailableExpressionsTest()
        {
            var TAC = GenTAC(@"var a, b, c, d, x, u, e,g, y,zz,i; 
2: a = x + y;
g = c + d;
3: zz = 1;
goto 1;
1: if(a < b) 
    c = 1; 
b = c + d;
goto 3;
e = zz + i;"
                             );

            var cfg = GenCFG(TAC);
            var resAvailableExpressions = new AvailableExpressions().Execute(cfg);
            var actual = cfg.GetCurrentBasicBlocks()
                         .Select(z => resAvailableExpressions[z])
                         .Select(p => ((IEnumerable <OneExpression>)p.In, (IEnumerable <OneExpression>)p.Out))
                         .ToList();

            var expected = new List <(IEnumerable <OneExpression>, IEnumerable <OneExpression>)>()
            {
                (new List <OneExpression>(),
                 new List <OneExpression>()),

                (new List <OneExpression>(),
                 new List <OneExpression>()
                {
                    new OneExpression("PLUS", "x", "y"), new OneExpression("PLUS", "c", "d")
                }),

                (new List <OneExpression>()
                {
                    new OneExpression("PLUS", "x", "y"), new OneExpression("PLUS", "c", "d")
                },
                 new List <OneExpression>()
                {
                    new OneExpression("PLUS", "x", "y"), new OneExpression("PLUS", "c", "d")
                }),

                (new List <OneExpression>()
                {
                    new OneExpression("PLUS", "x", "y"), new OneExpression("PLUS", "c", "d")
                },
                 new List <OneExpression>()
                {
                    new OneExpression("LESS", "a", "b"), new OneExpression("PLUS", "x", "y"), new OneExpression("PLUS", "c", "d")
                }),

                (new List <OneExpression>()
                {
                    new OneExpression("LESS", "a", "b"), new OneExpression("PLUS", "x", "y"), new OneExpression("PLUS", "c", "d")
                },
                 new List <OneExpression>()
                {
                    new OneExpression("LESS", "a", "b"), new OneExpression("PLUS", "x", "y"), new OneExpression("PLUS", "c", "d")
                }),

                (new List <OneExpression>()
                {
                    new OneExpression("LESS", "a", "b"), new OneExpression("PLUS", "x", "y"), new OneExpression("PLUS", "c", "d")
                },
                 new List <OneExpression>()
                {
                    new OneExpression("LESS", "a", "b"), new OneExpression("PLUS", "x", "y")
                }),

                (new List <OneExpression>()
                {
                    new OneExpression("PLUS", "x", "y"), new OneExpression("LESS", "a", "b")
                },
                 new List <OneExpression>()
                {
                    new OneExpression("PLUS", "c", "d"), new OneExpression("PLUS", "x", "y")
                }),

                (new List <OneExpression>(), new List <OneExpression>())
            };

            AssertSet(expected, actual);
        }