David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | public class Main { |
| 18 | |
| 19 | public static void assertBoolEquals(boolean expected, boolean result) { |
| 20 | if (expected != result) { |
| 21 | throw new Error("Expected: " + expected + ", found: " + result); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /* |
David Brazdil | 2fa194b | 2015-04-20 10:14:42 +0100 | [diff] [blame^] | 26 | * Test that zero/one constants are accepted as Boolean inputs. |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 27 | */ |
| 28 | |
David Brazdil | 2fa194b | 2015-04-20 10:14:42 +0100 | [diff] [blame^] | 29 | // CHECK-START: boolean Main.TestConstAsBoolean() inliner (before) |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 30 | // CHECK-DAG: [[Invoke:z\d+]] InvokeStaticOrDirect |
| 31 | // CHECK-DAG: BooleanNot [ [[Invoke]] ] |
| 32 | |
David Brazdil | 2fa194b | 2015-04-20 10:14:42 +0100 | [diff] [blame^] | 33 | // CHECK-START: boolean Main.TestConstAsBoolean() inliner (after) |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 34 | // CHECK-DAG: [[Const:i\d+]] IntConstant 1 |
| 35 | // CHECK-DAG: BooleanNot [ [[Const]] ] |
| 36 | |
| 37 | public static boolean InlineConst() { |
| 38 | return true; |
| 39 | } |
| 40 | |
David Brazdil | 2fa194b | 2015-04-20 10:14:42 +0100 | [diff] [blame^] | 41 | public static boolean TestConstAsBoolean() { |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 42 | return InlineConst() != true ? true : false; |
| 43 | } |
| 44 | |
| 45 | /* |
David Brazdil | 2fa194b | 2015-04-20 10:14:42 +0100 | [diff] [blame^] | 46 | * Test that integer Phis are accepted as Boolean inputs until |
| 47 | * we implement a suitable type analysis. |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 48 | */ |
| 49 | |
| 50 | // CHECK-START: boolean Main.TestPhiAsBoolean(int) inliner (before) |
| 51 | // CHECK-DAG: [[Invoke:z\d+]] InvokeStaticOrDirect |
| 52 | // CHECK-DAG: BooleanNot [ [[Invoke]] ] |
| 53 | |
| 54 | // CHECK-START: boolean Main.TestPhiAsBoolean(int) inliner (after) |
| 55 | // CHECK-DAG: [[Phi:i\d+]] Phi |
| 56 | // CHECK-DAG: BooleanNot [ [[Phi]] ] |
| 57 | |
| 58 | public static boolean f1; |
| 59 | public static boolean f2; |
| 60 | |
| 61 | public static boolean InlinePhi(int x) { |
| 62 | return (x == 42) ? f1 : f2; |
| 63 | } |
| 64 | |
| 65 | public static boolean TestPhiAsBoolean(int x) { |
| 66 | return InlinePhi(x) != true ? true : false; |
| 67 | } |
| 68 | |
David Brazdil | 2fa194b | 2015-04-20 10:14:42 +0100 | [diff] [blame^] | 69 | /* |
| 70 | * Test that integer And is accepted as a Boolean input until |
| 71 | * we implement a suitable type analysis. |
| 72 | */ |
| 73 | |
| 74 | // CHECK-START: boolean Main.TestAndAsBoolean(boolean, boolean) inliner (before) |
| 75 | // CHECK-DAG: [[Invoke:z\d+]] InvokeStaticOrDirect |
| 76 | // CHECK-DAG: BooleanNot [ [[Invoke]] ] |
| 77 | |
| 78 | // CHECK-START: boolean Main.TestAndAsBoolean(boolean, boolean) inliner (after) |
| 79 | // CHECK-DAG: [[And:i\d+]] And |
| 80 | // CHECK-DAG: BooleanNot [ [[And]] ] |
| 81 | |
| 82 | public static boolean InlineAnd(boolean x, boolean y) { |
| 83 | return x & y; |
| 84 | } |
| 85 | |
| 86 | public static boolean TestAndAsBoolean(boolean x, boolean y) { |
| 87 | return InlineAnd(x, y) != true ? true : false; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Test that integer Or is accepted as a Boolean input until |
| 92 | * we implement a suitable type analysis. |
| 93 | */ |
| 94 | |
| 95 | // CHECK-START: boolean Main.TestOrAsBoolean(boolean, boolean) inliner (before) |
| 96 | // CHECK-DAG: [[Invoke:z\d+]] InvokeStaticOrDirect |
| 97 | // CHECK-DAG: BooleanNot [ [[Invoke]] ] |
| 98 | |
| 99 | // CHECK-START: boolean Main.TestOrAsBoolean(boolean, boolean) inliner (after) |
| 100 | // CHECK-DAG: [[Or:i\d+]] Or |
| 101 | // CHECK-DAG: BooleanNot [ [[Or]] ] |
| 102 | |
| 103 | public static boolean InlineOr(boolean x, boolean y) { |
| 104 | return x | y; |
| 105 | } |
| 106 | |
| 107 | public static boolean TestOrAsBoolean(boolean x, boolean y) { |
| 108 | return InlineOr(x, y) != true ? true : false; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Test that integer Xor is accepted as a Boolean input until |
| 113 | * we implement a suitable type analysis. |
| 114 | */ |
| 115 | |
| 116 | // CHECK-START: boolean Main.TestXorAsBoolean(boolean, boolean) inliner (before) |
| 117 | // CHECK-DAG: [[Invoke:z\d+]] InvokeStaticOrDirect |
| 118 | // CHECK-DAG: BooleanNot [ [[Invoke]] ] |
| 119 | |
| 120 | // CHECK-START: boolean Main.TestXorAsBoolean(boolean, boolean) inliner (after) |
| 121 | // CHECK-DAG: [[Xor:i\d+]] Xor |
| 122 | // CHECK-DAG: BooleanNot [ [[Xor]] ] |
| 123 | |
| 124 | public static boolean InlineXor(boolean x, boolean y) { |
| 125 | return x ^ y; |
| 126 | } |
| 127 | |
| 128 | public static boolean TestXorAsBoolean(boolean x, boolean y) { |
| 129 | return InlineXor(x, y) != true ? true : false; |
| 130 | } |
| 131 | |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 132 | public static void main(String[] args) { |
| 133 | f1 = true; |
| 134 | f2 = false; |
David Brazdil | 2fa194b | 2015-04-20 10:14:42 +0100 | [diff] [blame^] | 135 | assertBoolEquals(false, TestConstAsBoolean()); |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 136 | assertBoolEquals(true, TestPhiAsBoolean(0)); |
| 137 | assertBoolEquals(false, TestPhiAsBoolean(42)); |
David Brazdil | 2fa194b | 2015-04-20 10:14:42 +0100 | [diff] [blame^] | 138 | assertBoolEquals(true, TestAndAsBoolean(true, false)); |
| 139 | assertBoolEquals(false, TestAndAsBoolean(true, true)); |
| 140 | assertBoolEquals(true, TestOrAsBoolean(false, false)); |
| 141 | assertBoolEquals(false, TestOrAsBoolean(true, true)); |
| 142 | assertBoolEquals(true, TestXorAsBoolean(true, true)); |
| 143 | assertBoolEquals(false, TestXorAsBoolean(true, false)); |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 144 | } |
| 145 | } |