1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, Arm Limited. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 
  26 
  27 package org.graalvm.compiler.core.aarch64.test;
  28 
  29 import org.graalvm.compiler.lir.LIRInstruction;
  30 import org.graalvm.compiler.lir.aarch64.AArch64ControlFlow;
  31 import org.junit.Test;
  32 
  33 import java.util.function.Predicate;
  34 
  35 public class AArch64CbzTest extends AArch64MatchRuleTest {
  36     private static final Predicate<LIRInstruction> predicate = op -> (op instanceof AArch64ControlFlow.CompareBranchZeroOp);
  37 
  38     public static int equalsTo(int x) {
  39         if (x == 0) {
  40             return 1;
  41         } else {
  42             return x - 1;
  43         }
  44     }
  45 
  46     public static int notEqualsTo(int x) {
  47         if (x != 0) {
  48             return x + 2;
  49         } else {
  50             return 3;
  51         }
  52     }
  53 
  54     public static String isNull(String s) {
  55         if (s == null) {
  56             return "abc";
  57         } else {
  58             return s + "abc";
  59         }
  60     }
  61 
  62     public static String isNotNull(String s) {
  63         if (s != null) {
  64             return s + "abc";
  65         } else {
  66             return "abc";
  67         }
  68     }
  69 
  70     public static String objectEqualsNull(String s1, String s2) {
  71         if (s1.equals(null)) {
  72             return s1 + "abc";
  73         } else {
  74             return s2 + "abd";
  75         }
  76     }
  77 
  78     public static String objectEquals(String s1, String s2) {
  79         if (s1.equals(s2)) {
  80             return s1 + "abc";
  81         } else {
  82             return s2 + "abd";
  83         }
  84     }
  85 
  86     @Test
  87     public void testEqualsTo() {
  88         test("equalsTo", 0);
  89         test("equalsTo", 1);
  90         checkLIR("equalsTo", predicate, 1);
  91     }
  92 
  93     @Test
  94     public void testNotEqualsTo() {
  95         test("notEqualsTo", 0);
  96         test("notEqualsTo", 1);
  97         checkLIR("notEqualsTo", predicate, 1);
  98     }
  99 
 100     @Test
 101     public void testIsNull() {
 102         test("isNull", new Object[]{null});
 103         test("isNull", "abc");
 104         checkLIR("isNull", predicate, 1);
 105     }
 106 
 107     @Test
 108     public void testIsNotNull() {
 109         test("isNotNull", new Object[]{null});
 110         test("isNotNull", "abc");
 111         checkLIR("isNotNull", predicate, 1);
 112     }
 113 
 114     @Test
 115     public void testObjectEqualsNull() {
 116         test("objectEqualsNull", "ab", "ac");
 117         test("objectEqualsNull", "abc", "abc");
 118         checkLIR("objectEqualsNull", predicate, 1);
 119     }
 120 
 121     @Test
 122     public void testObjectEquals() {
 123         test("objectEquals", "ab", "ac");
 124         test("objectEquals", "abc", "abc");
 125         checkLIR("objectEquals", predicate, 0);
 126     }
 127 }