1 /*
   2  * Copyright (c) 2014, 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.hotspot.amd64.test;
  24 
  25 import org.junit.Assert;
  26 import org.junit.Assume;
  27 import org.junit.Test;
  28 
  29 import org.graalvm.compiler.core.common.GraalOptions;
  30 import org.graalvm.compiler.hotspot.nodes.CompressionNode;
  31 import org.graalvm.compiler.hotspot.test.HotSpotGraalCompilerTest;
  32 import org.graalvm.compiler.nodes.StructuredGraph;
  33 import org.graalvm.compiler.nodes.ValueNode;
  34 import org.graalvm.compiler.nodes.calc.IsNullNode;
  35 import org.graalvm.compiler.options.OptionValue;
  36 import org.graalvm.compiler.options.OptionValue.OverrideScope;
  37 
  38 import jdk.vm.ci.meta.ResolvedJavaMethod;
  39 
  40 /**
  41  * Ensures that frame omission works in cases where it is expected to.
  42  */
  43 public class CompressedNullCheckTest extends HotSpotGraalCompilerTest {
  44 
  45     private static final class Container {
  46         Integer i;
  47     }
  48 
  49     public static void testSnippet(Container c) {
  50         c.i.intValue();
  51     }
  52 
  53     @SuppressWarnings("try")
  54     private void testImplicit(Integer i) {
  55         Assume.assumeTrue(runtime().getVMConfig().useCompressedOops);
  56 
  57         Container c = new Container();
  58         c.i = i;
  59 
  60         try (OverrideScope s = OptionValue.override(GraalOptions.OptImplicitNullChecks, true)) {
  61             ResolvedJavaMethod method = getResolvedJavaMethod("testSnippet");
  62             Result expect = executeExpected(method, null, c);
  63 
  64             // make sure we don't get a profile that removes the implicit null check
  65             method.reprofile();
  66 
  67             Result actual = executeActual(method, null, c);
  68             assertEquals(expect, actual);
  69         }
  70     }
  71 
  72     @SuppressWarnings("try")
  73     private void testExplicit(Integer i) {
  74         Assume.assumeTrue(runtime().getVMConfig().useCompressedOops);
  75 
  76         Container c = new Container();
  77         c.i = i;
  78 
  79         try (OverrideScope s = OptionValue.override(GraalOptions.OptImplicitNullChecks, false)) {
  80             test("testSnippet", c);
  81         }
  82     }
  83 
  84     @Test
  85     public void implicit() {
  86         testImplicit(new Integer(1));
  87     }
  88 
  89     @Test
  90     public void implicitNull() {
  91         testImplicit(null);
  92     }
  93 
  94     @Test
  95     public void explicit() {
  96         testExplicit(new Integer(1));
  97     }
  98 
  99     @Test
 100     public void explicitNull() {
 101         testExplicit(null);
 102     }
 103 
 104     @Override
 105     protected boolean checkMidTierGraph(StructuredGraph graph) {
 106         int count = 0;
 107         for (IsNullNode isNull : graph.getNodes().filter(IsNullNode.class).snapshot()) {
 108             ValueNode value = isNull.getValue();
 109             if (value instanceof CompressionNode) {
 110                 count++;
 111                 isNull.replaceFirstInput(value, ((CompressionNode) value).getValue());
 112             }
 113         }
 114         Assert.assertEquals("graph should contain exactly one IsNullNode", 1, count);
 115         return super.checkMidTierGraph(graph);
 116     }
 117 }