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 class SuperClass { 18 } 19 20 class ChildClass extends SuperClass { 21 } 22 23 public class Main { 24 main(String[] args)25 public static void main(String[] args) { 26 test1(); 27 test2(); 28 } 29 30 /// CHECK-START: void Main.test1() builder (after) 31 /// CHECK: BoundType klass:SuperClass can_be_null:false exact:false 32 33 /// CHECK-START: void Main.test1() builder (after) 34 /// CHECK-NOT: BoundType klass:SuperClass can_be_null:false exact:true test1()35 public static void test1() { 36 Object obj = new ChildClass(); 37 38 // We need a fixed point iteration to hit the bogus type update 39 // of 'obj' below, so create a loop that updates the type of 'obj'. 40 for (int i = 1; i < 1; i++) { 41 obj = new Object(); 42 } 43 44 if (obj instanceof SuperClass) { 45 // We used to wrongly type obj as an exact SuperClass from this point, 46 // meaning we were statically determining that the following instanceof 47 // would always fail. 48 if (!(obj instanceof ChildClass)) { 49 throw new Error("Expected a ChildClass, got " + obj.getClass()); 50 } 51 } 52 } 53 54 /// CHECK-START-X86: boolean Main.$noinline$instanceOfString(java.lang.Object) disassembly (after) 55 /// CHECK: InstanceOf check_kind:exact_check 56 /// CHECK-NOT: {{.*fs:.*}} 57 58 /// CHECK-START-X86_64: boolean Main.$noinline$instanceOfString(java.lang.Object) disassembly (after) 59 /// CHECK: InstanceOf check_kind:exact_check 60 /// CHECK-NOT: {{.*gs:.*}} 61 62 /// CHECK-START-{ARM,ARM64,MIPS,MIPS64}: boolean Main.$noinline$instanceOfString(java.lang.Object) disassembly (after) 63 /// CHECK: InstanceOf check_kind:exact_check 64 // For ARM and ARM64, the marking register (r8 and x20, respectively) can be used in 65 // non-CC configs for any other purpose, so we'd need a config-specific checker test. 66 // TODO: Add the checks when we support config-specific tests. $noinline$instanceOfString(Object o)67 public static boolean $noinline$instanceOfString(Object o) { 68 // String is a final class, so `instanceof String` should use exact check. 69 // String is in the boot image, so we should avoid read barriers. The presence 70 // of the read barrier can be checked in the architecture-specific disassembly. 71 return o instanceof String; 72 } 73 test2()74 public static void test2() { 75 if ($noinline$instanceOfString(new Object())) { 76 throw new Error(); 77 } 78 if (!$noinline$instanceOfString(new String())) { 79 throw new Error(); 80 } 81 } 82 } 83