示例#1
0
        public static void DeleteDeadCode(ControlFlowGraph graph)
        {
            var usedVars = CreateUsedVarsSets(graph);
            var info     = new ReachingDefinitions().Execute(graph);

            var usedDefinitions = new HashSet <Instruction>(
                info[graph.GetCurrentBasicBlocks()[graph.GetCurrentBasicBlocks().Count - 1]].In,
                new InstructionComparer());

            var possibleOperationTypes = new[] { "assign", "input", "PLUS" };

            var wasChanged = true;

            while (wasChanged)
            {
                wasChanged = false;

                foreach (var block in graph.GetCurrentBasicBlocks())
                {
                    var deadDefinitions = info[block].In.Except(info[block].Out);
                    foreach (var oldDef in deadDefinitions)
                    {
                        var variable = oldDef.Result;

                        // find first assign in current block that rewrites variable
                        var newDefIndex = block.GetInstructions()
                                          .Select((t, i) => (Instruction: t, Index: i))
                                          .First(t => possibleOperationTypes.Contains(t.Instruction.Operation) && t.Instruction.Result == variable)
                                          .Index;

                        var blockWithOldDef = graph.GetCurrentBasicBlocks()
                                              .Single(z => z.GetInstructions().Any(t => t == oldDef));
                        var oldDefIndex = blockWithOldDef.GetInstructions()
                                          .Select((t, i) => (Instruction: t, Index: i))
                                          .Single(t => t.Instruction == oldDef)
                                          .Index;

                        if (usedDefinitions.Contains(oldDef) ||
                            IsUsedInCurrentBlock(block, variable, newDefIndex) ||
                            IsUsedInOriginalBlock(blockWithOldDef, variable, oldDefIndex) ||
                            IsUsedInOtherBlocks(graph, blockWithOldDef, oldDef, usedVars, info))
                        {
                            _ = usedDefinitions.Add(oldDef);
                            continue;
                        }

                        // remove useless definition
                        blockWithOldDef.RemoveInstructionByIndex(oldDefIndex);
                        if (!string.IsNullOrEmpty(oldDef.Label))
                        {
                            blockWithOldDef.InsertInstruction(oldDefIndex, new Instruction(oldDef.Label, "noop", null, null, null));
                        }
                        info       = new ReachingDefinitions().Execute(graph);
                        wasChanged = true;
                    }
                }
            }
        }
        public ReachingTransferFunc(ControlFlowGraph g)
        {
            var basicBlocks = g.GetCurrentBasicBlocks();

            GetDefs(basicBlocks);
            GetGenKill(basicBlocks);
        }
示例#3
0
        /// <summary>
        /// Принимает Граф потока данных и по нему ищет все естественные циклы
        /// </summary>
        /// <param name="cfg">Граф потока управления</param>
        /// <returns>
        /// Вернёт все натуральные циклы
        /// </returns>
        public static IReadOnlyList <IReadOnlyList <BasicBlock> > GetAllNaturalLoops(ControlFlowGraph cfg)
        {
            if (cfg.IsReducibleGraph())
            {
                var natLoops     = new List <List <BasicBlock> >();
                var ForwardEdges = cfg.GetCurrentBasicBlocks();

                foreach (var(From, To) in cfg.GetBackEdges())
                {
                    if (cfg.VertexOf(To) > 0)
                    {
                        var tmp = new List <BasicBlock>();
                        for (var i = cfg.VertexOf(To); i < cfg.VertexOf(From) + 1; i++)
                        {
                            if (!tmp.Contains(ForwardEdges[i]))
                            {
                                tmp.Add(ForwardEdges[i]);
                            }
                        }

                        natLoops.Add(tmp);
                    }
                }

                return(natLoops.Where(loop => IsNaturalLoop(loop, cfg)).ToList());
            }
            else
            {
                Console.WriteLine("Граф не приводим");
                return(new List <List <BasicBlock> >());
            }
        }
示例#4
0
        private static Dictionary <BasicBlock, HashSet <string> > CreateUsedVarsSets(ControlFlowGraph graph)
        {
            var result = new Dictionary <BasicBlock, HashSet <string> >();

            foreach (var block in graph.GetCurrentBasicBlocks())
            {
                result[block] = new HashSet <string>();
                foreach (var instruction in block.GetInstructions())
                {
                    if (IsVariable(instruction.Argument1))
                    {
                        _ = result[block].Add(instruction.Argument1);
                    }
                    if (IsVariable(instruction.Argument2))
                    {
                        _ = result[block].Add(instruction.Argument2);
                    }
                }
            }

            return(result);
        }