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