blob: 3f3110fff4887f9395a08e0f1f8859177cad6f20 [file] [log] [blame]
David Brazdil46e2a392015-03-16 17:31:52 +00001/*
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
17public class Main {
18
19 // Note #1: `javac` flips the conditions of If statements.
20 // Note #2: In the optimizing compiler, the first input of Phi is always
21 // the fall-through path, i.e. the false branch.
22
23 public static void assertBoolEquals(boolean expected, boolean result) {
24 if (expected != result) {
25 throw new Error("Expected: " + expected + ", found: " + result);
26 }
27 }
28
David Brazdil769c9e52015-04-27 13:54:09 +010029 public static void assertIntEquals(int expected, int result) {
30 if (expected != result) {
31 throw new Error("Expected: " + expected + ", found: " + result);
32 }
33 }
34
David Brazdil46e2a392015-03-16 17:31:52 +000035 /*
David Brazdil0d13fee2015-04-17 14:52:19 +010036 * Elementary test negating a boolean. Verifies that blocks are merged and
37 * empty branches removed.
David Brazdil46e2a392015-03-16 17:31:52 +000038 */
39
40 // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +010041 // CHECK-DAG: <<Param:z\d+>> ParameterValue
42 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
43 // CHECK-DAG: <<Const1:i\d+>> IntConstant 1
44 // CHECK-DAG: If [ <<Param>> ]
45 // CHECK-DAG: <<Phi:i\d+>> Phi [ <<Const1>> <<Const0>> ]
46 // CHECK-DAG: Return [ <<Phi>> ]
David Brazdil46e2a392015-03-16 17:31:52 +000047
48 // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (before)
49 // CHECK: Goto
50 // CHECK: Goto
51 // CHECK: Goto
52 // CHECK-NOT: Goto
53
54 // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +010055 // CHECK-DAG: <<Param:z\d+>> ParameterValue
56 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
57 // CHECK-DAG: <<NotParam:z\d+>> BooleanNot [ <<Param>> ]
58 // CHECK-DAG: Return [ <<NotParam>> ]
David Brazdil46e2a392015-03-16 17:31:52 +000059
60 // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (after)
David Brazdil46e2a392015-03-16 17:31:52 +000061 // CHECK-NOT: If
62 // CHECK-NOT: Phi
63
64 // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (after)
65 // CHECK: Goto
66 // CHECK-NOT: Goto
67
68 public static boolean BooleanNot(boolean x) {
69 return !x;
70 }
71
72 /*
73 * Program which only delegates the condition, i.e. returns 1 when True
74 * and 0 when False.
75 */
76
77 // CHECK-START: boolean Main.GreaterThan(int, int) boolean_simplifier (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +010078 // CHECK-DAG: <<ParamX:i\d+>> ParameterValue
79 // CHECK-DAG: <<ParamY:i\d+>> ParameterValue
80 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
81 // CHECK-DAG: <<Const1:i\d+>> IntConstant 1
82 // CHECK-DAG: <<Cond:z\d+>> GreaterThan [ <<ParamX>> <<ParamY>> ]
83 // CHECK-DAG: If [ <<Cond>> ]
84 // CHECK-DAG: <<Phi:i\d+>> Phi [ <<Const0>> <<Const1>> ]
85 // CHECK-DAG: Return [ <<Phi>> ]
David Brazdil46e2a392015-03-16 17:31:52 +000086
87 // CHECK-START: boolean Main.GreaterThan(int, int) boolean_simplifier (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +010088 // CHECK-DAG: <<ParamX:i\d+>> ParameterValue
89 // CHECK-DAG: <<ParamY:i\d+>> ParameterValue
90 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
91 // CHECK-DAG: <<Const1:i\d+>> IntConstant 1
92 // CHECK-DAG: <<Cond:z\d+>> GreaterThan [ <<ParamX>> <<ParamY>> ]
93 // CHECK-DAG: Return [ <<Cond>> ]
David Brazdil46e2a392015-03-16 17:31:52 +000094
95 public static boolean GreaterThan(int x, int y) {
96 return (x <= y) ? false : true;
97 }
98
99 /*
100 * Program which negates a condition, i.e. returns 0 when True
101 * and 1 when False.
102 */
103
104 // CHECK-START: boolean Main.LessThan(int, int) boolean_simplifier (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100105 // CHECK-DAG: <<ParamX:i\d+>> ParameterValue
106 // CHECK-DAG: <<ParamY:i\d+>> ParameterValue
107 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
108 // CHECK-DAG: <<Const1:i\d+>> IntConstant 1
109 // CHECK-DAG: <<Cond:z\d+>> GreaterThanOrEqual [ <<ParamX>> <<ParamY>> ]
110 // CHECK-DAG: If [ <<Cond>> ]
111 // CHECK-DAG: <<Phi:i\d+>> Phi [ <<Const1>> <<Const0>> ]
112 // CHECK-DAG: Return [ <<Phi>> ]
David Brazdil46e2a392015-03-16 17:31:52 +0000113
114 // CHECK-START: boolean Main.LessThan(int, int) boolean_simplifier (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100115 // CHECK-DAG: <<ParamX:i\d+>> ParameterValue
116 // CHECK-DAG: <<ParamY:i\d+>> ParameterValue
117 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
118 // CHECK-DAG: <<Const1:i\d+>> IntConstant 1
119 // CHECK-DAG: <<Cond:z\d+>> LessThan [ <<ParamX>> <<ParamY>> ]
120 // CHECK-DAG: Return [ <<Cond>> ]
David Brazdil46e2a392015-03-16 17:31:52 +0000121
David Brazdil0d13fee2015-04-17 14:52:19 +0100122 // CHECK-START: boolean Main.LessThan(int, int) boolean_simplifier (after)
123 // CHECK-NOT: GreaterThanOrEqual
124
David Brazdil46e2a392015-03-16 17:31:52 +0000125 public static boolean LessThan(int x, int y) {
David Brazdilb2bd1c52015-03-25 11:17:37 +0000126 return (x < y) ? true : false;
David Brazdil46e2a392015-03-16 17:31:52 +0000127 }
128
129 /*
130 * Program which further uses negated conditions.
131 * Note that Phis are discovered retrospectively.
132 */
133
134 // CHECK-START: boolean Main.ValuesOrdered(int, int, int) boolean_simplifier (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100135 // CHECK-DAG: <<ParamX:i\d+>> ParameterValue
136 // CHECK-DAG: <<ParamY:i\d+>> ParameterValue
137 // CHECK-DAG: <<ParamZ:i\d+>> ParameterValue
138 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
139 // CHECK-DAG: <<Const1:i\d+>> IntConstant 1
140 // CHECK-DAG: <<CondXY:z\d+>> GreaterThan [ <<ParamX>> <<ParamY>> ]
141 // CHECK-DAG: If [ <<CondXY>> ]
142 // CHECK-DAG: <<CondYZ:z\d+>> GreaterThan [ <<ParamY>> <<ParamZ>> ]
143 // CHECK-DAG: If [ <<CondYZ>> ]
144 // CHECK-DAG: <<CondXYZ:z\d+>> NotEqual [ <<PhiXY:i\d+>> <<PhiYZ:i\d+>> ]
145 // CHECK-DAG: If [ <<CondXYZ>> ]
146 // CHECK-DAG: Return [ <<PhiXYZ:i\d+>> ]
147 // CHECK-DAG: <<PhiXY>> Phi [ <<Const1>> <<Const0>> ]
148 // CHECK-DAG: <<PhiYZ>> Phi [ <<Const1>> <<Const0>> ]
149 // CHECK-DAG: <<PhiXYZ>> Phi [ <<Const1>> <<Const0>> ]
David Brazdil46e2a392015-03-16 17:31:52 +0000150
151 // CHECK-START: boolean Main.ValuesOrdered(int, int, int) boolean_simplifier (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100152 // CHECK-DAG: <<ParamX:i\d+>> ParameterValue
153 // CHECK-DAG: <<ParamY:i\d+>> ParameterValue
154 // CHECK-DAG: <<ParamZ:i\d+>> ParameterValue
155 // CHECK-DAG: <<CmpXY:z\d+>> LessThanOrEqual [ <<ParamX>> <<ParamY>> ]
156 // CHECK-DAG: <<CmpYZ:z\d+>> LessThanOrEqual [ <<ParamY>> <<ParamZ>> ]
157 // CHECK-DAG: <<CmpXYZ:z\d+>> Equal [ <<CmpXY>> <<CmpYZ>> ]
158 // CHECK-DAG: Return [ <<CmpXYZ>> ]
David Brazdil46e2a392015-03-16 17:31:52 +0000159
160 public static boolean ValuesOrdered(int x, int y, int z) {
161 return (x <= y) == (y <= z);
162 }
163
David Brazdil769c9e52015-04-27 13:54:09 +0100164 // CHECK-START: int Main.NegatedCondition(boolean) boolean_simplifier (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100165 // CHECK-DAG: <<Param:z\d+>> ParameterValue
166 // CHECK-DAG: <<Const42:i\d+>> IntConstant 42
167 // CHECK-DAG: <<Const43:i\d+>> IntConstant 43
168 // CHECK-DAG: <<NotParam:z\d+>> BooleanNot [ <<Param>> ]
169 // CHECK-DAG: If [ <<NotParam>> ]
170 // CHECK-DAG: <<Phi:i\d+>> Phi [ <<Const42>> <<Const43>> ]
171 // CHECK-DAG: Return [ <<Phi>> ]
David Brazdil769c9e52015-04-27 13:54:09 +0100172
173 // CHECK-START: int Main.NegatedCondition(boolean) boolean_simplifier (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100174 // CHECK-DAG: <<Param:z\d+>> ParameterValue
175 // CHECK-DAG: <<Const42:i\d+>> IntConstant 42
176 // CHECK-DAG: <<Const43:i\d+>> IntConstant 43
177 // CHECK-DAG: If [ <<Param>> ]
178 // CHECK-DAG: <<Phi:i\d+>> Phi [ <<Const42>> <<Const43>> ]
179 // CHECK-DAG: Return [ <<Phi>> ]
David Brazdil769c9e52015-04-27 13:54:09 +0100180
181 // Note: The fact that branches are swapped is verified by running the test.
182
183 // CHECK-START: int Main.NegatedCondition(boolean) boolean_simplifier (after)
184 // CHECK-NOT: BooleanNot
185
186 public static int NegatedCondition(boolean x) {
187 if (x != false) {
188 return 42;
189 } else {
190 return 43;
191 }
192 }
193
David Brazdil46e2a392015-03-16 17:31:52 +0000194 public static void main(String[] args) {
195 assertBoolEquals(false, BooleanNot(true));
196 assertBoolEquals(true, BooleanNot(false));
197 assertBoolEquals(true, GreaterThan(10, 5));
198 assertBoolEquals(false, GreaterThan(10, 10));
199 assertBoolEquals(false, GreaterThan(5, 10));
200 assertBoolEquals(true, LessThan(5, 10));
201 assertBoolEquals(false, LessThan(10, 10));
202 assertBoolEquals(false, LessThan(10, 5));
203 assertBoolEquals(true, ValuesOrdered(1, 3, 5));
204 assertBoolEquals(true, ValuesOrdered(5, 3, 1));
205 assertBoolEquals(false, ValuesOrdered(1, 3, 2));
206 assertBoolEquals(false, ValuesOrdered(2, 3, 1));
207 assertBoolEquals(true, ValuesOrdered(3, 3, 3));
208 assertBoolEquals(true, ValuesOrdered(3, 3, 5));
209 assertBoolEquals(false, ValuesOrdered(5, 5, 3));
David Brazdil769c9e52015-04-27 13:54:09 +0100210 assertIntEquals(42, NegatedCondition(true));
211 assertIntEquals(43, NegatedCondition(false));
David Brazdil46e2a392015-03-16 17:31:52 +0000212 }
213}