001/*
002 * Copyright (c) 2011, 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.compiler.test;
024
025import com.oracle.graal.debug.*;
026import com.oracle.graal.debug.Debug.*;
027
028import org.junit.*;
029
030import com.oracle.graal.api.directives.*;
031import com.oracle.graal.nodes.*;
032import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
033import com.oracle.graal.nodes.memory.*;
034import com.oracle.graal.nodes.spi.*;
035import com.oracle.graal.phases.*;
036import com.oracle.graal.phases.common.*;
037import com.oracle.graal.phases.tiers.*;
038
039/**
040 * Tests that the hub access and the null check are folded.
041 */
042public class ImplicitNullCheckTest extends GraphScheduleTest {
043
044    public static final class Receiver {
045
046        public int a;
047    }
048
049    public static int test1Snippet(Object o) {
050        if (GraalDirectives.guardingNonNull(o) instanceof Receiver) {
051            return 42;
052        }
053        return 0;
054    }
055
056    @Ignore("temporarily disable until LoadHub lowering is clarified")
057    @Test
058    public void test1() {
059        test("test1Snippet");
060    }
061
062    private void test(final String snippet) {
063        try (Scope s = Debug.scope("FloatingReadTest", new DebugDumpScope(snippet))) {
064            StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
065            PhaseContext context = new PhaseContext(getProviders());
066            new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
067            new FloatingReadPhase().apply(graph);
068            MidTierContext midTierContext = new MidTierContext(getProviders(), getCodeCache().getTarget(), OptimisticOptimizations.ALL, graph.method().getProfilingInfo());
069            new GuardLoweringPhase().apply(graph, midTierContext);
070
071            Assert.assertEquals(0, graph.getNodes(DeoptimizeNode.TYPE).count());
072            Assert.assertTrue(graph.getNodes().filter(ReadNode.class).first().canNullCheck());
073
074        } catch (Throwable e) {
075            throw Debug.handle(e);
076        }
077    }
078}