< prev index next >

test/hotspot/jtreg/compiler/unsafe/UnsafeGetConstantField.java

Print this page




  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @summary tests on constant folding of unsafe get operations
  27  * @library /test/lib
  28  *
  29  * @requires vm.flavor == "server" & !vm.emulatedClient
  30  *
  31  * @modules java.base/jdk.internal.org.objectweb.asm
  32  *          java.base/jdk.internal.vm.annotation
  33  *          java.base/jdk.internal.misc
  34  *



  35  * @run main/bootclasspath/othervm -XX:+UnlockDiagnosticVMOptions
  36  *                                 -Xbatch -XX:-TieredCompilation
  37  *                                 -XX:+FoldStableValues
  38  *                                 -XX:CompileCommand=dontinline,compiler.unsafe.UnsafeGetConstantField::checkGetAddress
  39  *                                 -XX:CompileCommand=dontinline,*::test*
  40  *                                 -XX:+UseUnalignedAccesses
  41  *                                 --add-reads=java.base=ALL-UNNAMED
  42  *                                 compiler.unsafe.UnsafeGetConstantField
  43  *
  44  * @run main/bootclasspath/othervm -XX:+UnlockDiagnosticVMOptions
  45  *                                 -Xbatch -XX:-TieredCompilation
  46  *                                 -XX:+FoldStableValues
  47  *                                 -XX:CompileCommand=dontinline,compiler.unsafe.UnsafeGetConstantField::checkGetAddress
  48  *                                 -XX:CompileCommand=dontinline,*::test*
  49  *                                 -XX:CompileCommand=inline,*Unsafe::get*
  50  *                                 -XX:-UseUnalignedAccesses
  51  *                                 --add-reads=java.base=ALL-UNNAMED
  52  *                                 compiler.unsafe.UnsafeGetConstantField
  53  */
  54 
  55 package compiler.unsafe;
  56 
  57 import jdk.internal.misc.Unsafe;
  58 import jdk.internal.org.objectweb.asm.ClassWriter;
  59 import jdk.internal.org.objectweb.asm.FieldVisitor;
  60 import jdk.internal.org.objectweb.asm.MethodVisitor;
  61 import jdk.internal.org.objectweb.asm.Opcodes;
  62 import jdk.internal.org.objectweb.asm.Type;
  63 import jdk.internal.vm.annotation.Stable;
  64 import jdk.test.lib.Asserts;
  65 import jdk.test.lib.Platform;
  66 



  67 import java.io.IOException;
  68 import java.nio.file.Files;
  69 import java.nio.file.Path;
  70 import java.nio.file.Paths;
  71 
  72 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_FINAL;
  73 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC;
  74 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_STATIC;
  75 import static jdk.internal.org.objectweb.asm.Opcodes.ACONST_NULL;
  76 import static jdk.internal.org.objectweb.asm.Opcodes.ALOAD;
  77 import static jdk.internal.org.objectweb.asm.Opcodes.ARETURN;
  78 import static jdk.internal.org.objectweb.asm.Opcodes.DUP;
  79 import static jdk.internal.org.objectweb.asm.Opcodes.GETFIELD;
  80 import static jdk.internal.org.objectweb.asm.Opcodes.GETSTATIC;
  81 import static jdk.internal.org.objectweb.asm.Opcodes.INVOKESPECIAL;
  82 import static jdk.internal.org.objectweb.asm.Opcodes.INVOKESTATIC;
  83 import static jdk.internal.org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
  84 import static jdk.internal.org.objectweb.asm.Opcodes.NEW;
  85 import static jdk.internal.org.objectweb.asm.Opcodes.PUTFIELD;
  86 import static jdk.internal.org.objectweb.asm.Opcodes.PUTSTATIC;


 376                 mv.visitFieldInsn(PUTSTATIC, className, "FIELD_OFFSET", "J");
 377 
 378                 // Compute base offset for static field
 379                 if (isStatic()) {
 380                     getUnsafe(mv);
 381                     getField(mv);
 382                     mv.visitMethodInsn(INVOKEVIRTUAL, UNSAFE_NAME, "staticFieldBase", "(Ljava/lang/reflect/Field;)Ljava/lang/Object;", false);
 383                     mv.visitFieldInsn(PUTSTATIC, className, "STATIC_BASE", "Ljava/lang/Object;");
 384                     initField(mv);
 385                 }
 386 
 387                 mv.visitInsn(RETURN);
 388                 mv.visitMaxs(0, 0);
 389                 mv.visitEnd();
 390             }
 391 
 392             return cw.toByteArray();
 393         }
 394 
 395         Test generate() {
 396             Class<?> c = U.defineClass(className, classFile, 0, classFile.length, THIS_CLASS.getClassLoader(), null);
 397             try {


 398                 return (Test) c.newInstance();
 399             } catch(Exception e) {
 400                 throw new Error(e);
 401             }
 402         }
 403 
 404         boolean isStatic() {
 405             return (flags & ACC_STATIC) > 0;
 406         }
 407         boolean isFinal() {
 408             return (flags & ACC_FINAL) > 0;
 409         }
 410         void getUnsafe(MethodVisitor mv) {
 411             mv.visitFieldInsn(GETSTATIC, className, "U", UNSAFE_DESC);
 412         }
 413         void getField(MethodVisitor mv) {
 414             mv.visitLdcInsn(Type.getType(classDesc));
 415             mv.visitLdcInsn(FIELD_NAME);
 416             mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredField", "(Ljava/lang/String;)Ljava/lang/reflect/Field;", false);
 417         }




  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @summary tests on constant folding of unsafe get operations
  27  * @library /test/lib
  28  *
  29  * @requires vm.flavor == "server" & !vm.emulatedClient
  30  *
  31  * @modules java.base/jdk.internal.org.objectweb.asm
  32  *          java.base/jdk.internal.vm.annotation
  33  *          java.base/jdk.internal.misc
  34  *
  35  * @library ../jsr292/patches
  36  * @build java.base/java.lang.invoke.MethodHandleHelper
  37  *
  38  * @run main/bootclasspath/othervm -XX:+UnlockDiagnosticVMOptions
  39  *                                 -Xbatch -XX:-TieredCompilation
  40  *                                 -XX:+FoldStableValues
  41  *                                 -XX:CompileCommand=dontinline,compiler.unsafe.UnsafeGetConstantField::checkGetAddress
  42  *                                 -XX:CompileCommand=dontinline,*::test*
  43  *                                 -XX:+UseUnalignedAccesses
  44  *                                 --add-reads=java.base=ALL-UNNAMED
  45  *                                 compiler.unsafe.UnsafeGetConstantField
  46  *
  47  * @run main/bootclasspath/othervm -XX:+UnlockDiagnosticVMOptions
  48  *                                 -Xbatch -XX:-TieredCompilation
  49  *                                 -XX:+FoldStableValues
  50  *                                 -XX:CompileCommand=dontinline,compiler.unsafe.UnsafeGetConstantField::checkGetAddress
  51  *                                 -XX:CompileCommand=dontinline,*::test*
  52  *                                 -XX:CompileCommand=inline,*Unsafe::get*
  53  *                                 -XX:-UseUnalignedAccesses
  54  *                                 --add-reads=java.base=ALL-UNNAMED
  55  *                                 compiler.unsafe.UnsafeGetConstantField
  56  */
  57 
  58 package compiler.unsafe;
  59 
  60 import jdk.internal.misc.Unsafe;
  61 import jdk.internal.org.objectweb.asm.ClassWriter;
  62 import jdk.internal.org.objectweb.asm.FieldVisitor;
  63 import jdk.internal.org.objectweb.asm.MethodVisitor;
  64 import jdk.internal.org.objectweb.asm.Opcodes;
  65 import jdk.internal.org.objectweb.asm.Type;
  66 import jdk.internal.vm.annotation.Stable;
  67 import jdk.test.lib.Asserts;
  68 import jdk.test.lib.Platform;
  69 
  70 import java.lang.invoke.MethodHandleHelper;
  71 import java.lang.invoke.MethodHandles;
  72 import java.lang.invoke.MethodHandles.Lookup;
  73 import java.io.IOException;
  74 import java.nio.file.Files;
  75 import java.nio.file.Path;
  76 import java.nio.file.Paths;
  77 
  78 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_FINAL;
  79 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC;
  80 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_STATIC;
  81 import static jdk.internal.org.objectweb.asm.Opcodes.ACONST_NULL;
  82 import static jdk.internal.org.objectweb.asm.Opcodes.ALOAD;
  83 import static jdk.internal.org.objectweb.asm.Opcodes.ARETURN;
  84 import static jdk.internal.org.objectweb.asm.Opcodes.DUP;
  85 import static jdk.internal.org.objectweb.asm.Opcodes.GETFIELD;
  86 import static jdk.internal.org.objectweb.asm.Opcodes.GETSTATIC;
  87 import static jdk.internal.org.objectweb.asm.Opcodes.INVOKESPECIAL;
  88 import static jdk.internal.org.objectweb.asm.Opcodes.INVOKESTATIC;
  89 import static jdk.internal.org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
  90 import static jdk.internal.org.objectweb.asm.Opcodes.NEW;
  91 import static jdk.internal.org.objectweb.asm.Opcodes.PUTFIELD;
  92 import static jdk.internal.org.objectweb.asm.Opcodes.PUTSTATIC;


 382                 mv.visitFieldInsn(PUTSTATIC, className, "FIELD_OFFSET", "J");
 383 
 384                 // Compute base offset for static field
 385                 if (isStatic()) {
 386                     getUnsafe(mv);
 387                     getField(mv);
 388                     mv.visitMethodInsn(INVOKEVIRTUAL, UNSAFE_NAME, "staticFieldBase", "(Ljava/lang/reflect/Field;)Ljava/lang/Object;", false);
 389                     mv.visitFieldInsn(PUTSTATIC, className, "STATIC_BASE", "Ljava/lang/Object;");
 390                     initField(mv);
 391                 }
 392 
 393                 mv.visitInsn(RETURN);
 394                 mv.visitMaxs(0, 0);
 395                 mv.visitEnd();
 396             }
 397 
 398             return cw.toByteArray();
 399         }
 400 
 401         Test generate() {

 402             try {
 403                 Lookup lookup = MethodHandleHelper.IMPL_LOOKUP.in(MethodHandles.class);
 404                 Class<?> c = lookup.defineClass(classFile);
 405                 return (Test) c.newInstance();
 406             } catch(Exception e) {
 407                 throw new Error(e);
 408             }
 409         }
 410 
 411         boolean isStatic() {
 412             return (flags & ACC_STATIC) > 0;
 413         }
 414         boolean isFinal() {
 415             return (flags & ACC_FINAL) > 0;
 416         }
 417         void getUnsafe(MethodVisitor mv) {
 418             mv.visitFieldInsn(GETSTATIC, className, "U", UNSAFE_DESC);
 419         }
 420         void getField(MethodVisitor mv) {
 421             mv.visitLdcInsn(Type.getType(classDesc));
 422             mv.visitLdcInsn(FIELD_NAME);
 423             mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredField", "(Ljava/lang/String;)Ljava/lang/reflect/Field;", false);
 424         }


< prev index next >