001/*
002 * Copyright (c) 2015, 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 org.junit.*;
026
027/**
028 * Collection of tests for
029 * {@link com.oracle.graal.phases.common.DominatorConditionalEliminationPhase} including those that
030 * triggered bugs in this phase.
031 */
032public class ConditionalEliminationTest5 extends ConditionalEliminationTestBase {
033
034    interface A {
035    }
036
037    interface B extends A {
038    }
039
040    static final class DistinctA {
041    }
042
043    static final class DistinctB {
044    }
045
046    @SuppressWarnings("all")
047    public static int reference1Snippet(A a, B b) {
048        if (a instanceof B) {
049            return 1;
050        }
051        return 2;
052    }
053
054    @SuppressWarnings("all")
055    public static int test1Snippet(A a, B b) {
056        if (a instanceof B) {
057            if (a instanceof A) {
058                return 1;
059            }
060        }
061        return 2;
062    }
063
064    @Test
065    public void test1() {
066        testConditionalElimination("test1Snippet", "reference1Snippet");
067    }
068
069    public static int reference2Snippet(A a) {
070        if (a instanceof B) {
071            return 1;
072        }
073        return 2;
074    }
075
076    public static int test2Snippet(A a) {
077        if (a instanceof B) {
078            B newVal = (B) a;
079            if (newVal != null) {
080                return 1;
081            }
082        }
083        return 2;
084    }
085
086    @Test
087    public void test2() {
088        testConditionalElimination("test2Snippet", "reference2Snippet");
089    }
090
091    @SuppressWarnings("unused")
092    public static int reference3Snippet(Object a, Object b) {
093        if (a instanceof DistinctA) {
094            DistinctA proxyA = (DistinctA) a;
095            if (b instanceof DistinctB) {
096                return 1;
097            }
098        }
099        return 2;
100    }
101
102    @SuppressWarnings("all")
103    public static int test3Snippet(Object a, Object b) {
104        if (a instanceof DistinctA) {
105            DistinctA proxyA = (DistinctA) a;
106            if (b instanceof DistinctB) {
107                if (proxyA == b) {
108                    return 42;
109                }
110                return 1;
111            }
112        }
113        return 2;
114    }
115
116    @Test
117    public void test3() {
118        testConditionalElimination("test3Snippet", "reference3Snippet", true);
119    }
120}