001/*
002 * Copyright (c) 2014, 2015, 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.hotspot.amd64.test;
024
025import jdk.internal.jvmci.meta.*;
026import jdk.internal.jvmci.options.*;
027import jdk.internal.jvmci.options.OptionValue.*;
028
029import org.junit.*;
030
031import com.oracle.graal.compiler.common.*;
032import com.oracle.graal.compiler.test.*;
033import com.oracle.graal.hotspot.*;
034import com.oracle.graal.hotspot.nodes.*;
035import com.oracle.graal.nodes.*;
036import com.oracle.graal.nodes.calc.*;
037
038/**
039 * Ensures that frame omission works in cases where it is expected to.
040 */
041public class CompressedNullCheckTest extends GraalCompilerTest {
042
043    private static final class Container {
044        Integer i;
045    }
046
047    public static void testSnippet(Container c) {
048        c.i.intValue();
049    }
050
051    private void testImplicit(Integer i) {
052        Assume.assumeTrue(HotSpotGraalRuntime.runtime().getConfig().useCompressedOops);
053
054        Container c = new Container();
055        c.i = i;
056
057        try (OverrideScope s = OptionValue.override(GraalOptions.OptImplicitNullChecks, true)) {
058            ResolvedJavaMethod method = getResolvedJavaMethod("testSnippet");
059            Result expect = executeExpected(method, null, c);
060
061            // make sure we don't get a profile that removes the implicit null check
062            method.reprofile();
063
064            Result actual = executeActual(method, null, c);
065            assertEquals(expect, actual);
066        }
067    }
068
069    private void testExplicit(Integer i) {
070        Assume.assumeTrue(HotSpotGraalRuntime.runtime().getConfig().useCompressedOops);
071
072        Container c = new Container();
073        c.i = i;
074
075        try (OverrideScope s = OptionValue.override(GraalOptions.OptImplicitNullChecks, false)) {
076            test("testSnippet", c);
077        }
078    }
079
080    @Test
081    public void implicit() {
082        testImplicit(new Integer(1));
083    }
084
085    @Test
086    public void implicitNull() {
087        testImplicit(null);
088    }
089
090    @Test
091    public void explicit() {
092        testExplicit(new Integer(1));
093    }
094
095    @Test
096    public void explicitNull() {
097        testExplicit(null);
098    }
099
100    @Override
101    protected boolean checkMidTierGraph(StructuredGraph graph) {
102        int count = 0;
103        for (IsNullNode isNull : graph.getNodes().filter(IsNullNode.class).snapshot()) {
104            ValueNode value = isNull.getValue();
105            if (value instanceof CompressionNode) {
106                count++;
107                isNull.replaceFirstInput(value, ((CompressionNode) value).getValue());
108            }
109        }
110        Assert.assertEquals("graph should contain exactly one IsNullNode", 1, count);
111        return super.checkMidTierGraph(graph);
112    }
113}