Example #1
0
        /// <summary>
        /// Visits the specified method.
        /// </summary>
        /// <param name="methodDefinition">The method to visit.</param>
        public override void Visit(MethodDefinition methodDefinition)
        {
            if (this.visitedMethods.Contains(methodDefinition))
            {
                return;
            }

            this.visitedMethods.Add(methodDefinition);

            var body = methodDefinition.Body;
            if (body == null || body.Instructions.Count == 0)
            {
                return;
            }

            var worker = new OptimizationWorker(this, body);
            var instruction = body.Instructions[0];
            do
            {
                var instructionBefore = instruction.Previous;

                if (this.ApplyOptimizations(instruction, worker))
                {
                    instruction = instructionBefore;
                    if (instruction == null)
                    {
                        instruction = body.Instructions[0];
                        continue;
                    }
                }

                instruction = instruction.Next;
            }
            while (instruction != null);
        }
Example #2
0
 /// <summary>
 /// Performs the optimization starting at the specified instruction.
 /// </summary>
 /// <param name="instruction">The instruction to target.</param>
 /// <param name="worker">The worker for optimization actions.</param>
 public abstract void OptimizeInstruction(Instruction instruction, OptimizationWorker worker);
Example #3
0
        /// <summary>
        /// Applies all optimizations to the specified instruction.
        /// </summary>
        /// <param name="instruction">The instruction to optimize.</param>
        /// <param name="worker">The worker to apply optimizations on.</param>
        /// <returns><see langword="true"/> if an optimization has been performed; <see langword="false" /> otherwise.</returns>
        private bool ApplyOptimizations(Instruction instruction, OptimizationWorker worker)
        {
            var optimizationCount = this.optimizations.Length;
            for (var i = 0; i < optimizationCount; i++)
            {
                var optimization = this.optimizations[i];
                optimization.OptimizeInstruction(instruction, worker);
                if (worker.WasOptimized)
                {
                    worker.WasOptimized = false;
                    return true;
                }
            }

            return false;
        }