Exemplo n.º 1
0
        public override bool ApplyOptimization(CilMethodInterpreter interpreter, ClosureEntities closure)
        {
            var useDef     = interpreter.MidRepresentation.UseDef;
            var operations = useDef.GetLocalOperations();

            var labelTable = useDef.GetLabelTable(true);
            var reached    = new SortedSet <int>();

            Interpret(0, operations, labelTable, reached);
            if (reached.Count == operations.Length)
            {
                return(false);
            }

            var toDelete = new List <int>();

            for (var i = 0; i < operations.Length; i++)
            {
                if (!reached.Contains(i))
                {
                    toDelete.Add(i);
                }
            }
            interpreter.DeleteInstructions(toDelete);
            return(true);
        }
Exemplo n.º 2
0
        public override void OptimizeOperations(CilMethodInterpreter methodInterpreter)
        {
            var useDef          = methodInterpreter.MidRepresentation.UseDef;
            var localOperations = useDef.GetLocalOperations();

            var toRemove    = new List <int>();
            var callIndices = useDef.GetOperationsOfKind(OperationKind.Call);

            foreach (var index in callIndices)
            {
                var operation   = localOperations[index];
                var methodData  = operation.Get <CallMethodStatic>();
                var interpreter = methodData.GetInterpreter(Closure);
                if (interpreter == null)
                {
                    continue;
                }
                if (methodData.Result != null)
                {
                    continue;
                }
                var properties = interpreter.AnalyzeProperties;
                if (properties.IsReadOnly || properties.IsPure)
                {
                    toRemove.Add(index);
                }
            }
            if (toRemove.Count == 0)
            {
                return;
            }
            methodInterpreter.DeleteInstructions(toRemove);
            Result = true;
        }
Exemplo n.º 3
0
        public override void OptimizeOperations(CilMethodInterpreter interpreter)
        {
            var useDef     = interpreter.MidRepresentation.UseDef;
            var operations = useDef.GetLocalOperations();

            var labelTable = useDef.GetLabelTable(true);
            var reached    = new SortedSet <int>();

            Interpret(0, operations, labelTable, reached);
            if (reached.Count == operations.Length)
            {
                return;
            }
            Result = true;
            var toDelete = new List <int>();

            for (var i = 0; i < operations.Length; i++)
            {
                if (!reached.Contains(i))
                {
                    toDelete.Add(i);
                }
            }
            interpreter.DeleteInstructions(toDelete);
        }
        public override void OptimizeOperations(CilMethodInterpreter interpreter)
        {
            var useDef = interpreter.MidRepresentation.UseDef;

            List <int> newOperations;
            var        newObjOperations = FindAllNewOperators(useDef, out newOperations);

            if (newObjOperations.Length == 0)
            {
                return;
            }
            var getAllUsages = useDef.GetAllUsedVariables();
            var unusedNewObj = new List <int>();

            foreach (var newOperation in newOperations)
            {
                var definition = useDef.GetDefinition(newOperation);
                if (getAllUsages.Contains(definition))
                {
                    continue;
                }
                unusedNewObj.Add(newOperation);
            }
            if (unusedNewObj.Count == 0)
            {
                return;
            }
            Result = true;
            interpreter.DeleteInstructions(unusedNewObj);
        }
Exemplo n.º 5
0
        public override void OptimizeOperations(CilMethodInterpreter interpreter)
        {
            var useDef     = interpreter.MidRepresentation.UseDef;
            var operations = useDef.GetLocalOperations();

            var labelIndices = useDef.GetOperationsOfKind(OperationKind.Label);
            var found        = labelIndices.Length == 0;

            if (!found)
            {
                return;
            }
            foreach (var i in labelIndices)
            {
                var operation2 = operations[i + 1];
                if (operation2.Kind != OperationKind.Label)
                {
                    continue;
                }

                var operation = operations[i];
                var jumpId    = ((Label)operation).JumpTo;
                var jumpId2   = ((Label)operation2).JumpTo;;
                OptimizeConsecutiveLabels(operations, jumpId, jumpId2);
                interpreter.DeleteInstructions(new[] { i + 1 });
                Result = true;
            }
        }
        public override bool ApplyOptimization(CilMethodInterpreter interpreter, ClosureEntities closure)
        {
            var useDef = interpreter.MidRepresentation.UseDef;

            List <int> newOperations;
            var        newObjOperations = FindAllNewOperators(useDef, out newOperations);

            if (newObjOperations.Length == 0)
            {
                return(false);
            }
            var getAllUsages = useDef.GetAllUsedVariables();
            var unusedNewObj = new List <int>();

            foreach (var newOperation in newOperations)
            {
                var definition = useDef.GetDefinition(newOperation);
                if (getAllUsages.Contains(definition))
                {
                    continue;
                }
                unusedNewObj.Add(newOperation);
            }
            if (unusedNewObj.Count == 0)
            {
                return(false);
            }
            interpreter.DeleteInstructions(unusedNewObj);
            return(true);
        }
Exemplo n.º 7
0
 bool ApplyOptimization(CilMethodInterpreter methodInterpreter, List <int> toPatch,
                        LocalOperation[] localOperations)
 {
     foreach (var patchLine in toPatch)
     {
         var prevOp = localOperations[patchLine - 1];
         var assign = localOperations[patchLine].Get <Assignment>();
         prevOp.SwitchUsageWithDefinition(assign.Right as LocalVariable, assign.AssignedTo);
     }
     methodInterpreter.DeleteInstructions(toPatch);
     return(true);
 }
