Exemple #1
0
        public static void Optimize(StructuredProgramInfo info)
        {
            AstBlock mainBlock = info.MainBlock;

            AstBlockVisitor visitor = new AstBlockVisitor(mainBlock);

            foreach (IAstNode node in visitor.Visit())
            {
                if (node is AstAssignment assignment && assignment.Destination is AstOperand propVar)
                {
                    bool isWorthPropagating = propVar.Uses.Count == 1 || IsWorthPropagating(assignment.Source);

                    if (propVar.Defs.Count == 1 && isWorthPropagating)
                    {
                        PropagateExpression(propVar, assignment.Source);
                    }

                    if (propVar.Type == OperandType.LocalVariable && propVar.Uses.Count == 0)
                    {
                        visitor.Block.Remove(assignment);

                        info.Locals.Remove(propVar);
                    }
                }
            }

            RemoveEmptyBlocks(mainBlock);
        }
Exemple #2
0
        public static void Optimize(StructuredProgramContext context)
        {
            AstBlock mainBlock = context.CurrentFunction.MainBlock;

            // When debug mode is enabled, we disable expression propagation
            // (this makes comparison with the disassembly easier).
            if ((context.Config.Options.Flags & TranslationFlags.DebugMode) == 0)
            {
                AstBlockVisitor visitor = new AstBlockVisitor(mainBlock);

                foreach (IAstNode node in visitor.Visit())
                {
                    if (node is AstAssignment assignment && assignment.Destination is AstOperand propVar)
                    {
                        bool isWorthPropagating = propVar.Uses.Count == 1 || IsWorthPropagating(assignment.Source);

                        if (propVar.Defs.Count == 1 && isWorthPropagating)
                        {
                            PropagateExpression(propVar, assignment.Source);
                        }

                        if (propVar.Type == OperandType.LocalVariable && propVar.Uses.Count == 0)
                        {
                            visitor.Block.Remove(assignment);

                            context.CurrentFunction.Locals.Remove(propVar);
                        }
                    }
                }
            }

            RemoveEmptyBlocks(mainBlock);
        }