changeset 5433:d7b5cc23945e

refactored tests to share support for dump-aware compilation
author Doug Simon <doug.simon@oracle.com>
date Wed, 23 May 2012 14:42:28 +0200
parents 50598118cd0a
children 451327d8543b
files graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/CheckCastTest.java graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/CompiledMethodTest.java graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/GraphTest.java graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/InstanceOfTest.java graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/LowerCheckCastTest.java graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/TypeCheckTest.java
diffstat 6 files changed, 370 insertions(+), 174 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/CheckCastTest.java	Wed May 23 14:42:28 2012 +0200
@@ -0,0 +1,121 @@
+/*
+ * 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.compiler.tests;
+
+import org.junit.*;
+
+import com.oracle.graal.nodes.*;
+import com.oracle.graal.nodes.java.*;
+import com.oracle.max.cri.ri.*;
+
+/**
+ * Tests the implementation of checkcast, allowing profiling information to
+ * be manually specified.
+ */
+public class CheckCastTest extends TypeCheckTest {
+
+    @Override
+    protected void replaceProfile(StructuredGraph graph, RiTypeProfile profile) {
+        CheckCastNode ccn = graph.getNodes(CheckCastNode.class).first();
+        if (ccn != null) {
+            CheckCastNode ccnNew = graph.add(new CheckCastNode(ccn.targetClassInstruction(), ccn.targetClass(), ccn.object(), profile));
+            graph.replaceFixedWithFixed(ccn, ccnNew);
+        }
+    }
+
+    @Test
+    public void test1() {
+        test("asNumber",    profile(),                        111);
+        test("asNumber",    profile(Integer.class),           111);
+        test("asNumber",    profile(Long.class, Short.class), 111);
+        test("asNumberExt", profile(),                        111);
+        test("asNumberExt", profile(Integer.class),           111);
+        test("asNumberExt", profile(Long.class, Short.class), 111);
+    }
+
+    @Test
+    public void test2() {
+        test("asString",    profile(),             "111");
+        test("asString",    profile(String.class), "111");
+        test("asString",    profile(String.class), "111");
+
+        final String nullString = null;
+        test("asString",    profile(),             nullString);
+        test("asString",    profile(String.class), nullString);
+        test("asString",    profile(String.class), nullString);
+
+        test("asStringExt", profile(),             "111");
+        test("asStringExt", profile(String.class), "111");
+        test("asStringExt", profile(String.class), "111");
+    }
+
+    @Test
+    public void test3() {
+        test("asNumber", profile(), "111");
+    }
+
+    @Test
+    public void test4() {
+        test("asString", profile(String.class), 111);
+    }
+
+    @Test
+    public void test5() {
+        test("asNumberExt", profile(), "111");
+    }
+
+    @Test
+    public void test6() {
+        test("asStringExt", profile(String.class), 111);
+    }
+
+    public static Number asNumber(Object o) {
+        return (Number) o;
+    }
+
+    public static String asString(Object o) {
+        return (String) o;
+    }
+
+    public static Number asNumberExt(Object o) {
+        Number n = (Number) o;
+        return n.intValue() + 10;
+    }
+
+    public static String asStringExt(Object o) {
+        String s = (String) o;
+        return "#" + s;
+    }
+
+    @Test
+    public void test7() {
+        test("arrayFill", profile(), new Object[100], "111");
+    }
+
+    public static Object[] arrayFill(Object[] arr, Object value) {
+        for (int i = 0; i < arr.length; i++) {
+            arr[i] = value;
+        }
+        return arr;
+    }
+}
--- a/graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/CompiledMethodTest.java	Wed May 23 14:39:56 2012 +0200
+++ b/graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/CompiledMethodTest.java	Wed May 23 14:42:28 2012 +0200
@@ -68,8 +68,7 @@
         }
 
         final RiResolvedMethod riMethod = runtime.getRiMethod(method);
