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.Test;
  29 
  30 /**
  31  * Collection of tests for {@link org.graalvm.compiler.phases.common.ConditionalEliminationPhase}
  32  * including those that triggered bugs in this phase.
  33  */
  34 public class ConditionalEliminationTest1 extends ConditionalEliminationTestBase {
  35     protected static int sink3;
  36 
  37     private static final String REFERENCE_SNIPPET = "referenceSnippet";
  38 
  39     @SuppressWarnings("all")
  40     public static void referenceSnippet(int a) {
  41         if (a == 0) {
  42             sink1 = 1;
  43         }
  44         sink0 = 0;
  45     }
  46 
  47     @Test
  48     public void test1() {
  49         testConditionalElimination("test1Snippet", REFERENCE_SNIPPET);
  50     }
  51 
  52     @SuppressWarnings("all")
  53     public static void test1Snippet(int a) {
  54         if (a == 0) {
  55             if (a == 5) {
  56                 sink2 = 100;
  57             }
  58             if (a > 100) {
  59                 if (a == 0) {
  60                     sink3 = 200;
  61                 }
  62             }
  63             if (a != 2) {
  64                 sink1 = 1;
  65             }
  66         }
  67         sink0 = 0;
  68     }
  69 
  70     @Test
  71     public void test2() {
  72         testConditionalElimination("test2Snippet", REFERENCE_SNIPPET);
  73     }
  74 
  75     @SuppressWarnings("all")
  76     public static void test2Snippet(int a) {
  77         if (a == 0) {
  78             if (a > 100) {
  79                 if (a == 0) {
  80                     sink3 = 200;
  81                 }
  82             }
  83             if (a != 2) {
  84                 sink1 = 1;
  85             }
  86         }
  87         sink0 = 0;
  88     }
  89 
  90     @Test
  91     public void test3() {
  92         testConditionalElimination("test3Snippet", REFERENCE_SNIPPET);
  93     }
  94 
  95     @SuppressWarnings("all")
  96     public static void test3Snippet(int a) {
  97         if (a == 0) {
  98             if (a < 1) {
  99                 if (a < 2) {
 100                     if (a < 3) {
 101                         if (a > -1) {
 102                             if (a > -2) {
 103                                 if (a > -3) {
 104                                     if (a == 1) {
 105                                         sink2 = 42;
 106                                     } else {
 107                                         sink1 = 1;
 108                                     }
 109                                 }
 110                             }
 111                         }
 112                     }
 113                 }
 114             }
 115         }
 116         sink0 = 0;
 117     }
 118 
 119     @SuppressWarnings("all")
 120     public static void test4Snippet(int a, int b) {
 121         if (b < 1) {
 122             GraalDirectives.controlFlowAnchor();
 123             if (b < 0) {
 124                 sink1 = 1;
 125             }
 126         }
 127         sink0 = 0;
 128     }
 129 
 130     @Test
 131     public void test4() {
 132         testConditionalElimination("test4Snippet", "test4Snippet");
 133     }
 134 
 135     @SuppressWarnings("all")
 136     public static void test5Snippet(int a, int b) {
 137         if ((b & 3) == 0) {
 138             GraalDirectives.controlFlowAnchor();
 139             if ((b & 7) == 0) {
 140                 GraalDirectives.controlFlowAnchor();
 141                 sink1 = 1;
 142             }
 143         } else {
 144             GraalDirectives.controlFlowAnchor();
 145             if ((b & 1) == 0) {
 146                 GraalDirectives.controlFlowAnchor();
 147                 sink2 = 2;
 148             }
 149         }
 150         sink0 = 0;
 151     }
 152 
 153     @Test
 154     public void test5() {
 155         testConditionalElimination("test5Snippet", "test5Snippet");
 156     }
 157 }