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 ConditionalEliminationTest11 extends ConditionalEliminationTestBase {
  36     @SuppressWarnings("all")
  37     public static int referenceSnippet(int a) {
  38         if ((a & 15) != 15) {
  39             GraalDirectives.deoptimizeAndInvalidate();
  40         }
  41         return 0;
  42     }
  43 
  44     @Test
  45     public void test1() {
  46         testConditionalElimination("test1Snippet", "referenceSnippet");
  47     }
  48 
  49     @SuppressWarnings("all")
  50     public static int test1Snippet(int a) {
  51         if ((a & 8) != 8) {
  52             GraalDirectives.deoptimizeAndInvalidate();
  53         }
  54         if ((a & 15) != 15) {
  55             GraalDirectives.deoptimizeAndInvalidate();
  56         }
  57         return 0;
  58     }
  59 
  60     @SuppressWarnings("all")
  61     public static int test2Snippet(int a) {
  62         if ((a & 8) == 0) {
  63             GraalDirectives.deoptimizeAndInvalidate();
  64         }
  65         if ((a & 15) != 15) {
  66             GraalDirectives.deoptimizeAndInvalidate();
  67         }
  68         return 0;
  69     }
  70 
  71     @Test
  72     public void test2() {
  73         testConditionalElimination("test2Snippet", "referenceSnippet");
  74     }
  75 
  76     @SuppressWarnings("all")
  77     public static int test3Snippet(int a) {
  78         if ((a & 15) != 15) {
  79             GraalDirectives.deoptimizeAndInvalidate();
  80         }
  81         if ((a & 8) != 8) {
  82             GraalDirectives.deoptimizeAndInvalidate();
  83         }
  84         return 0;
  85     }
  86 
  87     @Test
  88     public void test3() {
  89         // Test forward elimination of bitwise tests
  90         testConditionalElimination("test3Snippet", "referenceSnippet");
  91     }
  92 
  93     @SuppressWarnings("all")
  94     public static int test4Snippet(int a) {
  95         if ((a & 15) != 15) {
  96             GraalDirectives.deoptimizeAndInvalidate();
  97         }
  98         if ((a & 8) == 0) {
  99             GraalDirectives.deoptimizeAndInvalidate();
 100         }
 101         return 0;
 102     }
 103 
 104     @Test
 105     public void test4() {
 106         // Test forward elimination of bitwise tests
 107         testConditionalElimination("test4Snippet", "referenceSnippet");
 108     }
 109 
 110     public static int test5Snippet(int a) {
 111         if ((a & 5) == 5) {
 112             GraalDirectives.deoptimizeAndInvalidate();
 113         }
 114         if ((a & 7) != 0) {
 115             return 0;
 116         }
 117         return 1;
 118     }
 119 
 120     @Test
 121     public void test5() {
 122         // Shouldn't be possible to optimize this
 123         testConditionalElimination("test5Snippet", "test5Snippet");
 124     }
 125 
 126     public static int test6Snippet(int a) {
 127         if ((a & 8) != 0) {
 128             GraalDirectives.deoptimize();
 129         }
 130         if ((a & 15) != 15) {
 131             GraalDirectives.deoptimize();
 132         }
 133         return 0;
 134     }
 135 
 136     public static int reference6Snippet(int a) {
 137         if ((a & 8) != 0) {
 138             GraalDirectives.deoptimize();
 139         }
 140         GraalDirectives.deoptimize();
 141         return 0;
 142     }
 143 
 144     @Test
 145     public void test6() {
 146         testConditionalElimination("test6Snippet", "reference6Snippet");
 147     }
 148 
 149     public static int test7Snippet(int a) {
 150         if ((a & 15) == 15) {
 151             GraalDirectives.deoptimizeAndInvalidate();
 152         }
 153         if ((a & 8) == 8) {
 154             GraalDirectives.deoptimizeAndInvalidate();
 155         }
 156         return a;
 157     }
 158 
 159     public static int reference7Snippet(int a) {
 160         if ((a & 8) == 8) {
 161             GraalDirectives.deoptimizeAndInvalidate();
 162         }
 163         return a;
 164     }
 165 
 166     @Test
 167     public void test7() {
 168         testConditionalElimination("test7Snippet", "reference7Snippet");
 169     }
 170 
 171     public static int test8Snippet(int a) {
 172         if ((a & 16) == 16) {
 173             GraalDirectives.deoptimizeAndInvalidate();
 174         }
 175         if ((a & 8) != 8) {
 176             GraalDirectives.deoptimizeAndInvalidate();
 177         }
 178         if ((a & 44) != 44) {
 179             GraalDirectives.deoptimizeAndInvalidate();
 180         }
 181         return a;
 182     }
 183 
 184     public static int reference8Snippet(int a) {
 185         if ((a & 60) != 44) {
 186             GraalDirectives.deoptimizeAndInvalidate();
 187         }
 188         return a;
 189     }
 190 
 191     @Ignore("requires merging of bit tests")
 192     @Test
 193     public void test8() {
 194         testConditionalElimination("test8Snippet", "reference8Snippet");
 195     }
 196 
 197     public static int test9Snippet(int a) {
 198         if ((a & 16) == 16) {
 199             GraalDirectives.deoptimizeAndInvalidate();
 200         }
 201         if ((a & 8) != 8) {
 202             GraalDirectives.deoptimizeAndInvalidate();
 203         }
 204         if ((a & 44) != 44) {
 205             GraalDirectives.deoptimizeAndInvalidate();
 206         }
 207         if (a != 44) {
 208             GraalDirectives.deoptimizeAndInvalidate();
 209         }
 210         return a;
 211     }
 212 
 213     public static int reference9Snippet(int a) {
 214         if (a != 44) {
 215             GraalDirectives.deoptimizeAndInvalidate();
 216         }
 217         return a;
 218     }
 219 
 220     @Test
 221     public void test9() {
 222         testConditionalElimination("test9Snippet", "reference9Snippet");
 223     }
 224 
 225     static class ByteHolder {
 226         public byte b;
 227 
 228         byte byteValue() {
 229             return b;
 230         }
 231     }
 232 
 233     public static int test10Snippet(ByteHolder b) {
 234         int v = b.byteValue();
 235         long a = v & 0xffffffff;
 236         if (v != 44) {
 237             GraalDirectives.deoptimizeAndInvalidate();
 238         }
 239         if ((a & 16) == 16) {
 240             GraalDirectives.deoptimizeAndInvalidate();
 241         }
 242         if ((a & 8) != 8) {
 243             GraalDirectives.deoptimizeAndInvalidate();
 244         }
 245         if ((a & 44) != 44) {
 246             GraalDirectives.deoptimizeAndInvalidate();
 247         }
 248 
 249         return v;
 250     }
 251 
 252     public static int reference10Snippet(ByteHolder b) {
 253         byte v = b.byteValue();
 254         if (v != 44) {
 255             GraalDirectives.deoptimizeAndInvalidate();
 256         }
 257         return v;
 258     }
 259 
 260     @Test
 261     @Ignore
 262     public void test10() {
 263         testConditionalElimination("test10Snippet", "reference10Snippet");
 264     }
 265 
 266     public static int test11Snippet(ByteHolder b) {
 267         int v = b.byteValue();
 268         long a = v & 0xffffffff;
 269 
 270         if ((a & 16) == 16) {
 271             GraalDirectives.deoptimizeAndInvalidate();
 272         }
 273         if ((a & 8) != 8) {
 274             GraalDirectives.deoptimizeAndInvalidate();
 275         }
 276         if ((a & 44) != 44) {
 277             GraalDirectives.deoptimizeAndInvalidate();
 278         }
 279         if (v != 44) {
 280             GraalDirectives.deoptimizeAndInvalidate();
 281         }
 282         return v;
 283     }
 284 
 285     public static int reference11Snippet(ByteHolder b) {
 286         byte v = b.byteValue();
 287         if (v != 44) {
 288             GraalDirectives.deoptimizeAndInvalidate();
 289         }
 290         return v;
 291     }
 292 
 293     @Test
 294     @Ignore
 295     public void test11() {
 296         testConditionalElimination("test11Snippet", "reference11Snippet");
 297     }
 298 
 299 }