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 
assertBoolEquals(boolean expected, boolean result)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   /*
26    * Test that integer Phis are accepted as Boolean inputs until
27    * we implement a suitable type analysis.
28    */
29 
30   // CHECK-START: boolean Main.TestPhiAsBoolean(int) boolean_simplifier (after)
31   // CHECK-DAG:     [[Phi:i\d+]]     Phi
32   // CHECK-DAG:                      BooleanNot [ [[Phi]] ]
33 
34   public static boolean f1;
35   public static boolean f2;
36 
InlinePhi(int x)37   public static boolean InlinePhi(int x) {
38     return (x == 42) ? f1 : f2;
39   }
40 
TestPhiAsBoolean(int x)41   public static boolean TestPhiAsBoolean(int x) {
42     return InlinePhi(x) != true ? true : false;
43   }
44 
45   /*
46    * Test that integer And is accepted as a Boolean input until
47    * we implement a suitable type analysis.
48    */
49 
50   // CHECK-START: boolean Main.TestAndAsBoolean(boolean, boolean) boolean_simplifier (after)
51   // CHECK-DAG:     [[And:i\d+]]     And
52   // CHECK-DAG:                      BooleanNot [ [[And]] ]
53 
InlineAnd(boolean x, boolean y)54   public static boolean InlineAnd(boolean x, boolean y) {
55     return x & y;
56   }
57 
TestAndAsBoolean(boolean x, boolean y)58   public static boolean TestAndAsBoolean(boolean x, boolean y) {
59     return InlineAnd(x, y) != true ? true : false;
60   }
61 
62   /*
63    * Test that integer Or is accepted as a Boolean input until
64    * we implement a suitable type analysis.
65    */
66 
67   // CHECK-START: boolean Main.TestOrAsBoolean(boolean, boolean) boolean_simplifier (after)
68   // CHECK-DAG:     [[Or:i\d+]]      Or
69   // CHECK-DAG:                      BooleanNot [ [[Or]] ]
70 
InlineOr(boolean x, boolean y)71   public static boolean InlineOr(boolean x, boolean y) {
72     return x | y;
73   }
74 
TestOrAsBoolean(boolean x, boolean y)75   public static boolean TestOrAsBoolean(boolean x, boolean y) {
76     return InlineOr(x, y) != true ? true : false;
77   }
78 
79   /*
80    * Test that integer Xor is accepted as a Boolean input until
81    * we implement a suitable type analysis.
82    */
83 
84   // CHECK-START: boolean Main.TestXorAsBoolean(boolean, boolean) boolean_simplifier (after)
85   // CHECK-DAG:     [[Xor:i\d+]]     Xor
86   // CHECK-DAG:                      BooleanNot [ [[Xor]] ]
87 
InlineXor(boolean x, boolean y)88   public static boolean InlineXor(boolean x, boolean y) {
89     return x ^ y;
90   }
91 
TestXorAsBoolean(boolean x, boolean y)92   public static boolean TestXorAsBoolean(boolean x, boolean y) {
93     return InlineXor(x, y) != true ? true : false;
94   }
95 
main(String[] args)96   public static void main(String[] args) {
97     f1 = true;
98     f2 = false;
99     assertBoolEquals(true, TestPhiAsBoolean(0));
100     assertBoolEquals(false, TestPhiAsBoolean(42));
101     assertBoolEquals(true, TestAndAsBoolean(true, false));
102     assertBoolEquals(false, TestAndAsBoolean(true, true));
103     assertBoolEquals(true, TestOrAsBoolean(false, false));
104     assertBoolEquals(false, TestOrAsBoolean(true, true));
105     assertBoolEquals(true, TestXorAsBoolean(true, true));
106     assertBoolEquals(false, TestXorAsBoolean(true, false));
107   }
108 }
109