1 /*
   2  * Copyright (c) 2015, 2018, 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 
  24 
  25 package org.graalvm.compiler.core.test;
  26 
  27 import org.graalvm.compiler.api.directives.GraalDirectives;
  28 import org.junit.Ignore;
  29 import org.junit.Test;
  30 
  31 /**
  32  * Collection of tests for {@link org.graalvm.compiler.phases.common.ConditionalEliminationPhase}
  33  * including those that triggered bugs in this phase.
  34  */
  35 public class ConditionalEliminationTest5 extends ConditionalEliminationTestBase {
  36 
  37     interface A {
  38     }
  39 
  40     interface B extends A {
  41     }
  42 
  43     interface C extends B {
  44 
  45     }
  46 
  47     static final class DistinctA {
  48     }
  49 
  50     static final class DistinctB {
  51     }
  52 
  53     public static void reference1Snippet(Object a) {
  54         if (a instanceof B) {
  55             sink1 = 1;
  56         }
  57         sink2 = 2;
  58     }
  59 
  60     public static void test1Snippet(Object a) {
  61         if (a instanceof B) {
  62             if (a instanceof A) {
  63                 sink1 = 1;
  64             }
  65         }
  66         sink2 = 2;
  67     }
  68 
  69     @Test
  70     public void test1() {
  71         testConditionalElimination("test1Snippet", "reference1Snippet");
  72     }
  73 
  74     public static void reference2Snippet(A a) {
  75         if (a instanceof B) {
  76             sink1 = 1;
  77         }
  78         sink2 = 2;
  79     }
  80 
  81     public static void test2Snippet(A a) {
  82         if (a instanceof B) {
  83             B newVal = (B) a;
  84             if (newVal != null) {
  85                 sink1 = 1;
  86             }
  87         }
  88         sink2 = 2;
  89     }
  90 
  91     @Test
  92     public void test2() {
  93         testConditionalElimination("test2Snippet", "reference2Snippet");
  94     }
  95 
  96     @SuppressWarnings("unused")
  97     public static void reference3Snippet(Object a, Object b) {
  98         if (a instanceof DistinctA) {
  99             DistinctA proxyA = (DistinctA) a;
 100             if (b instanceof DistinctB) {
 101                 sink1 = 1;
 102             }
 103         }
 104         sink2 = 2;
 105     }
 106 
 107     @SuppressWarnings("all")
 108     public static void test3Snippet(Object a, Object b) {
 109         if (a instanceof DistinctA) {
 110             DistinctA proxyA = (DistinctA) a;
 111             if (b instanceof DistinctB) {
 112                 if (proxyA == b) {
 113                     sink0 = 42;
 114                 }
 115                 sink1 = 1;
 116             }
 117         }
 118         sink2 = 2;
 119     }
 120 
 121     @Test
 122     public void test3() {
 123         testConditionalElimination("test3Snippet", "reference3Snippet", true, false);
 124     }
 125 
 126     public static void reference4Snippet(Object a) {
 127         if (!(a instanceof B)) {
 128             GraalDirectives.deoptimizeAndInvalidate();
 129         }
 130         sink1 = 1;
 131     }
 132 
 133     public static void test4Snippet1(Object a) {
 134         if (!(a instanceof B)) {
 135             GraalDirectives.deoptimizeAndInvalidate();
 136         }
 137         if (!(a instanceof A)) {
 138             GraalDirectives.deoptimizeAndInvalidate();
 139         }
 140         sink1 = 1;
 141     }
 142 
 143     public static void test4Snippet2(Object a) {
 144         if (!(a instanceof A)) {
 145             GraalDirectives.deoptimizeAndInvalidate();
 146         }
 147         if (!(a instanceof B)) {
 148             GraalDirectives.deoptimizeAndInvalidate();
 149         }
 150         sink1 = 1;
 151     }
 152 
 153     @SuppressWarnings({"cast", "unused"})
 154     public static void test4Snippet3(Object a) {
 155         Object pi = (A) a;
 156         if (!(a instanceof B)) {
 157             GraalDirectives.deoptimizeAndInvalidate();
 158         }
 159         sink1 = 1;
 160     }
 161 
 162     public static void test4Snippet4(Object a) {
 163         if (!(a instanceof A)) {
 164             GraalDirectives.deoptimizeAndInvalidate();
 165         }
 166         if (!(((A) a) instanceof B)) {
 167             GraalDirectives.deoptimizeAndInvalidate();
 168         }
 169         sink1 = 1;
 170     }
 171 
 172     @SuppressWarnings({"cast"})
 173     public static void test4Snippet5(Object a) {
 174         Object pi = (A) a;
 175         if (pi == null) {
 176             GraalDirectives.deoptimizeAndInvalidate();
 177         }
 178         if (!(a instanceof B)) {
 179             GraalDirectives.deoptimizeAndInvalidate();
 180         }
 181         sink1 = 1;
 182     }
 183 
 184     @Test
 185     public void test4() {
 186         testConditionalElimination("test4Snippet1", "reference4Snippet");
 187         testConditionalElimination("test4Snippet2", "reference4Snippet");
 188         testConditionalElimination("test4Snippet3", "reference4Snippet");
 189         testConditionalElimination("test4Snippet5", "reference4Snippet");
 190     }
 191 
 192     @Ignore
 193     @Test
 194     public void test5() {
 195         testConditionalElimination("test4Snippet4", "reference4Snippet", false, true);
 196     }
 197 }