blob: ebde3bf84573cc02d470f68395922f796d0d71f1 [file] [log] [blame]
Mingyao Yang8df69d42015-10-22 15:40:58 -07001/*
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
17class Circle {
18 Circle(double radius) {
19 this.radius = radius;
20 }
Mingyao Yange58bdca2016-10-28 11:07:24 -070021 public double getRadius() {
22 return radius;
23 }
Mingyao Yang8df69d42015-10-22 15:40:58 -070024 public double getArea() {
25 return radius * radius * Math.PI;
26 }
27 private double radius;
Mingyao Yangfb8464a2015-11-02 10:56:59 -080028}
Mingyao Yang8df69d42015-10-22 15:40:58 -070029
30class TestClass {
Mingyao Yang8ab1d642015-12-03 14:11:15 -080031 static {
32 sTestClassObj = new TestClass(-1, -2);
33 }
Mingyao Yang8df69d42015-10-22 15:40:58 -070034 TestClass() {
35 }
36 TestClass(int i, int j) {
37 this.i = i;
38 this.j = j;
39 }
40 int i;
41 int j;
42 volatile int k;
43 TestClass next;
Mingyao Yangfb8464a2015-11-02 10:56:59 -080044 String str;
Mingyao Yang8df69d42015-10-22 15:40:58 -070045 static int si;
Mingyao Yang8ab1d642015-12-03 14:11:15 -080046 static TestClass sTestClassObj;
Mingyao Yangfb8464a2015-11-02 10:56:59 -080047}
Mingyao Yang8df69d42015-10-22 15:40:58 -070048
49class SubTestClass extends TestClass {
50 int k;
Mingyao Yangfb8464a2015-11-02 10:56:59 -080051}
Mingyao Yang8df69d42015-10-22 15:40:58 -070052
53class TestClass2 {
54 int i;
55 int j;
Mingyao Yangfb8464a2015-11-02 10:56:59 -080056}
57
David Brazdilecf52df2015-12-14 16:58:08 +000058class TestClass3 {
59 float floatField = 8.0f;
60 boolean test1 = true;
61}
62
Mingyao Yangfb8464a2015-11-02 10:56:59 -080063class Finalizable {
64 static boolean sVisited = false;
Mingyao Yang025c1a62017-10-30 11:19:57 -070065 static final int VALUE1 = 0xbeef;
66 static final int VALUE2 = 0xcafe;
Mingyao Yangfb8464a2015-11-02 10:56:59 -080067 int i;
68
69 protected void finalize() {
Mingyao Yang025c1a62017-10-30 11:19:57 -070070 if (i != VALUE1) {
Mingyao Yangfb8464a2015-11-02 10:56:59 -080071 System.out.println("Where is the beef?");
72 }
73 sVisited = true;
74 }
75}
Mingyao Yang8df69d42015-10-22 15:40:58 -070076
Mingyao Yang062157f2016-03-02 10:15:36 -080077interface Filter {
78 public boolean isValid(int i);
79}
80
Mingyao Yang8df69d42015-10-22 15:40:58 -070081public class Main {
82
83 /// CHECK-START: double Main.calcCircleArea(double) load_store_elimination (before)
84 /// CHECK: NewInstance
85 /// CHECK: InstanceFieldSet
86 /// CHECK: InstanceFieldGet
87
88 /// CHECK-START: double Main.calcCircleArea(double) load_store_elimination (after)
Mingyao Yang062157f2016-03-02 10:15:36 -080089 /// CHECK-NOT: NewInstance
Mingyao Yangfb8464a2015-11-02 10:56:59 -080090 /// CHECK-NOT: InstanceFieldSet
Mingyao Yang8df69d42015-10-22 15:40:58 -070091 /// CHECK-NOT: InstanceFieldGet
92
93 static double calcCircleArea(double radius) {
94 return new Circle(radius).getArea();
95 }
96
97 /// CHECK-START: int Main.test1(TestClass, TestClass) load_store_elimination (before)
98 /// CHECK: InstanceFieldSet
99 /// CHECK: InstanceFieldSet
100 /// CHECK: InstanceFieldGet
101 /// CHECK: InstanceFieldGet
102
103 /// CHECK-START: int Main.test1(TestClass, TestClass) load_store_elimination (after)
104 /// CHECK: InstanceFieldSet
105 /// CHECK: InstanceFieldSet
106 /// CHECK-NOT: NullCheck
107 /// CHECK-NOT: InstanceFieldGet
108
109 // Different fields shouldn't alias.
110 static int test1(TestClass obj1, TestClass obj2) {
111 obj1.i = 1;
112 obj2.j = 2;
113 return obj1.i + obj2.j;
114 }
115
116 /// CHECK-START: int Main.test2(TestClass) load_store_elimination (before)
117 /// CHECK: InstanceFieldSet
118 /// CHECK: InstanceFieldSet
119 /// CHECK: InstanceFieldGet
120
121 /// CHECK-START: int Main.test2(TestClass) load_store_elimination (after)
122 /// CHECK: InstanceFieldSet
123 /// CHECK-NOT: NullCheck
124 /// CHECK-NOT: InstanceFieldSet
125 /// CHECK-NOT: InstanceFieldGet
126
127 // Redundant store of the same value.
128 static int test2(TestClass obj) {
129 obj.j = 1;
130 obj.j = 1;
131 return obj.j;
132 }
133
134 /// CHECK-START: int Main.test3(TestClass) load_store_elimination (before)
Mingyao Yang8ab1d642015-12-03 14:11:15 -0800135 /// CHECK: StaticFieldGet
136 /// CHECK: NewInstance
137 /// CHECK: InstanceFieldSet
138 /// CHECK: InstanceFieldSet
Mingyao Yang8df69d42015-10-22 15:40:58 -0700139 /// CHECK: InstanceFieldSet
140 /// CHECK: InstanceFieldSet
141 /// CHECK: InstanceFieldGet
142 /// CHECK: InstanceFieldGet
143 /// CHECK: InstanceFieldGet
144 /// CHECK: InstanceFieldGet
145
146 /// CHECK-START: int Main.test3(TestClass) load_store_elimination (after)
Mingyao Yang8ab1d642015-12-03 14:11:15 -0800147 /// CHECK: StaticFieldGet
148 /// CHECK: NewInstance
149 /// CHECK: InstanceFieldSet
150 /// CHECK: InstanceFieldSet
151 /// CHECK: InstanceFieldSet
152 /// CHECK: InstanceFieldSet
Mingyao Yang8df69d42015-10-22 15:40:58 -0700153 /// CHECK-NOT: InstanceFieldGet
Mingyao Yang8ab1d642015-12-03 14:11:15 -0800154 /// CHECK-NOT: StaticFieldGet
Mingyao Yang8df69d42015-10-22 15:40:58 -0700155
Mingyao Yang8ab1d642015-12-03 14:11:15 -0800156 // A new allocation (even non-singleton) shouldn't alias with pre-existing values.
Mingyao Yang8df69d42015-10-22 15:40:58 -0700157 static int test3(TestClass obj) {
Mingyao Yang8ab1d642015-12-03 14:11:15 -0800158 TestClass obj1 = TestClass.sTestClassObj;
159 TestClass obj2 = new TestClass(); // Cannot alias with obj or obj1 which pre-exist.
160 obj.next = obj2; // Make obj2 a non-singleton.
161 // All stores below need to stay since obj/obj1/obj2 are not singletons.
Mingyao Yang8df69d42015-10-22 15:40:58 -0700162 obj.i = 1;
Mingyao Yang8ab1d642015-12-03 14:11:15 -0800163 obj1.j = 2;
164 // Following stores won't kill values of obj.i and obj1.j.
Mingyao Yang8df69d42015-10-22 15:40:58 -0700165 obj2.i = 3;
166 obj2.j = 4;
Mingyao Yang8ab1d642015-12-03 14:11:15 -0800167 return obj.i + obj1.j + obj2.i + obj2.j;
Mingyao Yang8df69d42015-10-22 15:40:58 -0700168 }
169
170 /// CHECK-START: int Main.test4(TestClass, boolean) load_store_elimination (before)
171 /// CHECK: InstanceFieldSet
172 /// CHECK: InstanceFieldGet
173 /// CHECK: Return
174 /// CHECK: InstanceFieldSet
175
176 /// CHECK-START: int Main.test4(TestClass, boolean) load_store_elimination (after)
177 /// CHECK: InstanceFieldSet
178 /// CHECK-NOT: NullCheck
179 /// CHECK-NOT: InstanceFieldGet
180 /// CHECK: Return
181 /// CHECK: InstanceFieldSet
182
183 // Set and merge the same value in two branches.
184 static int test4(TestClass obj, boolean b) {
185 if (b) {
186 obj.i = 1;
187 } else {
188 obj.i = 1;
189 }
190 return obj.i;
191 }
192
193 /// CHECK-START: int Main.test5(TestClass, boolean) load_store_elimination (before)
194 /// CHECK: InstanceFieldSet
195 /// CHECK: InstanceFieldGet
196 /// CHECK: Return
197 /// CHECK: InstanceFieldSet
198
199 /// CHECK-START: int Main.test5(TestClass, boolean) load_store_elimination (after)
200 /// CHECK: InstanceFieldSet
201 /// CHECK: InstanceFieldGet
202 /// CHECK: Return
203 /// CHECK: InstanceFieldSet
204
205 // Set and merge different values in two branches.
206 static int test5(TestClass obj, boolean b) {
207 if (b) {
208 obj.i = 1;
209 } else {
210 obj.i = 2;
211 }
212 return obj.i;
213 }
214
215 /// CHECK-START: int Main.test6(TestClass, TestClass, boolean) load_store_elimination (before)
216 /// CHECK: InstanceFieldSet
217 /// CHECK: InstanceFieldSet
218 /// CHECK: InstanceFieldSet
219 /// CHECK: InstanceFieldGet
220 /// CHECK: InstanceFieldGet
221
222 /// CHECK-START: int Main.test6(TestClass, TestClass, boolean) load_store_elimination (after)
223 /// CHECK: InstanceFieldSet
224 /// CHECK: InstanceFieldSet
225 /// CHECK: InstanceFieldSet
226 /// CHECK: InstanceFieldGet
227 /// CHECK-NOT: NullCheck
228 /// CHECK-NOT: InstanceFieldGet
229
230 // Setting the same value doesn't clear the value for aliased locations.
231 static int test6(TestClass obj1, TestClass obj2, boolean b) {
232 obj1.i = 1;
233 obj1.j = 2;
234 if (b) {
235 obj2.j = 2;
236 }
237 return obj1.j + obj2.j;
238 }
239
240 /// CHECK-START: int Main.test7(TestClass) load_store_elimination (before)
241 /// CHECK: InstanceFieldSet
242 /// CHECK: InstanceFieldGet
243
244 /// CHECK-START: int Main.test7(TestClass) load_store_elimination (after)
245 /// CHECK: InstanceFieldSet
246 /// CHECK: InstanceFieldGet
247
248 // Invocation should kill values in non-singleton heap locations.
249 static int test7(TestClass obj) {
250 obj.i = 1;
251 System.out.print("");
252 return obj.i;
253 }
254
255 /// CHECK-START: int Main.test8() load_store_elimination (before)
256 /// CHECK: NewInstance
257 /// CHECK: InstanceFieldSet
258 /// CHECK: InvokeVirtual
259 /// CHECK: InstanceFieldGet
260
261 /// CHECK-START: int Main.test8() load_store_elimination (after)
Mingyao Yang062157f2016-03-02 10:15:36 -0800262 /// CHECK-NOT: NewInstance
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800263 /// CHECK-NOT: InstanceFieldSet
Mingyao Yang8df69d42015-10-22 15:40:58 -0700264 /// CHECK: InvokeVirtual
265 /// CHECK-NOT: NullCheck
266 /// CHECK-NOT: InstanceFieldGet
267
268 // Invocation should not kill values in singleton heap locations.
269 static int test8() {
270 TestClass obj = new TestClass();
271 obj.i = 1;
272 System.out.print("");
273 return obj.i;
274 }
275
276 /// CHECK-START: int Main.test9(TestClass) load_store_elimination (before)
277 /// CHECK: NewInstance
278 /// CHECK: InstanceFieldSet
279 /// CHECK: InstanceFieldSet
280 /// CHECK: InstanceFieldGet
281
282 /// CHECK-START: int Main.test9(TestClass) load_store_elimination (after)
283 /// CHECK: NewInstance
284 /// CHECK: InstanceFieldSet
285 /// CHECK: InstanceFieldSet
286 /// CHECK: InstanceFieldGet
287
288 // Invocation should kill values in non-singleton heap locations.
289 static int test9(TestClass obj) {
290 TestClass obj2 = new TestClass();
291 obj2.i = 1;
292 obj.next = obj2;
293 System.out.print("");
294 return obj2.i;
295 }
296
297 /// CHECK-START: int Main.test10(TestClass) load_store_elimination (before)
298 /// CHECK: StaticFieldGet
299 /// CHECK: InstanceFieldGet
300 /// CHECK: StaticFieldSet
301 /// CHECK: InstanceFieldGet
302
303 /// CHECK-START: int Main.test10(TestClass) load_store_elimination (after)
304 /// CHECK: StaticFieldGet
305 /// CHECK: InstanceFieldGet
306 /// CHECK: StaticFieldSet
307 /// CHECK-NOT: NullCheck
308 /// CHECK-NOT: InstanceFieldGet
309
310 // Static fields shouldn't alias with instance fields.
311 static int test10(TestClass obj) {
312 TestClass.si += obj.i;
313 return obj.i;
314 }
315
316 /// CHECK-START: int Main.test11(TestClass) load_store_elimination (before)
317 /// CHECK: InstanceFieldSet
318 /// CHECK: InstanceFieldGet
319
320 /// CHECK-START: int Main.test11(TestClass) load_store_elimination (after)
321 /// CHECK: InstanceFieldSet
322 /// CHECK-NOT: NullCheck
323 /// CHECK-NOT: InstanceFieldGet
324
325 // Loop without heap writes.
326 // obj.i is actually hoisted to the loop pre-header by licm already.
327 static int test11(TestClass obj) {
328 obj.i = 1;
329 int sum = 0;
330 for (int i = 0; i < 10; i++) {
331 sum += obj.i;
332 }
333 return sum;
334 }
335
336 /// CHECK-START: int Main.test12(TestClass, TestClass) load_store_elimination (before)
337 /// CHECK: InstanceFieldSet
338 /// CHECK: InstanceFieldGet
339 /// CHECK: InstanceFieldSet
340
341 /// CHECK-START: int Main.test12(TestClass, TestClass) load_store_elimination (after)
342 /// CHECK: InstanceFieldSet
343 /// CHECK: InstanceFieldGet
344 /// CHECK: InstanceFieldSet
345
346 // Loop with heap writes.
347 static int test12(TestClass obj1, TestClass obj2) {
348 obj1.i = 1;
349 int sum = 0;
350 for (int i = 0; i < 10; i++) {
351 sum += obj1.i;
352 obj2.i = sum;
353 }
354 return sum;
355 }
356
357 /// CHECK-START: int Main.test13(TestClass, TestClass2) load_store_elimination (before)
358 /// CHECK: InstanceFieldSet
359 /// CHECK: InstanceFieldSet
360 /// CHECK: InstanceFieldGet
361 /// CHECK: InstanceFieldGet
362
363 /// CHECK-START: int Main.test13(TestClass, TestClass2) load_store_elimination (after)
364 /// CHECK: InstanceFieldSet
365 /// CHECK: InstanceFieldSet
366 /// CHECK-NOT: NullCheck
367 /// CHECK-NOT: InstanceFieldGet
368
369 // Different classes shouldn't alias.
370 static int test13(TestClass obj1, TestClass2 obj2) {
371 obj1.i = 1;
372 obj2.i = 2;
373 return obj1.i + obj2.i;
374 }
375
376 /// CHECK-START: int Main.test14(TestClass, SubTestClass) load_store_elimination (before)
377 /// CHECK: InstanceFieldSet
378 /// CHECK: InstanceFieldSet
379 /// CHECK: InstanceFieldGet
380
381 /// CHECK-START: int Main.test14(TestClass, SubTestClass) load_store_elimination (after)
382 /// CHECK: InstanceFieldSet
383 /// CHECK: InstanceFieldSet
384 /// CHECK: InstanceFieldGet
385
386 // Subclass may alias with super class.
387 static int test14(TestClass obj1, SubTestClass obj2) {
388 obj1.i = 1;
389 obj2.i = 2;
390 return obj1.i;
391 }
392
393 /// CHECK-START: int Main.test15() load_store_elimination (before)
394 /// CHECK: StaticFieldSet
395 /// CHECK: StaticFieldSet
396 /// CHECK: StaticFieldGet
397
398 /// CHECK-START: int Main.test15() load_store_elimination (after)
399 /// CHECK: <<Const2:i\d+>> IntConstant 2
400 /// CHECK: StaticFieldSet
Mingyao Yang8df69d42015-10-22 15:40:58 -0700401 /// CHECK-NOT: StaticFieldGet
402 /// CHECK: Return [<<Const2>>]
403
404 // Static field access from subclass's name.
405 static int test15() {
406 TestClass.si = 1;
407 SubTestClass.si = 2;
408 return TestClass.si;
409 }
410
411 /// CHECK-START: int Main.test16() load_store_elimination (before)
412 /// CHECK: NewInstance
413 /// CHECK: InstanceFieldSet
414 /// CHECK: InstanceFieldSet
415 /// CHECK: InstanceFieldGet
416 /// CHECK: InstanceFieldGet
417
418 /// CHECK-START: int Main.test16() load_store_elimination (after)
Mingyao Yang062157f2016-03-02 10:15:36 -0800419 /// CHECK-NOT: NewInstance
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800420 /// CHECK-NOT: InstanceFieldSet
421 /// CHECK-NOT: InstanceFieldGet
Mingyao Yang8df69d42015-10-22 15:40:58 -0700422
423 // Test inlined constructor.
424 static int test16() {
425 TestClass obj = new TestClass(1, 2);
426 return obj.i + obj.j;
427 }
428
429 /// CHECK-START: int Main.test17() load_store_elimination (before)
430 /// CHECK: NewInstance
431 /// CHECK: InstanceFieldSet
432 /// CHECK: InstanceFieldGet
433
434 /// CHECK-START: int Main.test17() load_store_elimination (after)
435 /// CHECK: <<Const0:i\d+>> IntConstant 0
Mingyao Yang062157f2016-03-02 10:15:36 -0800436 /// CHECK-NOT: NewInstance
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800437 /// CHECK-NOT: InstanceFieldSet
438 /// CHECK-NOT: InstanceFieldGet
Mingyao Yang8df69d42015-10-22 15:40:58 -0700439 /// CHECK: Return [<<Const0>>]
440
441 // Test getting default value.
442 static int test17() {
443 TestClass obj = new TestClass();
444 obj.j = 1;
445 return obj.i;
446 }
447
448 /// CHECK-START: int Main.test18(TestClass) load_store_elimination (before)
449 /// CHECK: InstanceFieldSet
450 /// CHECK: InstanceFieldGet
451
452 /// CHECK-START: int Main.test18(TestClass) load_store_elimination (after)
453 /// CHECK: InstanceFieldSet
454 /// CHECK: InstanceFieldGet
455
456 // Volatile field load/store shouldn't be eliminated.
457 static int test18(TestClass obj) {
458 obj.k = 1;
459 return obj.k;
460 }
461
462 /// CHECK-START: float Main.test19(float[], float[]) load_store_elimination (before)
David Brazdil4833f5a2015-12-16 10:37:39 +0000463 /// CHECK: {{f\d+}} ArrayGet
464 /// CHECK: {{f\d+}} ArrayGet
Mingyao Yang8df69d42015-10-22 15:40:58 -0700465
466 /// CHECK-START: float Main.test19(float[], float[]) load_store_elimination (after)
David Brazdil4833f5a2015-12-16 10:37:39 +0000467 /// CHECK: {{f\d+}} ArrayGet
468 /// CHECK-NOT: {{f\d+}} ArrayGet
Mingyao Yang8df69d42015-10-22 15:40:58 -0700469
David Brazdil4833f5a2015-12-16 10:37:39 +0000470 // I/F, J/D aliasing should not happen any more and LSE should eliminate the load.
Mingyao Yang8df69d42015-10-22 15:40:58 -0700471 static float test19(float[] fa1, float[] fa2) {
472 fa1[0] = fa2[0];
473 return fa1[0];
474 }
475
476 /// CHECK-START: TestClass Main.test20() load_store_elimination (before)
477 /// CHECK: NewInstance
478 /// CHECK: InstanceFieldSet
479
480 /// CHECK-START: TestClass Main.test20() load_store_elimination (after)
481 /// CHECK: NewInstance
482 /// CHECK-NOT: InstanceFieldSet
483
484 // Storing default heap value is redundant if the heap location has the
485 // default heap value.
486 static TestClass test20() {
487 TestClass obj = new TestClass();
488 obj.i = 0;
489 return obj;
490 }
491
Mingyao Yang803cbb92015-12-01 12:24:36 -0800492 /// CHECK-START: void Main.test21(TestClass) load_store_elimination (before)
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800493 /// CHECK: NewInstance
494 /// CHECK: InstanceFieldSet
Mingyao Yang803cbb92015-12-01 12:24:36 -0800495 /// CHECK: InstanceFieldSet
496 /// CHECK: InstanceFieldSet
497 /// CHECK: InstanceFieldGet
498 /// CHECK: InstanceFieldGet
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800499
Mingyao Yang803cbb92015-12-01 12:24:36 -0800500 /// CHECK-START: void Main.test21(TestClass) load_store_elimination (after)
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800501 /// CHECK: NewInstance
502 /// CHECK: InstanceFieldSet
Mingyao Yang803cbb92015-12-01 12:24:36 -0800503 /// CHECK: InstanceFieldSet
504 /// CHECK: InstanceFieldSet
505 /// CHECK: InstanceFieldGet
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800506 /// CHECK: InstanceFieldGet
507
508 // Loop side effects can kill heap values, stores need to be kept in that case.
Mingyao Yang803cbb92015-12-01 12:24:36 -0800509 static void test21(TestClass obj0) {
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800510 TestClass obj = new TestClass();
Mingyao Yang803cbb92015-12-01 12:24:36 -0800511 obj0.str = "abc";
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800512 obj.str = "abc";
513 for (int i = 0; i < 2; i++) {
Mingyao Yang803cbb92015-12-01 12:24:36 -0800514 // Generate some loop side effect that writes into obj.
515 obj.str = "def";
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800516 }
Mingyao Yang803cbb92015-12-01 12:24:36 -0800517 System.out.print(obj0.str.substring(0, 0) + obj.str.substring(0, 0));
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800518 }
519
520 /// CHECK-START: int Main.test22() load_store_elimination (before)
521 /// CHECK: NewInstance
522 /// CHECK: InstanceFieldSet
523 /// CHECK: NewInstance
524 /// CHECK: InstanceFieldSet
525 /// CHECK: InstanceFieldGet
526 /// CHECK: NewInstance
527 /// CHECK: InstanceFieldSet
528 /// CHECK: InstanceFieldGet
529 /// CHECK: InstanceFieldGet
530
531 /// CHECK-START: int Main.test22() load_store_elimination (after)
Mingyao Yang062157f2016-03-02 10:15:36 -0800532 /// CHECK-NOT: NewInstance
Mingyao Yang803cbb92015-12-01 12:24:36 -0800533 /// CHECK-NOT: InstanceFieldSet
Mingyao Yang062157f2016-03-02 10:15:36 -0800534 /// CHECK-NOT: NewInstance
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800535 /// CHECK-NOT: InstanceFieldSet
536 /// CHECK-NOT: InstanceFieldGet
Mingyao Yang062157f2016-03-02 10:15:36 -0800537 /// CHECK-NOT: NewInstance
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800538 /// CHECK-NOT: InstanceFieldSet
Mingyao Yang803cbb92015-12-01 12:24:36 -0800539 /// CHECK-NOT: InstanceFieldGet
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800540 /// CHECK-NOT: InstanceFieldGet
541
Mingyao Yang803cbb92015-12-01 12:24:36 -0800542 // For a singleton, loop side effects can kill its field values only if:
543 // (1) it dominiates the loop header, and
544 // (2) its fields are stored into inside a loop.
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800545 static int test22() {
546 int sum = 0;
547 TestClass obj1 = new TestClass();
Mingyao Yang803cbb92015-12-01 12:24:36 -0800548 obj1.i = 2; // This store can be eliminated since obj1 is never stored into inside a loop.
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800549 for (int i = 0; i < 2; i++) {
550 TestClass obj2 = new TestClass();
Mingyao Yang803cbb92015-12-01 12:24:36 -0800551 obj2.i = 3; // This store can be eliminated since the singleton is inside the loop.
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800552 sum += obj2.i;
553 }
554 TestClass obj3 = new TestClass();
Mingyao Yang803cbb92015-12-01 12:24:36 -0800555 obj3.i = 5; // This store can be eliminated since the singleton is created after the loop.
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800556 sum += obj1.i + obj3.i;
557 return sum;
558 }
559
560 /// CHECK-START: int Main.test23(boolean) load_store_elimination (before)
561 /// CHECK: NewInstance
562 /// CHECK: InstanceFieldSet
563 /// CHECK: InstanceFieldGet
564 /// CHECK: InstanceFieldSet
565 /// CHECK: InstanceFieldGet
566 /// CHECK: Return
567 /// CHECK: InstanceFieldGet
568 /// CHECK: InstanceFieldSet
569
570 /// CHECK-START: int Main.test23(boolean) load_store_elimination (after)
571 /// CHECK: NewInstance
572 /// CHECK-NOT: InstanceFieldSet
573 /// CHECK-NOT: InstanceFieldGet
574 /// CHECK: InstanceFieldSet
575 /// CHECK: InstanceFieldGet
576 /// CHECK: Return
577 /// CHECK-NOT: InstanceFieldGet
578 /// CHECK: InstanceFieldSet
579
580 // Test store elimination on merging.
581 static int test23(boolean b) {
582 TestClass obj = new TestClass();
583 obj.i = 3; // This store can be eliminated since the value flows into each branch.
584 if (b) {
585 obj.i += 1; // This store cannot be eliminated due to the merge later.
586 } else {
587 obj.i += 2; // This store cannot be eliminated due to the merge later.
588 }
589 return obj.i;
590 }
591
David Brazdilecf52df2015-12-14 16:58:08 +0000592 /// CHECK-START: float Main.test24() load_store_elimination (before)
593 /// CHECK-DAG: <<True:i\d+>> IntConstant 1
594 /// CHECK-DAG: <<Float8:f\d+>> FloatConstant 8
595 /// CHECK-DAG: <<Float42:f\d+>> FloatConstant 42
596 /// CHECK-DAG: <<Obj:l\d+>> NewInstance
597 /// CHECK-DAG: InstanceFieldSet [<<Obj>>,<<True>>]
598 /// CHECK-DAG: InstanceFieldSet [<<Obj>>,<<Float8>>]
599 /// CHECK-DAG: <<GetTest:z\d+>> InstanceFieldGet [<<Obj>>]
David Brazdilecf52df2015-12-14 16:58:08 +0000600 /// CHECK-DAG: <<GetField:f\d+>> InstanceFieldGet [<<Obj>>]
David Brazdil74eb1b22015-12-14 11:44:01 +0000601 /// CHECK-DAG: <<Select:f\d+>> Select [<<Float42>>,<<GetField>>,<<GetTest>>]
602 /// CHECK-DAG: Return [<<Select>>]
David Brazdilecf52df2015-12-14 16:58:08 +0000603
604 /// CHECK-START: float Main.test24() load_store_elimination (after)
605 /// CHECK-DAG: <<True:i\d+>> IntConstant 1
606 /// CHECK-DAG: <<Float8:f\d+>> FloatConstant 8
607 /// CHECK-DAG: <<Float42:f\d+>> FloatConstant 42
David Brazdil74eb1b22015-12-14 11:44:01 +0000608 /// CHECK-DAG: <<Select:f\d+>> Select [<<Float42>>,<<Float8>>,<<True>>]
609 /// CHECK-DAG: Return [<<Select>>]
David Brazdilecf52df2015-12-14 16:58:08 +0000610
611 static float test24() {
612 float a = 42.0f;
613 TestClass3 obj = new TestClass3();
614 if (obj.test1) {
615 a = obj.floatField;
616 }
617 return a;
618 }
619
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800620 /// CHECK-START: void Main.testFinalizable() load_store_elimination (before)
621 /// CHECK: NewInstance
622 /// CHECK: InstanceFieldSet
Mingyao Yang025c1a62017-10-30 11:19:57 -0700623 /// CHECK: InstanceFieldSet
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800624
625 /// CHECK-START: void Main.testFinalizable() load_store_elimination (after)
626 /// CHECK: NewInstance
627 /// CHECK: InstanceFieldSet
Mingyao Yang025c1a62017-10-30 11:19:57 -0700628 /// CHECK-NOT: InstanceFieldSet
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800629
Mingyao Yang025c1a62017-10-30 11:19:57 -0700630 // Allocations of finalizable objects cannot be eliminated.
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800631 static void testFinalizable() {
632 Finalizable finalizable = new Finalizable();
Mingyao Yang025c1a62017-10-30 11:19:57 -0700633 finalizable.i = Finalizable.VALUE2;
634 finalizable.i = Finalizable.VALUE1;
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800635 }
636
637 static java.lang.ref.WeakReference<Object> getWeakReference() {
638 return new java.lang.ref.WeakReference<>(new Object());
639 }
640
641 static void testFinalizableByForcingGc() {
642 testFinalizable();
643 java.lang.ref.WeakReference<Object> reference = getWeakReference();
644
645 Runtime runtime = Runtime.getRuntime();
646 for (int i = 0; i < 20; ++i) {
647 runtime.gc();
648 System.runFinalization();
649 try {
650 Thread.sleep(1);
651 } catch (InterruptedException e) {
652 throw new AssertionError(e);
653 }
654
655 // Check to see if the weak reference has been garbage collected.
656 if (reference.get() == null) {
657 // A little bit more sleep time to make sure.
658 try {
659 Thread.sleep(100);
660 } catch (InterruptedException e) {
661 throw new AssertionError(e);
662 }
663 if (!Finalizable.sVisited) {
664 System.out.println("finalize() not called.");
665 }
666 return;
667 }
668 }
669 System.out.println("testFinalizableByForcingGc() failed to force gc.");
670 }
671
Mingyao Yang40bcb932016-02-03 05:46:57 -0800672 /// CHECK-START: int Main.$noinline$testHSelect(boolean) load_store_elimination (before)
Mingyao Yange5c71f92016-02-02 20:10:32 -0800673 /// CHECK: InstanceFieldSet
674 /// CHECK: Select
675
Mingyao Yang40bcb932016-02-03 05:46:57 -0800676 /// CHECK-START: int Main.$noinline$testHSelect(boolean) load_store_elimination (after)
Mingyao Yange5c71f92016-02-02 20:10:32 -0800677 /// CHECK: InstanceFieldSet
678 /// CHECK: Select
679
680 // Test that HSelect creates alias.
Mingyao Yang062157f2016-03-02 10:15:36 -0800681 static int $noinline$testHSelect(boolean b) {
Mingyao Yang40bcb932016-02-03 05:46:57 -0800682 if (sFlag) {
683 throw new Error();
684 }
Mingyao Yange5c71f92016-02-02 20:10:32 -0800685 TestClass obj = new TestClass();
686 TestClass obj2 = null;
687 obj.i = 0xdead;
688 if (b) {
689 obj2 = obj;
690 }
691 return obj2.i;
692 }
693
Mingyao Yang062157f2016-03-02 10:15:36 -0800694 static int sumWithFilter(int[] array, Filter f) {
695 int sum = 0;
696 for (int i = 0; i < array.length; i++) {
697 if (f.isValid(array[i])) {
698 sum += array[i];
699 }
700 }
701 return sum;
702 }
703
704 /// CHECK-START: int Main.sumWithinRange(int[], int, int) load_store_elimination (before)
705 /// CHECK: NewInstance
706 /// CHECK: InstanceFieldSet
707 /// CHECK: InstanceFieldSet
708 /// CHECK: InstanceFieldGet
709 /// CHECK: InstanceFieldGet
710
711 /// CHECK-START: int Main.sumWithinRange(int[], int, int) load_store_elimination (after)
712 /// CHECK-NOT: NewInstance
713 /// CHECK-NOT: InstanceFieldSet
714 /// CHECK-NOT: InstanceFieldGet
715
716 // A lambda-style allocation can be eliminated after inlining.
717 static int sumWithinRange(int[] array, final int low, final int high) {
718 Filter filter = new Filter() {
719 public boolean isValid(int i) {
720 return (i >= low) && (i <= high);
721 }
722 };
723 return sumWithFilter(array, filter);
724 }
725
Mingyao Yang0a845202016-10-14 16:26:08 -0700726 private static int mI = 0;
727 private static float mF = 0f;
728
729 /// CHECK-START: float Main.testAllocationEliminationWithLoops() load_store_elimination (before)
730 /// CHECK: NewInstance
731 /// CHECK: NewInstance
732 /// CHECK: NewInstance
733
734 /// CHECK-START: float Main.testAllocationEliminationWithLoops() load_store_elimination (after)
735 /// CHECK-NOT: NewInstance
736
737 private static float testAllocationEliminationWithLoops() {
738 for (int i0 = 0; i0 < 5; i0++) {
739 for (int i1 = 0; i1 < 5; i1++) {
740 for (int i2 = 0; i2 < 5; i2++) {
741 int lI0 = ((int) new Integer(((int) new Integer(mI))));
742 if (((boolean) new Boolean(false))) {
743 for (int i3 = 576 - 1; i3 >= 0; i3--) {
744 mF -= 976981405.0f;
745 }
746 }
747 }
748 }
749 }
750 return 1.0f;
751 }
752
Mingyao Yangeb2d2d346e2017-03-02 13:26:17 -0800753 /// CHECK-START: TestClass2 Main.testStoreStore() load_store_elimination (before)
754 /// CHECK: NewInstance
755 /// CHECK: InstanceFieldSet
756 /// CHECK: InstanceFieldSet
757 /// CHECK: InstanceFieldSet
758 /// CHECK: InstanceFieldSet
759
760 /// CHECK-START: TestClass2 Main.testStoreStore() load_store_elimination (after)
761 /// CHECK: NewInstance
762 /// CHECK: InstanceFieldSet
763 /// CHECK: InstanceFieldSet
764 /// CHECK-NOT: InstanceFieldSet
765
766 private static TestClass2 testStoreStore() {
767 TestClass2 obj = new TestClass2();
768 obj.i = 41;
769 obj.j = 42;
770 obj.i = 41;
771 obj.j = 43;
772 return obj;
773 }
774
Mingyao Yanga3540532018-01-25 12:17:28 -0800775 /// CHECK-START: void Main.testStoreStore2(TestClass2) load_store_elimination (before)
776 /// CHECK: InstanceFieldSet
777 /// CHECK: InstanceFieldSet
778 /// CHECK: InstanceFieldSet
779 /// CHECK: InstanceFieldSet
780
781 /// CHECK-START: void Main.testStoreStore2(TestClass2) load_store_elimination (after)
782 /// CHECK: InstanceFieldSet
783 /// CHECK: InstanceFieldSet
784 /// CHECK-NOT: InstanceFieldSet
785
786 private static void testStoreStore2(TestClass2 obj) {
787 obj.i = 41;
788 obj.j = 42;
789 obj.i = 43;
790 obj.j = 44;
791 }
792
793 /// CHECK-START: void Main.testStoreStore3(TestClass2, boolean) load_store_elimination (before)
794 /// CHECK: InstanceFieldSet
795 /// CHECK: InstanceFieldSet
796 /// CHECK: InstanceFieldSet
797 /// CHECK: InstanceFieldSet
798
799 /// CHECK-START: void Main.testStoreStore3(TestClass2, boolean) load_store_elimination (after)
800 /// CHECK: InstanceFieldSet
801 /// CHECK: InstanceFieldSet
802 /// CHECK: InstanceFieldSet
803 /// CHECK-NOT: InstanceFieldSet
804
805 private static void testStoreStore3(TestClass2 obj, boolean flag) {
806 obj.i = 41;
807 obj.j = 42; // redundant since it's overwritten in both branches below.
808 if (flag) {
809 obj.j = 43;
810 } else {
811 obj.j = 44;
812 }
813 }
814
815 /// CHECK-START: void Main.testStoreStore4() load_store_elimination (before)
816 /// CHECK: StaticFieldSet
817 /// CHECK: StaticFieldSet
818
819 /// CHECK-START: void Main.testStoreStore4() load_store_elimination (after)
820 /// CHECK: StaticFieldSet
821 /// CHECK-NOT: StaticFieldSet
822
823 private static void testStoreStore4() {
824 TestClass.si = 61;
825 TestClass.si = 62;
826 }
827
828 /// CHECK-START: int Main.testStoreStore5(TestClass2, TestClass2) load_store_elimination (before)
829 /// CHECK: InstanceFieldSet
830 /// CHECK: InstanceFieldGet
831 /// CHECK: InstanceFieldSet
832
833 /// CHECK-START: int Main.testStoreStore5(TestClass2, TestClass2) load_store_elimination (after)
834 /// CHECK: InstanceFieldSet
835 /// CHECK: InstanceFieldGet
836 /// CHECK: InstanceFieldSet
837
838 private static int testStoreStore5(TestClass2 obj1, TestClass2 obj2) {
839 obj1.i = 71; // This store is needed since obj2.i may load from it.
840 int i = obj2.i;
841 obj1.i = 72;
842 return i;
843 }
844
845 /// CHECK-START: int Main.testStoreStore6(TestClass2, TestClass2) load_store_elimination (before)
846 /// CHECK: InstanceFieldSet
847 /// CHECK: InstanceFieldGet
848 /// CHECK: InstanceFieldSet
849
850 /// CHECK-START: int Main.testStoreStore6(TestClass2, TestClass2) load_store_elimination (after)
851 /// CHECK-NOT: InstanceFieldSet
852 /// CHECK: InstanceFieldGet
853 /// CHECK: InstanceFieldSet
854
855 private static int testStoreStore6(TestClass2 obj1, TestClass2 obj2) {
856 obj1.i = 81; // This store is not needed since obj2.j cannot load from it.
857 int j = obj2.j;
858 obj1.i = 82;
859 return j;
860 }
861
862 /// CHECK-START: int Main.testNoSideEffects(int[]) load_store_elimination (before)
863 /// CHECK: ArraySet
864 /// CHECK: ArraySet
865 /// CHECK: ArraySet
866 /// CHECK: ArrayGet
867
868 /// CHECK-START: int Main.testNoSideEffects(int[]) load_store_elimination (after)
869 /// CHECK: ArraySet
870 /// CHECK: ArraySet
871 /// CHECK-NOT: ArraySet
872 /// CHECK-NOT: ArrayGet
873
874 private static int testNoSideEffects(int[] array) {
875 array[0] = 101;
876 array[1] = 102;
877 int bitCount = Integer.bitCount(0x3456);
878 array[1] = 103;
879 return array[0] + bitCount;
880 }
881
882 /// CHECK-START: void Main.testThrow(TestClass2, java.lang.Exception) load_store_elimination (before)
883 /// CHECK: InstanceFieldSet
884 /// CHECK: Throw
885
886 /// CHECK-START: void Main.testThrow(TestClass2, java.lang.Exception) load_store_elimination (after)
887 /// CHECK: InstanceFieldSet
888 /// CHECK: Throw
889
890 // Make sure throw keeps the store.
891 private static void testThrow(TestClass2 obj, Exception e) throws Exception {
892 obj.i = 55;
893 throw e;
894 }
895
Mingyao Yangeb2d2d346e2017-03-02 13:26:17 -0800896 /// CHECK-START: int Main.testStoreStoreWithDeoptimize(int[]) load_store_elimination (before)
897 /// CHECK: NewInstance
898 /// CHECK: InstanceFieldSet
899 /// CHECK: InstanceFieldSet
900 /// CHECK: InstanceFieldSet
901 /// CHECK: InstanceFieldSet
902 /// CHECK: Deoptimize
903 /// CHECK: ArraySet
904 /// CHECK: ArraySet
905 /// CHECK: ArraySet
906 /// CHECK: ArraySet
907 /// CHECK: ArrayGet
908 /// CHECK: ArrayGet
909 /// CHECK: ArrayGet
910 /// CHECK: ArrayGet
911
912 /// CHECK-START: int Main.testStoreStoreWithDeoptimize(int[]) load_store_elimination (after)
913 /// CHECK: NewInstance
914 /// CHECK: InstanceFieldSet
915 /// CHECK: InstanceFieldSet
916 /// CHECK-NOT: InstanceFieldSet
917 /// CHECK: Deoptimize
918 /// CHECK: ArraySet
919 /// CHECK: ArraySet
920 /// CHECK: ArraySet
921 /// CHECK: ArraySet
922 /// CHECK-NOT: ArrayGet
923
924 private static int testStoreStoreWithDeoptimize(int[] arr) {
925 TestClass2 obj = new TestClass2();
926 obj.i = 41;
927 obj.j = 42;
928 obj.i = 41;
929 obj.j = 43;
930 arr[0] = 1; // One HDeoptimize here.
931 arr[1] = 1;
932 arr[2] = 1;
933 arr[3] = 1;
934 return arr[0] + arr[1] + arr[2] + arr[3];
935 }
936
Mingyao Yang58d9bfc2016-11-01 13:31:58 -0700937 /// CHECK-START: double Main.getCircleArea(double, boolean) load_store_elimination (before)
938 /// CHECK: NewInstance
939
940 /// CHECK-START: double Main.getCircleArea(double, boolean) load_store_elimination (after)
941 /// CHECK-NOT: NewInstance
942
943 private static double getCircleArea(double radius, boolean b) {
944 double area = 0d;
945 if (b) {
946 area = new Circle(radius).getArea();
947 }
948 return area;
949 }
950
Mingyao Yange58bdca2016-10-28 11:07:24 -0700951 /// CHECK-START: double Main.testDeoptimize(int[], double[], double) load_store_elimination (before)
952 /// CHECK: Deoptimize
953 /// CHECK: NewInstance
954 /// CHECK: Deoptimize
955 /// CHECK: NewInstance
956
957 /// CHECK-START: double Main.testDeoptimize(int[], double[], double) load_store_elimination (after)
958 /// CHECK: Deoptimize
959 /// CHECK: NewInstance
960 /// CHECK: Deoptimize
961 /// CHECK-NOT: NewInstance
962
963 private static double testDeoptimize(int[] iarr, double[] darr, double radius) {
964 iarr[0] = 1; // One HDeoptimize here. Not triggered.
965 iarr[1] = 1;
966 Circle circle1 = new Circle(radius);
967 iarr[2] = 1;
968 darr[0] = circle1.getRadius(); // One HDeoptimize here, which holds circle1 live. Triggered.
969 darr[1] = circle1.getRadius();
970 darr[2] = circle1.getRadius();
971 darr[3] = circle1.getRadius();
972 return new Circle(Math.PI).getArea();
973 }
974
Mingyao Yang86974902017-03-01 14:03:51 -0800975 /// CHECK-START: int Main.testAllocationEliminationOfArray1() load_store_elimination (before)
976 /// CHECK: NewArray
977 /// CHECK: ArraySet
978 /// CHECK: ArraySet
979 /// CHECK: ArrayGet
980 /// CHECK: ArrayGet
981 /// CHECK: ArrayGet
982 /// CHECK: ArrayGet
983
984 /// CHECK-START: int Main.testAllocationEliminationOfArray1() load_store_elimination (after)
985 /// CHECK-NOT: NewArray
986 /// CHECK-NOT: ArraySet
987 /// CHECK-NOT: ArrayGet
988 private static int testAllocationEliminationOfArray1() {
989 int[] array = new int[4];
990 array[2] = 4;
991 array[3] = 7;
992 return array[0] + array[1] + array[2] + array[3];
993 }
994
995 /// CHECK-START: int Main.testAllocationEliminationOfArray2() load_store_elimination (before)
996 /// CHECK: NewArray
997 /// CHECK: ArraySet
998 /// CHECK: ArraySet
999 /// CHECK: ArrayGet
1000
1001 /// CHECK-START: int Main.testAllocationEliminationOfArray2() load_store_elimination (after)
1002 /// CHECK: NewArray
1003 /// CHECK: ArraySet
1004 /// CHECK: ArraySet
1005 /// CHECK: ArrayGet
1006 private static int testAllocationEliminationOfArray2() {
1007 // Cannot eliminate array allocation since array is accessed with non-constant
Aart Bik0148de42017-09-05 09:25:01 -07001008 // index (only 3 elements to prevent vectorization of the reduction).
1009 int[] array = new int[3];
1010 array[1] = 4;
1011 array[2] = 7;
Mingyao Yang86974902017-03-01 14:03:51 -08001012 int sum = 0;
1013 for (int e : array) {
1014 sum += e;
1015 }
1016 return sum;
1017 }
1018
1019 /// CHECK-START: int Main.testAllocationEliminationOfArray3(int) load_store_elimination (before)
1020 /// CHECK: NewArray
1021 /// CHECK: ArraySet
1022 /// CHECK: ArrayGet
1023
1024 /// CHECK-START: int Main.testAllocationEliminationOfArray3(int) load_store_elimination (after)
1025 /// CHECK-NOT: NewArray
1026 /// CHECK-NOT: ArraySet
1027 /// CHECK-NOT: ArrayGet
1028 private static int testAllocationEliminationOfArray3(int i) {
1029 int[] array = new int[4];
1030 array[i] = 4;
1031 return array[i];
1032 }
1033
1034 /// CHECK-START: int Main.testAllocationEliminationOfArray4(int) load_store_elimination (before)
1035 /// CHECK: NewArray
1036 /// CHECK: ArraySet
1037 /// CHECK: ArraySet
1038 /// CHECK: ArrayGet
1039 /// CHECK: ArrayGet
1040
1041 /// CHECK-START: int Main.testAllocationEliminationOfArray4(int) load_store_elimination (after)
1042 /// CHECK: NewArray
1043 /// CHECK: ArraySet
1044 /// CHECK: ArraySet
1045 /// CHECK: ArrayGet
1046 /// CHECK-NOT: ArrayGet
1047 private static int testAllocationEliminationOfArray4(int i) {
1048 // Cannot eliminate array allocation due to index aliasing between 1 and i.
1049 int[] array = new int[4];
1050 array[1] = 2;
1051 array[i] = 4;
1052 return array[1] + array[i];
1053 }
1054
Mingyao Yang7cf9af22018-02-06 15:02:42 -08001055 /// CHECK-START: int Main.testAllocationEliminationOfArray5(int) load_store_elimination (before)
1056 /// CHECK: NewArray
1057 /// CHECK: ArraySet
1058 /// CHECK: ArrayGet
1059
1060 /// CHECK-START: int Main.testAllocationEliminationOfArray5(int) load_store_elimination (after)
1061 /// CHECK: NewArray
1062 /// CHECK-NOT: ArraySet
1063 /// CHECK-NOT: ArrayGet
1064 private static int testAllocationEliminationOfArray5(int i) {
1065 // Cannot eliminate array allocation due to unknown i that may
1066 // cause NegativeArraySizeException.
1067 int[] array = new int[i];
1068 array[1] = 12;
1069 return array[1];
1070 }
1071
Mingyao Yang46721ef2017-10-05 14:45:17 -07001072 /// CHECK-START: int Main.testExitMerge(boolean) load_store_elimination (before)
1073 /// CHECK: NewInstance
1074 /// CHECK: InstanceFieldSet
1075 /// CHECK: InstanceFieldGet
1076 /// CHECK: Return
1077 /// CHECK: InstanceFieldSet
1078 /// CHECK: Throw
1079
1080 /// CHECK-START: int Main.testExitMerge(boolean) load_store_elimination (after)
1081 /// CHECK-NOT: NewInstance
1082 /// CHECK-NOT: InstanceFieldSet
1083 /// CHECK-NOT: InstanceFieldGet
1084 /// CHECK: Return
1085 /// CHECK-NOT: InstanceFieldSet
1086 /// CHECK: Throw
1087 private static int testExitMerge(boolean cond) {
1088 TestClass obj = new TestClass();
1089 if (cond) {
1090 obj.i = 1;
1091 return obj.i + 1;
1092 } else {
1093 obj.i = 2;
1094 throw new Error();
1095 }
1096 }
1097
1098 /// CHECK-START: int Main.testExitMerge2(boolean) load_store_elimination (before)
1099 /// CHECK: NewInstance
1100 /// CHECK: InstanceFieldSet
1101 /// CHECK: InstanceFieldGet
1102 /// CHECK: InstanceFieldSet
1103 /// CHECK: InstanceFieldGet
1104
1105 /// CHECK-START: int Main.testExitMerge2(boolean) load_store_elimination (after)
1106 /// CHECK-NOT: NewInstance
1107 /// CHECK-NOT: InstanceFieldSet
1108 /// CHECK-NOT: InstanceFieldGet
1109 private static int testExitMerge2(boolean cond) {
1110 TestClass obj = new TestClass();
1111 int res;
1112 if (cond) {
1113 obj.i = 1;
1114 res = obj.i + 1;
1115 } else {
1116 obj.i = 2;
1117 res = obj.j + 2;
1118 }
1119 return res;
1120 }
1121
Mingyao Yang206070c2017-11-29 23:01:58 -08001122 /// CHECK-START: void Main.testStoreSameValue() load_store_elimination (before)
1123 /// CHECK: NewArray
1124 /// CHECK: ArrayGet
1125 /// CHECK: ArraySet
1126
1127 /// CHECK-START: void Main.testStoreSameValue() load_store_elimination (after)
1128 /// CHECK: NewArray
1129 /// CHECK-NOT: ArrayGet
1130 /// CHECK-NOT: ArraySet
1131 private static void testStoreSameValue() {
1132 Object[] array = new Object[2];
1133 sArray = array;
1134 Object obj = array[0];
1135 array[1] = obj; // store the same value as the defaut value.
1136 }
1137
1138 static Object[] sArray;
1139
Mingyao Yang062157f2016-03-02 10:15:36 -08001140 static void assertIntEquals(int result, int expected) {
Mingyao Yang8df69d42015-10-22 15:40:58 -07001141 if (expected != result) {
1142 throw new Error("Expected: " + expected + ", found: " + result);
1143 }
1144 }
1145
Mingyao Yang062157f2016-03-02 10:15:36 -08001146 static void assertFloatEquals(float result, float expected) {
Mingyao Yang8df69d42015-10-22 15:40:58 -07001147 if (expected != result) {
1148 throw new Error("Expected: " + expected + ", found: " + result);
1149 }
1150 }
1151
Mingyao Yang062157f2016-03-02 10:15:36 -08001152 static void assertDoubleEquals(double result, double expected) {
Mingyao Yang8df69d42015-10-22 15:40:58 -07001153 if (expected != result) {
1154 throw new Error("Expected: " + expected + ", found: " + result);
1155 }
1156 }
1157
1158 public static void main(String[] args) {
1159 assertDoubleEquals(Math.PI * Math.PI * Math.PI, calcCircleArea(Math.PI));
1160 assertIntEquals(test1(new TestClass(), new TestClass()), 3);
1161 assertIntEquals(test2(new TestClass()), 1);
1162 TestClass obj1 = new TestClass();
1163 TestClass obj2 = new TestClass();
1164 obj1.next = obj2;
1165 assertIntEquals(test3(obj1), 10);
1166 assertIntEquals(test4(new TestClass(), true), 1);
1167 assertIntEquals(test4(new TestClass(), false), 1);
1168 assertIntEquals(test5(new TestClass(), true), 1);
1169 assertIntEquals(test5(new TestClass(), false), 2);
1170 assertIntEquals(test6(new TestClass(), new TestClass(), true), 4);
1171 assertIntEquals(test6(new TestClass(), new TestClass(), false), 2);
1172 assertIntEquals(test7(new TestClass()), 1);
1173 assertIntEquals(test8(), 1);
1174 obj1 = new TestClass();
1175 obj2 = new TestClass();
1176 obj1.next = obj2;
1177 assertIntEquals(test9(new TestClass()), 1);
1178 assertIntEquals(test10(new TestClass(3, 4)), 3);
1179 assertIntEquals(TestClass.si, 3);
1180 assertIntEquals(test11(new TestClass()), 10);
1181 assertIntEquals(test12(new TestClass(), new TestClass()), 10);
1182 assertIntEquals(test13(new TestClass(), new TestClass2()), 3);
1183 SubTestClass obj3 = new SubTestClass();
1184 assertIntEquals(test14(obj3, obj3), 2);
1185 assertIntEquals(test15(), 2);
1186 assertIntEquals(test16(), 3);
1187 assertIntEquals(test17(), 0);
1188 assertIntEquals(test18(new TestClass()), 1);
1189 float[] fa1 = { 0.8f };
1190 float[] fa2 = { 1.8f };
1191 assertFloatEquals(test19(fa1, fa2), 1.8f);
1192 assertFloatEquals(test20().i, 0);
Mingyao Yang803cbb92015-12-01 12:24:36 -08001193 test21(new TestClass());
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001194 assertIntEquals(test22(), 13);
1195 assertIntEquals(test23(true), 4);
1196 assertIntEquals(test23(false), 5);
David Brazdilecf52df2015-12-14 16:58:08 +00001197 assertFloatEquals(test24(), 8.0f);
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001198 testFinalizableByForcingGc();
Mingyao Yang40bcb932016-02-03 05:46:57 -08001199 assertIntEquals($noinline$testHSelect(true), 0xdead);
Mingyao Yang062157f2016-03-02 10:15:36 -08001200 int[] array = {2, 5, 9, -1, -3, 10, 8, 4};
1201 assertIntEquals(sumWithinRange(array, 1, 5), 11);
Mingyao Yang0a845202016-10-14 16:26:08 -07001202 assertFloatEquals(testAllocationEliminationWithLoops(), 1.0f);
1203 assertFloatEquals(mF, 0f);
Mingyao Yang58d9bfc2016-11-01 13:31:58 -07001204 assertDoubleEquals(Math.PI * Math.PI * Math.PI, getCircleArea(Math.PI, true));
1205 assertDoubleEquals(0d, getCircleArea(Math.PI, false));
Mingyao Yange58bdca2016-10-28 11:07:24 -07001206
1207 int[] iarray = {0, 0, 0};
1208 double[] darray = {0d, 0d, 0d};
1209 try {
1210 assertDoubleEquals(Math.PI * Math.PI * Math.PI, testDeoptimize(iarray, darray, Math.PI));
1211 } catch (Exception e) {
1212 System.out.println(e);
1213 }
1214 assertIntEquals(iarray[0], 1);
1215 assertIntEquals(iarray[1], 1);
1216 assertIntEquals(iarray[2], 1);
1217 assertDoubleEquals(darray[0], Math.PI);
1218 assertDoubleEquals(darray[1], Math.PI);
1219 assertDoubleEquals(darray[2], Math.PI);
Mingyao Yang86974902017-03-01 14:03:51 -08001220
1221 assertIntEquals(testAllocationEliminationOfArray1(), 11);
1222 assertIntEquals(testAllocationEliminationOfArray2(), 11);
1223 assertIntEquals(testAllocationEliminationOfArray3(2), 4);
1224 assertIntEquals(testAllocationEliminationOfArray4(2), 6);
Mingyao Yang7cf9af22018-02-06 15:02:42 -08001225 assertIntEquals(testAllocationEliminationOfArray5(2), 12);
1226 try {
1227 testAllocationEliminationOfArray5(-2);
1228 } catch (NegativeArraySizeException e) {
1229 System.out.println("Got NegativeArraySizeException.");
1230 }
Mingyao Yangeb2d2d346e2017-03-02 13:26:17 -08001231
1232 assertIntEquals(testStoreStore().i, 41);
1233 assertIntEquals(testStoreStore().j, 43);
Mingyao Yang293f1c02017-11-08 15:22:17 -08001234
Mingyao Yang46721ef2017-10-05 14:45:17 -07001235 assertIntEquals(testExitMerge(true), 2);
1236 assertIntEquals(testExitMerge2(true), 2);
1237 assertIntEquals(testExitMerge2(false), 2);
1238
Mingyao Yanga3540532018-01-25 12:17:28 -08001239 TestClass2 testclass2 = new TestClass2();
1240 testStoreStore2(testclass2);
1241 assertIntEquals(testclass2.i, 43);
1242 assertIntEquals(testclass2.j, 44);
1243
1244 testStoreStore3(testclass2, true);
1245 assertIntEquals(testclass2.i, 41);
1246 assertIntEquals(testclass2.j, 43);
1247 testStoreStore3(testclass2, false);
1248 assertIntEquals(testclass2.i, 41);
1249 assertIntEquals(testclass2.j, 44);
1250
1251 testStoreStore4();
1252 assertIntEquals(TestClass.si, 62);
1253
1254 int ret = testStoreStore5(testclass2, testclass2);
1255 assertIntEquals(testclass2.i, 72);
1256 assertIntEquals(ret, 71);
1257
1258 testclass2.j = 88;
1259 ret = testStoreStore6(testclass2, testclass2);
1260 assertIntEquals(testclass2.i, 82);
1261 assertIntEquals(ret, 88);
1262
1263 ret = testNoSideEffects(iarray);
Mingyao Yang293f1c02017-11-08 15:22:17 -08001264 assertIntEquals(iarray[0], 101);
Mingyao Yanga3540532018-01-25 12:17:28 -08001265 assertIntEquals(iarray[1], 103);
Mingyao Yang293f1c02017-11-08 15:22:17 -08001266 assertIntEquals(ret, 108);
Mingyao Yanga3540532018-01-25 12:17:28 -08001267
1268 try {
1269 testThrow(testclass2, new Exception());
1270 } catch (Exception e) {}
1271 assertIntEquals(testclass2.i, 55);
1272
1273 assertIntEquals(testStoreStoreWithDeoptimize(new int[4]), 4);
Mingyao Yang8df69d42015-10-22 15:40:58 -07001274 }
Mingyao Yang40bcb932016-02-03 05:46:57 -08001275
1276 static boolean sFlag;
Mingyao Yang8df69d42015-10-22 15:40:58 -07001277}