Exemple #1
0
        public void GenerateSSA(MethodDefinition method)
        {
            var methodInfo = programInfo.GetOrAdd(method);

            //GenerateIL(method);
            //GenerateTAC(method);
            //GenerateCFG(method);
            GenerateWebs(method);

            if (!methodInfo.Contains("SSA_TEXT"))
            {
                var cfg = methodInfo.Get <ControlFlowGraph>("CFG");

                // Live Variables
                var liveVariables = new LiveVariablesAnalysis(cfg);
                var livenessInfo  = liveVariables.Analyze();

                // SSA
                var ssa = new StaticSingleAssignment(method.Body, cfg);
                ssa.Transform();
                ssa.Prune(livenessInfo);

                method.Body.UpdateVariables();

                var text = method.Body.ToString();
                methodInfo.Add("SSA_TEXT", text);

                text = DGMLSerializer.Serialize(cfg);
                methodInfo.Set("CFG_TEXT", text);

                text = DGMLSerializer.SerializeDominanceTree(cfg);
                methodInfo.Set("DT_TEXT", text);

                text = DGMLSerializer.SerializePostDominanceTree(cfg);
                methodInfo.Set("PDT_TEXT", text);

                text = DGMLSerializer.SerializeControlDependenceGraph(cfg);
                methodInfo.Set("CDG_TEXT", text);
            }
        }
Exemple #2
0
        //string _code = String.Empty;

        /// <summary>
        /// For a given methodDefinition, create a CFG and run basic analyses such as
        /// stack removal, SSA transformation, live-variables analysis, and copy-propagation.
        /// </summary>
        /// <param name="methodDefinition"></param>
        /// <returns></returns>
        private ControlFlowGraph PrepareMethod(IMethodDefinition methodDefinition)
        {
            var sw2 = new Stopwatch();

            sw2.Start();
            var disassembler = new Disassembler(mhost, methodDefinition, sourceLocationProvider);
            var methodBody   = disassembler.Execute();

            var cfAnalysis = new ControlFlowAnalysis(methodBody);
            var cfg        = cfAnalysis.GenerateNormalControlFlow();

            var domAnalysis = new DominanceAnalysis(cfg);

            domAnalysis.Analyze();
            domAnalysis.GenerateDominanceTree();

            var loopAnalysis = new NaturalLoopAnalysis(cfg);

            loopAnalysis.Analyze();

            var domFrontierAnalysis = new DominanceFrontierAnalysis(cfg);

            domFrontierAnalysis.Analyze();

            var splitter = new WebAnalysis(cfg, methodDefinition);

            splitter.Analyze();
            splitter.Transform();

            methodBody.UpdateVariables();

            var analysis = new TypeInferenceAnalysis(cfg, methodBody.MethodDefinition.Type);

            analysis.Analyze();

            var copyProgapagtion = new ForwardCopyPropagationAnalysis(cfg);

            copyProgapagtion.Analyze();
            copyProgapagtion.Transform(methodBody);

            var backwardCopyProgapagtion = new BackwardCopyPropagationAnalysis(cfg);

            backwardCopyProgapagtion.Analyze();
            backwardCopyProgapagtion.Transform(methodBody);

            var liveVariables = new LiveVariablesAnalysis(cfg);
            var resultLiveVar = liveVariables.Analyze();

            var ssa = new StaticSingleAssignment(methodBody, cfg);

            ssa.Transform();
            ssa.Prune(liveVariables);
            methodBody.UpdateVariables();
            sw2.Stop();
            this.nonAnalysisOverhead = sw2.Elapsed;
            return(cfg);

            //var cfg = ControlFlowGraph.GenerateNormalControlFlow(methodBody);
            //ControlFlowGraph.ComputeDominators(cfg);
            //ControlFlowGraph.IdentifyLoops(cfg);

            //ControlFlowGraph.ComputeDominatorTree(cfg);
            //ControlFlowGraph.ComputeDominanceFrontiers(cfg);

            //// Uniquely rename stack variables.
            //var splitter = new WebAnalysis(cfg);
            //splitter.Analyze();
            //splitter.Transform();

            //methodBody.UpdateVariables();

            //// Infer types for stack variables.
            //var typeAnalysis = new TypeInferenceAnalysis(cfg);
            //typeAnalysis.Analyze();

            //var backwardCopyAnalysis = new BackwardCopyPropagationAnalysis(cfg);
            //backwardCopyAnalysis.Analyze();
            //backwardCopyAnalysis.Transform(methodBody);

            //var lva = new LiveVariablesAnalysis(cfg);
            //lva.Analyze();

            //var ssa = new StaticSingleAssignmentAnalysis(methodBody, cfg);
            //ssa.Transform();
            //ssa.Prune(lva);

            //methodBody.UpdateVariables();

            ////_code = methodBody.ToString();
            //return cfg;
        }
