blob: b7863be6cebed61d8a03925181bcea50a6339026 [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 Brazdila06d66a2015-05-28 11:14:54 +010054 /// CHECK-START: int Main.IntNegation() constant_folding (before)
55 /// CHECK-DAG: <<Const42:i\d+>> IntConstant 42
56 /// CHECK-DAG: <<Neg:i\d+>> Neg [<<Const42>>]
57 /// CHECK-DAG: Return [<<Neg>>]
David Brazdilee690a32014-12-01 17:04:16 +000058
David Brazdila06d66a2015-05-28 11:14:54 +010059 /// CHECK-START: int Main.IntNegation() constant_folding (after)
60 /// CHECK-DAG: <<ConstN42:i\d+>> IntConstant -42
61 /// 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 Brazdila06d66a2015-05-28 11:14:54 +010075 /// CHECK-START: int Main.IntAddition1() constant_folding (before)
76 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
77 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
78 /// CHECK-DAG: <<Add:i\d+>> Add [<<Const1>>,<<Const2>>]
79 /// CHECK-DAG: Return [<<Add>>]
David Brazdilee690a32014-12-01 17:04:16 +000080
David Brazdila06d66a2015-05-28 11:14:54 +010081 /// CHECK-START: int Main.IntAddition1() constant_folding (after)
82 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
83 /// 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 Brazdila06d66a2015-05-28 11:14:54 +010098 /// CHECK-START: int Main.IntAddition2() constant_folding (before)
99 /// 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
103 /// 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 Brazdila06d66a2015-05-28 11:14:54 +0100108 /// CHECK-START: int Main.IntAddition2() constant_folding (after)
109 /// CHECK-DAG: <<Const14:i\d+>> IntConstant 14
110 /// 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 Brazdila06d66a2015-05-28 11:14:54 +0100129 /// CHECK-START: int Main.IntSubtraction() constant_folding (before)
130 /// CHECK-DAG: <<Const6:i\d+>> IntConstant 6
131 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
132 /// CHECK-DAG: <<Sub:i\d+>> Sub [<<Const6>>,<<Const2>>]
133 /// CHECK-DAG: Return [<<Sub>>]
David Brazdilee690a32014-12-01 17:04:16 +0000134
David Brazdila06d66a2015-05-28 11:14:54 +0100135 /// CHECK-START: int Main.IntSubtraction() constant_folding (after)
136 /// CHECK-DAG: <<Const4:i\d+>> IntConstant 4
137 /// 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 Brazdila06d66a2015-05-28 11:14:54 +0100152 /// CHECK-START: long Main.LongAddition() constant_folding (before)
153 /// CHECK-DAG: <<Const1:j\d+>> LongConstant 1
154 /// CHECK-DAG: <<Const2:j\d+>> LongConstant 2
155 /// CHECK-DAG: <<Add:j\d+>> Add [<<Const1>>,<<Const2>>]
156 /// CHECK-DAG: Return [<<Add>>]
David Brazdilee690a32014-12-01 17:04:16 +0000157
David Brazdila06d66a2015-05-28 11:14:54 +0100158 /// CHECK-START: long Main.LongAddition() constant_folding (after)
159 /// CHECK-DAG: <<Const3:j\d+>> LongConstant 3
160 /// 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 Brazdila06d66a2015-05-28 11:14:54 +0100175 /// CHECK-START: long Main.LongSubtraction() constant_folding (before)
176 /// CHECK-DAG: <<Const6:j\d+>> LongConstant 6
177 /// CHECK-DAG: <<Const2:j\d+>> LongConstant 2
178 /// CHECK-DAG: <<Sub:j\d+>> Sub [<<Const6>>,<<Const2>>]
179 /// CHECK-DAG: Return [<<Sub>>]
David Brazdilee690a32014-12-01 17:04:16 +0000180
David Brazdila06d66a2015-05-28 11:14:54 +0100181 /// CHECK-START: long Main.LongSubtraction() constant_folding (after)
182 /// CHECK-DAG: <<Const4:j\d+>> LongConstant 4
183 /// 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 Brazdila06d66a2015-05-28 11:14:54 +0100197 /// CHECK-START: int Main.StaticCondition() constant_folding (before)
198 /// CHECK-DAG: <<Const7:i\d+>> IntConstant 7
199 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
200 /// CHECK-DAG: <<Cond:z\d+>> GreaterThanOrEqual [<<Const7>>,<<Const2>>]
201 /// CHECK-DAG: If [<<Cond>>]
David Brazdilee690a32014-12-01 17:04:16 +0000202
David Brazdila06d66a2015-05-28 11:14:54 +0100203 /// CHECK-START: int Main.StaticCondition() constant_folding (after)
204 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
205 /// 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 Brazdila06d66a2015-05-28 11:14:54 +0100227 /// CHECK-START: int Main.JumpsAndConditionals(boolean) constant_folding (before)
228 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
229 /// CHECK-DAG: <<Const5:i\d+>> IntConstant 5
230 /// 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 Brazdila06d66a2015-05-28 11:14:54 +0100235 /// CHECK-START: int Main.JumpsAndConditionals(boolean) constant_folding (after)
236 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
237 /// CHECK-DAG: <<Const7:i\d+>> IntConstant 7
238 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100256 /// CHECK-START: int Main.And0(int) constant_folding (before)
257 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
258 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
259 /// CHECK-DAG: <<And:i\d+>> And [<<Arg>>,<<Const0>>]
260 /// CHECK-DAG: Return [<<And>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000261
David Brazdila06d66a2015-05-28 11:14:54 +0100262 /// CHECK-START: int Main.And0(int) constant_folding (after)
263 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
264 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
265 /// CHECK-NOT: And
266 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100272 /// CHECK-START: long Main.Mul0(long) constant_folding (before)
273 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
274 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
275 /// CHECK-DAG: <<Mul:j\d+>> Mul [<<Arg>>,<<Const0>>]
276 /// CHECK-DAG: Return [<<Mul>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000277
David Brazdila06d66a2015-05-28 11:14:54 +0100278 /// CHECK-START: long Main.Mul0(long) constant_folding (after)
279 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
280 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
281 /// CHECK-NOT: Mul
282 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100288 /// CHECK-START: int Main.OrAllOnes(int) constant_folding (before)
289 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
290 /// CHECK-DAG: <<ConstF:i\d+>> IntConstant -1
291 /// CHECK-DAG: <<Or:i\d+>> Or [<<Arg>>,<<ConstF>>]
292 /// CHECK-DAG: Return [<<Or>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000293
David Brazdila06d66a2015-05-28 11:14:54 +0100294 /// CHECK-START: int Main.OrAllOnes(int) constant_folding (after)
295 /// CHECK-DAG: <<ConstF:i\d+>> IntConstant -1
296 /// CHECK-NOT: Or
297 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100303 /// CHECK-START: long Main.Rem0(long) constant_folding (before)
304 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
305 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
306 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100310 /// CHECK-START: long Main.Rem0(long) constant_folding (after)
311 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
312 /// CHECK-NOT: Rem
313 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100319 /// CHECK-START: int Main.Rem1(int) constant_folding (before)
320 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
321 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
322 /// CHECK-DAG: <<Rem:i\d+>> Rem [<<Arg>>,<<Const1>>]
323 /// CHECK-DAG: Return [<<Rem>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000324
David Brazdila06d66a2015-05-28 11:14:54 +0100325 /// CHECK-START: int Main.Rem1(int) constant_folding (after)
326 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
327 /// CHECK-NOT: Rem
328 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100334 /// CHECK-START: long Main.RemN1(long) constant_folding (before)
335 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
336 /// CHECK-DAG: <<ConstN1:j\d+>> LongConstant -1
337 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100341 /// CHECK-START: long Main.RemN1(long) constant_folding (after)
342 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
343 /// CHECK-NOT: Rem
344 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100350 /// CHECK-START: int Main.Shl0(int) constant_folding (before)
351 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
352 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
353 /// CHECK-DAG: <<Shl:i\d+>> Shl [<<Const0>>,<<Arg>>]
354 /// CHECK-DAG: Return [<<Shl>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000355
David Brazdila06d66a2015-05-28 11:14:54 +0100356 /// CHECK-START: int Main.Shl0(int) constant_folding (after)
357 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
358 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
359 /// CHECK-NOT: Shl
360 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100366 /// CHECK-START: long Main.Shr0(int) constant_folding (before)
367 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
368 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
369 /// CHECK-DAG: <<Shr:j\d+>> Shr [<<Const0>>,<<Arg>>]
370 /// CHECK-DAG: Return [<<Shr>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000371
David Brazdila06d66a2015-05-28 11:14:54 +0100372 /// CHECK-START: long Main.Shr0(int) constant_folding (after)
373 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
374 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
375 /// CHECK-NOT: Shr
376 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100382 /// CHECK-START: long Main.SubSameLong(long) constant_folding (before)
383 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
384 /// CHECK-DAG: <<Sub:j\d+>> Sub [<<Arg>>,<<Arg>>]
385 /// CHECK-DAG: Return [<<Sub>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000386
David Brazdila06d66a2015-05-28 11:14:54 +0100387 /// CHECK-START: long Main.SubSameLong(long) constant_folding (after)
388 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
389 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
390 /// CHECK-NOT: Sub
391 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100397 /// CHECK-START: int Main.UShr0(int) constant_folding (before)
398 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
399 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
400 /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Const0>>,<<Arg>>]
401 /// CHECK-DAG: Return [<<UShr>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000402
David Brazdila06d66a2015-05-28 11:14:54 +0100403 /// CHECK-START: int Main.UShr0(int) constant_folding (after)
404 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
405 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
406 /// CHECK-NOT: UShr
407 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100413 /// CHECK-START: int Main.XorSameInt(int) constant_folding (before)
414 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
415 /// CHECK-DAG: <<Xor:i\d+>> Xor [<<Arg>>,<<Arg>>]
416 /// CHECK-DAG: Return [<<Xor>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000417
David Brazdila06d66a2015-05-28 11:14:54 +0100418 /// CHECK-START: int Main.XorSameInt(int) constant_folding (after)
419 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
420 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
421 /// CHECK-NOT: Xor
422 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100428 /// CHECK-START: boolean Main.CmpFloatGreaterThanNaN(float) constant_folding (before)
429 /// CHECK-DAG: <<Arg:f\d+>> ParameterValue
430 /// CHECK-DAG: <<ConstNan:f\d+>> FloatConstant nan
431 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
432 /// CHECK-DAG: IntConstant 1
433 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100437 /// CHECK-START: boolean Main.CmpFloatGreaterThanNaN(float) constant_folding (after)
438 /// CHECK-DAG: ParameterValue
439 /// CHECK-DAG: FloatConstant nan
440 /// CHECK-DAG: IntConstant 0
441 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
442 /// CHECK-DAG: If [<<Const1>>]
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100443
David Brazdila06d66a2015-05-28 11:14:54 +0100444 /// CHECK-START: boolean Main.CmpFloatGreaterThanNaN(float) constant_folding (after)
445 /// CHECK-NOT: Compare
446 /// CHECK-NOT: LessThanOrEqual
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100447
448 public static boolean CmpFloatGreaterThanNaN(float arg) {
449 return arg > Float.NaN;
450 }
451
David Brazdila06d66a2015-05-28 11:14:54 +0100452 /// CHECK-START: boolean Main.CmpDoubleLessThanNaN(double) constant_folding (before)
453 /// CHECK-DAG: <<Arg:d\d+>> ParameterValue
454 /// CHECK-DAG: <<ConstNan:d\d+>> DoubleConstant nan
455 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
456 /// CHECK-DAG: IntConstant 1
457 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100461 /// CHECK-START: boolean Main.CmpDoubleLessThanNaN(double) constant_folding (after)
462 /// CHECK-DAG: ParameterValue
463 /// CHECK-DAG: DoubleConstant nan
464 /// CHECK-DAG: IntConstant 0
465 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
466 /// CHECK-DAG: If [<<Const1>>]
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100467
David Brazdila06d66a2015-05-28 11:14:54 +0100468 /// CHECK-START: boolean Main.CmpDoubleLessThanNaN(double) constant_folding (after)
469 /// CHECK-NOT: Compare
470 /// CHECK-NOT: GreaterThanOrEqual
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100471
472 public static boolean CmpDoubleLessThanNaN(double arg) {
473 return arg < Double.NaN;
474 }
475
David Brazdila06d66a2015-05-28 11:14:54 +0100476 /// CHECK-START: int Main.ReturnInt33() constant_folding (before)
477 /// CHECK-DAG: <<Const33:j\d+>> LongConstant 33
478 /// CHECK-DAG: <<Convert:i\d+>> TypeConversion [<<Const33>>]
479 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400480
David Brazdila06d66a2015-05-28 11:14:54 +0100481 /// CHECK-START: int Main.ReturnInt33() constant_folding (after)
482 /// CHECK-DAG: <<Const33:i\d+>> IntConstant 33
483 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100490 /// CHECK-START: int Main.ReturnIntMax() constant_folding (before)
491 /// CHECK-DAG: <<ConstMax:f\d+>> FloatConstant 1e+34
492 /// CHECK-DAG: <<Convert:i\d+>> TypeConversion [<<ConstMax>>]
493 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400494
David Brazdila06d66a2015-05-28 11:14:54 +0100495 /// CHECK-START: int Main.ReturnIntMax() constant_folding (after)
496 /// CHECK-DAG: <<ConstMax:i\d+>> IntConstant 2147483647
497 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100504 /// CHECK-START: int Main.ReturnInt0() constant_folding (before)
505 /// CHECK-DAG: <<ConstNaN:d\d+>> DoubleConstant nan
506 /// CHECK-DAG: <<Convert:i\d+>> TypeConversion [<<ConstNaN>>]
507 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400508
David Brazdila06d66a2015-05-28 11:14:54 +0100509 /// CHECK-START: int Main.ReturnInt0() constant_folding (after)
510 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
511 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100518 /// CHECK-START: long Main.ReturnLong33() constant_folding (before)
519 /// CHECK-DAG: <<Const33:i\d+>> IntConstant 33
520 /// CHECK-DAG: <<Convert:j\d+>> TypeConversion [<<Const33>>]
521 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400522
David Brazdila06d66a2015-05-28 11:14:54 +0100523 /// CHECK-START: long Main.ReturnLong33() constant_folding (after)
524 /// CHECK-DAG: <<Const33:j\d+>> LongConstant 33
525 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100532 /// CHECK-START: long Main.ReturnLong34() constant_folding (before)
533 /// CHECK-DAG: <<Const34:f\d+>> FloatConstant 34
534 /// CHECK-DAG: <<Convert:j\d+>> TypeConversion [<<Const34>>]
535 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400536
David Brazdila06d66a2015-05-28 11:14:54 +0100537 /// CHECK-START: long Main.ReturnLong34() constant_folding (after)
538 /// CHECK-DAG: <<Const34:j\d+>> LongConstant 34
539 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100546 /// CHECK-START: long Main.ReturnLong0() constant_folding (before)
547 /// CHECK-DAG: <<ConstNaN:d\d+>> DoubleConstant nan
548 /// CHECK-DAG: <<Convert:j\d+>> TypeConversion [<<ConstNaN>>]
549 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400550
David Brazdila06d66a2015-05-28 11:14:54 +0100551 /// CHECK-START: long Main.ReturnLong0() constant_folding (after)
552 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
553 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100560 /// CHECK-START: float Main.ReturnFloat33() constant_folding (before)
561 /// CHECK-DAG: <<Const33:i\d+>> IntConstant 33
562 /// CHECK-DAG: <<Convert:f\d+>> TypeConversion [<<Const33>>]
563 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400564
David Brazdila06d66a2015-05-28 11:14:54 +0100565 /// CHECK-START: float Main.ReturnFloat33() constant_folding (after)
566 /// CHECK-DAG: <<Const33:f\d+>> FloatConstant 33
567 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100574 /// CHECK-START: float Main.ReturnFloat34() constant_folding (before)
575 /// CHECK-DAG: <<Const34:j\d+>> LongConstant 34
576 /// CHECK-DAG: <<Convert:f\d+>> TypeConversion [<<Const34>>]
577 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400578
David Brazdila06d66a2015-05-28 11:14:54 +0100579 /// CHECK-START: float Main.ReturnFloat34() constant_folding (after)
580 /// CHECK-DAG: <<Const34:f\d+>> FloatConstant 34
581 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100588 /// CHECK-START: float Main.ReturnFloat99P25() constant_folding (before)
589 /// CHECK-DAG: <<Const:d\d+>> DoubleConstant 99.25
590 /// CHECK-DAG: <<Convert:f\d+>> TypeConversion [<<Const>>]
591 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400592
David Brazdila06d66a2015-05-28 11:14:54 +0100593 /// CHECK-START: float Main.ReturnFloat99P25() constant_folding (after)
594 /// CHECK-DAG: <<Const:f\d+>> FloatConstant 99.25
595 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100602 /// CHECK-START: double Main.ReturnDouble33() constant_folding (before)
603 /// CHECK-DAG: <<Const33:i\d+>> IntConstant 33
604 /// CHECK-DAG: <<Convert:d\d+>> TypeConversion [<<Const33>>]
605 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400606
David Brazdila06d66a2015-05-28 11:14:54 +0100607 /// CHECK-START: double Main.ReturnDouble33() constant_folding (after)
608 /// CHECK-DAG: <<Const33:d\d+>> DoubleConstant 33
609 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100616 /// CHECK-START: double Main.ReturnDouble34() constant_folding (before)
617 /// CHECK-DAG: <<Const34:j\d+>> LongConstant 34
618 /// CHECK-DAG: <<Convert:d\d+>> TypeConversion [<<Const34>>]
619 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400620
David Brazdila06d66a2015-05-28 11:14:54 +0100621 /// CHECK-START: double Main.ReturnDouble34() constant_folding (after)
622 /// CHECK-DAG: <<Const34:d\d+>> DoubleConstant 34
623 /// 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
David Brazdila06d66a2015-05-28 11:14:54 +0100630 /// CHECK-START: double Main.ReturnDouble99P25() constant_folding (before)
631 /// CHECK-DAG: <<Const:f\d+>> FloatConstant 99.25
632 /// CHECK-DAG: <<Convert:d\d+>> TypeConversion [<<Const>>]
633 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400634
David Brazdila06d66a2015-05-28 11:14:54 +0100635 /// CHECK-START: double Main.ReturnDouble99P25() constant_folding (after)
636 /// CHECK-DAG: <<Const:d\d+>> DoubleConstant 99.25
637 /// 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}