1 /*
   2  * Copyright (c) 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.nodes.CallTargetNode.InvokeKind;
  28 import org.junit.Test;
  29 
  30 public class ConditionalNodeTest extends GraalCompilerTest {
  31 
  32     @SuppressWarnings("unused") private static int sink0;
  33     @SuppressWarnings("unused") private static int sink1;
  34 
  35     @Test
  36     public void test0() {
  37         test("conditionalTest0", 0);
  38         test("conditionalTest0", 1);
  39     }
  40 
  41     public static int conditionalTest0(int a) {
  42         int value;
  43         if (a == 1) {
  44             value = -1;
  45             sink1 = 0;
  46         } else {
  47             value = 6;
  48             sink1 = 1;
  49         }
  50         sink0 = 1;
  51         return Math.max(value, 6);
  52     }
  53 
  54     @Test
  55     public void test1() {
  56         test("conditionalTest1", 0);
  57         test("conditionalTest1", 1);
  58     }
  59 
  60     public static int conditionalTest1(int a) {
  61         int value;
  62         if (a == 1) {
  63             value = -1;
  64             sink1 = 0;
  65         } else {
  66             value = 6;
  67             sink1 = 1;
  68         }
  69         sink0 = 1;
  70         return Math.max(6, value);
  71     }
  72 
  73     @Test
  74     public void test2() {
  75         test("conditionalTest2", 0);
  76         test("conditionalTest2", 1);
  77     }
  78 
  79     public static int conditionalTest2(int a) {
  80         int value;
  81         if (a == 1) {
  82             value = -1;
  83             sink1 = 0;
  84         } else {
  85             value = 6;
  86             sink1 = 1;
  87         }
  88         sink0 = 1;
  89         return Math.min(value, -1);
  90     }
  91 
  92     @Test
  93     public void test3() {
  94         test("conditionalTest3", 0);
  95         test("conditionalTest3", 1);
  96     }
  97 
  98     public static int conditionalTest3(int a) {
  99         int value;
 100         if (a == 1) {
 101             value = -1;
 102             sink1 = 0;
 103         } else {
 104             value = 6;
 105             sink1 = 1;
 106         }
 107         sink0 = 1;
 108         return Math.min(-1, value);
 109     }
 110 
 111     @Test
 112     public void test4() {
 113         test("conditionalTest4", this, 0);
 114         test("conditionalTest4", this, 1);
 115     }
 116 
 117     int a;
 118     InvokeKind b;
 119 
 120     public static int conditionalTest4(ConditionalNodeTest node, int a) {
 121         if (a == 1) {
 122             node.b = InvokeKind.Virtual;
 123         } else {
 124             node.b = InvokeKind.Special;
 125         }
 126         node.a = a;
 127         return a;
 128     }
 129 }