1 /*
   2  * Copyright (c) 2015, 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.core.test;
  24 
  25 import org.junit.Test;
  26 
  27 import org.graalvm.compiler.api.directives.GraalDirectives;
  28 
  29 /**
  30  * Collection of tests for
  31  * {@link org.graalvm.compiler.phases.common.DominatorConditionalEliminationPhase} including those
  32  * that triggered bugs in this phase.
  33  */
  34 public class ConditionalEliminationTest5 extends ConditionalEliminationTestBase {
  35 
  36     interface A {
  37     }
  38 
  39     interface B extends A {
  40     }
  41 
  42     static final class DistinctA {
  43     }
  44 
  45     static final class DistinctB {
  46     }
  47 
  48     public static int reference1Snippet(Object a) {
  49         if (a instanceof B) {
  50             return 1;
  51         }
  52         return 2;
  53     }
  54 
  55     public static int test1Snippet(Object a) {
  56         if (a instanceof B) {
  57             if (a instanceof A) {
  58                 return 1;
  59             }
  60         }
  61         return 2;
  62     }
  63 
  64     @Test
  65     public void test1() {
  66         testConditionalElimination("test1Snippet", "reference1Snippet");
  67     }
  68 
  69     public static int reference2Snippet(A a) {
  70         if (a instanceof B) {
  71             return 1;
  72         }
  73         return 2;
  74     }
  75 
  76     public static int test2Snippet(A a) {
  77         if (a instanceof B) {
  78             B newVal = (B) a;
  79             if (newVal != null) {
  80                 return 1;
  81             }
  82         }
  83         return 2;
  84     }
  85 
  86     @Test
  87     public void test2() {
  88         testConditionalElimination("test2Snippet", "reference2Snippet");
  89     }
  90 
  91     @SuppressWarnings("unused")
  92     public static int reference3Snippet(Object a, Object b) {
  93         if (a instanceof DistinctA) {
  94             DistinctA proxyA = (DistinctA) a;
  95             if (b instanceof DistinctB) {
  96                 return 1;
  97             }
  98         }
  99         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 
 121     public static int reference4Snippet(Object a) {
 122         if (!(a instanceof B)) {
 123             GraalDirectives.deoptimize();
 124         }
 125         return 1;
 126     }
 127 
 128     public static int test4Snippet1(Object a) {
 129         if (!(a instanceof B)) {
 130             GraalDirectives.deoptimize();
 131         }
 132         if (!(a instanceof A)) {
 133             GraalDirectives.deoptimize();
 134         }
 135         return 1;
 136     }
 137 
 138     public static int test4Snippet2(Object a) {
 139         if (!(a instanceof A)) {
 140             GraalDirectives.deoptimize();
 141         }
 142         if (!(a instanceof B)) {
 143             GraalDirectives.deoptimize();
 144         }
 145         return 1;
 146     }
 147 
 148     @Test
 149     public void test4() {
 150         testConditionalElimination("test4Snippet1", "reference4Snippet");
 151         testConditionalElimination("test4Snippet2", "reference4Snippet");
 152     }
 153 }