1 /*
2  * Copyright (C) 2021 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 package com.android.art_cts;
18 
19 import junit.framework.TestCase;
20 
21 import static junit.framework.Assert.assertEquals;
22 
23 public class ArtTest extends TestCase {
24 
25   static class Helper_b197981962 {
26     int myField;
27     static Helper_b197981962 escape;
28 
runTest(boolean condition)29     static void runTest(boolean condition) {
30       Helper_b197981962 l = new Helper_b197981962();
31       // LSE will find that this store can be removed, as both branches override the value
32       // with a new one.
33       l.myField = 42;
34       if (condition) {
35         // LSE will remove this store as well, as it's the value after the store of 42 is removed.
36         l.myField = 0;
37         // This makes sure `m` gets materialized. At this point, the bug is that the partial LSE
38         // optimization thinks the value incoming this block for `m.myField` is 42, however that
39         // store, as well as the store to 0, have been removed.
40         escape = l;
41         // Do something the compiler cannot explore.
42         escape.getClass().getDeclaredMethods();
43         assertEquals(0, escape.myField);
44       } else {
45         l.myField = 3;
46         assertEquals(3, l.myField);
47       }
48     }
49   }
50 
test_b197981962()51   public void test_b197981962() {
52     // Run enough times to trigger compilation.
53     for (int i = 0; i < 100000; ++i) {
54       Helper_b197981962.runTest(true);
55       Helper_b197981962.runTest(false);
56     }
57   }
58 }
59