-        CiTargetMethod targetMethod = runtime.compile(riMethod, graph);
-        RiCompiledMethod compiledMethod = addMethod(riMethod, targetMethod);
+        RiCompiledMethod compiledMethod = compile(riMethod, graph);
         try {
             Object result = compiledMethod.execute("1", "2", "3");
             Assert.assertEquals("1-2-3", result);
@@ -83,8 +82,7 @@
         Method method = getMethod("testMethod");
         final StructuredGraph graph = parse(method);
         final RiResolvedMethod riMethod = runtime.getRiMethod(method);
-        CiTargetMethod targetMethod = runtime.compile(riMethod, graph);
-        RiCompiledMethod compiledMethod = addMethod(riMethod, targetMethod);
+        RiCompiledMethod compiledMethod = compile(riMethod, graph);
         try {
             Object result = compiledMethod.executeVarargs("1", "2", "3");
             Assert.assertEquals("1 2 3", result);
@@ -98,8 +96,7 @@
         Method method = getMethod("testMethodVirtual");
         final StructuredGraph graph = parse(method);
         final RiResolvedMethod riMethod = runtime.getRiMethod(method);
-        CiTargetMethod targetMethod = runtime.compile(riMethod, graph);
-        RiCompiledMethod compiledMethod = addMethod(riMethod, targetMethod);
+        RiCompiledMethod compiledMethod = compile(riMethod, graph);
         try {
             f1 = "0";
             Object result = compiledMethod.executeVarargs(this, "1", "2", "3");
@@ -127,9 +124,7 @@
             }
         }
 
-        CiTargetMethod targetMethod = runtime.compile(riMethod, graph);
-        final RiCompiledMethod compiledMethod = addMethod(riMethod, targetMethod);
-
+        RiCompiledMethod compiledMethod = compile(riMethod, graph);
         final CompilableObject compilableObject = new CompilableObjectImpl(0);
 
         Object result;
--- a/graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/GraphTest.java	Wed May 23 14:39:56 2012 +0200
+++ b/graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/GraphTest.java	Wed May 23 14:42:28 2012 +0200
@@ -146,6 +146,17 @@
         }
     }
 
