public override ISet <Expression> TransferFunction(ISet <Expression> input, BasicBlock block)
        {
            var kill   = GetKill(block).Cast <Identifier>();
            var result = SetFactory.GetSet(
                input.Where(inputExpression =>
                            !kill.Any(inputExpression.HasIdentifiedSubexpression)));

            result.UnionWith(GetGen(block));

            return(result);

            /*
             * Non-LINQ realization
             * var difference = SetFactory.GetSet();
             * var foundInKill = false;
             * foreach (var inputExpression in input)
             * {
             *  foreach (var killExpression in kill)
             *  {
             *      if (!inputExpression.HasIdentifiedSubexpression(killExpression as Identifier))
             *          continue;
             *
             *      foundInKill = true;
             *      break;
             *  }
             *
             *  if (!foundInKill)
             *      difference.Add(inputExpression);
             *
             *  foundInKill = false;
             * }
             * return SetFactory.GetSet(GetGen(block).Union(difference));
             */
        }
 public override ISet <Expression> GetGen(BasicBlock block)
 {
     return(SetFactory.GetSet(
                block.Commands
                .OfType <Assignment>()
                .Select(x => x.Value)));
 }
 public override ISet <Expression> GetKill(BasicBlock block)
 {
     return(SetFactory.GetSet(
                block.Commands
                .OfType <Assignment>()
                .Select(x => x.Target as Expression)));
 }
Пример #4
0
        public static Dictionary <Edge <BasicBlock>, ISet <int> > FindAllNaturalLoops(ControlFlowGraph.Graph g)
        {
            var classify = EdgeClassification.EdgeClassification.ClassifyEdge(g);
            var result   = new Dictionary <Edge <BasicBlock>, ISet <int> >();

            foreach (var pair in classify)
            {
                if (pair.Value == EdgeClassification.Model.EdgeType.Retreating)
                {
                    Stack = new Stack <int>();
                    Loop  = SetFactory.GetSet(new int[] { pair.Key.Target.BlockId });
                    Insert(pair.Key.Source.BlockId);
                    while (Stack.Count() > 0)
                    {
                        int m = Stack.Pop();
                        foreach (BasicBlock p in g.getParents(m))
                        {
                            Insert(p.BlockId);
                        }
                    }
                    result.Add(pair.Key, Loop);
                }
            }
            return(result);
        }
 public override ISet <CommandNumber> GatherOperation(IEnumerable <ISet <CommandNumber> > blocks)
 {
     return(blocks.Aggregate(SetFactory.GetSet <CommandNumber>(), (res, b) =>
     {
         res.UnionWith(b);
         return res;
     }));
 }
        public override ISet <CommandNumber> CommandTransferFunction(ISet <CommandNumber> input, BasicBlock block, int commandNumber)
        {
            GenKillOneCommand genKill = calculator.CalculateGenAndKill(block, commandNumber); var result = SetFactory.GetSet <CommandNumber>(input);

            result.ExceptWith(genKill.Kill);
            result.UnionWith(genKill.Gen == null ? SetFactory.GetSet <CommandNumber>() : SetFactory.GetSet(genKill.Gen));
            return(result);
        }
        public override ISet <T> TransferFunction(ISet <T> input, BasicBlock block)
        {
            var result = SetFactory.GetSet <T>(input);

            result.ExceptWith(GetKill(block));
            result.UnionWith(GetGen(block));
            return(result);
        }
Пример #8
0
        public override ISet <CommandNumber> GatherOperation(IEnumerable <ISet <CommandNumber> > blocks)
        {
            ISet <CommandNumber> res = SetFactory.GetSet <CommandNumber>();

            foreach (var command in blocks)
            {
                res.UnionWith(command);
            }
            return(res);
        }
        public override ISet <Expression> GatherOperation(IEnumerable <ISet <Expression> > blocks)
        {
            ISet <Expression> intersection = SetFactory.GetSet((IEnumerable <Expression>)blocks.First());

            foreach (var block in blocks.Skip(1))
            {
                intersection.IntersectWith(block);
            }

            return(intersection);
        }
        public override ISet <string> GatherOperation(IEnumerable <ISet <string> > blocks)
        {
            ISet <string> union = SetFactory.GetSet((IEnumerable <string>)blocks.First());

            foreach (var block in blocks.Skip(1))
            {
                union.UnionWith(block);
            }

            return(union);
        }
 public GenKillOneCommandCalculator(Graph g)
 {
     graph = g;
     CommandStorage = new Dictionary<string, ISet<CommandNumber>>();
     foreach (BasicBlock block in graph)
         for (int i = 0; i < block.Commands.Count(); i++)
         {
             var command = block.Commands[i];
             if (command.GetType() == typeof(Assignment))
                 if (CommandStorage.ContainsKey((command as Assignment).Target.Name))
                     CommandStorage[(command as Assignment).Target.Name].Add(new CommandNumber(block.BlockId, i));
                 else
                     CommandStorage.Add((command as Assignment).Target.Name,
                         SetFactory.GetSet(new CommandNumber(block.BlockId, i)));
         }
     Kill = SetFactory.GetSet<CommandNumber>();
 }
