changeset 55602:99ed2dfcc0fa intrinsics-project

dont intrinsify Objects::hash
author vromero
date Tue, 02 Apr 2019 17:39:13 -0400
parents 01ead3d6c184
children 1bd391c995f4
files src/java.base/share/classes/java/lang/invoke/IntrinsicFactory.java src/java.base/share/classes/java/lang/invoke/ObjectsBootstraps.java src/java.base/share/classes/java/util/Objects.java src/jdk.compiler/share/classes/com/sun/tools/javac/intrinsics/HashProcessor.java src/jdk.compiler/share/classes/module-info.java test/langtools/tools/javac/intrinsics/CheckIndyGeneratedTest2.java
diffstat 6 files changed, 1 insertions(+), 372 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/invoke/IntrinsicFactory.java	Tue Apr 02 16:45:57 2019 -0400
+++ b/src/java.base/share/classes/java/lang/invoke/IntrinsicFactory.java	Tue Apr 02 17:39:13 2019 -0400
@@ -303,22 +303,4 @@
             throws NoSuchMethodException, IllegalAccessException, StringConcatException {
         return FormatterBootstraps.formatterBootstrap(lookup, name, methodType, format, true, true);
     }
-
-    /**
-     * objectsHashBootstrap bootstrap.
-     * @param lookup      MethodHandles lookup
-     * @param name        Name of method
-     * @param methodType  Method signature
-     * @throws NoSuchMethodException no such method
-     * @throws IllegalAccessException illegal access
-     * @throws StringConcatException string concat error
-     * @return Callsite for intrinsic method
-     */
-    public static CallSite objectsHashBootstrap(MethodHandles.Lookup lookup,
-                                                String name,
-                                                MethodType methodType)
-            throws NoSuchMethodException, IllegalAccessException {
-        return ObjectsBootstraps.hashBootstrap(lookup, name, methodType);
-    }
-
 }
--- a/src/java.base/share/classes/java/lang/invoke/ObjectsBootstraps.java	Tue Apr 02 16:45:57 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,155 +0,0 @@
-/*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package java.lang.invoke;
-
-import java.lang.invoke.ConstantCallSite;
-import java.lang.invoke.MethodHandles;
-import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodType;
-import java.util.HashMap;
-import java.util.Map;
-
-import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
-
-/**
- * Bootstrapping support for Objects intrinsics.
- */
-public final class ObjectsBootstraps {
-    /**
-     * Bootstrap for Objects intrinsics.
-     * @param lookup         MethodHandles lookup
-     * @param name           Name of method
-     * @param methodType     Method signature
-     * @return Callsite for intrinsic method
-     */
-    public static CallSite hashBootstrap(MethodHandles.Lookup lookup,
-                                         String name,
-                                         MethodType methodType) {
-        assert methodType.returnType() == int.class;
-        initialize();
-        return new ConstantCallSite(createHashMethodHandle(methodType.parameterArray()));
-    }
-
-    static Map<Class<?>, MethodHandle> HASH_METHODS;
-    static MethodHandle HASH_OBJECT;
-    static MethodHandle ALL_HASHES_MH;
-
-    static void initialize() {
-        if (HASH_METHODS == null) {
-            HASH_METHODS = new HashMap<>();
-            HASH_METHODS.put(boolean.class, findHashMethod(boolean.class));
-            HASH_METHODS.put(byte.class, findHashMethod(byte.class));
-            HASH_METHODS.put(char.class, findHashMethod(char.class));
-            HASH_METHODS.put(short.class, findHashMethod(short.class));
-            HASH_METHODS.put(int.class, findHashMethod(int.class));
-            HASH_METHODS.put(float.class, findHashMethod(float.class));
-            HASH_METHODS.put(double.class, findHashMethod(double.class));
-
-            HASH_OBJECT = findHashMethod(Object.class);
-            try {
-                ALL_HASHES_MH = IMPL_LOOKUP.findStatic(ObjectsBootstraps.class, "combineHashes", MethodType.methodType(int.class, int[].class));
-            } catch (NoSuchMethodException | IllegalAccessException ex) {
-
-            }
-        }
-    }
-
-    static MethodHandle createHashMethodHandle(Class<?>... argTypes) {
-        Class<?>[] intArgs = new Class<?>[argTypes.length];
-        for (int i = 0; i < argTypes.length; i++) {
-            intArgs[i] = int.class;
-        }
-        MethodType methodType = MethodType.methodType(int.class, intArgs);
-        MethodHandle mhHash = ALL_HASHES_MH.asType(methodType);
-        MethodHandle[] filters = new MethodHandle[argTypes.length];
-        for (int i = 0; i < argTypes.length; i++) {
-            filters[i] = getHashMethod(argTypes[i]);
-        }
-        mhHash = MethodHandles.filterArguments(mhHash, 0, filters);
-        return mhHash;
-    }
-
-    static int combineHashes(int... hashes) {
-        if (hashes == null) {
-            return 0;
-        }
-        int result = 1;
-        for (int hash : hashes) {
-            result = 31 * result + hash;
-        }
-        return result;
-    }
-
-    static MethodHandle getHashMethod(Class<?> type) {
-        MethodHandle hashMH = HASH_METHODS.get(type);
-        if (hashMH == null) {
-            hashMH = HASH_OBJECT;
-            hashMH = hashMH.asType(MethodType.methodType(int.class, type));
-        }
-        return hashMH;
-    }
-
-    static MethodHandle findHashMethod(Class<?> cls) {
-        try {
-            MethodType mt = MethodType.methodType(int.class, cls);
-            return IMPL_LOOKUP.findStatic(ObjectsBootstraps.class, "hash", mt);
-        } catch (NoSuchMethodException | IllegalAccessException ex) {
-            return null;
-        }
-     }
-
-    static int hash(boolean value) {
-        return Boolean.hashCode(value);
-    }
-
-    static int hash(char value) {
-        return value;
-    }
-
-    static int hash(byte value) {
-        return value;
-    }
-
-    static int hash(short value) {
-        return value;
-    }
-
-    static int hash(int value) {
-        return value;
-    }
-
-    static int hash(float value) {
-        return Float.hashCode(value);
-    }
-
-    static int hash(double value) {
-        return Double.hashCode(value);
-    }
-
-    static int hash(Object value) {
-        return (value == null ? 0 : value.hashCode());
-    }
-}
--- a/src/java.base/share/classes/java/util/Objects.java	Tue Apr 02 16:45:57 2019 -0400
+++ b/src/java.base/share/classes/java/util/Objects.java	Tue Apr 02 17:39:13 2019 -0400
@@ -28,7 +28,6 @@
 import jdk.internal.util.Preconditions;
 import jdk.internal.vm.annotation.ForceInline;
 
