Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | public 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 Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 38 | /// CHECK: <<Invoke:z\d+>> InvokeVirtual |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 39 | /// 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 Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 50 | /// CHECK: InvokeVirtual {{.*\.equals.*}} |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 51 | /// 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 Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 59 | /// CHECK: InvokeVirtual |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 60 | /// 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 | } |