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.boxing;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.kotlin.codegen.optimization.transformer.MethodTransformer;
021    import org.jetbrains.org.objectweb.asm.Opcodes;
022    import org.jetbrains.org.objectweb.asm.tree.*;
023    import org.jetbrains.org.objectweb.asm.tree.analysis.BasicValue;
024    import org.jetbrains.org.objectweb.asm.tree.analysis.Frame;
025    
026    import java.util.ArrayList;
027    import java.util.List;
028    
029    public class RedundantNullCheckMethodTransformer extends MethodTransformer {
030    
031        @Override
032        public void transform(@NotNull String internalClassName, @NotNull MethodNode methodNode) {
033            while (removeRedundantNullCheckPass(internalClassName, methodNode)) {
034                //do nothing
035            }
036        }
037    
038        private static boolean removeRedundantNullCheckPass(@NotNull String internalClassName, @NotNull MethodNode methodNode) {
039            InsnList insnList = methodNode.instructions;
040            Frame<BasicValue>[] frames = analyze(
041                    internalClassName, methodNode,
042                    new NullabilityInterpreter(insnList)
043            );
044    
045            List<AbstractInsnNode> insnsToOptimize = new ArrayList<AbstractInsnNode>();
046    
047            for (int i = 0; i < insnList.size(); i++) {
048                Frame<BasicValue> frame = frames[i];
049                AbstractInsnNode insn = insnList.get(i);
050    
051                if ((insn.getOpcode() == Opcodes.IFNULL || insn.getOpcode() == Opcodes.IFNONNULL) &&
052                    frame != null && frame.getStack(frame.getStackSize() - 1) instanceof NotNullBasicValue) {
053                    insnsToOptimize.add(insn);
054                }
055            }
056    
057            for (AbstractInsnNode insn : insnsToOptimize) {
058                if (insn.getPrevious() != null && insn.getPrevious().getOpcode() == Opcodes.DUP) {
059                    insnList.remove(insn.getPrevious());
060                }
061                else {
062                    insnList.insertBefore(insn, new InsnNode(Opcodes.POP));
063                }
064    
065                assert insn.getOpcode() == Opcodes.IFNULL
066                       || insn.getOpcode() == Opcodes.IFNONNULL : "only IFNULL/IFNONNULL are supported";
067    
068                if (insn.getOpcode() == Opcodes.IFNULL) {
069                    insnList.remove(insn);
070                }
071                else {
072                    insnList.set(
073                            insn,
074                            new JumpInsnNode(Opcodes.GOTO, ((JumpInsnNode) insn).label)
075                    );
076                }
077            }
078    
079            return insnsToOptimize.size() > 0;
080        }
081    }