# HG changeset patch # User Tom Rodriguez # Date 1389723268 28800 # Node ID 5348da19751d88ea00290940a2f1abe3a0f9eb54 # Parent 110795e38ac6fd6e3dd6df77037c2c8e96ab7082 Add locally specified guards to substitutions diff -r 110795e38ac6 -r 5348da19751d graal/com.oracle.graal.api.replacements/src/com/oracle/graal/api/replacements/ClassSubstitution.java --- a/graal/com.oracle.graal.api.replacements/src/com/oracle/graal/api/replacements/ClassSubstitution.java Tue Jan 14 16:26:40 2014 +0100 +++ b/graal/com.oracle.graal.api.replacements/src/com/oracle/graal/api/replacements/ClassSubstitution.java Tue Jan 14 10:14:28 2014 -0800 @@ -56,4 +56,11 @@ * Substitutions for such classes are omitted if the original classes cannot be found. */ boolean optional() default false; + + /** + * Determines if the substitutions in a class are globally enabled. Individual + * MethodSubstitutions can also have guards and those override this guard. + */ + + Class defaultGuard() default SubstitutionGuard.class; } diff -r 110795e38ac6 -r 5348da19751d graal/com.oracle.graal.api.replacements/src/com/oracle/graal/api/replacements/MethodSubstitution.java --- a/graal/com.oracle.graal.api.replacements/src/com/oracle/graal/api/replacements/MethodSubstitution.java Tue Jan 14 16:26:40 2014 +0100 +++ b/graal/com.oracle.graal.api.replacements/src/com/oracle/graal/api/replacements/MethodSubstitution.java Tue Jan 14 10:14:28 2014 -0800 @@ -70,4 +70,10 @@ * omitted if the original method cannot be found. */ boolean optional() default false; + + /** + * Determines if the substitution is globally enabled. + */ + + Class guard() default SubstitutionGuard.class; } diff -r 110795e38ac6 -r 5348da19751d graal/com.oracle.graal.api.replacements/src/com/oracle/graal/api/replacements/SubstitutionGuard.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.api.replacements/src/com/oracle/graal/api/replacements/SubstitutionGuard.java Tue Jan 14 10:14:28 2014 -0800 @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2014, 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. + */ + +package com.oracle.graal.api.replacements; + +/** + * Guards the installation of substitutions for {@link ClassSubstitution} and + * {@link MethodSubstitution}. + */ +public interface SubstitutionGuard { + + /** + * Return true if the substitution should be enabled. + */ + boolean execute(); +} diff -r 110795e38ac6 -r 5348da19751d graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotReplacementsImpl.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotReplacementsImpl.java Tue Jan 14 16:26:40 2014 +0100 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotReplacementsImpl.java Tue Jan 14 10:14:28 2014 -0800 @@ -59,14 +59,6 @@ return null; } } - } else if (substituteClass == AESCryptSubstitutions.class || substituteClass == CipherBlockChainingSubstitutions.class) { - if (!config.useAESIntrinsics) { - return null; - } - assert config.aescryptEncryptBlockStub != 0L; - assert config.aescryptDecryptBlockStub != 0L; - assert config.cipherBlockChainingEncryptAESCryptStub != 0L; - assert config.cipherBlockChainingDecryptAESCryptStub != 0L; } else if (substituteClass == CRC32Substitutions.class) { if (!config.useCRC32Intrinsics) { return null; diff -r 110795e38ac6 -r 5348da19751d graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/AESCryptSubstitutions.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/AESCryptSubstitutions.java Tue Jan 14 16:26:40 2014 +0100 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/AESCryptSubstitutions.java Tue Jan 14 10:14:28 2014 -0800 @@ -23,6 +23,7 @@ package com.oracle.graal.hotspot.replacements; import static com.oracle.graal.hotspot.replacements.HotSpotReplacementsUtil.*; + import sun.misc.*; import com.oracle.graal.api.meta.*; @@ -30,6 +31,7 @@ import com.oracle.graal.graph.*; import com.oracle.graal.graph.Node.ConstantNodeParameter; import com.oracle.graal.graph.Node.NodeIntrinsic; +import com.oracle.graal.hotspot.*; import com.oracle.graal.hotspot.meta.*; import com.oracle.graal.hotspot.nodes.*; import com.oracle.graal.nodes.extended.*; @@ -38,9 +40,22 @@ /** * Substitutions for {@code com.sun.crypto.provider.AESCrypt} methods. */ -@ClassSubstitution(className = "com.sun.crypto.provider.AESCrypt", optional = true) +@ClassSubstitution(className = "com.sun.crypto.provider.AESCrypt", optional = true, defaultGuard = AESCryptSubstitutions.Guard.class) public class AESCryptSubstitutions { + public static class Guard implements SubstitutionGuard { + public boolean execute() { + HotSpotVMConfig config = HotSpotGraalRuntime.runtime().getConfig(); + if (config.useAESIntrinsics) { + assert config.aescryptEncryptBlockStub != 0L; + assert config.aescryptDecryptBlockStub != 0L; + assert config.cipherBlockChainingEncryptAESCryptStub != 0L; + assert config.cipherBlockChainingDecryptAESCryptStub != 0L; + } + return config.useAESIntrinsics; + } + } + static final long kOffset; static final LocationIdentity kLocationIdentity; static final Class AESCryptClass; diff -r 110795e38ac6 -r 5348da19751d graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/CRC32Substitutions.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/CRC32Substitutions.java Tue Jan 14 16:26:40 2014 +0100 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/CRC32Substitutions.java Tue Jan 14 10:14:28 2014 -0800 @@ -38,9 +38,15 @@ /** * Substitutions for {@link CRC32}. */ -@ClassSubstitution(value = CRC32.class) +@ClassSubstitution(value = CRC32.class, defaultGuard = CRC32Substitutions.Guard.class) public class CRC32Substitutions { + public static class Guard implements SubstitutionGuard { + public boolean execute() { + return runtime().getConfig().useCRC32Intrinsics; + } + } + /** * Gets the address of {@code StubRoutines::x86::_crc_table} in {@code stubRoutines_x86.hpp}. */ diff -r 110795e38ac6 -r 5348da19751d graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/CipherBlockChainingSubstitutions.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/CipherBlockChainingSubstitutions.java Tue Jan 14 16:26:40 2014 +0100 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/CipherBlockChainingSubstitutions.java Tue Jan 14 10:14:28 2014 -0800 @@ -39,7 +39,7 @@ /** * Substitutions for {@code com.sun.crypto.provider.CipherBlockChaining} methods. */ -@ClassSubstitution(className = "com.sun.crypto.provider.CipherBlockChaining", optional = true) +@ClassSubstitution(className = "com.sun.crypto.provider.CipherBlockChaining", optional = true, defaultGuard = AESCryptSubstitutions.Guard.class) public class CipherBlockChainingSubstitutions { private static final long embeddedCipherOffset; diff -r 110795e38ac6 -r 5348da19751d graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/ReplacementsImpl.java --- a/graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/ReplacementsImpl.java Tue Jan 14 16:26:40 2014 +0100 +++ b/graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/ReplacementsImpl.java Tue Jan 14 10:14:28 2014 -0800 @@ -148,10 +148,22 @@ return assumptions; } + private static SubstitutionGuard getGuard(Class guardClass) { + if (guardClass != SubstitutionGuard.class) { + try { + return guardClass.newInstance(); + } catch (Exception e) { + throw new GraalInternalError(e); + } + } + return null; + } + public void registerSubstitutions(Class substitutions) { ClassSubstitution classSubstitution = substitutions.getAnnotation(ClassSubstitution.class); assert classSubstitution != null; assert !Snippets.class.isAssignableFrom(substitutions); + SubstitutionGuard defaultGuard = getGuard(classSubstitution.defaultGuard()); for (Method substituteMethod : substitutions.getDeclaredMethods()) { MethodSubstitution methodSubstitution = substituteMethod.getAnnotation(MethodSubstitution.class); MacroSubstitution macroSubstitution = substituteMethod.getAnnotation(MacroSubstitution.class); @@ -165,6 +177,11 @@ } if (methodSubstitution != null) { + SubstitutionGuard guard = getGuard(methodSubstitution.guard()); + if (guard == null) { + guard = defaultGuard; + } + if (macroSubstitution != null && macroSubstitution.isStatic() != methodSubstitution.isStatic()) { throw new GraalInternalError("Macro and method substitution must agree on isStatic attribute: " + substituteMethod); } @@ -174,7 +191,7 @@ String originalName = originalName(substituteMethod, methodSubstitution.value()); JavaSignature originalSignature = originalSignature(substituteMethod, methodSubstitution.signature(), methodSubstitution.isStatic()); Member originalMethod = originalMethod(classSubstitution, methodSubstitution.optional(), originalName, originalSignature); - if (originalMethod != null) { + if (originalMethod != null && (guard == null || guard.execute())) { ResolvedJavaMethod original = registerMethodSubstitution(originalMethod, substituteMethod); if (original != null && methodSubstitution.forced() && shouldIntrinsify(original)) { forcedSubstitutions.add(original);