Path: blob/master/test/hotspot/jtreg/compiler/osr/TestOSRWithNonEmptyStack.java
41149 views
/*1* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 805134426* @summary Force OSR compilation with non-empty stack at the OSR entry point.27* @modules java.base/jdk.internal.org.objectweb.asm28* @run main/othervm -XX:CompileCommand=compileonly,TestCase::test29* compiler.osr.TestOSRWithNonEmptyStack30*/3132package compiler.osr;3334import jdk.internal.org.objectweb.asm.ClassWriter;35import jdk.internal.org.objectweb.asm.Label;36import jdk.internal.org.objectweb.asm.MethodVisitor;3738import java.lang.reflect.Constructor;39import java.lang.reflect.Method;4041import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC;42import static jdk.internal.org.objectweb.asm.Opcodes.ALOAD;43import static jdk.internal.org.objectweb.asm.Opcodes.DUP;44import static jdk.internal.org.objectweb.asm.Opcodes.IADD;45import static jdk.internal.org.objectweb.asm.Opcodes.ICONST_0;46import static jdk.internal.org.objectweb.asm.Opcodes.ICONST_1;47import static jdk.internal.org.objectweb.asm.Opcodes.IF_ICMPLT;48import static jdk.internal.org.objectweb.asm.Opcodes.ILOAD;49import static jdk.internal.org.objectweb.asm.Opcodes.INVOKESPECIAL;50import static jdk.internal.org.objectweb.asm.Opcodes.ISTORE;51import static jdk.internal.org.objectweb.asm.Opcodes.POP;52import static jdk.internal.org.objectweb.asm.Opcodes.RETURN;5354public class TestOSRWithNonEmptyStack extends ClassLoader {55private static final int CLASS_FILE_VERSION = 52;56private static final String CLASS_NAME = "TestCase";57private static final String METHOD_NAME = "test";58private static final int ITERATIONS = 1_000_000;5960private static byte[] generateTestClass() {61ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);6263cw.visit(TestOSRWithNonEmptyStack.CLASS_FILE_VERSION, ACC_PUBLIC,64TestOSRWithNonEmptyStack.CLASS_NAME, null, "java/lang/Object",65null);6667TestOSRWithNonEmptyStack.generateConstructor(cw);68TestOSRWithNonEmptyStack.generateTestMethod(cw);6970cw.visitEnd();71return cw.toByteArray();72}7374private static void generateConstructor(ClassWriter classWriter) {75MethodVisitor mv = classWriter.visitMethod(ACC_PUBLIC, "<init>", "()V",76null, null);7778mv.visitCode();7980mv.visitVarInsn(ALOAD, 0);81mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V",82false);83mv.visitInsn(RETURN);8485mv.visitMaxs(0, 0);86mv.visitEnd();87}8889private static void generateTestMethod(ClassWriter classWriter) {90MethodVisitor mv = classWriter.visitMethod(ACC_PUBLIC,91TestOSRWithNonEmptyStack.METHOD_NAME, "()V", null, null);92Label osrEntryPoint = new Label();9394mv.visitCode();95// Push 'this' into stack before OSR entry point to bail out compilation96mv.visitVarInsn(ALOAD, 0);97// Setup loop counter98mv.visitInsn(ICONST_0);99mv.visitVarInsn(ISTORE, 1);100// Begin loop101mv.visitLabel(osrEntryPoint);102// Increment loop counter103mv.visitVarInsn(ILOAD, 1);104mv.visitInsn(ICONST_1);105mv.visitInsn(IADD);106// Duplicate it for loop condition check107mv.visitInsn(DUP);108mv.visitVarInsn(ISTORE, 1);109// Check loop condition110mv.visitLdcInsn(TestOSRWithNonEmptyStack.ITERATIONS);111mv.visitJumpInsn(IF_ICMPLT, osrEntryPoint);112// Pop 'this'.113mv.visitInsn(POP);114mv.visitInsn(RETURN);115116mv.visitMaxs(0, 0);117mv.visitEnd();118}119120private void run() {121byte[] bytecode = TestOSRWithNonEmptyStack.generateTestClass();122123try {124Class klass = defineClass(TestOSRWithNonEmptyStack.CLASS_NAME,125bytecode, 0, bytecode.length);126127Constructor ctor = klass.getConstructor();128Method method = klass.getDeclaredMethod(129TestOSRWithNonEmptyStack.METHOD_NAME);130131Object testCase = ctor.newInstance();132method.invoke(testCase);133} catch (Exception e) {134throw new RuntimeException(135"Test bug: generated class should be valid.", e);136}137}138139public static void main(String args[]) {140new TestOSRWithNonEmptyStack().run();141}142}143144145