Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | class Circle { |
| 18 | Circle(double radius) { |
| 19 | this.radius = radius; |
| 20 | } |
| 21 | public double getArea() { |
| 22 | return radius * radius * Math.PI; |
| 23 | } |
| 24 | private double radius; |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 25 | } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 26 | |
| 27 | class TestClass { |
| 28 | TestClass() { |
| 29 | } |
| 30 | TestClass(int i, int j) { |
| 31 | this.i = i; |
| 32 | this.j = j; |
| 33 | } |
| 34 | int i; |
| 35 | int j; |
| 36 | volatile int k; |
| 37 | TestClass next; |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 38 | String str; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 39 | static int si; |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 40 | } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 41 | |
| 42 | class SubTestClass extends TestClass { |
| 43 | int k; |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 44 | } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 45 | |
| 46 | class TestClass2 { |
| 47 | int i; |
| 48 | int j; |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | class Finalizable { |
| 52 | static boolean sVisited = false; |
| 53 | static final int VALUE = 0xbeef; |
| 54 | int i; |
| 55 | |
| 56 | protected void finalize() { |
| 57 | if (i != VALUE) { |
| 58 | System.out.println("Where is the beef?"); |
| 59 | } |
| 60 | sVisited = true; |
| 61 | } |
| 62 | } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 63 | |
| 64 | public class Main { |
| 65 | |
| 66 | /// CHECK-START: double Main.calcCircleArea(double) load_store_elimination (before) |
| 67 | /// CHECK: NewInstance |
| 68 | /// CHECK: InstanceFieldSet |
| 69 | /// CHECK: InstanceFieldGet |
| 70 | |
| 71 | /// CHECK-START: double Main.calcCircleArea(double) load_store_elimination (after) |
| 72 | /// CHECK: NewInstance |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 73 | /// CHECK-NOT: InstanceFieldSet |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 74 | /// CHECK-NOT: InstanceFieldGet |
| 75 | |
| 76 | static double calcCircleArea(double radius) { |
| 77 | return new Circle(radius).getArea(); |
| 78 | } |
| 79 | |
| 80 | /// CHECK-START: int Main.test1(TestClass, TestClass) load_store_elimination (before) |
| 81 | /// CHECK: InstanceFieldSet |
| 82 | /// CHECK: InstanceFieldSet |
| 83 | /// CHECK: InstanceFieldGet |
| 84 | /// CHECK: InstanceFieldGet |
| 85 | |
| 86 | /// CHECK-START: int Main.test1(TestClass, TestClass) load_store_elimination (after) |
| 87 | /// CHECK: InstanceFieldSet |
| 88 | /// CHECK: InstanceFieldSet |
| 89 | /// CHECK-NOT: NullCheck |
| 90 | /// CHECK-NOT: InstanceFieldGet |
| 91 | |
| 92 | // Different fields shouldn't alias. |
| 93 | static int test1(TestClass obj1, TestClass obj2) { |
| 94 | obj1.i = 1; |
| 95 | obj2.j = 2; |
| 96 | return obj1.i + obj2.j; |
| 97 | } |
| 98 | |
| 99 | /// CHECK-START: int Main.test2(TestClass) load_store_elimination (before) |
| 100 | /// CHECK: InstanceFieldSet |
| 101 | /// CHECK: InstanceFieldSet |
| 102 | /// CHECK: InstanceFieldGet |
| 103 | |
| 104 | /// CHECK-START: int Main.test2(TestClass) load_store_elimination (after) |
| 105 | /// CHECK: InstanceFieldSet |
| 106 | /// CHECK-NOT: NullCheck |
| 107 | /// CHECK-NOT: InstanceFieldSet |
| 108 | /// CHECK-NOT: InstanceFieldGet |
| 109 | |
| 110 | // Redundant store of the same value. |
| 111 | static int test2(TestClass obj) { |
| 112 | obj.j = 1; |
| 113 | obj.j = 1; |
| 114 | return obj.j; |
| 115 | } |
| 116 | |
| 117 | /// CHECK-START: int Main.test3(TestClass) load_store_elimination (before) |
| 118 | /// CHECK: InstanceFieldSet |
| 119 | /// CHECK: InstanceFieldGet |
| 120 | /// CHECK: InstanceFieldSet |
| 121 | /// CHECK: NewInstance |
| 122 | /// CHECK: InstanceFieldSet |
| 123 | /// CHECK: InstanceFieldSet |
| 124 | /// CHECK: InstanceFieldGet |
| 125 | /// CHECK: InstanceFieldGet |
| 126 | /// CHECK: InstanceFieldGet |
| 127 | /// CHECK: InstanceFieldGet |
| 128 | |
| 129 | /// CHECK-START: int Main.test3(TestClass) load_store_elimination (after) |
| 130 | /// CHECK: InstanceFieldSet |
| 131 | /// CHECK: InstanceFieldGet |
| 132 | /// CHECK: InstanceFieldSet |
| 133 | /// CHECK: NewInstance |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 134 | /// CHECK-NOT: InstanceFieldSet |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 135 | /// CHECK-NOT: InstanceFieldGet |
| 136 | |
| 137 | // A new allocation shouldn't alias with pre-existing values. |
| 138 | static int test3(TestClass obj) { |
| 139 | obj.i = 1; |
| 140 | obj.next.j = 2; |
| 141 | TestClass obj2 = new TestClass(); |
| 142 | obj2.i = 3; |
| 143 | obj2.j = 4; |
| 144 | return obj.i + obj.next.j + obj2.i + obj2.j; |
| 145 | } |
| 146 | |
| 147 | /// CHECK-START: int Main.test4(TestClass, boolean) load_store_elimination (before) |
| 148 | /// CHECK: InstanceFieldSet |
| 149 | /// CHECK: InstanceFieldGet |
| 150 | /// CHECK: Return |
| 151 | /// CHECK: InstanceFieldSet |
| 152 | |
| 153 | /// CHECK-START: int Main.test4(TestClass, boolean) load_store_elimination (after) |
| 154 | /// CHECK: InstanceFieldSet |
| 155 | /// CHECK-NOT: NullCheck |
| 156 | /// CHECK-NOT: InstanceFieldGet |
| 157 | /// CHECK: Return |
| 158 | /// CHECK: InstanceFieldSet |
| 159 | |
| 160 | // Set and merge the same value in two branches. |
| 161 | static int test4(TestClass obj, boolean b) { |
| 162 | if (b) { |
| 163 | obj.i = 1; |
| 164 | } else { |
| 165 | obj.i = 1; |
| 166 | } |
| 167 | return obj.i; |
| 168 | } |
| 169 | |
| 170 | /// CHECK-START: int Main.test5(TestClass, boolean) load_store_elimination (before) |
| 171 | /// CHECK: InstanceFieldSet |
| 172 | /// CHECK: InstanceFieldGet |
| 173 | /// CHECK: Return |
| 174 | /// CHECK: InstanceFieldSet |
| 175 | |
| 176 | /// CHECK-START: int Main.test5(TestClass, boolean) load_store_elimination (after) |
| 177 | /// CHECK: InstanceFieldSet |
| 178 | /// CHECK: InstanceFieldGet |
| 179 | /// CHECK: Return |
| 180 | /// CHECK: InstanceFieldSet |
| 181 | |
| 182 | // Set and merge different values in two branches. |
| 183 | static int test5(TestClass obj, boolean b) { |
| 184 | if (b) { |
| 185 | obj.i = 1; |
| 186 | } else { |
| 187 | obj.i = 2; |
| 188 | } |
| 189 | return obj.i; |
| 190 | } |
| 191 | |
| 192 | /// CHECK-START: int Main.test6(TestClass, TestClass, boolean) load_store_elimination (before) |
| 193 | /// CHECK: InstanceFieldSet |
| 194 | /// CHECK: InstanceFieldSet |
| 195 | /// CHECK: InstanceFieldSet |
| 196 | /// CHECK: InstanceFieldGet |
| 197 | /// CHECK: InstanceFieldGet |
| 198 | |
| 199 | /// CHECK-START: int Main.test6(TestClass, TestClass, boolean) load_store_elimination (after) |
| 200 | /// CHECK: InstanceFieldSet |
| 201 | /// CHECK: InstanceFieldSet |
| 202 | /// CHECK: InstanceFieldSet |
| 203 | /// CHECK: InstanceFieldGet |
| 204 | /// CHECK-NOT: NullCheck |
| 205 | /// CHECK-NOT: InstanceFieldGet |
| 206 | |
| 207 | // Setting the same value doesn't clear the value for aliased locations. |
| 208 | static int test6(TestClass obj1, TestClass obj2, boolean b) { |
| 209 | obj1.i = 1; |
| 210 | obj1.j = 2; |
| 211 | if (b) { |
| 212 | obj2.j = 2; |
| 213 | } |
| 214 | return obj1.j + obj2.j; |
| 215 | } |
| 216 | |
| 217 | /// CHECK-START: int Main.test7(TestClass) load_store_elimination (before) |
| 218 | /// CHECK: InstanceFieldSet |
| 219 | /// CHECK: InstanceFieldGet |
| 220 | |
| 221 | /// CHECK-START: int Main.test7(TestClass) load_store_elimination (after) |
| 222 | /// CHECK: InstanceFieldSet |
| 223 | /// CHECK: InstanceFieldGet |
| 224 | |
| 225 | // Invocation should kill values in non-singleton heap locations. |
| 226 | static int test7(TestClass obj) { |
| 227 | obj.i = 1; |
| 228 | System.out.print(""); |
| 229 | return obj.i; |
| 230 | } |
| 231 | |
| 232 | /// CHECK-START: int Main.test8() load_store_elimination (before) |
| 233 | /// CHECK: NewInstance |
| 234 | /// CHECK: InstanceFieldSet |
| 235 | /// CHECK: InvokeVirtual |
| 236 | /// CHECK: InstanceFieldGet |
| 237 | |
| 238 | /// CHECK-START: int Main.test8() load_store_elimination (after) |
| 239 | /// CHECK: NewInstance |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 240 | /// CHECK-NOT: InstanceFieldSet |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 241 | /// CHECK: InvokeVirtual |
| 242 | /// CHECK-NOT: NullCheck |
| 243 | /// CHECK-NOT: InstanceFieldGet |
| 244 | |
| 245 | // Invocation should not kill values in singleton heap locations. |
| 246 | static int test8() { |
| 247 | TestClass obj = new TestClass(); |
| 248 | obj.i = 1; |
| 249 | System.out.print(""); |
| 250 | return obj.i; |
| 251 | } |
| 252 | |
| 253 | /// CHECK-START: int Main.test9(TestClass) load_store_elimination (before) |
| 254 | /// CHECK: NewInstance |
| 255 | /// CHECK: InstanceFieldSet |
| 256 | /// CHECK: InstanceFieldSet |
| 257 | /// CHECK: InstanceFieldGet |
| 258 | |
| 259 | /// CHECK-START: int Main.test9(TestClass) load_store_elimination (after) |
| 260 | /// CHECK: NewInstance |
| 261 | /// CHECK: InstanceFieldSet |
| 262 | /// CHECK: InstanceFieldSet |
| 263 | /// CHECK: InstanceFieldGet |
| 264 | |
| 265 | // Invocation should kill values in non-singleton heap locations. |
| 266 | static int test9(TestClass obj) { |
| 267 | TestClass obj2 = new TestClass(); |
| 268 | obj2.i = 1; |
| 269 | obj.next = obj2; |
| 270 | System.out.print(""); |
| 271 | return obj2.i; |
| 272 | } |
| 273 | |
| 274 | /// CHECK-START: int Main.test10(TestClass) load_store_elimination (before) |
| 275 | /// CHECK: StaticFieldGet |
| 276 | /// CHECK: InstanceFieldGet |
| 277 | /// CHECK: StaticFieldSet |
| 278 | /// CHECK: InstanceFieldGet |
| 279 | |
| 280 | /// CHECK-START: int Main.test10(TestClass) load_store_elimination (after) |
| 281 | /// CHECK: StaticFieldGet |
| 282 | /// CHECK: InstanceFieldGet |
| 283 | /// CHECK: StaticFieldSet |
| 284 | /// CHECK-NOT: NullCheck |
| 285 | /// CHECK-NOT: InstanceFieldGet |
| 286 | |
| 287 | // Static fields shouldn't alias with instance fields. |
| 288 | static int test10(TestClass obj) { |
| 289 | TestClass.si += obj.i; |
| 290 | return obj.i; |
| 291 | } |
| 292 | |
| 293 | /// CHECK-START: int Main.test11(TestClass) load_store_elimination (before) |
| 294 | /// CHECK: InstanceFieldSet |
| 295 | /// CHECK: InstanceFieldGet |
| 296 | |
| 297 | /// CHECK-START: int Main.test11(TestClass) load_store_elimination (after) |
| 298 | /// CHECK: InstanceFieldSet |
| 299 | /// CHECK-NOT: NullCheck |
| 300 | /// CHECK-NOT: InstanceFieldGet |
| 301 | |
| 302 | // Loop without heap writes. |
| 303 | // obj.i is actually hoisted to the loop pre-header by licm already. |
| 304 | static int test11(TestClass obj) { |
| 305 | obj.i = 1; |
| 306 | int sum = 0; |
| 307 | for (int i = 0; i < 10; i++) { |
| 308 | sum += obj.i; |
| 309 | } |
| 310 | return sum; |
| 311 | } |
| 312 | |
| 313 | /// CHECK-START: int Main.test12(TestClass, TestClass) load_store_elimination (before) |
| 314 | /// CHECK: InstanceFieldSet |
| 315 | /// CHECK: InstanceFieldGet |
| 316 | /// CHECK: InstanceFieldSet |
| 317 | |
| 318 | /// CHECK-START: int Main.test12(TestClass, TestClass) load_store_elimination (after) |
| 319 | /// CHECK: InstanceFieldSet |
| 320 | /// CHECK: InstanceFieldGet |
| 321 | /// CHECK: InstanceFieldSet |
| 322 | |
| 323 | // Loop with heap writes. |
| 324 | static int test12(TestClass obj1, TestClass obj2) { |
| 325 | obj1.i = 1; |
| 326 | int sum = 0; |
| 327 | for (int i = 0; i < 10; i++) { |
| 328 | sum += obj1.i; |
| 329 | obj2.i = sum; |
| 330 | } |
| 331 | return sum; |
| 332 | } |
| 333 | |
| 334 | /// CHECK-START: int Main.test13(TestClass, TestClass2) load_store_elimination (before) |
| 335 | /// CHECK: InstanceFieldSet |
| 336 | /// CHECK: InstanceFieldSet |
| 337 | /// CHECK: InstanceFieldGet |
| 338 | /// CHECK: InstanceFieldGet |
| 339 | |
| 340 | /// CHECK-START: int Main.test13(TestClass, TestClass2) load_store_elimination (after) |
| 341 | /// CHECK: InstanceFieldSet |
| 342 | /// CHECK: InstanceFieldSet |
| 343 | /// CHECK-NOT: NullCheck |
| 344 | /// CHECK-NOT: InstanceFieldGet |
| 345 | |
| 346 | // Different classes shouldn't alias. |
| 347 | static int test13(TestClass obj1, TestClass2 obj2) { |
| 348 | obj1.i = 1; |
| 349 | obj2.i = 2; |
| 350 | return obj1.i + obj2.i; |
| 351 | } |
| 352 | |
| 353 | /// CHECK-START: int Main.test14(TestClass, SubTestClass) load_store_elimination (before) |
| 354 | /// CHECK: InstanceFieldSet |
| 355 | /// CHECK: InstanceFieldSet |
| 356 | /// CHECK: InstanceFieldGet |
| 357 | |
| 358 | /// CHECK-START: int Main.test14(TestClass, SubTestClass) load_store_elimination (after) |
| 359 | /// CHECK: InstanceFieldSet |
| 360 | /// CHECK: InstanceFieldSet |
| 361 | /// CHECK: InstanceFieldGet |
| 362 | |
| 363 | // Subclass may alias with super class. |
| 364 | static int test14(TestClass obj1, SubTestClass obj2) { |
| 365 | obj1.i = 1; |
| 366 | obj2.i = 2; |
| 367 | return obj1.i; |
| 368 | } |
| 369 | |
| 370 | /// CHECK-START: int Main.test15() load_store_elimination (before) |
| 371 | /// CHECK: StaticFieldSet |
| 372 | /// CHECK: StaticFieldSet |
| 373 | /// CHECK: StaticFieldGet |
| 374 | |
| 375 | /// CHECK-START: int Main.test15() load_store_elimination (after) |
| 376 | /// CHECK: <<Const2:i\d+>> IntConstant 2 |
| 377 | /// CHECK: StaticFieldSet |
| 378 | /// CHECK: StaticFieldSet |
| 379 | /// CHECK-NOT: StaticFieldGet |
| 380 | /// CHECK: Return [<<Const2>>] |
| 381 | |
| 382 | // Static field access from subclass's name. |
| 383 | static int test15() { |
| 384 | TestClass.si = 1; |
| 385 | SubTestClass.si = 2; |
| 386 | return TestClass.si; |
| 387 | } |
| 388 | |
| 389 | /// CHECK-START: int Main.test16() load_store_elimination (before) |
| 390 | /// CHECK: NewInstance |
| 391 | /// CHECK: InstanceFieldSet |
| 392 | /// CHECK: InstanceFieldSet |
| 393 | /// CHECK: InstanceFieldGet |
| 394 | /// CHECK: InstanceFieldGet |
| 395 | |
| 396 | /// CHECK-START: int Main.test16() load_store_elimination (after) |
| 397 | /// CHECK: NewInstance |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 398 | /// CHECK-NOT: InstanceFieldSet |
| 399 | /// CHECK-NOT: InstanceFieldGet |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 400 | |
| 401 | // Test inlined constructor. |
| 402 | static int test16() { |
| 403 | TestClass obj = new TestClass(1, 2); |
| 404 | return obj.i + obj.j; |
| 405 | } |
| 406 | |
| 407 | /// CHECK-START: int Main.test17() load_store_elimination (before) |
| 408 | /// CHECK: NewInstance |
| 409 | /// CHECK: InstanceFieldSet |
| 410 | /// CHECK: InstanceFieldGet |
| 411 | |
| 412 | /// CHECK-START: int Main.test17() load_store_elimination (after) |
| 413 | /// CHECK: <<Const0:i\d+>> IntConstant 0 |
| 414 | /// CHECK: NewInstance |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 415 | /// CHECK-NOT: InstanceFieldSet |
| 416 | /// CHECK-NOT: InstanceFieldGet |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 417 | /// CHECK: Return [<<Const0>>] |
| 418 | |
| 419 | // Test getting default value. |
| 420 | static int test17() { |
| 421 | TestClass obj = new TestClass(); |
| 422 | obj.j = 1; |
| 423 | return obj.i; |
| 424 | } |
| 425 | |
| 426 | /// CHECK-START: int Main.test18(TestClass) load_store_elimination (before) |
| 427 | /// CHECK: InstanceFieldSet |
| 428 | /// CHECK: InstanceFieldGet |
| 429 | |
| 430 | /// CHECK-START: int Main.test18(TestClass) load_store_elimination (after) |
| 431 | /// CHECK: InstanceFieldSet |
| 432 | /// CHECK: InstanceFieldGet |
| 433 | |
| 434 | // Volatile field load/store shouldn't be eliminated. |
| 435 | static int test18(TestClass obj) { |
| 436 | obj.k = 1; |
| 437 | return obj.k; |
| 438 | } |
| 439 | |
| 440 | /// CHECK-START: float Main.test19(float[], float[]) load_store_elimination (before) |
| 441 | /// CHECK: <<IntTypeValue:i\d+>> ArrayGet |
| 442 | /// CHECK: ArraySet |
| 443 | /// CHECK: <<FloatTypeValue:f\d+>> ArrayGet |
| 444 | |
| 445 | /// CHECK-START: float Main.test19(float[], float[]) load_store_elimination (after) |
| 446 | /// CHECK: <<IntTypeValue:i\d+>> ArrayGet |
| 447 | /// CHECK: ArraySet |
| 448 | /// CHECK: <<FloatTypeValue:f\d+>> ArrayGet |
| 449 | |
| 450 | // I/F, J/D aliasing should keep the load/store. |
| 451 | static float test19(float[] fa1, float[] fa2) { |
| 452 | fa1[0] = fa2[0]; |
| 453 | return fa1[0]; |
| 454 | } |
| 455 | |
| 456 | /// CHECK-START: TestClass Main.test20() load_store_elimination (before) |
| 457 | /// CHECK: NewInstance |
| 458 | /// CHECK: InstanceFieldSet |
| 459 | |
| 460 | /// CHECK-START: TestClass Main.test20() load_store_elimination (after) |
| 461 | /// CHECK: NewInstance |
| 462 | /// CHECK-NOT: InstanceFieldSet |
| 463 | |
| 464 | // Storing default heap value is redundant if the heap location has the |
| 465 | // default heap value. |
| 466 | static TestClass test20() { |
| 467 | TestClass obj = new TestClass(); |
| 468 | obj.i = 0; |
| 469 | return obj; |
| 470 | } |
| 471 | |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 472 | /// CHECK-START: void Main.test21() load_store_elimination (before) |
| 473 | /// CHECK: NewInstance |
| 474 | /// CHECK: InstanceFieldSet |
| 475 | /// CHECK: StaticFieldSet |
| 476 | /// CHECK: StaticFieldGet |
| 477 | |
| 478 | /// CHECK-START: void Main.test21() load_store_elimination (after) |
| 479 | /// CHECK: NewInstance |
| 480 | /// CHECK: InstanceFieldSet |
| 481 | /// CHECK: StaticFieldSet |
| 482 | /// CHECK: InstanceFieldGet |
| 483 | |
| 484 | // Loop side effects can kill heap values, stores need to be kept in that case. |
| 485 | static void test21() { |
| 486 | TestClass obj = new TestClass(); |
| 487 | obj.str = "abc"; |
| 488 | for (int i = 0; i < 2; i++) { |
| 489 | // Generate some loop side effect that does write. |
| 490 | obj.si = 1; |
| 491 | } |
| 492 | System.out.print(obj.str.substring(0, 0)); |
| 493 | } |
| 494 | |
| 495 | /// CHECK-START: int Main.test22() load_store_elimination (before) |
| 496 | /// CHECK: NewInstance |
| 497 | /// CHECK: InstanceFieldSet |
| 498 | /// CHECK: NewInstance |
| 499 | /// CHECK: InstanceFieldSet |
| 500 | /// CHECK: InstanceFieldGet |
| 501 | /// CHECK: NewInstance |
| 502 | /// CHECK: InstanceFieldSet |
| 503 | /// CHECK: InstanceFieldGet |
| 504 | /// CHECK: InstanceFieldGet |
| 505 | |
| 506 | /// CHECK-START: int Main.test22() load_store_elimination (after) |
| 507 | /// CHECK: NewInstance |
| 508 | /// CHECK: InstanceFieldSet |
| 509 | /// CHECK: NewInstance |
| 510 | /// CHECK-NOT: InstanceFieldSet |
| 511 | /// CHECK-NOT: InstanceFieldGet |
| 512 | /// CHECK: NewInstance |
| 513 | /// CHECK-NOT: InstanceFieldSet |
| 514 | /// CHECK: InstanceFieldGet |
| 515 | /// CHECK-NOT: InstanceFieldGet |
| 516 | |
| 517 | // Loop side effects only affects stores into singletons that dominiates the loop header. |
| 518 | static int test22() { |
| 519 | int sum = 0; |
| 520 | TestClass obj1 = new TestClass(); |
| 521 | obj1.i = 2; // This store can't be eliminated since it can be killed by loop side effects. |
| 522 | for (int i = 0; i < 2; i++) { |
| 523 | TestClass obj2 = new TestClass(); |
| 524 | obj2.i = 3; // This store can be eliminated since the singleton is inside the loop. |
| 525 | sum += obj2.i; |
| 526 | } |
| 527 | TestClass obj3 = new TestClass(); |
| 528 | obj3.i = 5; // This store can be eliminated since the singleton is created after the loop. |
| 529 | sum += obj1.i + obj3.i; |
| 530 | return sum; |
| 531 | } |
| 532 | |
| 533 | /// CHECK-START: int Main.test23(boolean) load_store_elimination (before) |
| 534 | /// CHECK: NewInstance |
| 535 | /// CHECK: InstanceFieldSet |
| 536 | /// CHECK: InstanceFieldGet |
| 537 | /// CHECK: InstanceFieldSet |
| 538 | /// CHECK: InstanceFieldGet |
| 539 | /// CHECK: Return |
| 540 | /// CHECK: InstanceFieldGet |
| 541 | /// CHECK: InstanceFieldSet |
| 542 | |
| 543 | /// CHECK-START: int Main.test23(boolean) load_store_elimination (after) |
| 544 | /// CHECK: NewInstance |
| 545 | /// CHECK-NOT: InstanceFieldSet |
| 546 | /// CHECK-NOT: InstanceFieldGet |
| 547 | /// CHECK: InstanceFieldSet |
| 548 | /// CHECK: InstanceFieldGet |
| 549 | /// CHECK: Return |
| 550 | /// CHECK-NOT: InstanceFieldGet |
| 551 | /// CHECK: InstanceFieldSet |
| 552 | |
| 553 | // Test store elimination on merging. |
| 554 | static int test23(boolean b) { |
| 555 | TestClass obj = new TestClass(); |
| 556 | obj.i = 3; // This store can be eliminated since the value flows into each branch. |
| 557 | if (b) { |
| 558 | obj.i += 1; // This store cannot be eliminated due to the merge later. |
| 559 | } else { |
| 560 | obj.i += 2; // This store cannot be eliminated due to the merge later. |
| 561 | } |
| 562 | return obj.i; |
| 563 | } |
| 564 | |
| 565 | /// CHECK-START: void Main.testFinalizable() load_store_elimination (before) |
| 566 | /// CHECK: NewInstance |
| 567 | /// CHECK: InstanceFieldSet |
| 568 | |
| 569 | /// CHECK-START: void Main.testFinalizable() load_store_elimination (after) |
| 570 | /// CHECK: NewInstance |
| 571 | /// CHECK: InstanceFieldSet |
| 572 | |
| 573 | // Allocations and stores into finalizable objects cannot be eliminated. |
| 574 | static void testFinalizable() { |
| 575 | Finalizable finalizable = new Finalizable(); |
| 576 | finalizable.i = Finalizable.VALUE; |
| 577 | } |
| 578 | |
| 579 | static java.lang.ref.WeakReference<Object> getWeakReference() { |
| 580 | return new java.lang.ref.WeakReference<>(new Object()); |
| 581 | } |
| 582 | |
| 583 | static void testFinalizableByForcingGc() { |
| 584 | testFinalizable(); |
| 585 | java.lang.ref.WeakReference<Object> reference = getWeakReference(); |
| 586 | |
| 587 | Runtime runtime = Runtime.getRuntime(); |
| 588 | for (int i = 0; i < 20; ++i) { |
| 589 | runtime.gc(); |
| 590 | System.runFinalization(); |
| 591 | try { |
| 592 | Thread.sleep(1); |
| 593 | } catch (InterruptedException e) { |
| 594 | throw new AssertionError(e); |
| 595 | } |
| 596 | |
| 597 | // Check to see if the weak reference has been garbage collected. |
| 598 | if (reference.get() == null) { |
| 599 | // A little bit more sleep time to make sure. |
| 600 | try { |
| 601 | Thread.sleep(100); |
| 602 | } catch (InterruptedException e) { |
| 603 | throw new AssertionError(e); |
| 604 | } |
| 605 | if (!Finalizable.sVisited) { |
| 606 | System.out.println("finalize() not called."); |
| 607 | } |
| 608 | return; |
| 609 | } |
| 610 | } |
| 611 | System.out.println("testFinalizableByForcingGc() failed to force gc."); |
| 612 | } |
| 613 | |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 614 | public static void assertIntEquals(int expected, int result) { |
| 615 | if (expected != result) { |
| 616 | throw new Error("Expected: " + expected + ", found: " + result); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | public static void assertFloatEquals(float expected, float result) { |
| 621 | if (expected != result) { |
| 622 | throw new Error("Expected: " + expected + ", found: " + result); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | public static void assertDoubleEquals(double expected, double result) { |
| 627 | if (expected != result) { |
| 628 | throw new Error("Expected: " + expected + ", found: " + result); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | public static void main(String[] args) { |
| 633 | assertDoubleEquals(Math.PI * Math.PI * Math.PI, calcCircleArea(Math.PI)); |
| 634 | assertIntEquals(test1(new TestClass(), new TestClass()), 3); |
| 635 | assertIntEquals(test2(new TestClass()), 1); |
| 636 | TestClass obj1 = new TestClass(); |
| 637 | TestClass obj2 = new TestClass(); |
| 638 | obj1.next = obj2; |
| 639 | assertIntEquals(test3(obj1), 10); |
| 640 | assertIntEquals(test4(new TestClass(), true), 1); |
| 641 | assertIntEquals(test4(new TestClass(), false), 1); |
| 642 | assertIntEquals(test5(new TestClass(), true), 1); |
| 643 | assertIntEquals(test5(new TestClass(), false), 2); |
| 644 | assertIntEquals(test6(new TestClass(), new TestClass(), true), 4); |
| 645 | assertIntEquals(test6(new TestClass(), new TestClass(), false), 2); |
| 646 | assertIntEquals(test7(new TestClass()), 1); |
| 647 | assertIntEquals(test8(), 1); |
| 648 | obj1 = new TestClass(); |
| 649 | obj2 = new TestClass(); |
| 650 | obj1.next = obj2; |
| 651 | assertIntEquals(test9(new TestClass()), 1); |
| 652 | assertIntEquals(test10(new TestClass(3, 4)), 3); |
| 653 | assertIntEquals(TestClass.si, 3); |
| 654 | assertIntEquals(test11(new TestClass()), 10); |
| 655 | assertIntEquals(test12(new TestClass(), new TestClass()), 10); |
| 656 | assertIntEquals(test13(new TestClass(), new TestClass2()), 3); |
| 657 | SubTestClass obj3 = new SubTestClass(); |
| 658 | assertIntEquals(test14(obj3, obj3), 2); |
| 659 | assertIntEquals(test15(), 2); |
| 660 | assertIntEquals(test16(), 3); |
| 661 | assertIntEquals(test17(), 0); |
| 662 | assertIntEquals(test18(new TestClass()), 1); |
| 663 | float[] fa1 = { 0.8f }; |
| 664 | float[] fa2 = { 1.8f }; |
| 665 | assertFloatEquals(test19(fa1, fa2), 1.8f); |
| 666 | assertFloatEquals(test20().i, 0); |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 667 | test21(); |
| 668 | assertIntEquals(test22(), 13); |
| 669 | assertIntEquals(test23(true), 4); |
| 670 | assertIntEquals(test23(false), 5); |
| 671 | testFinalizableByForcingGc(); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 672 | } |
| 673 | } |