# HG changeset patch # User Matthias Grimmer # Date 1364486303 -3600 # Node ID bbd8167f21f90e644c34146a372b78273a72ad6e # Parent da674936800c9ed467a2f2b298d0a63f184c6fa2 Added executeHelper to HotSpotInstalled code that gets replaced by a MethodSubstitution diff -r da674936800c -r bbd8167f21f9 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotInstalledCode.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotInstalledCode.java Wed Mar 27 18:12:03 2013 +0100 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotInstalledCode.java Thu Mar 28 16:58:23 2013 +0100 @@ -51,6 +51,10 @@ return isDefault; } + public long getnmethod() { + return nmethod; + } + @Override public ResolvedJavaMethod getMethod() { return method; @@ -104,4 +108,9 @@ public byte[] getCode() { return HotSpotGraalRuntime.getInstance().getCompilerToVM().getCode(nmethod); } + + public static Object executeHelper(long nmethod, long metaspaceMethod, Object arg1, Object arg2, Object arg3) { +// throw new IllegalStateException(); + return HotSpotGraalRuntime.getInstance().getCompilerToVM().executeCompiledMethod(metaspaceMethod, nmethod, arg1, arg2, arg3); + } } diff -r da674936800c -r bbd8167f21f9 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaMethod.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaMethod.java Wed Mar 27 18:12:03 2013 +0100 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaMethod.java Thu Mar 28 16:58:23 2013 +0100 @@ -73,6 +73,10 @@ return holder; } + public long getMetaspaceMethod() { + return metaspaceMethod; + } + /** * Gets the address of the C++ Method object for this method. */ diff -r da674936800c -r bbd8167f21f9 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/HotSpotSnippetUtils.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/HotSpotSnippetUtils.java Wed Mar 27 18:12:03 2013 +0100 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/HotSpotSnippetUtils.java Thu Mar 28 16:58:23 2013 +0100 @@ -594,4 +594,9 @@ return IdentityHashCodeStubCall.call(x); } + + @Fold + public static int verifiedEntryPointOffset() { + return config().nmethodEntryOffset; + } } diff -r da674936800c -r bbd8167f21f9 graal/com.oracle.graal.phases/src/com/oracle/graal/phases/GraalOptions.java --- a/graal/com.oracle.graal.phases/src/com/oracle/graal/phases/GraalOptions.java Wed Mar 27 18:12:03 2013 +0100 +++ b/graal/com.oracle.graal.phases/src/com/oracle/graal/phases/GraalOptions.java Thu Mar 28 16:58:23 2013 +0100 @@ -209,7 +209,7 @@ public static boolean IntrinsifyUnsafeMethods = true; public static boolean IntrinsifyMathMethods = true; public static boolean IntrinsifyAESMethods = true; - + public static boolean IntrinsifyInstalledCodeMethods = true; /** * Counts the various paths taken through snippets. */ diff -r da674936800c -r bbd8167f21f9 graal/com.oracle.graal.rawnativecall.test/test/com/oracle/graal/rawnativecall/test/InstalledCodeExecuteHelperTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.rawnativecall.test/test/com/oracle/graal/rawnativecall/test/InstalledCodeExecuteHelperTest.java Thu Mar 28 16:58:23 2013 +0100 @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2012, 2012, 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.rawnativecall.test; + +import java.lang.reflect.*; + +import org.junit.*; + +import com.oracle.graal.api.meta.*; +import com.oracle.graal.api.runtime.*; +import com.oracle.graal.compiler.test.*; +import com.oracle.graal.hotspot.meta.*; +import com.oracle.graal.nodes.spi.*; + +public class InstalledCodeExecuteHelperTest extends GraalCompilerTest { + + private static final int ITERATIONS = 10000000; + + @Ignore + @Test + public void test1() throws NoSuchMethodException, SecurityException { + + final Method benchrMethod = InstalledCodeExecuteHelperTest.class.getMethod("bench", long.class, long.class); + final ResolvedJavaMethod benchJavaMethod = Graal.getRequiredCapability(GraalCodeCacheProvider.class).lookupJavaMethod(benchrMethod); + HotSpotInstalledCode benchCode = (HotSpotInstalledCode) getCode(benchJavaMethod, parse(benchrMethod)); + + final Method wrapperMethod = InstalledCodeExecuteHelperTest.class.getMethod("executeWrapper", long.class, long.class, Object.class, Object.class, Object.class); + final ResolvedJavaMethod wrapperJavaMethod = Graal.getRequiredCapability(GraalCodeCacheProvider.class).lookupJavaMethod(wrapperMethod); + HotSpotInstalledCode wrapperCode = (HotSpotInstalledCode) getCode(wrapperJavaMethod, parse(wrapperMethod)); + + final Method fooMethod = InstalledCodeExecuteHelperTest.class.getMethod("foo", Object.class, Object.class, Object.class); + final HotSpotResolvedJavaMethod fooJavaMethod = (HotSpotResolvedJavaMethod) Graal.getRequiredCapability(GraalCodeCacheProvider.class).lookupJavaMethod(fooMethod); + HotSpotInstalledCode fooCode = (HotSpotInstalledCode) getCode(fooJavaMethod, parse(fooMethod)); + + System.out.println(wrapperCode.executeVarargs(fooCode.getnmethod(), fooJavaMethod.getMetaspaceMethod(), null, null, null)); + + long nmethod = fooCode.getnmethod(); + long metaspacemethod = fooJavaMethod.getMetaspaceMethod(); + + System.out.println("Without replaced InstalledCode.execute:" + bench(nmethod, metaspacemethod)); + + System.out.println("WITH replaced InstalledCode.execute:" + benchCode.executeVarargs(nmethod, metaspacemethod)); + + } + + public static Long bench(long nmethod, long metaspacemethod) { + long start = System.currentTimeMillis(); + + for (int i = 0; i < ITERATIONS; i++) { + HotSpotInstalledCode.executeHelper(nmethod, metaspacemethod, null, null, null); + } + + long end = System.currentTimeMillis(); + return (end - start); + } + + public static Object foo(@SuppressWarnings("unused") Object a1, @SuppressWarnings("unused") Object a2, @SuppressWarnings("unused") Object a3) { + return 42; + } + + public static Object executeWrapper(long nmethod, long metaspaceMethod, Object arg1, Object arg2, Object arg3) { + return HotSpotInstalledCode.executeHelper(nmethod, metaspaceMethod, arg1, arg2, arg3); + } + +} diff -r da674936800c -r bbd8167f21f9 graal/com.oracle.graal.rawnativecall/src/com/oracle/graal/rawnativecall/nodes/HotSpotInstalledCodeExecuteNode.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.rawnativecall/src/com/oracle/graal/rawnativecall/nodes/HotSpotInstalledCodeExecuteNode.java Thu Mar 28 16:58:23 2013 +0100 @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2011, 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.rawnativecall.nodes; + +import com.oracle.graal.api.code.*; +import com.oracle.graal.api.meta.*; +import com.oracle.graal.hotspot.nodes.*; +import com.oracle.graal.nodes.*; +import com.oracle.graal.nodes.extended.*; +import com.oracle.graal.nodes.spi.*; +import com.oracle.graal.nodes.type.*; +import com.oracle.graal.word.*; + +public class HotSpotInstalledCodeExecuteNode extends AbstractCallNode implements Lowerable { + + @Input private final ValueNode targetAddress; + @Input private final ValueNode metaspaceObject; + private final Class[] signature; + + public HotSpotInstalledCodeExecuteNode(Kind kind, ValueNode targetAddress, ValueNode metaspaceObject, Class[] signature, ValueNode arg1, ValueNode arg2, ValueNode arg3) { + super(StampFactory.forKind(kind), new ValueNode[]{arg1, arg2, arg3}); + this.targetAddress = targetAddress; + this.metaspaceObject = metaspaceObject; + this.signature = signature; + } + + @Override + public Object[] getLocationIdentities() { + return new Object[]{LocationNode.ANY_LOCATION}; + } + + @Override + public void lower(LoweringTool tool) { + replaceWithInvoke(tool); + } + + private InvokeNode replaceWithInvoke(LoweringTool tool) { + InvokeNode invoke = createInvoke(tool); + ((StructuredGraph) graph()).replaceFixedWithFixed(this, invoke); + return invoke; + } + + protected InvokeNode createInvoke(LoweringTool tool) { + ResolvedJavaMethod method = null; + try { + method = tool.getRuntime().lookupJavaMethod(HotSpotInstalledCodeExecuteNode.class.getMethod("placeholder", Object.class, Object.class, Object.class)); + } catch (NoSuchMethodException | SecurityException e) { + throw new IllegalStateException(); + } + ResolvedJavaType[] signatureTypes = new ResolvedJavaType[signature.length]; + for (int i = 0; i < signature.length; i++) { + signatureTypes[i] = tool.getRuntime().lookupJavaType(signature[i]); + } + HotSpotIndirectCallTargetNode callTarget = graph().add( + new HotSpotIndirectCallTargetNode(metaspaceObject, targetAddress, arguments, stamp(), signatureTypes, method, CallingConvention.Type.JavaCall)); + InvokeNode invoke = graph().add(new InvokeNode(callTarget, 0)); + invoke.setStateAfter(stateAfter()); + return invoke; + } + + public static Object placeholder(@SuppressWarnings("unused") Object a1, @SuppressWarnings("unused") Object a2, @SuppressWarnings("unused") Object a3) { + return 1; + } + + @NodeIntrinsic + public static native T call(@ConstantNodeParameter Kind kind, Word targetAddress, long metaspaceObject, @ConstantNodeParameter Class[] signature, Object arg1, Object arg2, Object arg3); + +} diff -r da674936800c -r bbd8167f21f9 graal/com.oracle.graal.rawnativecall/src/com/oracle/graal/rawnativecall/replacements/HotSpotInstalledCodeIntrinsics.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.rawnativecall/src/com/oracle/graal/rawnativecall/replacements/HotSpotInstalledCodeIntrinsics.java Thu Mar 28 16:58:23 2013 +0100 @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2013, 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.rawnativecall.replacements; + +import com.oracle.graal.api.runtime.*; +import com.oracle.graal.phases.*; +import com.oracle.graal.replacements.*; + +@ServiceProvider(ReplacementsProvider.class) +public class HotSpotInstalledCodeIntrinsics implements ReplacementsProvider { + + @Override + public void installReplacements(ReplacementsInstaller installer) { + if (GraalOptions.IntrinsifyInstalledCodeMethods) { + installer.installSubstitutions(HotSpotInstalledCodeSubstitutions.class); + } + } +} diff -r da674936800c -r bbd8167f21f9 graal/com.oracle.graal.rawnativecall/src/com/oracle/graal/rawnativecall/replacements/HotSpotInstalledCodeSubstitutions.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.rawnativecall/src/com/oracle/graal/rawnativecall/replacements/HotSpotInstalledCodeSubstitutions.java Thu Mar 28 16:58:23 2013 +0100 @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2013, 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.rawnativecall.replacements; + +import com.oracle.graal.api.meta.*; +import com.oracle.graal.api.replacements.*; +import com.oracle.graal.hotspot.meta.*; +import com.oracle.graal.hotspot.replacements.*; +import com.oracle.graal.rawnativecall.nodes.*; +import com.oracle.graal.replacements.Snippet.Fold; +import com.oracle.graal.word.*; + +@ClassSubstitution(HotSpotInstalledCode.class) +public class HotSpotInstalledCodeSubstitutions { + + @MethodSubstitution + public static Object executeHelper(long nmethod, long metaspaceMethod, final Object arg1, final Object arg2, final Object arg3) { + final int verifiedEntryPointOffset = HotSpotSnippetUtils.verifiedEntryPointOffset(); + final Word callTarget = Word.unsigned(nmethod).readWord(verifiedEntryPointOffset); + return HotSpotInstalledCodeExecuteNode.call(Kind.Object, callTarget, metaspaceMethod, getSignature(), arg1, arg2, arg3); + } + + @Fold + private static Class[] getSignature() { + return new Class[]{Object.class, Object.class, Object.class}; + } +} diff -r da674936800c -r bbd8167f21f9 make/build-graal.xml --- a/make/build-graal.xml Wed Mar 27 18:12:03 2013 +0100 +++ b/make/build-graal.xml Thu Mar 28 16:58:23 2013 +0100 @@ -64,6 +64,7 @@ + diff -r da674936800c -r bbd8167f21f9 mx/projects --- a/mx/projects Wed Mar 27 18:12:03 2013 +0100 +++ b/mx/projects Thu Mar 28 16:58:23 2013 +0100 @@ -23,7 +23,7 @@ library@DACAPO_SCALA@urls=http://repo.scalabench.org/snapshots/org/scalabench/benchmarks/scala-benchmark-suite/0.1.0-SNAPSHOT/scala-benchmark-suite-0.1.0-20120216.103539-3.jar distribution@GRAAL@path=graal.jar -distribution@GRAAL@dependencies=com.oracle.graal.hotspot.amd64,com.oracle.graal.hotspot.sparc +distribution@GRAAL@dependencies=com.oracle.graal.hotspot.amd64,com.oracle.graal.hotspot.sparc, com.oracle.graal.rawnativecall # graal.api.runtime project@com.oracle.graal.api.runtime@subDir=graal @@ -386,6 +386,21 @@ project@com.oracle.graal.asm.sparc@checkstyle=com.oracle.graal.graph project@com.oracle.graal.asm.sparc@javaCompliance=1.7 +# graal.rawnativecall +project@com.oracle.graal.rawnativecall@subDir=graal +project@com.oracle.graal.rawnativecall@sourceDirs=src +project@com.oracle.graal.rawnativecall@dependencies=com.oracle.graal.hotspot +project@com.oracle.graal.rawnativecall@checkstyle=com.oracle.graal.graph +project@com.oracle.graal.rawnativecall@annotationProcessors=com.oracle.graal.replacements.verifier, com.oracle.graal.service.processor +project@com.oracle.graal.rawnativecall@javaCompliance=1.7 + +# graal.rawnativecall.test +project@com.oracle.graal.rawnativecall.test@subDir=graal +project@com.oracle.graal.rawnativecall.test@sourceDirs=test +project@com.oracle.graal.rawnativecall.test@dependencies=com.oracle.graal.compiler.test,com.oracle.graal.hotspot +project@com.oracle.graal.rawnativecall.test@checkstyle=com.oracle.graal.graph +project@com.oracle.graal.rawnativecall.test@javaCompliance=1.7 + # truffle.api project@com.oracle.truffle.api@subDir=graal project@com.oracle.truffle.api@sourceDirs=src @@ -437,3 +452,5 @@ project@com.oracle.truffle.sl.test@checkstyle=com.oracle.graal.graph project@com.oracle.truffle.sl.test@javaCompliance=1.7 + +