1 /* 2 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.graalvm.compiler.core.test; 24 25 import jdk.vm.ci.meta.ResolvedJavaField; 26 import jdk.vm.ci.meta.ResolvedJavaType; 27 28 import org.junit.Assert; 29 import org.junit.Test; 30 31 import org.graalvm.compiler.debug.Debug; 32 import org.graalvm.compiler.debug.Debug.Scope; 33 import org.graalvm.compiler.debug.DebugDumpScope; 34 import org.graalvm.compiler.nodes.ParameterNode; 35 import org.graalvm.compiler.nodes.PiNode; 36 import org.graalvm.compiler.nodes.StructuredGraph; 37 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 38 import org.graalvm.compiler.nodes.calc.IsNullNode; 39 import org.graalvm.compiler.nodes.memory.ReadNode; 40 import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode; 41 import org.graalvm.compiler.nodes.spi.LoweringTool; 42 import org.graalvm.compiler.nodes.type.StampTool; 43 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 44 import org.graalvm.compiler.phases.common.LoweringPhase; 45 import org.graalvm.compiler.phases.common.PushThroughPiPhase; 46 import org.graalvm.compiler.phases.tiers.PhaseContext; 47 48 public class PushNodesThroughPiTest extends GraalCompilerTest { 49 50 public static class A { 51 52 public long x = 20; 53 } 54 55 public static class B extends A { 56 57 public long y = 10; 58 } 59 60 public static class C extends B { 61 62 public long z = 5; 63 } 64 65 public static long test1Snippet(A a) { 66 C c = (C) a; 67 long ret = c.x; // this can be pushed before the checkcast 68 ret += c.y; // not allowed to push 69 ret += c.z; // not allowed to push 70 // the null-check should be canonicalized with the null-check of the checkcast 71 ret += c != null ? 100 : 200; 72 return ret; 73 } 74 75 @Test 76 @SuppressWarnings("try") 77 public void test1() { 78 final String snippet = "test1Snippet"; 79 try (Scope s = Debug.scope("PushThroughPi", new DebugDumpScope(snippet))) { 80 StructuredGraph graph = compileTestSnippet(snippet); 81 for (ReadNode rn : graph.getNodes().filter(ReadNode.class)) { 82 OffsetAddressNode address = (OffsetAddressNode) rn.getAddress(); 83 long disp = address.getOffset().asJavaConstant().asLong(); 84 85 ResolvedJavaType receiverType = StampTool.typeOrNull(address.getBase()); 86 ResolvedJavaField field = receiverType.findInstanceFieldWithOffset(disp, rn.getStackKind()); 87 88 assert field != null : "Node " + rn + " tries to access a field which doesn't exists for this type"; 89 if (field.getName().equals("x")) { 90 Assert.assertTrue(address.getBase() instanceof ParameterNode); 91 } else { 92 Assert.assertTrue(address.getBase().toString(), address.getBase() instanceof PiNode); 93 } 94 } 95 96 Assert.assertTrue(graph.getNodes().filter(IsNullNode.class).count() == 1); 97 } catch (Throwable e) { 98 throw Debug.handle(e); 99 } 100 } 101 102 private StructuredGraph compileTestSnippet(final String snippet) { 103 StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO); 104 PhaseContext context = new PhaseContext(getProviders()); 105 CanonicalizerPhase canonicalizer = new CanonicalizerPhase(); 106 new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context); 107 canonicalizer.apply(graph, context); 108 new PushThroughPiPhase().apply(graph); 109 canonicalizer.apply(graph, context); 110 111 return graph; 112 } 113 }