1 /*
   2  * Copyright (c) 2013, 2019, 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 
  24 
  25 package org.graalvm.compiler.core.test;
  26 
  27 import org.graalvm.compiler.debug.DebugContext;
  28 import org.graalvm.compiler.debug.DebugDumpScope;
  29 import org.graalvm.compiler.nodes.ParameterNode;
  30 import org.graalvm.compiler.nodes.PiNode;
  31 import org.graalvm.compiler.nodes.StructuredGraph;
  32 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  33 import org.graalvm.compiler.nodes.calc.IsNullNode;
  34 import org.graalvm.compiler.nodes.memory.ReadNode;
  35 import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
  36 import org.graalvm.compiler.nodes.spi.CoreProviders;
  37 import org.graalvm.compiler.nodes.spi.LoweringTool;
  38 import org.graalvm.compiler.nodes.type.StampTool;
  39 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  40 import org.graalvm.compiler.phases.common.LoweringPhase;
  41 import org.junit.Assert;
  42 import org.junit.Ignore;
  43 import org.junit.Test;
  44 
  45 import jdk.vm.ci.meta.ResolvedJavaField;
  46 import jdk.vm.ci.meta.ResolvedJavaType;
  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     @Ignore
  76     @Test
  77     @SuppressWarnings("try")
  78     public void test1() {
  79         final String snippet = "test1Snippet";
  80         DebugContext debug = getDebugContext();
  81         try (DebugContext.Scope s = debug.scope("PushThroughPi", new DebugDumpScope(snippet))) {
  82             StructuredGraph graph = compileTestSnippet(snippet);
  83             for (ReadNode rn : graph.getNodes().filter(ReadNode.class)) {
  84                 OffsetAddressNode address = (OffsetAddressNode) rn.getAddress();
  85                 long disp = address.getOffset().asJavaConstant().asLong();
  86 
  87                 ResolvedJavaType receiverType = StampTool.typeOrNull(address.getBase());
  88                 ResolvedJavaField field = receiverType.findInstanceFieldWithOffset(disp, rn.getStackKind());
  89 
  90                 assert field != null : "Node " + rn + " tries to access a field which doesn't exists for this type";
  91                 if (field.getName().equals("x")) {
  92                     Assert.assertTrue(address.getBase() instanceof ParameterNode);
  93                 } else {
  94                     Assert.assertTrue(address.getBase().toString(), address.getBase() instanceof PiNode);
  95                 }
  96             }
  97 
  98             Assert.assertTrue(graph.getNodes().filter(IsNullNode.class).count() == 1);
  99         } catch (Throwable e) {
 100             throw debug.handle(e);
 101         }
 102     }
 103 
 104     private StructuredGraph compileTestSnippet(final String snippet) {
 105         StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO);
 106         CoreProviders context = getProviders();
 107         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
 108         new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
 109         canonicalizer.apply(graph, context);
 110         canonicalizer.apply(graph, context);
 111 
 112         return graph;
 113     }
 114 }