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 { |
Vladimir Marko | dce016e | 2016-04-28 13:10:02 +0100 | [diff] [blame] | 19 | public static boolean doThrow = false; |
| 20 | |
| 21 | public static void assertIntEquals(int expected, int result) { |
| 22 | if (expected != result) { |
| 23 | throw new Error("Expected: " + expected + ", found: " + result); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | public static void assertBooleanEquals(boolean expected, boolean result) { |
| 28 | if (expected != result) { |
| 29 | throw new Error("Expected: " + expected + ", found: " + result); |
| 30 | } |
| 31 | } |
| 32 | |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 33 | public static void main(String[] args) { |
| 34 | stringEqualsSame(); |
| 35 | stringArgumentNotNull("Foo"); |
Vladimir Marko | dce016e | 2016-04-28 13:10:02 +0100 | [diff] [blame] | 36 | |
| 37 | assertIntEquals(0, $opt$noinline$getStringLength("")); |
| 38 | assertIntEquals(3, $opt$noinline$getStringLength("abc")); |
| 39 | assertIntEquals(10, $opt$noinline$getStringLength("0123456789")); |
| 40 | |
| 41 | assertBooleanEquals(true, $opt$noinline$isStringEmpty("")); |
| 42 | assertBooleanEquals(false, $opt$noinline$isStringEmpty("abc")); |
| 43 | assertBooleanEquals(false, $opt$noinline$isStringEmpty("0123456789")); |
| 44 | } |
| 45 | |
| 46 | /// CHECK-START: int Main.$opt$noinline$getStringLength(java.lang.String) instruction_simplifier (before) |
| 47 | /// CHECK-DAG: <<Length:i\d+>> InvokeVirtual intrinsic:StringLength |
| 48 | /// CHECK-DAG: Return [<<Length>>] |
| 49 | |
| 50 | /// CHECK-START: int Main.$opt$noinline$getStringLength(java.lang.String) instruction_simplifier (after) |
| 51 | /// CHECK-DAG: <<String:l\d+>> ParameterValue |
| 52 | /// CHECK-DAG: <<NullCk:l\d+>> NullCheck [<<String>>] |
| 53 | /// CHECK-DAG: <<Length:i\d+>> ArrayLength [<<NullCk>>] is_string_length:true |
| 54 | /// CHECK-DAG: Return [<<Length>>] |
| 55 | |
| 56 | /// CHECK-START: int Main.$opt$noinline$getStringLength(java.lang.String) instruction_simplifier (after) |
| 57 | /// CHECK-NOT: InvokeVirtual intrinsic:StringLength |
| 58 | |
| 59 | static public int $opt$noinline$getStringLength(String s) { |
| 60 | if (doThrow) { throw new Error(); } |
| 61 | return s.length(); |
| 62 | } |
| 63 | |
| 64 | /// CHECK-START: boolean Main.$opt$noinline$isStringEmpty(java.lang.String) instruction_simplifier (before) |
| 65 | /// CHECK-DAG: <<IsEmpty:z\d+>> InvokeVirtual intrinsic:StringIsEmpty |
| 66 | /// CHECK-DAG: Return [<<IsEmpty>>] |
| 67 | |
| 68 | /// CHECK-START: boolean Main.$opt$noinline$isStringEmpty(java.lang.String) instruction_simplifier (after) |
| 69 | /// CHECK-DAG: <<String:l\d+>> ParameterValue |
| 70 | /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0 |
| 71 | /// CHECK-DAG: <<NullCk:l\d+>> NullCheck [<<String>>] |
| 72 | /// CHECK-DAG: <<Length:i\d+>> ArrayLength [<<NullCk>>] is_string_length:true |
| 73 | /// CHECK-DAG: <<IsEmpty:z\d+>> Equal [<<Length>>,<<Const0>>] |
| 74 | /// CHECK-DAG: Return [<<IsEmpty>>] |
| 75 | |
| 76 | /// CHECK-START: boolean Main.$opt$noinline$isStringEmpty(java.lang.String) instruction_simplifier (after) |
| 77 | /// CHECK-NOT: InvokeVirtual intrinsic:StringIsEmpty |
| 78 | |
| 79 | static public boolean $opt$noinline$isStringEmpty(String s) { |
| 80 | if (doThrow) { throw new Error(); } |
| 81 | return s.isEmpty(); |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /// CHECK-START: boolean Main.stringEqualsSame() instruction_simplifier (before) |
| 85 | /// CHECK: InvokeStaticOrDirect |
| 86 | |
| 87 | /// CHECK-START: boolean Main.stringEqualsSame() register (before) |
| 88 | /// CHECK: <<Const1:i\d+>> IntConstant 1 |
| 89 | /// CHECK: Return [<<Const1>>] |
| 90 | |
| 91 | /// CHECK-START: boolean Main.stringEqualsSame() register (before) |
| 92 | /// CHECK-NOT: InvokeStaticOrDirect |
| 93 | public static boolean stringEqualsSame() { |
| 94 | return $inline$callStringEquals("obj", "obj"); |
| 95 | } |
| 96 | |
| 97 | /// CHECK-START: boolean Main.stringEqualsNull() register (after) |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 98 | /// CHECK: <<Invoke:z\d+>> InvokeVirtual |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 99 | /// CHECK: Return [<<Invoke>>] |
| 100 | public static boolean stringEqualsNull() { |
| 101 | String o = (String)myObject; |
| 102 | return $inline$callStringEquals(o, o); |
| 103 | } |
| 104 | |
| 105 | public static boolean $inline$callStringEquals(String a, String b) { |
| 106 | return a.equals(b); |
| 107 | } |
| 108 | |
| 109 | /// CHECK-START-X86: boolean Main.stringArgumentNotNull(java.lang.Object) disassembly (after) |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 110 | /// CHECK: InvokeVirtual {{.*\.equals.*}} |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 111 | /// CHECK-NOT: test |
| 112 | public static boolean stringArgumentNotNull(Object obj) { |
| 113 | obj.getClass(); |
| 114 | return "foo".equals(obj); |
| 115 | } |
| 116 | |
| 117 | // Test is very brittle as it depends on the order we emit instructions. |
| 118 | /// CHECK-START-X86: boolean Main.stringArgumentIsString() disassembly (after) |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 119 | /// CHECK: InvokeVirtual |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 120 | /// CHECK: test |
| 121 | /// CHECK: jz/eq |
| 122 | // Check that we don't try to compare the classes. |
| 123 | /// CHECK-NOT: mov |
| 124 | /// CHECK: cmp |
| 125 | public static boolean stringArgumentIsString() { |
| 126 | return "foo".equals(myString); |
| 127 | } |
| 128 | |
| 129 | static String myString; |
| 130 | static Object myObject; |
| 131 | } |