Beispiel #1
0
        public void Run(ILFunction function, ILTransformContext context)
        {
            var visitor = new DefiniteAssignmentVisitor(function, context.CancellationToken);

            function.Body.AcceptVisitor(visitor);
            foreach (var v in function.Variables)
            {
                if (v.Kind != VariableKind.Parameter && !visitor.IsPotentiallyUsedUninitialized(v))
                {
                    v.HasInitialValue = false;
                }
            }
            if (function.IsIterator || function.IsAsync)
            {
                // In yield return + async, the C# compiler tends to store null/default(T) to variables
                // when the variable goes out of scope. Remove such useless stores.
                foreach (var v in function.Variables)
                {
                    if (v.Kind == VariableKind.Local && v.StoreCount == 1 && v.LoadCount == 0 && v.AddressCount == 0)
                    {
                        if (v.StoreInstructions[0] is StLoc stloc && (stloc.Value.MatchLdNull() || stloc.Value is DefaultValue) && stloc.Parent is Block block)
                        {
                            block.Instructions.Remove(stloc);
                        }
                    }
                }
            }
        }
        public void Run(ILFunction function, ILTransformContext context)
        {
            var visitor = new DefiniteAssignmentVisitor(function, context.CancellationToken);

            function.Body.AcceptVisitor(visitor);
            foreach (var v in function.Variables)
            {
                if (v.Kind != VariableKind.Parameter && !visitor.IsPotentiallyUsedUninitialized(v))
                {
                    v.HasInitialValue = false;
                }
            }
            // Remove dead stores to variables that are never read from.
            // If the stored value has some side-effect, the value is unwrapped.
            // This is necessary to remove useless stores generated by some compilers, e.g., the F# compiler.
            // In yield return + async, the C# compiler tends to store null/default(T) to variables
            // when the variable goes out of scope.
            if (function.IsAsync || function.IsIterator || context.Settings.RemoveDeadCode)
            {
                var variableQueue = new Queue <ILVariable>(function.Variables);
                while (variableQueue.Count > 0)
                {
                    var v = variableQueue.Dequeue();
                    if (v.Kind != VariableKind.Local && v.Kind != VariableKind.StackSlot)
                    {
                        continue;
                    }
                    if (v.LoadCount != 0 || v.AddressCount != 0)
                    {
                        continue;
                    }
                    foreach (var stloc in v.StoreInstructions.OfType <StLoc>().ToArray())
                    {
                        if (stloc.Parent is Block block)
                        {
                            if (SemanticHelper.IsPure(stloc.Value.Flags))
                            {
                                block.Instructions.Remove(stloc);
                            }
                            else
                            {
                                stloc.ReplaceWith(stloc.Value);
                            }
                            if (stloc.Value is LdLoc ldloc)
                            {
                                variableQueue.Enqueue(ldloc.Variable);
                            }
                        }
                    }
                }
            }
        }
Beispiel #3
0
        internal static void ResetHasInitialValueFlag(ILFunction function, ILTransformContext context)
        {
            var visitor = new DefiniteAssignmentVisitor(function, context.CancellationToken);

            function.AcceptVisitor(visitor);
            foreach (var v in function.Variables)
            {
                if (v.Kind != VariableKind.Parameter && !visitor.IsPotentiallyUsedUninitialized(v))
                {
                    v.HasInitialValue = false;
                }
            }
        }
Beispiel #4
0
        public void Run(ILFunction function, ILTransformContext context)
        {
            var visitor = new DefiniteAssignmentVisitor(function, context.CancellationToken);

            function.Body.AcceptVisitor(visitor);
            foreach (var v in function.Variables)
            {
                if (v.Kind != VariableKind.Parameter && !visitor.IsPotentiallyUsedUninitialized(v))
                {
                    v.HasInitialValue = false;
                }
            }
            // Remove dead stores to variables that are never read from.
            // If the stored value has some side-effect, the value is unwrapped.
            // This is necessary to remove useless stores generated by some compilers, e.g., the F# compiler.
            // In yield return + async, the C# compiler tends to store null/default(T) to variables
            // when the variable goes out of scope.
            if (function.IsAsync || function.IsIterator || context.Settings.RemoveDeadCode)
            {
                var variableQueue = new Queue <ILVariable>(function.Variables);
                while (variableQueue.Count > 0)
                {
                    var v = variableQueue.Dequeue();
                    if (v.Kind != VariableKind.Local && v.Kind != VariableKind.StackSlot)
                    {
                        continue;
                    }
                    // Skip variables that are captured in a mcs yield state-machine
                    // loads of these will only be visible after DelegateConstruction step.
                    if (function.StateMachineCompiledWithMono && v.StateMachineField != null)
                    {
                        continue;
                    }
                    if (v.LoadCount != 0 || v.AddressCount != 0)
                    {
                        continue;
                    }
                    foreach (var stloc in v.StoreInstructions.OfType <StLoc>().ToArray())
                    {
                        if (stloc.Parent is Block block)
                        {
                            if (SemanticHelper.IsPure(stloc.Value.Flags))
                            {
                                block.Instructions.Remove(stloc);
                            }
                            else
                            {
                                stloc.ReplaceWith(stloc.Value);
                            }
                            if (stloc.Value is LdLoc ldloc)
                            {
                                variableQueue.Enqueue(ldloc.Variable);
                            }
                        }
                    }
                }
            }

            // Try to infer IType of stack slots that are of StackType.Ref:
            foreach (var v in function.Variables)
            {
                if (v.Kind == VariableKind.StackSlot && v.StackType == StackType.Ref && v.AddressCount == 0)
                {
                    IType newType = null;
                    // Multiple store are possible in case of (c ? ref a : ref b) += 1, for example.
                    foreach (var stloc in v.StoreInstructions.OfType <StLoc>())
                    {
                        var inferredType = stloc.Value.InferType(context.TypeSystem);
                        // cancel, if types of values do not match exactly
                        if (newType != null && !newType.Equals(inferredType))
                        {
                            newType = SpecialType.UnknownType;
                            break;
                        }
                        newType = inferredType;
                    }
                    // Only overwrite existing type, if a "better" type was found.
                    if (newType != null && newType != SpecialType.UnknownType)
                    {
                        v.Type = newType;
                    }
                }
            }
        }