Пример #12
0
        static TransferFunctionStorage <ISet <V> > ApplyAscendingPart <V>(Graph graph, List <Region> regions, SetIterativeAlgorithmParameters <V> param)
        {
            TransferFunctionStorage <ISet <V> > result = new TransferFunctionStorage <ISet <V> >();

            foreach (Region r in regions)
            {
                LeafRegion leaf = r as LeafRegion;
                TransferFunctionStorage <ISet <V> > clone = result.Clone();
                if (leaf != null)
                {
                    //////
                    for (int i = graph.Count(); i < regions.Count; ++i)
                    {
                        result[regions[i], RegionDirection.Out, leaf] = input => param.TransferFunction(input, leaf.Block);
                    }
                    /////
                    result[leaf, RegionDirection.In, leaf]  = Identity;
                    result[leaf, RegionDirection.Out, leaf] = input => param.TransferFunction(input, leaf.Block);
                }
                BodyRegion body = r as BodyRegion;
                if (body != null)
                {
                    foreach (Region s in body.Regions)
                    {
                        LeafRegion header = s as LeafRegion;
                        if (header != null)
                        {
                            result[body, RegionDirection.In, s] = Identity;
                        }
                        else
                        {
                            result[body, RegionDirection.In, s] = input => GatherFunctionsResults(input, clone, body, s.Header.InputBlocks, graph, param);
                        }
                        CalculateForOutputBlocks(result, body, s, s.OutputBlocks, graph);
                    }
                }
                LoopRegion loop = r as LoopRegion;
                if (loop != null)
                {
                    result[loop, RegionDirection.In, loop.Body] = input => SetFactory.GetSet <V>(input.Union(GatherFunctionsResults(input, clone, loop.Body, loop.Header.InputBlocks, graph, param)));
                    CalculateForOutputBlocks(result, loop, loop.Body, loop.OutputBlocks, graph);
                }
            }
            return(result);
        }
Пример #13
0
        public override ISet <CommandNumber> GetGen(BasicBlock block)
        {
            List <GenKillOneCommand> listGenKill = block.Commands.Select((x, i) => commandCalc.CalculateGenAndKill(block, i)).ToList();

            ISet <CommandNumber> genB  = SetFactory.GetSet <CommandNumber>();
            ISet <CommandNumber> killS = SetFactory.GetSet <CommandNumber>();

            for (int i = block.Commands.Count - 1; i >= 0; i--)
            {
                ISet <CommandNumber> genS = SetFactory.GetSet <CommandNumber>();
                if (listGenKill[i].Gen != null)
                {
                    genS.Add(listGenKill[i].Gen);
                }
                genS.ExceptWith(killS);
                genB.UnionWith(genS);

                killS.UnionWith(listGenKill[i].Kill);
            }

            return(genB);
        }
 public override ISet <int> TransferFunction(ISet <int> input, BasicBlock block)
 {
     return(SetFactory.GetSet <int>(input.Union(new int[] { block.BlockId })));
 }
 public override ISet <string> TransferFunction(ISet <string> input, BasicBlock block)
 {
     return(SetFactory.GetSet(GetGen(block).Union(input.Except(GetKill(block)))));
 }
Пример #16
0
 public override ISet <CommandNumber> GetKill(BasicBlock block)
 {
     return(block.Commands.Select((b, i) => commandCalc.CalculateGenAndKill(block, i).Kill).Aggregate(SetFactory.GetSet <CommandNumber>(),
                                                                                                      (result, x) =>
     {
         result.UnionWith(x);
         return result;
     }));
 }