changeset 9197:9b455ec15820

canonicalizer: eliminate nested checkcasts
author Bernhard Urban <bernhard.urban@jku.at>
date Fri, 19 Apr 2013 16:41:54 +0200
parents 31c1168e1a8e
children cf470d096a8f 9d29e5aa54d2
files graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/EliminateNestedCheckCastsTest.java graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/ReadAfterCheckCastTest.java graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/java/CheckCastNode.java
diffstat 2 files changed, 120 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/EliminateNestedCheckCastsTest.java	Fri Apr 19 16:41:54 2013 +0200
@@ -0,0 +1,111 @@
+/*
+ * 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.compiler.test;
+
+import java.util.concurrent.*;
+
+import junit.framework.Assert;
+
+import org.junit.*;
+
+import com.oracle.graal.debug.*;
+import com.oracle.graal.nodes.*;
+import com.oracle.graal.nodes.java.*;
+import com.oracle.graal.phases.common.*;
+
+public class EliminateNestedCheckCastsTest extends GraalCompilerTest {
+
+    public static long test1Snippet(A1 a1) {
+        A2 a2 = (A2) a1;
+        A3 a3 = (A3) a2;
+        A4 a4 = (A4) a3;
+        A5 a5 = (A5) a4;
+        A6 a6 = (A6) a5;
+        return a6.x6;
+    }
+
+    @Test
+    public void test1() {
+        compileSnippet("test1Snippet", 5, 1);
+    }
+
+    public static long test2Snippet(A1 a1) {
+        A2 a2 = (A2) a1;
+        A3 a3 = (A3) a2;
+        A4 a4 = (A4) a3;
+        A5 a5 = (A5) a4;
+        A6 a6 = (A6) a5;
+        // as a3 has an usage, thus don't remove the A3 checkcast.
+        return a3.x2 + a6.x6;
+    }
+
+    @Test
+    public void test2() {
+        compileSnippet("test2Snippet", 5, 2);
+    }
+
+    private StructuredGraph compileSnippet(final String snippet, final int checkcasts, final int afterCanon) {
+        return Debug.scope(snippet, new Callable<StructuredGraph>() {
+
+            @Override
+            public StructuredGraph call() throws Exception {
+                StructuredGraph graph = parse(snippet);
+                Assert.assertEquals(checkcasts, graph.getNodes().filter(CheckCastNode.class).count());
+                new CanonicalizerPhase.Instance(runtime(), null).apply(graph);
+                Assert.assertEquals(afterCanon, graph.getNodes(CheckCastNode.class).count());
+                return graph;
+            }
+
+        });
+    }
+
+    public static class A1 {
+
+        public long x1 = 1;
+    }
+
+    public static class A2 extends A1 {
+
+        public long x2 = 2;
+    }
+
+    public static class A3 extends A2 {
+
+        public long x3 = 3;
+    }
+
+    public static class A4 extends A3 {
+
+        public long x4 = 4;
+    }
+
+    public static class A5 extends A4 {
+
+        public long x5 = 5;
+    }
+
+    public static class A6 extends A5 {
+
+        public long x6 = 6;
+    }
+}
--- a/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/java/CheckCastNode.java	Fri Apr 19 11:22:54 2013 +0200
+++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/java/CheckCastNode.java	Fri Apr 19 16:41:54 2013 +0200
@@ -75,11 +75,20 @@
                 // checkcast.
                 return object();
             }
+
+            // remove checkcast if the only usage is a more specific checkcast
+            if (usages().count() == 1) {
+                CheckCastNode ccn = usages().filter(CheckCastNode.class).first();
+                if (ccn != null && ccn.type() != null && type.isAssignableFrom(ccn.type())) {
+                    return object();
+                }
+            }
         }
 
         if (object().objectStamp().alwaysNull()) {
             return object();
         }
+
         return this;
     }