blob: c258db9c4521cdb2b2d68065f307286dfe621965 [file] [log] [blame]
David Brazdilee690a32014-12-01 17:04:16 +00001/*
2* Copyright (C) 2014 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
David Brazdil4846d132015-01-15 19:07:08 +000017public class Main {
David Brazdilee690a32014-12-01 17:04:16 +000018
Roland Levillain3b55ebb2015-05-08 13:13:19 +010019 public static void assertFalse(boolean condition) {
20 if (condition) {
21 throw new Error();
22 }
23 }
24
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000025 public static void assertIntEquals(int expected, int result) {
26 if (expected != result) {
27 throw new Error("Expected: " + expected + ", found: " + result);
28 }
29 }
30
31 public static void assertLongEquals(long expected, long result) {
32 if (expected != result) {
33 throw new Error("Expected: " + expected + ", found: " + result);
34 }
35 }
36
Mark Mendelle82549b2015-05-06 10:55:34 -040037 public static void assertFloatEquals(float expected, float result) {
38 if (expected != result) {
39 throw new Error("Expected: " + expected + ", found: " + result);
40 }
41 }
42
43 public static void assertDoubleEquals(double expected, double result) {
44 if (expected != result) {
45 throw new Error("Expected: " + expected + ", found: " + result);
46 }
47 }
48
David Brazdilee690a32014-12-01 17:04:16 +000049 /**
50 * Tiny three-register program exercising int constant folding
51 * on negation.
52 */
53
David Brazdil4846d132015-01-15 19:07:08 +000054 // CHECK-START: int Main.IntNegation() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +010055 // CHECK-DAG: <<Const42:i\d+>> IntConstant 42
David Brazdilc57397b2015-05-15 16:01:59 +010056 // CHECK-DAG: <<Neg:i\d+>> Neg [<<Const42>>]
57 // CHECK-DAG: Return [<<Neg>>]
David Brazdilee690a32014-12-01 17:04:16 +000058
David Brazdil4846d132015-01-15 19:07:08 +000059 // CHECK-START: int Main.IntNegation() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +010060 // CHECK-DAG: <<ConstN42:i\d+>> IntConstant -42
David Brazdilc57397b2015-05-15 16:01:59 +010061 // CHECK-DAG: Return [<<ConstN42>>]
David Brazdilee690a32014-12-01 17:04:16 +000062
63 public static int IntNegation() {
64 int x, y;
65 x = 42;
66 y = -x;
67 return y;
68 }
69
70 /**
71 * Tiny three-register program exercising int constant folding
72 * on addition.
73 */
74
David Brazdil4846d132015-01-15 19:07:08 +000075 // CHECK-START: int Main.IntAddition1() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +010076 // CHECK-DAG: <<Const1:i\d+>> IntConstant 1
77 // CHECK-DAG: <<Const2:i\d+>> IntConstant 2
David Brazdilc57397b2015-05-15 16:01:59 +010078 // CHECK-DAG: <<Add:i\d+>> Add [<<Const1>>,<<Const2>>]
79 // CHECK-DAG: Return [<<Add>>]
David Brazdilee690a32014-12-01 17:04:16 +000080
David Brazdil4846d132015-01-15 19:07:08 +000081 // CHECK-START: int Main.IntAddition1() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +010082 // CHECK-DAG: <<Const3:i\d+>> IntConstant 3
David Brazdilc57397b2015-05-15 16:01:59 +010083 // CHECK-DAG: Return [<<Const3>>]
David Brazdilee690a32014-12-01 17:04:16 +000084
85 public static int IntAddition1() {
86 int a, b, c;
87 a = 1;
88 b = 2;
89 c = a + b;
90 return c;
91 }
92
93 /**
94 * Small three-register program exercising int constant folding
95 * on addition.
96 */
97
David Brazdil4846d132015-01-15 19:07:08 +000098 // CHECK-START: int Main.IntAddition2() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +010099 // CHECK-DAG: <<Const1:i\d+>> IntConstant 1
100 // CHECK-DAG: <<Const2:i\d+>> IntConstant 2
101 // CHECK-DAG: <<Const5:i\d+>> IntConstant 5
102 // CHECK-DAG: <<Const6:i\d+>> IntConstant 6
David Brazdilc57397b2015-05-15 16:01:59 +0100103 // CHECK-DAG: <<Add1:i\d+>> Add [<<Const1>>,<<Const2>>]
104 // CHECK-DAG: <<Add2:i\d+>> Add [<<Const5>>,<<Const6>>]
105 // CHECK-DAG: <<Add3:i\d+>> Add [<<Add1>>,<<Add2>>]
106 // CHECK-DAG: Return [<<Add3>>]
David Brazdilee690a32014-12-01 17:04:16 +0000107
David Brazdil4846d132015-01-15 19:07:08 +0000108 // CHECK-START: int Main.IntAddition2() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100109 // CHECK-DAG: <<Const14:i\d+>> IntConstant 14
David Brazdilc57397b2015-05-15 16:01:59 +0100110 // CHECK-DAG: Return [<<Const14>>]
David Brazdilee690a32014-12-01 17:04:16 +0000111
112 public static int IntAddition2() {
113 int a, b, c;
114 a = 1;
115 b = 2;
116 a += b;
117 b = 5;
118 c = 6;
119 b += c;
120 c = a + b;
121 return c;
122 }
123
124 /**
125 * Tiny three-register program exercising int constant folding
126 * on subtraction.
127 */
128
David Brazdil4846d132015-01-15 19:07:08 +0000129 // CHECK-START: int Main.IntSubtraction() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100130 // CHECK-DAG: <<Const6:i\d+>> IntConstant 6
131 // CHECK-DAG: <<Const2:i\d+>> IntConstant 2
David Brazdilc57397b2015-05-15 16:01:59 +0100132 // CHECK-DAG: <<Sub:i\d+>> Sub [<<Const6>>,<<Const2>>]
133 // CHECK-DAG: Return [<<Sub>>]
David Brazdilee690a32014-12-01 17:04:16 +0000134
David Brazdil4846d132015-01-15 19:07:08 +0000135 // CHECK-START: int Main.IntSubtraction() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100136 // CHECK-DAG: <<Const4:i\d+>> IntConstant 4
David Brazdilc57397b2015-05-15 16:01:59 +0100137 // CHECK-DAG: Return [<<Const4>>]
David Brazdilee690a32014-12-01 17:04:16 +0000138
139 public static int IntSubtraction() {
140 int a, b, c;
David Brazdil4846d132015-01-15 19:07:08 +0000141 a = 6;
David Brazdilee690a32014-12-01 17:04:16 +0000142 b = 2;
143 c = a - b;
144 return c;
145 }
146
147 /**
148 * Tiny three-register program exercising long constant folding
149 * on addition.
150 */
151
David Brazdil4846d132015-01-15 19:07:08 +0000152 // CHECK-START: long Main.LongAddition() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100153 // CHECK-DAG: <<Const1:j\d+>> LongConstant 1
154 // CHECK-DAG: <<Const2:j\d+>> LongConstant 2
David Brazdilc57397b2015-05-15 16:01:59 +0100155 // CHECK-DAG: <<Add:j\d+>> Add [<<Const1>>,<<Const2>>]
156 // CHECK-DAG: Return [<<Add>>]
David Brazdilee690a32014-12-01 17:04:16 +0000157
David Brazdil4846d132015-01-15 19:07:08 +0000158 // CHECK-START: long Main.LongAddition() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100159 // CHECK-DAG: <<Const3:j\d+>> LongConstant 3
David Brazdilc57397b2015-05-15 16:01:59 +0100160 // CHECK-DAG: Return [<<Const3>>]
David Brazdilee690a32014-12-01 17:04:16 +0000161
162 public static long LongAddition() {
163 long a, b, c;
164 a = 1L;
165 b = 2L;
166 c = a + b;
167 return c;
168 }
169
170 /**
171 * Tiny three-register program exercising long constant folding
172 * on subtraction.
173 */
174
David Brazdil4846d132015-01-15 19:07:08 +0000175 // CHECK-START: long Main.LongSubtraction() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100176 // CHECK-DAG: <<Const6:j\d+>> LongConstant 6
177 // CHECK-DAG: <<Const2:j\d+>> LongConstant 2
David Brazdilc57397b2015-05-15 16:01:59 +0100178 // CHECK-DAG: <<Sub:j\d+>> Sub [<<Const6>>,<<Const2>>]
179 // CHECK-DAG: Return [<<Sub>>]
David Brazdilee690a32014-12-01 17:04:16 +0000180
David Brazdil4846d132015-01-15 19:07:08 +0000181 // CHECK-START: long Main.LongSubtraction() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100182 // CHECK-DAG: <<Const4:j\d+>> LongConstant 4
David Brazdilc57397b2015-05-15 16:01:59 +0100183 // CHECK-DAG: Return [<<Const4>>]
David Brazdilee690a32014-12-01 17:04:16 +0000184
185 public static long LongSubtraction() {
186 long a, b, c;
David Brazdil4846d132015-01-15 19:07:08 +0000187 a = 6L;
David Brazdilee690a32014-12-01 17:04:16 +0000188 b = 2L;
189 c = a - b;
190 return c;
191 }
192
193 /**
194 * Three-register program with a constant (static) condition.
195 */
196
David Brazdil4846d132015-01-15 19:07:08 +0000197 // CHECK-START: int Main.StaticCondition() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100198 // CHECK-DAG: <<Const7:i\d+>> IntConstant 7
199 // CHECK-DAG: <<Const2:i\d+>> IntConstant 2
David Brazdilc57397b2015-05-15 16:01:59 +0100200 // CHECK-DAG: <<Cond:z\d+>> GreaterThanOrEqual [<<Const7>>,<<Const2>>]
201 // CHECK-DAG: If [<<Cond>>]
David Brazdilee690a32014-12-01 17:04:16 +0000202
David Brazdil4846d132015-01-15 19:07:08 +0000203 // CHECK-START: int Main.StaticCondition() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100204 // CHECK-DAG: <<Const1:i\d+>> IntConstant 1
David Brazdilc57397b2015-05-15 16:01:59 +0100205 // CHECK-DAG: If [<<Const1>>]
David Brazdilee690a32014-12-01 17:04:16 +0000206
207 public static int StaticCondition() {
208 int a, b, c;
David Brazdil4846d132015-01-15 19:07:08 +0000209 a = 7;
David Brazdilee690a32014-12-01 17:04:16 +0000210 b = 2;
211 if (a < b)
212 c = a + b;
213 else
214 c = a - b;
215 return c;
216 }
217
218 /**
219 * Four-variable program with jumps leading to the creation of many
220 * blocks.
221 *
222 * The intent of this test is to ensure that all constant expressions
223 * are actually evaluated at compile-time, thanks to the reverse
224 * (forward) post-order traversal of the the dominator tree.
225 */
226
David Brazdil4846d132015-01-15 19:07:08 +0000227 // CHECK-START: int Main.JumpsAndConditionals(boolean) constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100228 // CHECK-DAG: <<Const2:i\d+>> IntConstant 2
229 // CHECK-DAG: <<Const5:i\d+>> IntConstant 5
David Brazdilc57397b2015-05-15 16:01:59 +0100230 // CHECK-DAG: <<Add:i\d+>> Add [<<Const5>>,<<Const2>>]
231 // CHECK-DAG: <<Sub:i\d+>> Sub [<<Const5>>,<<Const2>>]
232 // CHECK-DAG: <<Phi:i\d+>> Phi [<<Add>>,<<Sub>>]
233 // CHECK-DAG: Return [<<Phi>>]
David Brazdilee690a32014-12-01 17:04:16 +0000234
David Brazdil4846d132015-01-15 19:07:08 +0000235 // CHECK-START: int Main.JumpsAndConditionals(boolean) constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100236 // CHECK-DAG: <<Const3:i\d+>> IntConstant 3
237 // CHECK-DAG: <<Const7:i\d+>> IntConstant 7
David Brazdilc57397b2015-05-15 16:01:59 +0100238 // CHECK-DAG: <<Phi:i\d+>> Phi [<<Const7>>,<<Const3>>]
239 // CHECK-DAG: Return [<<Phi>>]
David Brazdilee690a32014-12-01 17:04:16 +0000240
241 public static int JumpsAndConditionals(boolean cond) {
242 int a, b, c;
243 a = 5;
244 b = 2;
245 if (cond)
246 c = a + b;
247 else
248 c = a - b;
249 return c;
250 }
David Brazdil4846d132015-01-15 19:07:08 +0000251
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000252 /**
253 * Test optimizations of arithmetic identities yielding a constant result.
254 */
255
256 // CHECK-START: int Main.And0(int) constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100257 // CHECK-DAG: <<Arg:i\d+>> ParameterValue
258 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
David Brazdilc57397b2015-05-15 16:01:59 +0100259 // CHECK-DAG: <<And:i\d+>> And [<<Arg>>,<<Const0>>]
260 // CHECK-DAG: Return [<<And>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000261
262 // CHECK-START: int Main.And0(int) constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100263 // CHECK-DAG: <<Arg:i\d+>> ParameterValue
264 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000265 // CHECK-NOT: And
David Brazdilc57397b2015-05-15 16:01:59 +0100266 // CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000267
268 public static int And0(int arg) {
269 return arg & 0;
270 }
271
272 // CHECK-START: long Main.Mul0(long) constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100273 // CHECK-DAG: <<Arg:j\d+>> ParameterValue
274 // CHECK-DAG: <<Const0:j\d+>> LongConstant 0
David Brazdilc57397b2015-05-15 16:01:59 +0100275 // CHECK-DAG: <<Mul:j\d+>> Mul [<<Arg>>,<<Const0>>]
276 // CHECK-DAG: Return [<<Mul>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000277
278 // CHECK-START: long Main.Mul0(long) constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100279 // CHECK-DAG: <<Arg:j\d+>> ParameterValue
280 // CHECK-DAG: <<Const0:j\d+>> LongConstant 0
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000281 // CHECK-NOT: Mul
David Brazdilc57397b2015-05-15 16:01:59 +0100282 // CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000283
284 public static long Mul0(long arg) {
285 return arg * 0;
286 }
287
288 // CHECK-START: int Main.OrAllOnes(int) constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100289 // CHECK-DAG: <<Arg:i\d+>> ParameterValue
290 // CHECK-DAG: <<ConstF:i\d+>> IntConstant -1
David Brazdilc57397b2015-05-15 16:01:59 +0100291 // CHECK-DAG: <<Or:i\d+>> Or [<<Arg>>,<<ConstF>>]
292 // CHECK-DAG: Return [<<Or>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000293
294 // CHECK-START: int Main.OrAllOnes(int) constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100295 // CHECK-DAG: <<ConstF:i\d+>> IntConstant -1
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000296 // CHECK-NOT: Or
David Brazdilc57397b2015-05-15 16:01:59 +0100297 // CHECK-DAG: Return [<<ConstF>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000298
299 public static int OrAllOnes(int arg) {
300 return arg | -1;
301 }
302
303 // CHECK-START: long Main.Rem0(long) constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100304 // CHECK-DAG: <<Arg:j\d+>> ParameterValue
305 // CHECK-DAG: <<Const0:j\d+>> LongConstant 0
David Brazdilc57397b2015-05-15 16:01:59 +0100306 // CHECK-DAG: <<DivZeroCheck:j\d+>> DivZeroCheck [<<Arg>>]
307 // CHECK-DAG: <<Rem:j\d+>> Rem [<<Const0>>,<<DivZeroCheck>>]
308 // CHECK-DAG: Return [<<Rem>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000309
310 // CHECK-START: long Main.Rem0(long) constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100311 // CHECK-DAG: <<Const0:j\d+>> LongConstant 0
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000312 // CHECK-NOT: Rem
David Brazdilc57397b2015-05-15 16:01:59 +0100313 // CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000314
315 public static long Rem0(long arg) {
316 return 0 % arg;
317 }
318
319 // CHECK-START: int Main.Rem1(int) constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100320 // CHECK-DAG: <<Arg:i\d+>> ParameterValue
321 // CHECK-DAG: <<Const1:i\d+>> IntConstant 1
David Brazdilc57397b2015-05-15 16:01:59 +0100322 // CHECK-DAG: <<Rem:i\d+>> Rem [<<Arg>>,<<Const1>>]
323 // CHECK-DAG: Return [<<Rem>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000324
325 // CHECK-START: int Main.Rem1(int) constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100326 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000327 // CHECK-NOT: Rem
David Brazdilc57397b2015-05-15 16:01:59 +0100328 // CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000329
330 public static int Rem1(int arg) {
331 return arg % 1;
332 }
333
334 // CHECK-START: long Main.RemN1(long) constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100335 // CHECK-DAG: <<Arg:j\d+>> ParameterValue
336 // CHECK-DAG: <<ConstN1:j\d+>> LongConstant -1
David Brazdilc57397b2015-05-15 16:01:59 +0100337 // CHECK-DAG: <<DivZeroCheck:j\d+>> DivZeroCheck [<<ConstN1>>]
338 // CHECK-DAG: <<Rem:j\d+>> Rem [<<Arg>>,<<DivZeroCheck>>]
339 // CHECK-DAG: Return [<<Rem>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000340
341 // CHECK-START: long Main.RemN1(long) constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100342 // CHECK-DAG: <<Const0:j\d+>> LongConstant 0
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000343 // CHECK-NOT: Rem
David Brazdilc57397b2015-05-15 16:01:59 +0100344 // CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000345
346 public static long RemN1(long arg) {
347 return arg % -1;
348 }
349
350 // CHECK-START: int Main.Shl0(int) constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100351 // CHECK-DAG: <<Arg:i\d+>> ParameterValue
352 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
David Brazdilc57397b2015-05-15 16:01:59 +0100353 // CHECK-DAG: <<Shl:i\d+>> Shl [<<Const0>>,<<Arg>>]
354 // CHECK-DAG: Return [<<Shl>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000355
356 // CHECK-START: int Main.Shl0(int) constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100357 // CHECK-DAG: <<Arg:i\d+>> ParameterValue
358 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000359 // CHECK-NOT: Shl
David Brazdilc57397b2015-05-15 16:01:59 +0100360 // CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000361
362 public static int Shl0(int arg) {
363 return 0 << arg;
364 }
365
366 // CHECK-START: long Main.Shr0(int) constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100367 // CHECK-DAG: <<Arg:i\d+>> ParameterValue
368 // CHECK-DAG: <<Const0:j\d+>> LongConstant 0
David Brazdilc57397b2015-05-15 16:01:59 +0100369 // CHECK-DAG: <<Shr:j\d+>> Shr [<<Const0>>,<<Arg>>]
370 // CHECK-DAG: Return [<<Shr>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000371
372 // CHECK-START: long Main.Shr0(int) constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100373 // CHECK-DAG: <<Arg:i\d+>> ParameterValue
374 // CHECK-DAG: <<Const0:j\d+>> LongConstant 0
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000375 // CHECK-NOT: Shr
David Brazdilc57397b2015-05-15 16:01:59 +0100376 // CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000377
378 public static long Shr0(int arg) {
379 return (long)0 >> arg;
380 }
381
382 // CHECK-START: long Main.SubSameLong(long) constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100383 // CHECK-DAG: <<Arg:j\d+>> ParameterValue
David Brazdilc57397b2015-05-15 16:01:59 +0100384 // CHECK-DAG: <<Sub:j\d+>> Sub [<<Arg>>,<<Arg>>]
385 // CHECK-DAG: Return [<<Sub>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000386
387 // CHECK-START: long Main.SubSameLong(long) constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100388 // CHECK-DAG: <<Arg:j\d+>> ParameterValue
389 // CHECK-DAG: <<Const0:j\d+>> LongConstant 0
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000390 // CHECK-NOT: Sub
David Brazdilc57397b2015-05-15 16:01:59 +0100391 // CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000392
393 public static long SubSameLong(long arg) {
394 return arg - arg;
395 }
396
397 // CHECK-START: int Main.UShr0(int) constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100398 // CHECK-DAG: <<Arg:i\d+>> ParameterValue
399 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
David Brazdilc57397b2015-05-15 16:01:59 +0100400 // CHECK-DAG: <<UShr:i\d+>> UShr [<<Const0>>,<<Arg>>]
401 // CHECK-DAG: Return [<<UShr>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000402
403 // CHECK-START: int Main.UShr0(int) constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100404 // CHECK-DAG: <<Arg:i\d+>> ParameterValue
405 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000406 // CHECK-NOT: UShr
David Brazdilc57397b2015-05-15 16:01:59 +0100407 // CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000408
409 public static int UShr0(int arg) {
410 return 0 >>> arg;
411 }
412
413 // CHECK-START: int Main.XorSameInt(int) constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100414 // CHECK-DAG: <<Arg:i\d+>> ParameterValue
David Brazdilc57397b2015-05-15 16:01:59 +0100415 // CHECK-DAG: <<Xor:i\d+>> Xor [<<Arg>>,<<Arg>>]
416 // CHECK-DAG: Return [<<Xor>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000417
418 // CHECK-START: int Main.XorSameInt(int) constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100419 // CHECK-DAG: <<Arg:i\d+>> ParameterValue
420 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000421 // CHECK-NOT: Xor
David Brazdilc57397b2015-05-15 16:01:59 +0100422 // CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000423
424 public static int XorSameInt(int arg) {
425 return arg ^ arg;
426 }
427
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100428 // CHECK-START: boolean Main.CmpFloatGreaterThanNaN(float) constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100429 // CHECK-DAG: <<Arg:f\d+>> ParameterValue
430 // CHECK-DAG: <<ConstNan:f\d+>> FloatConstant nan
431 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100432 // CHECK-DAG: IntConstant 1
David Brazdilc57397b2015-05-15 16:01:59 +0100433 // CHECK-DAG: <<Cmp:i\d+>> Compare [<<Arg>>,<<ConstNan>>]
434 // CHECK-DAG: <<Le:z\d+>> LessThanOrEqual [<<Cmp>>,<<Const0>>]
435 // CHECK-DAG: If [<<Le>>]
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100436
437 // CHECK-START: boolean Main.CmpFloatGreaterThanNaN(float) constant_folding (after)
438 // CHECK-DAG: ParameterValue
439 // CHECK-DAG: FloatConstant nan
440 // CHECK-DAG: IntConstant 0
David Brazdilc2c48ff2015-05-15 14:24:31 +0100441 // CHECK-DAG: <<Const1:i\d+>> IntConstant 1
David Brazdilc57397b2015-05-15 16:01:59 +0100442 // CHECK-DAG: If [<<Const1>>]
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100443
444 // CHECK-START: boolean Main.CmpFloatGreaterThanNaN(float) constant_folding (after)
445 // CHECK-NOT: Compare
446 // CHECK-NOT: LessThanOrEqual
447
448 public static boolean CmpFloatGreaterThanNaN(float arg) {
449 return arg > Float.NaN;
450 }
451
452 // CHECK-START: boolean Main.CmpDoubleLessThanNaN(double) constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100453 // CHECK-DAG: <<Arg:d\d+>> ParameterValue
454 // CHECK-DAG: <<ConstNan:d\d+>> DoubleConstant nan
455 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100456 // CHECK-DAG: IntConstant 1
David Brazdilc57397b2015-05-15 16:01:59 +0100457 // CHECK-DAG: <<Cmp:i\d+>> Compare [<<Arg>>,<<ConstNan>>]
458 // CHECK-DAG: <<Ge:z\d+>> GreaterThanOrEqual [<<Cmp>>,<<Const0>>]
459 // CHECK-DAG: If [<<Ge>>]
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100460
461 // CHECK-START: boolean Main.CmpDoubleLessThanNaN(double) constant_folding (after)
462 // CHECK-DAG: ParameterValue
463 // CHECK-DAG: DoubleConstant nan
464 // CHECK-DAG: IntConstant 0
David Brazdilc2c48ff2015-05-15 14:24:31 +0100465 // CHECK-DAG: <<Const1:i\d+>> IntConstant 1
David Brazdilc57397b2015-05-15 16:01:59 +0100466 // CHECK-DAG: If [<<Const1>>]
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100467
468 // CHECK-START: boolean Main.CmpDoubleLessThanNaN(double) constant_folding (after)
469 // CHECK-NOT: Compare
470 // CHECK-NOT: GreaterThanOrEqual
471
472 public static boolean CmpDoubleLessThanNaN(double arg) {
473 return arg < Double.NaN;
474 }
475
Mark Mendelle82549b2015-05-06 10:55:34 -0400476 // CHECK-START: int Main.ReturnInt33() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100477 // CHECK-DAG: <<Const33:j\d+>> LongConstant 33
David Brazdilc57397b2015-05-15 16:01:59 +0100478 // CHECK-DAG: <<Convert:i\d+>> TypeConversion [<<Const33>>]
479 // CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400480
481 // CHECK-START: int Main.ReturnInt33() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100482 // CHECK-DAG: <<Const33:i\d+>> IntConstant 33
David Brazdilc57397b2015-05-15 16:01:59 +0100483 // CHECK-DAG: Return [<<Const33>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400484
485 public static int ReturnInt33() {
486 long imm = 33L;
487 return (int) imm;
488 }
489
490 // CHECK-START: int Main.ReturnIntMax() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100491 // CHECK-DAG: <<ConstMax:f\d+>> FloatConstant 1e+34
David Brazdilc57397b2015-05-15 16:01:59 +0100492 // CHECK-DAG: <<Convert:i\d+>> TypeConversion [<<ConstMax>>]
493 // CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400494
495 // CHECK-START: int Main.ReturnIntMax() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100496 // CHECK-DAG: <<ConstMax:i\d+>> IntConstant 2147483647
David Brazdilc57397b2015-05-15 16:01:59 +0100497 // CHECK-DAG: Return [<<ConstMax>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400498
499 public static int ReturnIntMax() {
500 float imm = 1.0e34f;
501 return (int) imm;
502 }
503
504 // CHECK-START: int Main.ReturnInt0() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100505 // CHECK-DAG: <<ConstNaN:d\d+>> DoubleConstant nan
David Brazdilc57397b2015-05-15 16:01:59 +0100506 // CHECK-DAG: <<Convert:i\d+>> TypeConversion [<<ConstNaN>>]
507 // CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400508
509 // CHECK-START: int Main.ReturnInt0() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100510 // CHECK-DAG: <<Const0:i\d+>> IntConstant 0
David Brazdilc57397b2015-05-15 16:01:59 +0100511 // CHECK-DAG: Return [<<Const0>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400512
513 public static int ReturnInt0() {
514 double imm = Double.NaN;
515 return (int) imm;
516 }
517
518 // CHECK-START: long Main.ReturnLong33() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100519 // CHECK-DAG: <<Const33:i\d+>> IntConstant 33
David Brazdilc57397b2015-05-15 16:01:59 +0100520 // CHECK-DAG: <<Convert:j\d+>> TypeConversion [<<Const33>>]
521 // CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400522
523 // CHECK-START: long Main.ReturnLong33() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100524 // CHECK-DAG: <<Const33:j\d+>> LongConstant 33
David Brazdilc57397b2015-05-15 16:01:59 +0100525 // CHECK-DAG: Return [<<Const33>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400526
527 public static long ReturnLong33() {
528 int imm = 33;
529 return (long) imm;
530 }
531
532 // CHECK-START: long Main.ReturnLong34() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100533 // CHECK-DAG: <<Const34:f\d+>> FloatConstant 34
David Brazdilc57397b2015-05-15 16:01:59 +0100534 // CHECK-DAG: <<Convert:j\d+>> TypeConversion [<<Const34>>]
535 // CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400536
537 // CHECK-START: long Main.ReturnLong34() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100538 // CHECK-DAG: <<Const34:j\d+>> LongConstant 34
David Brazdilc57397b2015-05-15 16:01:59 +0100539 // CHECK-DAG: Return [<<Const34>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400540
541 public static long ReturnLong34() {
542 float imm = 34.0f;
543 return (long) imm;
544 }
545
546 // CHECK-START: long Main.ReturnLong0() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100547 // CHECK-DAG: <<ConstNaN:d\d+>> DoubleConstant nan
David Brazdilc57397b2015-05-15 16:01:59 +0100548 // CHECK-DAG: <<Convert:j\d+>> TypeConversion [<<ConstNaN>>]
549 // CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400550
551 // CHECK-START: long Main.ReturnLong0() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100552 // CHECK-DAG: <<Const0:j\d+>> LongConstant 0
David Brazdilc57397b2015-05-15 16:01:59 +0100553 // CHECK-DAG: Return [<<Const0>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400554
555 public static long ReturnLong0() {
556 double imm = -Double.NaN;
557 return (long) imm;
558 }
559
560 // CHECK-START: float Main.ReturnFloat33() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100561 // CHECK-DAG: <<Const33:i\d+>> IntConstant 33
David Brazdilc57397b2015-05-15 16:01:59 +0100562 // CHECK-DAG: <<Convert:f\d+>> TypeConversion [<<Const33>>]
563 // CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400564
565 // CHECK-START: float Main.ReturnFloat33() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100566 // CHECK-DAG: <<Const33:f\d+>> FloatConstant 33
David Brazdilc57397b2015-05-15 16:01:59 +0100567 // CHECK-DAG: Return [<<Const33>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400568
569 public static float ReturnFloat33() {
570 int imm = 33;
571 return (float) imm;
572 }
573
574 // CHECK-START: float Main.ReturnFloat34() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100575 // CHECK-DAG: <<Const34:j\d+>> LongConstant 34
David Brazdilc57397b2015-05-15 16:01:59 +0100576 // CHECK-DAG: <<Convert:f\d+>> TypeConversion [<<Const34>>]
577 // CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400578
579 // CHECK-START: float Main.ReturnFloat34() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100580 // CHECK-DAG: <<Const34:f\d+>> FloatConstant 34
David Brazdilc57397b2015-05-15 16:01:59 +0100581 // CHECK-DAG: Return [<<Const34>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400582
583 public static float ReturnFloat34() {
584 long imm = 34L;
585 return (float) imm;
586 }
587
588 // CHECK-START: float Main.ReturnFloat99P25() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100589 // CHECK-DAG: <<Const:d\d+>> DoubleConstant 99.25
David Brazdilc57397b2015-05-15 16:01:59 +0100590 // CHECK-DAG: <<Convert:f\d+>> TypeConversion [<<Const>>]
591 // CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400592
593 // CHECK-START: float Main.ReturnFloat99P25() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100594 // CHECK-DAG: <<Const:f\d+>> FloatConstant 99.25
David Brazdilc57397b2015-05-15 16:01:59 +0100595 // CHECK-DAG: Return [<<Const>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400596
597 public static float ReturnFloat99P25() {
598 double imm = 99.25;
599 return (float) imm;
600 }
601
602 // CHECK-START: double Main.ReturnDouble33() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100603 // CHECK-DAG: <<Const33:i\d+>> IntConstant 33
David Brazdilc57397b2015-05-15 16:01:59 +0100604 // CHECK-DAG: <<Convert:d\d+>> TypeConversion [<<Const33>>]
605 // CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400606
607 // CHECK-START: double Main.ReturnDouble33() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100608 // CHECK-DAG: <<Const33:d\d+>> DoubleConstant 33
David Brazdilc57397b2015-05-15 16:01:59 +0100609 // CHECK-DAG: Return [<<Const33>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400610
611 public static double ReturnDouble33() {
612 int imm = 33;
613 return (double) imm;
614 }
615
616 // CHECK-START: double Main.ReturnDouble34() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100617 // CHECK-DAG: <<Const34:j\d+>> LongConstant 34
David Brazdilc57397b2015-05-15 16:01:59 +0100618 // CHECK-DAG: <<Convert:d\d+>> TypeConversion [<<Const34>>]
619 // CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400620
621 // CHECK-START: double Main.ReturnDouble34() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100622 // CHECK-DAG: <<Const34:d\d+>> DoubleConstant 34
David Brazdilc57397b2015-05-15 16:01:59 +0100623 // CHECK-DAG: Return [<<Const34>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400624
625 public static double ReturnDouble34() {
626 long imm = 34L;
627 return (double) imm;
628 }
629
630 // CHECK-START: double Main.ReturnDouble99P25() constant_folding (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100631 // CHECK-DAG: <<Const:f\d+>> FloatConstant 99.25
David Brazdilc57397b2015-05-15 16:01:59 +0100632 // CHECK-DAG: <<Convert:d\d+>> TypeConversion [<<Const>>]
633 // CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400634
635 // CHECK-START: double Main.ReturnDouble99P25() constant_folding (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +0100636 // CHECK-DAG: <<Const:d\d+>> DoubleConstant 99.25
David Brazdilc57397b2015-05-15 16:01:59 +0100637 // CHECK-DAG: Return [<<Const>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400638
639 public static double ReturnDouble99P25() {
640 float imm = 99.25f;
641 return (double) imm;
642 }
643
David Brazdil4846d132015-01-15 19:07:08 +0000644 public static void main(String[] args) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000645 assertIntEquals(IntNegation(), -42);
646 assertIntEquals(IntAddition1(), 3);
647 assertIntEquals(IntAddition2(), 14);
648 assertIntEquals(IntSubtraction(), 4);
649 assertLongEquals(LongAddition(), 3L);
650 assertLongEquals(LongSubtraction(), 4L);
651 assertIntEquals(StaticCondition(), 5);
652 assertIntEquals(JumpsAndConditionals(true), 7);
653 assertIntEquals(JumpsAndConditionals(false), 3);
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100654 int arbitrary = 123456; // Value chosen arbitrarily.
655 assertIntEquals(And0(arbitrary), 0);
656 assertLongEquals(Mul0(arbitrary), 0);
657 assertIntEquals(OrAllOnes(arbitrary), -1);
658 assertLongEquals(Rem0(arbitrary), 0);
659 assertIntEquals(Rem1(arbitrary), 0);
660 assertLongEquals(RemN1(arbitrary), 0);
661 assertIntEquals(Shl0(arbitrary), 0);
662 assertLongEquals(Shr0(arbitrary), 0);
663 assertLongEquals(SubSameLong(arbitrary), 0);
664 assertIntEquals(UShr0(arbitrary), 0);
665 assertIntEquals(XorSameInt(arbitrary), 0);
666 assertFalse(CmpFloatGreaterThanNaN(arbitrary));
667 assertFalse(CmpDoubleLessThanNaN(arbitrary));
Mark Mendelle82549b2015-05-06 10:55:34 -0400668 assertIntEquals(ReturnInt33(), 33);
669 assertIntEquals(ReturnIntMax(), 2147483647);
670 assertIntEquals(ReturnInt0(), 0);
671 assertLongEquals(ReturnLong33(), 33);
672 assertLongEquals(ReturnLong34(), 34);
673 assertLongEquals(ReturnLong0(), 0);
674 assertFloatEquals(ReturnFloat33(), 33);
675 assertFloatEquals(ReturnFloat34(), 34);
676 assertFloatEquals(ReturnFloat99P25(), 99.25f);
677 assertDoubleEquals(ReturnDouble33(), 33);
678 assertDoubleEquals(ReturnDouble34(), 34);
679 assertDoubleEquals(ReturnDouble99P25(), 99.25);
David Brazdil4846d132015-01-15 19:07:08 +0000680 }
David Brazdilee690a32014-12-01 17:04:16 +0000681}