Nicolas Geoffray | b93a165 | 2016-06-27 10:03:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | // We make Main extend an unresolved super class. This will lead to an |
| 18 | // unresolved access to Foo.field, as we won't know if Main can access |
| 19 | // a package private field. |
| 20 | public class Main extends MissingSuperClass { |
| 21 | |
| 22 | public static void main(String[] args) { |
| 23 | instanceFieldTest(); |
| 24 | staticFieldTest(); |
| 25 | instanceFieldTest2(); |
| 26 | } |
| 27 | |
| 28 | /// CHECK-START: void Main.instanceFieldTest() inliner (before) |
| 29 | /// CHECK-NOT: InstanceFieldSet |
| 30 | |
| 31 | /// CHECK-START: void Main.instanceFieldTest() inliner (after) |
| 32 | /// CHECK: InstanceFieldSet |
| 33 | /// CHECK: UnresolvedInstanceFieldGet |
| 34 | |
| 35 | // Load store elimination used to remove the InstanceFieldSet, thinking |
| 36 | // that the UnresolvedInstanceFieldGet was not related. However inlining |
| 37 | // can put you in a situation where the UnresolvedInstanceFieldGet resolves |
| 38 | // to the same field as the one in InstanceFieldSet. So the InstanceFieldSet |
| 39 | // must be preserved. |
| 40 | |
| 41 | /// CHECK-START: void Main.instanceFieldTest() load_store_elimination (after) |
| 42 | /// CHECK: InstanceFieldSet |
| 43 | /// CHECK: UnresolvedInstanceFieldGet |
| 44 | public static void instanceFieldTest() { |
| 45 | Foo f = new Foo(); |
| 46 | if (f.iField != 42) { |
| 47 | throw new Error("Expected 42, got " + f.iField); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /// CHECK-START: void Main.instanceFieldTest2() inliner (before) |
| 52 | /// CHECK-NOT: InstanceFieldSet |
| 53 | /// CHECK-NOT: InstanceFieldGet |
| 54 | |
| 55 | /// CHECK-START: void Main.instanceFieldTest2() inliner (after) |
| 56 | /// CHECK: InstanceFieldSet |
| 57 | /// CHECK: InstanceFieldGet |
| 58 | /// CHECK: UnresolvedInstanceFieldSet |
| 59 | /// CHECK: InstanceFieldGet |
| 60 | |
| 61 | // Load store elimination will eliminate the first InstanceFieldGet because |
| 62 | // it simply follows an InstanceFieldSet. It must however not eliminate the second |
| 63 | // InstanceFieldGet, as the UnresolvedInstanceFieldSet might resolve to the same |
| 64 | // field. |
| 65 | |
| 66 | /// CHECK-START: void Main.instanceFieldTest2() load_store_elimination (after) |
| 67 | /// CHECK: InstanceFieldSet |
| 68 | /// CHECK-NOT: InstanceFieldGet |
| 69 | /// CHECK: UnresolvedInstanceFieldSet |
| 70 | /// CHECK: InstanceFieldGet |
| 71 | public static void instanceFieldTest2() { |
| 72 | Foo f = new Foo(); |
| 73 | int a = f.$inline$GetInstanceField(); |
| 74 | f.iField = 43; |
| 75 | a = f.$inline$GetInstanceField(); |
| 76 | if (a != 43) { |
| 77 | throw new Error("Expected 43, got " + a); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// CHECK-START: void Main.staticFieldTest() inliner (before) |
| 82 | /// CHECK-NOT: StaticFieldSet |
| 83 | |
| 84 | /// CHECK-START: void Main.staticFieldTest() inliner (after) |
| 85 | /// CHECK: StaticFieldSet |
| 86 | /// CHECK: StaticFieldSet |
| 87 | /// CHECK: UnresolvedStaticFieldGet |
| 88 | |
| 89 | /// CHECK-START: void Main.staticFieldTest() load_store_elimination (after) |
| 90 | /// CHECK: StaticFieldSet |
| 91 | /// CHECK: StaticFieldSet |
| 92 | /// CHECK: UnresolvedStaticFieldGet |
| 93 | public static void staticFieldTest() { |
| 94 | // Ensure Foo is initialized. |
| 95 | Foo f = new Foo(); |
| 96 | f.$inline$StaticSet42(); |
| 97 | f.$inline$StaticSet43(); |
| 98 | if (Foo.sField != 43) { |
| 99 | throw new Error("Expected 43, got " + Foo.sField); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | class Foo { |
| 105 | // field needs to be package-private to make the access in Main.main |
| 106 | // unresolved. |
| 107 | int iField; |
| 108 | static int sField; |
| 109 | |
| 110 | public void $inline$StaticSet42() { |
| 111 | sField = 42; |
| 112 | } |
| 113 | |
| 114 | public void $inline$StaticSet43() { |
| 115 | sField = 43; |
| 116 | } |
| 117 | |
| 118 | public int $inline$GetInstanceField() { |
| 119 | return iField; |
| 120 | } |
| 121 | |
| 122 | // Constructor needs to be public to get it resolved in Main.main |
| 123 | // and therefore inlined. |
| 124 | public Foo() { |
| 125 | iField = 42; |
| 126 | } |
| 127 | } |