Exemple #3
0
        public MethodCfgAndTac AnalyzeIntraProcedural(IMethodDefinition methodDefinition)
        {
            // System.Console.WriteLine("Traversing: {0}", methodDefinition.GetName());
            if (Stubber.SuppressM(methodDefinition))
            {
                return(null);
            }
            if (methodDefinition.IsExternal)
            {
                return(null);
            }
            if (methodDefinition.IsAbstract)
            {
                return(null);
            }

            ITypeDefinition         containingDefn         = methodDefinition.ContainingTypeDefinition;
            ISourceLocationProvider sourceLocationProvider = null;

            if (containingDefn != null)
            {
                IModule mod = TypeHelper.GetDefiningUnit(containingDefn) as IModule;
                if (moduleToPdbMap.ContainsKey(mod))
                {
                    sourceLocationProvider = moduleToPdbMap[mod];
                }
                else
                {
                    if (!(mod == null || mod == Dummy.Module || mod == Dummy.Assembly))
                    {
                        sourceLocationProvider = GetPdbReader(mod.Location);
                        moduleToPdbMap[mod]    = sourceLocationProvider;
                    }
                }
            }
            var disassembler = new Disassembler(host, methodDefinition, sourceLocationProvider);
            var methodBody   = disassembler.Execute();

            var cfAnalysis = new ControlFlowAnalysis(methodBody);
            // var cfg = cfAnalysis.GenerateNormalControlFlow();
            var cfg = cfAnalysis.GenerateExceptionalControlFlow();

            var domAnalysis = new DominanceAnalysis(cfg);

            domAnalysis.Analyze();
            domAnalysis.GenerateDominanceTree();

            var loopAnalysis = new NaturalLoopAnalysis(cfg);

            loopAnalysis.Analyze();

            var domFrontierAnalysis = new DominanceFrontierAnalysis(cfg);

            domFrontierAnalysis.Analyze();

            var splitter = new WebAnalysis(cfg, methodDefinition);

            splitter.Analyze();
            splitter.Transform();

            methodBody.UpdateVariables();

            var typeAnalysis = new TypeInferenceAnalysis(cfg, methodDefinition.Type);

            typeAnalysis.Analyze();

            var forwardCopyAnalysis = new ForwardCopyPropagationAnalysis(cfg);

            forwardCopyAnalysis.Analyze();
            forwardCopyAnalysis.Transform(methodBody);

            // backwardCopyAnalysis is buggy - it says so in the source file - see notes in src/test
            // var backwardCopyAnalysis = new BackwardCopyPropagationAnalysis(cfg);
            // backwardCopyAnalysis.Analyze();
            // backwardCopyAnalysis.Transform(methodBody);

            var liveVariables = new LiveVariablesAnalysis(cfg);

            liveVariables.Analyze();

            var ssa = new StaticSingleAssignment(methodBody, cfg);

            ssa.Transform();
            ssa.Prune(liveVariables);

            methodBody.UpdateVariables();

            MethodCfgAndTac mct = new MethodCfgAndTac(cfg, methodBody);

            foreach (IExceptionHandlerBlock ehInfo in disassembler.GetExceptionHandlers())
            {
                if (ehInfo is CatchExceptionHandler)
                {
                    mct.ehInfoList.Add(ehInfo as CatchExceptionHandler);
                }
            }
            return(mct);
        }
        public static ControlFlowGraph DoAnalysisPhases(this IMethodDefinition method, IMetadataHost host, ISourceLocationProvider locationProvider,
                                                        IEnumerable <IMethodReference> methodsToTryToInline = null)
        {
            AnalysisStats.extraAnalysisOverHead.Start();
            var disassembler = new Disassembler(host, method, locationProvider);

            var methodBody = disassembler.Execute();

            MethodBodyProvider.Instance.AddBody(method, methodBody);

            if (methodsToTryToInline != null)
            {
                DoInlining(method, host, methodBody, locationProvider, methodsToTryToInline);
            }

            var cfAnalysis = new ControlFlowAnalysis(methodBody);
            //var cfg = cfAnalysis.GenerateExceptionalControlFlow();
            var cfg = cfAnalysis.GenerateNormalControlFlow();

            var domAnalysis = new DominanceAnalysis(cfg);

            domAnalysis.Analyze();
            domAnalysis.GenerateDominanceTree();

            var loopAnalysis = new NaturalLoopAnalysis(cfg);

            loopAnalysis.Analyze();

            var domFrontierAnalysis = new DominanceFrontierAnalysis(cfg);

            domFrontierAnalysis.Analyze();

            var splitter = new WebAnalysis(cfg, method);

            splitter.Analyze();
            splitter.Transform();

            methodBody.UpdateVariables();


            var analysis = new TypeInferenceAnalysis(cfg, methodBody.MethodDefinition.Type);

            analysis.Analyze();

            var copyProgapagtion = new ForwardCopyPropagationAnalysis(cfg);

            copyProgapagtion.Analyze();
            copyProgapagtion.Transform(methodBody);

            //var backwardCopyProgapagtion = new BackwardCopyPropagationAnalysis(cfg);
            //backwardCopyProgapagtion.Analyze();
            //backwardCopyProgapagtion.Transform(methodBody);

            var liveVariables = new LiveVariablesAnalysis(cfg);
            var resultLiveVar = liveVariables.Analyze();


            var ssa = new StaticSingleAssignment(methodBody, cfg);

            ssa.Transform();
            ssa.Prune(liveVariables);
            methodBody.UpdateVariables();
            AnalysisStats.extraAnalysisOverHead.Stop();
            return(cfg);
        }