001/* 002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.compiler.test; 024 025import com.oracle.graal.debug.*; 026import com.oracle.graal.debug.Debug.*; 027 028import org.junit.*; 029 030import com.oracle.graal.nodes.*; 031import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions; 032import com.oracle.graal.nodes.java.*; 033import com.oracle.graal.phases.common.*; 034import com.oracle.graal.phases.tiers.*; 035 036public class EliminateNestedCheckCastsTest extends GraalCompilerTest { 037 038 public static long test1Snippet(A1 a1) { 039 A2 a2 = (A2) a1; 040 A3 a3 = (A3) a2; 041 A4 a4 = (A4) a3; 042 A5 a5 = (A5) a4; 043 A6 a6 = (A6) a5; 044 return a6.x6; 045 } 046 047 @Test 048 public void test1() { 049 compileSnippet("test1Snippet", 5, 1); 050 } 051 052 public static long test2Snippet(A1 a1) { 053 A2 a2 = (A2) a1; 054 A3 a3 = (A3) a2; 055 A4 a4 = (A4) a3; 056 A5 a5 = (A5) a4; 057 A6 a6 = (A6) a5; 058 // as a3 has an usage, thus don't remove the A3 checkcast. 059 return a3.x2 + a6.x6; 060 } 061 062 @Test 063 public void test2() { 064 compileSnippet("test2Snippet", 5, 1); 065 } 066 067 public static long test3Snippet(A1 a1) { 068 long result = a1.x1; 069 A2 a2 = (A2) a1; 070 if (a1.x1 == 42) { 071 A3 a3 = (A3) a2; 072 result = a3.x3; 073 } 074 return result; 075 } 076 077 @Test 078 public void test3() { 079 compileSnippet("test3Snippet", 2, 2); 080 } 081 082 public static long test4Snippet(A1 a1, A1 b1) { 083 A2 a2 = (A2) a1; 084 A3 b3 = (A3) b1; 085 return a2.x2 + b3.x3; 086 } 087 088 @Test 089 public void test4() { 090 compileSnippet("test4Snippet", 2, 2); 091 } 092 093 public static long test5Snippet(A1 a1) { 094 long sum = 0; 095 A2 a2 = (A2) a1; 096 A3 a3 = (A3) a2; 097 sum += a2.x2; 098 return sum + a3.x3; 099 } 100 101 @Test 102 public void test5() { 103 StructuredGraph graph = compileSnippet("test5Snippet", 2, 1); 104 for (LoadFieldNode lfn : graph.getNodes().filter(LoadFieldNode.class)) { 105 Assert.assertTrue(lfn.object() instanceof CheckCastNode); 106 } 107 } 108 109 private StructuredGraph compileSnippet(final String snippet, final int checkcasts, final int afterCanon) { 110 final StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES); 111 try (Scope s = Debug.scope("NestedCheckCastsTest", graph)) { 112 Debug.dump(graph, "After parsing: " + snippet); 113 Assert.assertEquals(checkcasts, graph.getNodes().filter(CheckCastNode.class).count()); 114 new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders())); 115 Assert.assertEquals(afterCanon, graph.getNodes().filter(CheckCastNode.class).count()); 116 return graph; 117 } catch (Throwable e) { 118 throw Debug.handle(e); 119 } 120 121 } 122 123 public static class A1 { 124 125 public long x1 = 1; 126 } 127 128 public static class A2 extends A1 { 129 130 public long x2 = 2; 131 } 132 133 public static class A3 extends A2 { 134 135 public long x3 = 3; 136 } 137 138 public static class A4 extends A3 { 139 140 public long x4 = 4; 141 } 142 143 public static class A5 extends A4 { 144 145 public long x5 = 5; 146 } 147 148 public static class A6 extends A5 { 149 150 public long x6 = 6; 151 } 152}