blob: fdc0919f2b5145f8a4017f9f295eb80bd86756bd [file] [log] [blame]
Roland Levillainf355c3f2016-03-30 19:09:03 +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
Igor Murashkin16909072017-06-27 11:20:31 -070017import java.lang.reflect.Method;
18
Roland Levillainf355c3f2016-03-30 19:09:03 +010019public class Main {
20
21 public static void main(String args[]) {
22 expectEqualsByte((byte)1, booleanToByte(true));
23 expectEqualsShort((short)1, booleanToShort(true));
24 expectEqualsChar((char)1, booleanToChar(true));
25 expectEqualsInt(1, booleanToInt(true));
26 expectEqualsLong(1L, booleanToLong(true));
Igor Murashkin16909072017-06-27 11:20:31 -070027 expectEqualsLong(1L, $noinline$runSmaliTest("booleanToLong", true));
Roland Levillainf355c3f2016-03-30 19:09:03 +010028
29 expectEqualsInt(1, longToIntOfBoolean());
Igor Murashkin16909072017-06-27 11:20:31 -070030 expectEqualsInt(1, $noinline$runSmaliTest("longToIntOfBoolean"));
Roland Levillainf355c3f2016-03-30 19:09:03 +010031
32 System.out.println("passed");
33 }
34
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -070035 /// CHECK-START: byte Main.booleanToByte(boolean) instruction_simplifier$after_bce (after)
Roland Levillainf355c3f2016-03-30 19:09:03 +010036 /// CHECK: <<Arg:z\d+>> ParameterValue
37 /// CHECK-DAG: Return [<<Arg>>]
38
39 static byte booleanToByte(boolean b) {
40 return (byte)(b ? 1 : 0);
41 }
42
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -070043 /// CHECK-START: short Main.booleanToShort(boolean) instruction_simplifier$after_bce (after)
Roland Levillainf355c3f2016-03-30 19:09:03 +010044 /// CHECK: <<Arg:z\d+>> ParameterValue
45 /// CHECK-DAG: Return [<<Arg>>]
46
47 static short booleanToShort(boolean b) {
48 return (short)(b ? 1 : 0);
49 }
50
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -070051 /// CHECK-START: char Main.booleanToChar(boolean) instruction_simplifier$after_bce (after)
Roland Levillainf355c3f2016-03-30 19:09:03 +010052 /// CHECK: <<Arg:z\d+>> ParameterValue
53 /// CHECK-DAG: Return [<<Arg>>]
54
55 static char booleanToChar(boolean b) {
56 return (char)(b ? 1 : 0);
57 }
58
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -070059 /// CHECK-START: int Main.booleanToInt(boolean) instruction_simplifier$after_bce (after)
Roland Levillainf355c3f2016-03-30 19:09:03 +010060 /// CHECK: <<Arg:z\d+>> ParameterValue
61 /// CHECK-DAG: Return [<<Arg>>]
62
63 static int booleanToInt(boolean b) {
64 return b ? 1 : 0;
65 }
66
67 /// CHECK-START: long Main.booleanToLong(boolean) builder (after)
68 /// CHECK: <<Arg:z\d+>> ParameterValue
Igor Murashkin16909072017-06-27 11:20:31 -070069 /// CHECK-DAG: <<IZero:i\d+>> IntConstant 0
70 /// CHECK-DAG: <<Zero:j\d+>> LongConstant 0
71 /// CHECK-DAG: <<One:j\d+>> LongConstant 1
72 /// CHECK-DAG: <<Cond:z\d+>> Equal [<<Arg>>,<<IZero>>]
Roland Levillainf355c3f2016-03-30 19:09:03 +010073 /// CHECK-DAG: If [<<Cond>>]
Igor Murashkin16909072017-06-27 11:20:31 -070074 /// CHECK-DAG: <<Phi:j\d+>> Phi [<<One>>,<<Zero>>]
75 /// CHECK-DAG: Return [<<Phi>>]
76
77 /// CHECK-START: long Main.booleanToLong(boolean) select_generator (after)
78 /// CHECK-NOT: IntConstant
79 /// CHECK-NOT: Equal
80 /// CHECK-NOT: If
81 /// CHECK-NOT: Phi
Roland Levillainf355c3f2016-03-30 19:09:03 +010082
83 /// CHECK-START: long Main.booleanToLong(boolean) select_generator (after)
84 /// CHECK: <<Arg:z\d+>> ParameterValue
Igor Murashkin16909072017-06-27 11:20:31 -070085 /// CHECK-DAG: <<Zero:j\d+>> LongConstant 0
86 /// CHECK-DAG: <<One:j\d+>> LongConstant 1
87 /// CHECK-DAG: <<Sel:j\d+>> Select [<<Zero>>,<<One>>,<<Arg>>]
88 /// CHECK-DAG: Return [<<Sel>>]
Roland Levillainf355c3f2016-03-30 19:09:03 +010089
Igor Murashkin16909072017-06-27 11:20:31 -070090 // As of now, the code is not optimized any further than the above.
91 // TODO: Re-enable checks below after simplifier is updated to handle this pattern: b/63064517
92
93 // CHECK-START: long Main.booleanToLong(boolean) instruction_simplifier$after_bce (after)
94 // CHECK: <<Arg:z\d+>> ParameterValue
95 // CHECK-DAG: <<ZToJ:j\d+>> TypeConversion [<<Arg>>]
96 // CHECK-DAG: Return [<<ZToJ>>]
Roland Levillainf355c3f2016-03-30 19:09:03 +010097
98 static long booleanToLong(boolean b) {
99 return b ? 1 : 0;
100 }
101
102 /// CHECK-START: int Main.longToIntOfBoolean() builder (after)
103 /// CHECK-DAG: <<Method:[ij]\d+>> CurrentMethod
104 /// CHECK-DAG: <<Sget:z\d+>> StaticFieldGet
105 /// CHECK-DAG: <<ZToJ:j\d+>> InvokeStaticOrDirect [<<Sget>>,<<Method>>]
106 /// CHECK-DAG: <<JToI:i\d+>> TypeConversion [<<ZToJ>>]
107 /// CHECK-DAG: Return [<<JToI>>]
108
109 /// CHECK-START: int Main.longToIntOfBoolean() inliner (after)
110 /// CHECK-DAG: <<Method:[ij]\d+>> CurrentMethod
Igor Murashkin16909072017-06-27 11:20:31 -0700111 /// CHECK-DAG: <<Zero:j\d+>> LongConstant 0
112 /// CHECK-DAG: <<One:j\d+>> LongConstant 1
Roland Levillainf355c3f2016-03-30 19:09:03 +0100113 /// CHECK-DAG: <<Sget:z\d+>> StaticFieldGet
114 /// CHECK-DAG: If [<<Sget>>]
Igor Murashkin16909072017-06-27 11:20:31 -0700115 /// CHECK-DAG: <<Phi:j\d+>> Phi [<<One>>,<<Zero>>]
116 /// CHECK-DAG: <<JToI:i\d+>> TypeConversion [<<Phi>>]
Roland Levillainf355c3f2016-03-30 19:09:03 +0100117 /// CHECK-DAG: Return [<<JToI>>]
118
Igor Murashkin16909072017-06-27 11:20:31 -0700119 /// CHECK-START: long Main.booleanToLong(boolean) select_generator (after)
120 /// CHECK-NOT: IntConstant
121 /// CHECK-NOT: Equal
122 /// CHECK-NOT: If
123 /// CHECK-NOT: Phi
124
Roland Levillainf355c3f2016-03-30 19:09:03 +0100125 /// CHECK-START: int Main.longToIntOfBoolean() select_generator (after)
126 /// CHECK-DAG: <<Method:[ij]\d+>> CurrentMethod
Igor Murashkin16909072017-06-27 11:20:31 -0700127 /// CHECK-DAG: <<Zero:j\d+>> LongConstant 0
128 /// CHECK-DAG: <<One:j\d+>> LongConstant 1
Roland Levillainf355c3f2016-03-30 19:09:03 +0100129 /// CHECK-DAG: <<Sget:z\d+>> StaticFieldGet
Igor Murashkin16909072017-06-27 11:20:31 -0700130 /// CHECK-DAG: <<Sel:j\d+>> Select [<<Zero>>,<<One>>,<<Sget>>]
131 /// CHECK-DAG: <<JToI:i\d+>> TypeConversion [<<Sel>>]
Roland Levillainf355c3f2016-03-30 19:09:03 +0100132 /// CHECK-DAG: Return [<<JToI>>]
133
Igor Murashkin16909072017-06-27 11:20:31 -0700134 // As of now, the code is not optimized any further than the above.
135 // TODO: Re-enable checks below after simplifier is updated to handle this pattern: b/63064517
136
137 // CHECK-START: int Main.longToIntOfBoolean() instruction_simplifier$after_bce (after)
138 // CHECK-DAG: <<Method:[ij]\d+>> CurrentMethod
139 // CHECK-DAG: <<Sget:z\d+>> StaticFieldGet
140 // CHECK-DAG: Return [<<Sget>>]
Roland Levillainf355c3f2016-03-30 19:09:03 +0100141
142 static int longToIntOfBoolean() {
143 long l = booleanToLong(booleanField);
144 return (int) l;
145 }
146
147
148 private static void expectEqualsByte(byte expected, byte result) {
149 if (expected != result) {
150 throw new Error("Expected: " + expected + ", found: " + result);
151 }
152 }
153
154 private static void expectEqualsShort(short expected, short result) {
155 if (expected != result) {
156 throw new Error("Expected: " + expected + ", found: " + result);
157 }
158 }
159
160 private static void expectEqualsChar(char expected, char result) {
161 if (expected != result) {
162 throw new Error("Expected: " + expected + ", found: " + result);
163 }
164 }
165
166 private static void expectEqualsInt(int expected, int result) {
167 if (expected != result) {
168 throw new Error("Expected: " + expected + ", found: " + result);
169 }
170 }
171
172 private static void expectEqualsLong(long expected, long result) {
173 if (expected != result) {
174 throw new Error("Expected: " + expected + ", found: " + result);
175 }
176 }
177
Igor Murashkin16909072017-06-27 11:20:31 -0700178 public static long $noinline$runSmaliTest(String name, boolean input) {
179 try {
180 Class<?> c = Class.forName("SmaliTests");
181 Method m = c.getMethod(name, boolean.class);
182 return (Long) m.invoke(null, input);
183 } catch (Exception ex) {
184 throw new Error(ex);
185 }
186 }
187
188 public static int $noinline$runSmaliTest(String name) {
189 try {
190 Class<?> c = Class.forName("SmaliTests");
191 Method m = c.getMethod(name);
192 return (Integer) m.invoke(null);
193 } catch (Exception ex) {
194 throw new Error(ex);
195 }
196 }
197
Roland Levillainf355c3f2016-03-30 19:09:03 +0100198
199 public static boolean booleanField = true;
200
201}