blob: 2c97bedbaa126e6929ba06b66fb46e60b079def7 [file] [log] [blame]
Nicolas Geoffraya90d4892016-06-01 16:30:20 +01001/*
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
17class SuperClass {
18}
19
20class ChildClass extends SuperClass {
21}
22
23public class Main {
24
Vladimir Marko87584542017-12-12 17:47:52 +000025 public static void main(String[] args) {
26 test1();
27 test2();
28 }
29
30 /// CHECK-START: void Main.test1() builder (after)
Nicolas Geoffraya90d4892016-06-01 16:30:20 +010031 /// CHECK: BoundType klass:SuperClass can_be_null:false exact:false
32
Vladimir Marko87584542017-12-12 17:47:52 +000033 /// CHECK-START: void Main.test1() builder (after)
Nicolas Geoffraya90d4892016-06-01 16:30:20 +010034 /// CHECK-NOT: BoundType klass:SuperClass can_be_null:false exact:true
Vladimir Marko87584542017-12-12 17:47:52 +000035 public static void test1() {
Nicolas Geoffraya90d4892016-06-01 16:30:20 +010036 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 }
Vladimir Marko87584542017-12-12 17:47:52 +000053
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
Alexey Frunzedfc30af2018-01-24 16:25:10 -080062 /// CHECK-START-{ARM,ARM64,MIPS,MIPS64}: boolean Main.$noinline$instanceOfString(java.lang.Object) disassembly (after)
Vladimir Marko87584542017-12-12 17:47:52 +000063 /// 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.
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
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 }
Nicolas Geoffraya90d4892016-06-01 16:30:20 +010082}