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 org.graalvm.compiler.debug.DebugContext;
  26 import org.graalvm.compiler.debug.DebugDumpScope;
  27 import org.graalvm.compiler.nodes.ParameterNode;
  28 import org.graalvm.compiler.nodes.PiNode;
  29 import org.graalvm.compiler.nodes.StructuredGraph;
  30 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  31 import org.graalvm.compiler.nodes.calc.IsNullNode;
  32 import org.graalvm.compiler.nodes.memory.ReadNode;
  33 import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
  34 import org.graalvm.compiler.nodes.spi.LoweringTool;
  35 import org.graalvm.compiler.nodes.type.StampTool;
  36 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  37 import org.graalvm.compiler.phases.common.LoweringPhase;
  38 import org.graalvm.compiler.phases.tiers.PhaseContext;
  39 import org.junit.Assert;
  40 import org.junit.Ignore;
  41 import org.junit.Test;
  42 
  43 import jdk.vm.ci.meta.ResolvedJavaField;
  44 import jdk.vm.ci.meta.ResolvedJavaType;
  45 
  46 public class PushNodesThroughPiTest extends GraalCompilerTest {
  47 
  48     public static class A {
  49 
  50         public long x = 20;
  51     }
  52 
  53     public static class B extends A {
  54 
  55         public long y = 10;
  56     }
  57 
  58     public static class C extends B {
  59 
  60         public long z = 5;
  61     }
  62 
  63     public static long test1Snippet(A a) {
  64         C c = (C) a;
  65         long ret = c.x; // this can be pushed before the checkcast
  66         ret += c.y; // not allowed to push
  67         ret += c.z; // not allowed to push
  68         // the null-check should be canonicalized with the null-check of the checkcast
  69         ret += c != null ? 100 : 200;
  70         return ret;
  71     }
  72 
  73     @Ignore
  74     @Test
  75     @SuppressWarnings("try")
  76     public void test1() {
  77         final String snippet = "test1Snippet";
  78         DebugContext debug = getDebugContext();
  79         try (DebugContext.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         canonicalizer.apply(graph, context);
 109 
 110         return graph;
 111     }
 112 }