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