public static (bool wasChanged, List <Instruction> instructions) LiveVariableDeleteDeadCode(List <Instruction> instructions)
        {
            var wasChanged        = false;
            var newInstructions   = new List <Instruction>();
            var divResult         = BasicBlockLeader.DivideLeaderToLeader(newInstructions);
            var cfg               = new ControlFlowGraph(divResult);
            var activeVariable    = new LiveVariableAnalysis();
            var resActiveVariable = activeVariable.Execute(cfg);

            newInstructions = instructions.GetRange(0, instructions.Count - 1);
            foreach (var x in resActiveVariable)
            {
                foreach (var y in x.Value.Out)
                {
                    if (instructions[instructions.Count - 1].Result != y && instructions[instructions.Count - 1].Operation == "assign" && instructions[instructions.Count - 1].Argument1 != y)
                    {
                        wasChanged = true;
                    }
                    else
                    {
                        return(wasChanged, newInstructions);
                    }
                }
            }
            newInstructions.Add(instructions[instructions.Count - 1]);
            return(wasChanged, newInstructions);
        }
Beispiel #2
0
        public static (bool wasChanged, IReadOnlyList <Instruction> instructions) LiveVariableDeleteDeadCode(IReadOnlyList <Instruction> instructions)
        {
            var wasChanged        = false;
            var newInstructions   = new List <Instruction>();
            var divResult         = BasicBlockLeader.DivideLeaderToLeader(instructions);
            var cfg               = new ControlFlowGraph(divResult);
            var activeVariable    = new LiveVariableAnalysis();
            var resActiveVariable = activeVariable.Execute(cfg);

            foreach (var x in divResult)
            {
                var instructionsTemp = x.GetInstructions();
                if (resActiveVariable.ContainsKey(x))
                {
                    var InOutTemp = resActiveVariable[x];
                    foreach (var i in instructionsTemp)
                    {
                        if (!InOutTemp.Out.Contains(i.Result) && i.Operation == "assign" && i.Argument1 != i.Result)
                        {
                            wasChanged = true;
                            if (i.Label != "")
                            {
                                newInstructions.Add(new Instruction(i.Label, "noop", "", "", ""));
                            }
                        }
                        else
                        {
                            newInstructions.Add(i);
                        }
                    }
                }
            }
            return(wasChanged, newInstructions);
        }