001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.kotlin.codegen.optimization;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.kotlin.codegen.inline.InlineCodegenUtil;
022    import org.jetbrains.kotlin.codegen.optimization.boxing.RedundantBoxingMethodTransformer;
023    import org.jetbrains.kotlin.codegen.optimization.boxing.RedundantNullCheckMethodTransformer;
024    import org.jetbrains.kotlin.codegen.optimization.common.UtilKt;
025    import org.jetbrains.kotlin.codegen.optimization.transformer.MethodTransformer;
026    import org.jetbrains.org.objectweb.asm.MethodVisitor;
027    import org.jetbrains.org.objectweb.asm.Opcodes;
028    import org.jetbrains.org.objectweb.asm.tree.LocalVariableNode;
029    import org.jetbrains.org.objectweb.asm.tree.MethodNode;
030    import org.jetbrains.org.objectweb.asm.util.Textifier;
031    import org.jetbrains.org.objectweb.asm.util.TraceMethodVisitor;
032    
033    import java.util.ArrayList;
034    import java.util.List;
035    
036    public class OptimizationMethodVisitor extends MethodVisitor {
037        private static final int MEMORY_LIMIT_BY_METHOD_MB = 50;
038    
039        private static final MethodTransformer MANDATORY_METHOD_TRANSFORMER = new MandatoryMethodTransformer();
040    
041        private static final MethodTransformer[] OPTIMIZATION_TRANSFORMERS = new MethodTransformer[] {
042                new RedundantNullCheckMethodTransformer(),
043                new RedundantBoxingMethodTransformer(),
044                new DeadCodeEliminationMethodTransformer(),
045                new RedundantGotoMethodTransformer()
046        };
047    
048        private final MethodNode methodNode;
049        private final MethodVisitor delegate;
050        private final boolean disableOptimization;
051    
052        public OptimizationMethodVisitor(
053                @NotNull MethodVisitor delegate,
054                boolean disableOptimization,
055                int access,
056                @NotNull String name,
057                @NotNull String desc,
058                @Nullable String signature,
059                @Nullable String[] exceptions
060        ) {
061            super(Opcodes.ASM5);
062            this.delegate = delegate;
063            this.methodNode = new MethodNode(access, name, desc, signature, exceptions);
064            this.methodNode.localVariables = new ArrayList<LocalVariableNode>(5);
065            this.mv = InlineCodegenUtil.wrapWithMaxLocalCalc(methodNode);
066            this.disableOptimization = disableOptimization;
067        }
068    
069        @Override
070        public void visitEnd() {
071            // force mv to calculate maxStack/maxLocals in case it didn't yet done
072            if (methodNode.maxLocals <= 0 || methodNode.maxStack <= 0) {
073                mv.visitMaxs(-1, -1);
074            }
075    
076            super.visitEnd();
077    
078            if (shouldBeTransformed(methodNode)) {
079                MANDATORY_METHOD_TRANSFORMER.transform("fake", methodNode);
080                if (canBeOptimized(methodNode) && !disableOptimization) {
081                    for (MethodTransformer transformer : OPTIMIZATION_TRANSFORMERS) {
082                        transformer.transform("fake", methodNode);
083                    }
084                }
085                UtilKt.prepareForEmitting(methodNode);
086            }
087    
088            methodNode.accept(new EndIgnoringMethodVisitorDecorator(Opcodes.ASM5, delegate));
089    
090    
091            // In case of empty instructions list MethodNode.accept doesn't call visitLocalVariables of delegate
092            // So we just do it here
093            if (methodNode.instructions.size() == 0) {
094                List<LocalVariableNode> localVariables = methodNode.localVariables;
095                // visits local variables
096                int n = localVariables == null ? 0 : localVariables.size();
097                for (int i = 0; i < n; ++i) {
098                    localVariables.get(i).accept(delegate);
099                }
100            }
101    
102            delegate.visitEnd();
103        }
104    
105        /**
106         * You can use it when you need to ignore visit end
107         */
108        private static class EndIgnoringMethodVisitorDecorator extends MethodVisitor {
109            public EndIgnoringMethodVisitorDecorator(int api, @NotNull MethodVisitor mv) {
110                super(api, mv);
111            }
112    
113            @Override
114            public void visitEnd() {
115    
116            }
117        }
118    
119        @Nullable
120        public TraceMethodVisitor getTraceMethodVisitorIfPossible() {
121            TraceMethodVisitor traceMethodVisitor = new TraceMethodVisitor(new Textifier());
122            try {
123                methodNode.accept(traceMethodVisitor);
124            }
125            catch (Throwable e) {
126                return null;
127            }
128    
129            return traceMethodVisitor;
130        }
131    
132        private static boolean shouldBeTransformed(@NotNull MethodNode node) {
133            return node.instructions.size() > 0;
134        }
135    
136        private static boolean canBeOptimized(@NotNull MethodNode node) {
137            int totalFramesSizeMb = node.instructions.size() * (node.maxLocals + node.maxStack) / (1024 * 1024);
138            return totalFramesSizeMb < MEMORY_LIMIT_BY_METHOD_MB;
139        }
140    }