-import java.lang.compiler.IntrinsicCandidate;
 import java.util.function.Supplier;
 
 /**
@@ -138,21 +137,11 @@
     * value does not equal the hash code of that object reference.</b> This
     * value can be computed by calling {@link #hashCode(Object)}.
     *
-    * @implNote
-    * An invocation of this method may be intrinsified see {@link java.lang.compiler.IntrinsicCandidate}.
-    * If all the arguments are constant expressions, then intrinsification generates a hash code directly
-    * from the arguments, and uses it as the run-time value of the method invocation. If some arguments
-    * are not constant expressions, then intrinsification replaces the method invocation with code that
-    * will generate a hash code for each argument in a type-specific way at run time, then combine the
-    * results. Intrinsification is advantageous because it avoids both the boxing of primitive arguments
-    * into objects, and the boxing of all arguments into a varargs array.
-    *
     * @param values the values to be hashed
     * @return a hash value of the sequence of input values
     * @see Arrays#hashCode(Object[])
     * @see List#hashCode
     */
-    @IntrinsicCandidate
     public static int hash(Object... values) {
         return Arrays.hashCode(values);
     }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/intrinsics/HashProcessor.java	Tue Apr 02 16:45:57 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,91 +0,0 @@
-/*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package com.sun.tools.javac.intrinsics;
-
-import java.lang.constant.ClassDesc;
-import java.lang.constant.ConstantDesc;
-import java.lang.constant.ConstantDescs;
-import java.lang.constant.DynamicCallSiteDesc;
-import java.lang.constant.MethodTypeDesc;
-import java.util.Arrays;
-import java.util.Objects;
-
-/**
- *  <p><b>This is NOT part of any supported API.
- *  If you write code that depends on this, you do so at your own risk.
- *  This code and its internal interfaces are subject to change or
- *  deletion without notice.</b>
- */
-public class HashProcessor implements IntrinsicProcessor {
-    @Override
-    public void register(Intrinsics intrinsics) {
-        this.intrinsics = intrinsics;
-        intrinsics.register(this,
-                Objects.class, "hash", int.class, Object[].class);
-    }
-
-    Intrinsics intrinsics;
-
-    private static final ClassDesc CD_Objects = ClassDesc.of("java.util.Objects");
-    private static final ClassDesc CD_IntrinsicFactory = ClassDesc.of("java.lang.invoke.IntrinsicFactory");
-
-    @Override
-    public Result tryIntrinsify(ClassDesc ownerDesc,
-                                String methodName,
-                                MethodTypeDesc methodType,
-                                boolean isStatic,
-                                ClassDesc[] argClassDescs,
-                                ConstantDesc[] constantArgs) {
-        if (CD_Objects.equals(ownerDesc)) {
-            switch (methodName) {
-                case "hash":
-                    if (intrinsics.isAllConstants(constantArgs, false)) {
-                        Object[] constants =
-                                intrinsics.getConstants(argClassDescs, constantArgs, false);
-                        return new Result.Ldc(Arrays.hashCode(constants));
-                    } else {
-                        if (intrinsics.isArrayVarArg(argClassDescs, 0)) {
-                            return new Result.None();
-                        }
-                        return new Result.Indy(
-                                DynamicCallSiteDesc.of(
-                                        ConstantDescs.ofCallsiteBootstrap(
-                                                CD_IntrinsicFactory,
-                                                "objectsHashBootstrap",
-                                                ConstantDescs.CD_CallSite
-                                        ),
-                                        methodName,
-                                        methodType,
-                                        new ConstantDesc[0]),
-                                argClassDescs.length
-                        );
-                    }
-             }
-        }
-        return new Result.None();
-    }
-}
-
--- a/src/jdk.compiler/share/classes/module-info.java	Tue Apr 02 16:45:57 2019 -0400
+++ b/src/jdk.compiler/share/classes/module-info.java	Tue Apr 02 17:39:13 2019 -0400
@@ -138,6 +138,5 @@
     uses com.sun.tools.javac.intrinsics.IntrinsicProcessor;
 
     provides com.sun.tools.javac.intrinsics.IntrinsicProcessor with
