# HG changeset patch # User Christian Wimmer # Date 1435782459 25200 # Node ID 09fcfb9d4ac3b4d7a224c92d3e451b32d3fbba1c # Parent ae67bd822493fafb7109031b56f053b8ce9c46d7 Add test for Truffle inlining of intrinsic methods diff -r ae67bd822493 -r 09fcfb9d4ac3 graal/com.oracle.graal.truffle.test/src/com/oracle/graal/truffle/test/SimplePartialEvaluationTest.java --- a/graal/com.oracle.graal.truffle.test/src/com/oracle/graal/truffle/test/SimplePartialEvaluationTest.java Wed Jul 01 16:06:56 2015 +0200 +++ b/graal/com.oracle.graal.truffle.test/src/com/oracle/graal/truffle/test/SimplePartialEvaluationTest.java Wed Jul 01 13:27:39 2015 -0700 @@ -27,8 +27,10 @@ import org.junit.*; import com.oracle.graal.replacements.*; +import com.oracle.graal.truffle.*; import com.oracle.graal.truffle.test.nodes.*; import com.oracle.truffle.api.frame.*; +import com.oracle.truffle.api.nodes.*; public class SimplePartialEvaluationTest extends PartialEvaluationTest { @@ -167,4 +169,33 @@ AbstractTestNode result = new RecursionTestNode(PEGraphDecoder.Options.InliningDepthError.getValue()); assertPartialEvalEquals("constant42", new RootTestNode(fd, "tooDeepRecursion", result)); } + + @Test + public void intrinsicStatic() { + /* + * The intrinsic for String.equals() is inlined early during bytecode parsing, because we + * call equals() on a value that has the static type String. + */ + FrameDescriptor fd = new FrameDescriptor(); + AbstractTestNode result = new StringEqualsNode("abc", "abf"); + RootNode rootNode = new RootTestNode(fd, "intrinsicStatic", result); + OptimizedCallTarget compilable = compileHelper("intrinsicStatic", rootNode, new Object[0]); + + Assert.assertEquals(42, compilable.call(new Object[0])); + } + + @Test + public void intrinsicVirtual() { + /* + * The intrinsic for String.equals() is inlined late during Truffle partial evaluation, + * because we call equals() on a value that has the static type Object, but during partial + * evaluation the more precise type String is known. + */ + FrameDescriptor fd = new FrameDescriptor(); + AbstractTestNode result = new ObjectEqualsNode("abc", "abf"); + RootNode rootNode = new RootTestNode(fd, "intrinsicVirtual", result); + OptimizedCallTarget compilable = compileHelper("intrinsicVirtual", rootNode, new Object[0]); + + Assert.assertEquals(42, compilable.call(new Object[0])); + } } diff -r ae67bd822493 -r 09fcfb9d4ac3 graal/com.oracle.graal.truffle.test/src/com/oracle/graal/truffle/test/nodes/ObjectEqualsNode.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.truffle.test/src/com/oracle/graal/truffle/test/nodes/ObjectEqualsNode.java Wed Jul 01 13:27:39 2015 -0700 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2015, 2015, 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.truffle.test.nodes; + +import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; +import com.oracle.truffle.api.frame.*; +import com.oracle.truffle.api.nodes.*; + +@NodeInfo +public class ObjectEqualsNode extends AbstractTestNode { + + @CompilationFinal private Object o1; + private Object o2; + + public ObjectEqualsNode(Object o1, Object o2) { + this.o1 = o1; + this.o2 = o2; + } + + @Override + public int execute(VirtualFrame frame) { + return o1.equals(o2) ? 1 : 42; + } +} diff -r ae67bd822493 -r 09fcfb9d4ac3 graal/com.oracle.graal.truffle.test/src/com/oracle/graal/truffle/test/nodes/StringEqualsNode.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.truffle.test/src/com/oracle/graal/truffle/test/nodes/StringEqualsNode.java Wed Jul 01 13:27:39 2015 -0700 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2015, 2015, 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.truffle.test.nodes; + +import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; +import com.oracle.truffle.api.frame.*; +import com.oracle.truffle.api.nodes.*; + +@NodeInfo +public class StringEqualsNode extends AbstractTestNode { + + @CompilationFinal private String o1; + private Object o2; + + public StringEqualsNode(String o1, Object o2) { + this.o1 = o1; + this.o2 = o2; + } + + @Override + public int execute(VirtualFrame frame) { + return o1.equals(o2) ? 1 : 42; + } +}