Exemplo n.º 8
0
        public override bool ApplyOptimization(CilMethodInterpreter interpreter, ClosureEntities closure)
        {
            var definitionsDictionary = new Dictionary <LocalVariable, int>();

            var usagesDictionary = new Dictionary <LocalVariable, int>();

            var metaMidRepresentation = interpreter.MidRepresentation;
            var localOperations       = interpreter.MidRepresentation.UseDef.GetLocalOperations();

            definitionsDictionary.Clear();
            BuildDefinitionDictionary(localOperations, metaMidRepresentation.UseDef, definitionsDictionary,
                                      usagesDictionary);
            RemoveNonUniqueDefinitions(definitionsDictionary);
            RemoveNonUniqueDefinitions(usagesDictionary);
            var toPatch = new List <int>();

            foreach (var targetOp in localOperations.Where(op => op.Kind == OperationKind.Assignment))
            {
                var assignment = targetOp.Get <Assignment>();
                var rightVar   = assignment.Right as LocalVariable;
                if (rightVar == null)
                {
                    continue;
                }
                if (!usagesDictionary.ContainsKey(rightVar))
                {
                    continue;
                }
                var leftVar = assignment.AssignedTo;
                if (!definitionsDictionary.ContainsKey(rightVar) ||
                    !definitionsDictionary.ContainsKey(leftVar))
                {
                    continue;
                }
                var rightId = definitionsDictionary[rightVar];
                var leftId  = definitionsDictionary[leftVar];
                if (leftId - rightId != 1)
                {
                    continue;
                }
                toPatch.Add(leftId);
            }
            if (toPatch.Count == 0)
            {
                return(false);
            }
            var toRemove = PatchInstructions(localOperations, toPatch);

            interpreter.DeleteInstructions(toRemove);
            return(true);
        }
Exemplo n.º 9
0
        public override void OptimizeOperations(CilMethodInterpreter interpreter)
        {
            var operations = interpreter.MidRepresentation.UseDef.GetLocalOperations();
            var labelTable = interpreter.MidRepresentation.UseDef.GetLabelTable();
            var toRemove   = new List <int>();

            foreach (var labelInfo in labelTable)
            {
                var i = labelInfo.Value - 1;
                if (i < 0)
                {
                    continue;
                }
                var operation = operations[i];
                switch (operation.Kind)
                {
                case OperationKind.AlwaysBranch:
                    var jumpLabel = labelTable[operation.Get <AlwaysBranch>().JumpTo];
                    if (jumpLabel != labelInfo.Value)
                    {
                        continue;
                    }
                    toRemove.Add(i);
                    continue;

                case OperationKind.BranchOperator:
                    var destAssignment = (BranchOperator)operation;
                    var jumpTo         = labelTable[destAssignment.JumpTo];
                    if (jumpTo != labelInfo.Value)
                    {
                        continue;
                    }
                    toRemove.Add(i);
                    continue;

                default:
                    continue;
                }
            }
            if (toRemove.Count == 0)
            {
                return;
            }
            interpreter.DeleteInstructions(toRemove);
            Result = true;
        }
        public override void OptimizeOperations(CilMethodInterpreter interpreter)
        {
            var useDef = interpreter.MidRepresentation.UseDef;

            var found = useDef.GetOperationsOfKind(OperationKind.Label).Length != 0;

            if (!found)
            {
                return;
            }

            var operationIndexes = useDef.GetOperationsOfKind(OperationKind.BranchOperator).ToList();

            operationIndexes.AddRange(useDef.GetOperationsOfKind(OperationKind.AlwaysBranch));
            var operations = useDef.GetLocalOperations();

            var candidateLabelTable = interpreter.MidRepresentation.UseDef.GetLabelTable(true);

            foreach (var index in operationIndexes)
            {
                var operation = operations[index];
                if (operation.Kind == OperationKind.BranchOperator)
                {
                    var destAssignment = (BranchOperator)operation;
                    candidateLabelTable.Remove(destAssignment.JumpTo);
                    continue;
                }
                if (operation.Kind == OperationKind.AlwaysBranch)
                {
                    candidateLabelTable.Remove(operation.Get <AlwaysBranch>().JumpTo);
                }
            }
            if (candidateLabelTable.Count == 0)
            {
                return;
            }
            var labelsToRemove = candidateLabelTable.Values.ToList();

            interpreter.DeleteInstructions(labelsToRemove);
            Result = true;
        }
Exemplo n.º 11
0
        public override void OptimizeOperations(CilMethodInterpreter methodInterpreter)
        {
            var toRemove        = new List <int>();
            var useDef          = methodInterpreter.MidRepresentation.UseDef;
            var localOperations = useDef.GetLocalOperations();
            var calls           = useDef.GetOperationsOfKind(OperationKind.Call);

            foreach (var index in calls)
            {
                var localOperation = localOperations[index];

                if (localOperation.Kind != OperationKind.Call)
                {
                    continue;
                }

                var methodData  = (CallMethodStatic)localOperation;
                var interpreter = methodData.Info.GetInterpreter(Closure);
                if (interpreter == null)
                {
                    continue;
                }
                var isEmpty = interpreter.AnalyzeProperties.IsEmpty;
                if (!isEmpty)
                {
                    continue;
                }
                toRemove.Add(index);
            }
            if (toRemove.Count == 0)
            {
                return;
            }
            methodInterpreter.DeleteInstructions(toRemove);
            Result = true;
        }