+    private static int compilationId = 0;
+
+    protected RiCompiledMethod compile(final RiResolvedMethod method, final StructuredGraph graph) {
+        return Debug.scope("Compiling", new DebugDumpScope(String.valueOf(compilationId++), true), new Callable<RiCompiledMethod>() {
+            public RiCompiledMethod call() throws Exception {
+                CiTargetMethod targetMethod = runtime.compile(method, graph);
+                return addMethod(method, targetMethod);
+            }
+        });
+    }
+
     protected RiCompiledMethod addMethod(final RiResolvedMethod method, final CiTargetMethod tm) {
         Debug.scope("CodeInstall", new Object[] {graalCompiler, method}, new Callable<RiCompiledMethod>() {
             @Override
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/InstanceOfTest.java	Wed May 23 14:42:28 2012 +0200
@@ -0,0 +1,150 @@
+/*
+ * 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.compiler.tests;
+
+import java.util.*;
+
+import org.junit.*;
+
+import com.oracle.graal.nodes.*;
+import com.oracle.graal.nodes.java.*;
+import com.oracle.max.cri.ri.*;
+
+/**
+ * Tests the implementation of instanceof, allowing profiling information to
+ * be manually specified.
+ */
+public class InstanceOfTest extends TypeCheckTest {
+
+    @Override
+    protected void replaceProfile(StructuredGraph graph, RiTypeProfile profile) {
+        InstanceOfNode ion = graph.getNodes().filter(InstanceOfNode.class).first();
+        if (ion != null) {
+            InstanceOfNode ionNew = graph.add(new InstanceOfNode(ion.targetClassInstruction(), ion.targetClass(), ion.object(), profile));
+            graph.replaceFloating(ion, ionNew);
+        }
+    }
+
+    @Test
+    public void test1() {
+        test("isString",    profile(),                        "object");
+        test("isString",    profile(String.class),            "object");
+        test("isString",    profile(Long.class, Short.class), "object");
+
+        test("isString",    profile(),                        Object.class);
+        test("isString",    profile(String.class),            Object.class);
+        test("isString",    profile(Long.class, Short.class), Object.class);
+    }
+
+    @Test
+    public void test2() {
+        test("isStringInt",    profile(),                        "object");
+        test("isStringInt",    profile(String.class),            "object");
+        test("isStringInt",    profile(Long.class, Short.class), "object");
+
+        test("isStringInt",    profile(),                        Object.class);
+        test("isStringInt",    profile(String.class),            Object.class);
+        test("isStringInt",    profile(Long.class, Short.class), Object.class);
+    }
+
+    @Test
+    public void test3() {
+        Throwable throwable = new Exception();
+        test("isThrowable",    profile(),                        throwable);
+        test("isThrowable",    profile(String.class),            throwable);
+        test("isThrowable",    profile(Long.class, Short.class), throwable);
+
+        test("isThrowable",    profile(),                        Object.class);
+        test("isThrowable",    profile(String.class),            Object.class);
+        test("isThrowable",    profile(Long.class, Short.class), Object.class);
+    }
+
+    @Test
+    public void test4() {
+        Throwable throwable = new Exception();
+        test("isThrowableInt",    profile(),                        throwable);
+        test("isThrowableInt",    profile(String.class),            throwable);
+        test("isThrowableInt",    profile(Long.class, Short.class), throwable);
+
+        test("isThrowableInt",    profile(),                        Object.class);
+        test("isThrowableInt",    profile(String.class),            Object.class);
+        test("isThrowableInt",    profile(Long.class, Short.class), Object.class);
+    }
+
+    @Test
+    public void test5() {
+        Map map = new HashMap<>();
+        test("isMap",    profile(),                        map);
+        test("isMap",    profile(String.class),            map);
+        test("isMap",    profile(Long.class, Short.class), map);
+
+        test("isMap",    profile(),                        Object.class);
+        test("isMap",    profile(String.class),            Object.class);
+        test("isMap",    profile(Long.class, Short.class), Object.class);
+    }
+
+    @Test
+    public void test6() {
+        Map map = new HashMap<>();
+        test("isMapInt",    profile(),                        map);
+        test("isMapInt",    profile(String.class),            map);
+        test("isMapInt",    profile(Long.class, Short.class), map);
+
+        test("isMapInt",    profile(),                        Object.class);
+        test("isMapInt",    profile(String.class),            Object.class);
+        test("isMapInt",    profile(Long.class, Short.class), Object.class);
+    }
+
+    public static boolean isString(Object o) {
+        return o instanceof String;
+    }
+
+    public static int isStringInt(Object o) {
+        if (o instanceof String) {
+            return 1;
+        }
+        return 0;
+    }
+
+    public static boolean isThrowable(Object o) {
+        return o instanceof Throwable;
+    }
+
+    public static int isThrowableInt(Object o) {
+        if (o instanceof Throwable) {
+            return 1;
+        }
+        return 0;
+    }
+
+    public static boolean isMap(Object o) {
+        return o instanceof Map;
+    }
+
+    public static int isMapInt(Object o) {
+        if (o instanceof Map) {
+            return 1;
+        }
+        return 0;
+    }
+}
--- a/graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/LowerCheckCastTest.java	Wed May 23 14:39:56 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,165 +0,0 @@
-/*
- * 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.compiler.tests;
-
-import java.lang.reflect.*;
-
-import org.junit.*;
-
-import com.oracle.graal.nodes.*;
-import com.oracle.graal.nodes.java.*;
-import com.oracle.max.cri.ci.*;
-import com.oracle.max.cri.ri.*;
-import com.oracle.max.cri.ri.RiTypeProfile.ProfiledType;
-
-public class LowerCheckCastTest extends GraphTest {
-
-    private RiCompiledMethod compile(Method method, RiTypeProfile profile) {
-        final StructuredGraph graph = parse(method);
-
-        CheckCastNode ccn = graph.getNodes(CheckCastNode.class).first();
-        if (ccn != null) {
-            CheckCastNode ccnNew = graph.add(new CheckCastNode(ccn.targetClassInstruction(), ccn.targetClass(), ccn.object(), profile));
-            graph.replaceFixedWithFixed(ccn, ccnNew);
-        }
-
-        final RiResolvedMethod riMethod = runtime.getRiMethod(method);
-        CiTargetMethod targetMethod = runtime.compile(riMethod, graph);
-        return addMethod(riMethod, targetMethod);
-    }
-
-    private RiTypeProfile profile(Class... types) {
-        if (types.length == 0) {
-            return null;
-        }
-        ProfiledType[] ptypes = new ProfiledType[types.length];
-        for (int i = 0; i < types.length; i++) {
-            ptypes[i] = new ProfiledType(runtime.getType(types[i]), 1.0D / types.length);
-        }
-        return new RiTypeProfile(0.0D, ptypes);
-    }
-
-    private void test(String name, RiTypeProfile profile, Object... args) {
-        Method method = getMethod(name);
-        Object expect = null;
-        Throwable exception = null;
-        try {
-            // This gives us both the expected return value as well as ensuring that the method to be compiled is fully resolved
-            expect = method.invoke(null, args);
-        } catch (InvocationTargetException e) {
-            exception = e.getTargetException();
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
-        RiCompiledMethod compiledMethod = compile(method, profile);
-        compiledMethod.method();
-
-        if (exception != null) {
-            try {
-                compiledMethod.executeVarargs(args);
-                Assert.fail("expected " + exception);
-            } catch (Throwable e) {
-                Assert.assertEquals(exception.getClass(), e.getClass());
-            }
-        } else {
-            Object actual = compiledMethod.executeVarargs(args);
-            Assert.assertEquals(expect, actual);
-        }
-    }
-
-    @Test
-    public void test1() {
-        test("asNumber",    profile(),                        111);
-        test("asNumber",    profile(Integer.class),           111);
-        test("asNumber",    profile(Long.class, Short.class), 111);
-        test("asNumberExt", profile(),                        111);
-        test("asNumberExt", profile(Integer.class),           111);
-        test("asNumberExt", profile(Long.class, Short.class), 111);
-    }
-
-    @Test
-    public void test2() {
-        test("asString",    profile(),             "111");
-        test("asString",    profile(String.class), "111");
-        test("asString",    profile(String.class), "111");
-
-        final String nullString = null;
-        test("asString",    profile(),             nullString);
-        test("asString",    profile(String.class), nullString);
-        test("asString",    profile(String.class), nullString);
-
-        test("asStringExt", profile(),             "111");
-        test("asStringExt", profile(String.class), "111");
-        test("asStringExt", profile(String.class), "111");
-    }
-
-    @Test
-    public void test3() {
-        test("asNumber", profile(), "111");
-    }
-
-    @Test
-    public void test4() {
-        test("asString", profile(String.class), 111);
-    }
-
-    @Test
-    public void test5() {
-        test("asNumberExt", profile(), "111");
-    }
-
-    @Test
-    public void test6() {
-        test("asStringExt", profile(String.class), 111);
-    }
-
-    public static Number asNumber(Object o) {
-        return (Number) o;
-    }
-
-    public static String asString(Object o) {
-        return (String) o;
-    }
-
-    public static Number asNumberExt(Object o) {
-        Number n = (Number) o;
-        return n.intValue() + 10;
-    }
-
-    public static String asStringExt(Object o) {
-        String s = (String) o;
-        return "#" + s;
-    }
-
-    @Test
-    public void test7() {
-        test("arrayFill", profile(), new Object[100], "111");
-    }
-
-    public static Object[] arrayFill(Object[] arr, Object value) {
-        for (int i = 0; i < arr.length; i++) {
-            arr[i] = value;
-        }
-        return arr;
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/TypeCheckTest.java	Wed May 23 14:42:28 2012 +0200
@@ -0,0 +1,84 @@
+/*
+ * 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.compiler.tests;
+
+import java.lang.reflect.*;
+
+import org.junit.*;
+
+import com.oracle.graal.nodes.*;
+import com.oracle.max.cri.ri.*;
+import com.oracle.max.cri.ri.RiTypeProfile.ProfiledType;
+
+/**
+ * Base class for checkcast and instanceof test classes.
+ */
+public abstract class TypeCheckTest extends GraphTest {
+
+    protected abstract void replaceProfile(StructuredGraph graph, RiTypeProfile profile);
+
+    private RiCompiledMethod compile(Method method, RiTypeProfile profile) {
+        final StructuredGraph graph = parse(method);
+        replaceProfile(graph, profile);
+        return compile(runtime.getRiMethod(method), graph);
+    }
+
+    protected RiTypeProfile profile(Class... types) {
+        if (types.length == 0) {
+            return null;
+        }
+        ProfiledType[] ptypes = new ProfiledType[types.length];
+        for (int i = 0; i < types.length; i++) {
+            ptypes[i] = new ProfiledType(runtime.getType(types[i]), 1.0D / types.length);
+        }
+        return new RiTypeProfile(0.0D, ptypes);
+    }
+
+    protected void test(String name, RiTypeProfile profile, Object... args) {
+        Method method = getMethod(name);
+        Object expect = null;
+        Throwable exception = null;
+        try {
+            // This gives us both the expected return value as well as ensuring that the method to be compiled is fully resolved
+            expect = method.invoke(null, args);
+        } catch (InvocationTargetException e) {
+            exception = e.getTargetException();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+        RiCompiledMethod compiledMethod = compile(method, profile);
+        compiledMethod.method();
+
+        if (exception != null) {
+            try {
+                compiledMethod.executeVarargs(args);
+                Assert.fail("expected " + exception);
+            } catch (Throwable e) {
+                Assert.assertEquals(exception.getClass(), e.getClass());
+            }
+        } else {
+            Object actual = compiledMethod.executeVarargs(args);
+            Assert.assertEquals(expect, actual);
+        }
+    }
+}