blob: be666e94fade8a0a20c55c62907d69757f02c78b [file] [log] [blame]
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001/*
2 * Copyright (C) 2015 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
18public class Main {
19 public static void main(String[] args) {
20 stringEqualsSame();
21 stringArgumentNotNull("Foo");
22 }
23
24 /// CHECK-START: boolean Main.stringEqualsSame() instruction_simplifier (before)
25 /// CHECK: InvokeStaticOrDirect
26
27 /// CHECK-START: boolean Main.stringEqualsSame() register (before)
28 /// CHECK: <<Const1:i\d+>> IntConstant 1
29 /// CHECK: Return [<<Const1>>]
30
31 /// CHECK-START: boolean Main.stringEqualsSame() register (before)
32 /// CHECK-NOT: InvokeStaticOrDirect
33 public static boolean stringEqualsSame() {
34 return $inline$callStringEquals("obj", "obj");
35 }
36
37 /// CHECK-START: boolean Main.stringEqualsNull() register (after)
Nicolas Geoffraye5234232015-12-02 09:06:11 +000038 /// CHECK: <<Invoke:z\d+>> InvokeVirtual
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010039 /// CHECK: Return [<<Invoke>>]
40 public static boolean stringEqualsNull() {
41 String o = (String)myObject;
42 return $inline$callStringEquals(o, o);
43 }
44
45 public static boolean $inline$callStringEquals(String a, String b) {
46 return a.equals(b);
47 }
48
49 /// CHECK-START-X86: boolean Main.stringArgumentNotNull(java.lang.Object) disassembly (after)
Jeff Haodcdc85b2015-12-04 14:06:18 -080050 /// CHECK: InvokeVirtual {{.*\.equals.*}}
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010051 /// CHECK-NOT: test
52 public static boolean stringArgumentNotNull(Object obj) {
53 obj.getClass();
54 return "foo".equals(obj);
55 }
56
57 // Test is very brittle as it depends on the order we emit instructions.
58 /// CHECK-START-X86: boolean Main.stringArgumentIsString() disassembly (after)
Nicolas Geoffraye5234232015-12-02 09:06:11 +000059 /// CHECK: InvokeVirtual
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010060 /// CHECK: test
61 /// CHECK: jz/eq
62 // Check that we don't try to compare the classes.
63 /// CHECK-NOT: mov
64 /// CHECK: cmp
65 public static boolean stringArgumentIsString() {
66 return "foo".equals(myString);
67 }
68
69 static String myString;
70 static Object myObject;
71}