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 class Main {
18 
$noinline$testMain(Main m)19   public static void $noinline$testMain(Main m) {
20     expectEquals(0, m.myField);
21   }
22 
main(String[] args)23   public static void main(String[] args) {
24     $noinline$doTest(true);
25     $noinline$doTest(false);
26   }
27 
$noinline$doTest(boolean testValue)28   public static void $noinline$doTest(boolean testValue) {
29     Main m = new Main();
30     // LSE will find that this store can be removed, as both branches override the value with a new
31     // one.
32     m.myField = 42;
33     if (testValue) {
34       // LSE will remove this store as well, as it's the value after the store of 42 is removed.
35       m.myField = 0;
36       // This makes sure `m` gets materialized. At this point, the bug is that the partial LSE
37       // optimization thinks the value incoming this block for `m.myField` is 42, however that
38       // store, as well as the store to 0, have been removed.
39       $noinline$testMain(m);
40     } else {
41       m.myField = 3;
42       expectEquals(3, m.myField);
43     }
44   }
45 
expectEquals(int expected, int actual)46   public static void expectEquals(int expected, int actual) {
47     if (expected != actual) {
48       throw new Error("Expected " + expected + ", got " + actual);
49     }
50   }
51 
52   int myField = 0;
53 }
54