-        com.sun.tools.javac.intrinsics.FormatterProcessor,
-        com.sun.tools.javac.intrinsics.HashProcessor;
+        com.sun.tools.javac.intrinsics.FormatterProcessor;
 }
--- a/test/langtools/tools/javac/intrinsics/CheckIndyGeneratedTest2.java	Tue Apr 02 16:45:57 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,95 +0,0 @@
-/*
- * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-/*
- * @test
- * @summary intrinsics: check that javac is generating the expected bytecode
- * @modules jdk.jdeps/com.sun.tools.classfile
- *          jdk.compiler/com.sun.tools.javac.util
- * @run main CheckIndyGeneratedTest2
- */
-
-import java.io.*;
-import java.util.*;
-
-import com.sun.tools.javac.util.Assert;
-import com.sun.tools.classfile.*;
-import com.sun.tools.classfile.BootstrapMethods_attribute.*;
-import com.sun.tools.classfile.ConstantPool.*;
-
-public class CheckIndyGeneratedTest2 {
-    static class CheckIndyGeneratedTest2sub {
-        int test() {
-            return Objects.hash(1, 2);
-        }
-    }
-
-    static final String SUBTEST_NAME = CheckIndyGeneratedTest2sub.class.getName() + ".class";
-    static final String TEST_METHOD_NAME = "test";
-
-    public static void main(String... args) throws Exception {
-        new CheckIndyGeneratedTest2().run();
-    }
-
-    public void run() throws Exception {
-        String workDir = System.getProperty("test.classes");
-        File compiledTest = new File(workDir, SUBTEST_NAME);
-        verifysipushGeneated(compiledTest);
-    }
-
-    void verifysipushGeneated(File f) {
-        try {
-            int count = 0;
-            ClassFile cf = ClassFile.read(f);
-            Method testMethod = null;
-            for (Method m : cf.methods) {
-                if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) {
-                    testMethod = m;
-                    break;
-                }
-            }
-            if (testMethod == null) {
-                throw new Error("Test method not found");
-            }
-            Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code);
-            if (testMethod == null) {
-                throw new Error("Code attribute for test() method not found");
-            }
-            boolean sipushFound = false;
-            for (Instruction inst : ea.getInstructions()) {
-                if (inst.getMnemonic().equals("invokestatic")) {
-                    throw new AssertionError("unexpected invoke static instruction");
-                }
-                if (inst.getMnemonic().equals("sipush")) {
-                    sipushFound = true;
-                }
-            }
-            if (!sipushFound) {
-                throw new AssertionError("sipush instruction not found");
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-            throw new Error(e.getMessage());
-        }
-    }
-}