blob: 1f0017e6e0021b981abad40a87eaef100387369b [file] [log] [blame]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001/*
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
17public class Main {
18
19 public static void assertIntEquals(int expected, int result) {
20 if (expected != result) {
21 throw new Error("Expected: " + expected + ", found: " + result);
22 }
23 }
24
25 public static void assertLongEquals(long expected, long result) {
26 if (expected != result) {
27 throw new Error("Expected: " + expected + ", found: " + result);
28 }
29 }
30
31 /**
32 * Tiny programs exercising optimizations of arithmetic identities.
33 */
34
35 // CHECK-START: long Main.Add0(long) instruction_simplifier (before)
36 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
37 // CHECK-DAG: [[Const0:j\d+]] LongConstant 0
38 // CHECK-DAG: [[Add:j\d+]] Add [ [[Const0]] [[Arg]] ]
39 // CHECK-DAG: Return [ [[Add]] ]
40
41 // CHECK-START: long Main.Add0(long) instruction_simplifier (after)
42 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000043 // CHECK-DAG: Return [ [[Arg]] ]
Alexandre Rames74417692015-04-09 15:21:41 +010044 //
45 // CHECK-START: long Main.Add0(long) instruction_simplifier (after)
46 // CHECK-NOT: Add
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000047
48 public static long Add0(long arg) {
49 return 0 + arg;
50 }
51
52 // CHECK-START: int Main.AndAllOnes(int) instruction_simplifier (before)
53 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
54 // CHECK-DAG: [[ConstF:i\d+]] IntConstant -1
55 // CHECK-DAG: [[And:i\d+]] And [ [[Arg]] [[ConstF]] ]
56 // CHECK-DAG: Return [ [[And]] ]
57
58 // CHECK-START: int Main.AndAllOnes(int) instruction_simplifier (after)
59 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000060 // CHECK-DAG: Return [ [[Arg]] ]
61
Alexandre Rames74417692015-04-09 15:21:41 +010062 // CHECK-START: int Main.AndAllOnes(int) instruction_simplifier (after)
63 // CHECK-NOT: And
64
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000065 public static int AndAllOnes(int arg) {
66 return arg & -1;
67 }
68
69 // CHECK-START: long Main.Div1(long) instruction_simplifier (before)
70 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
71 // CHECK-DAG: [[Const1:j\d+]] LongConstant 1
72 // CHECK-DAG: [[Div:j\d+]] Div [ [[Arg]] [[Const1]] ]
73 // CHECK-DAG: Return [ [[Div]] ]
74
75 // CHECK-START: long Main.Div1(long) instruction_simplifier (after)
76 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000077 // CHECK-DAG: Return [ [[Arg]] ]
78
Alexandre Rames74417692015-04-09 15:21:41 +010079 // CHECK-START: long Main.Div1(long) instruction_simplifier (after)
80 // CHECK-NOT: Div
81
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000082 public static long Div1(long arg) {
83 return arg / 1;
84 }
85
86 // CHECK-START: int Main.DivN1(int) instruction_simplifier (before)
87 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
88 // CHECK-DAG: [[ConstN1:i\d+]] IntConstant -1
89 // CHECK-DAG: [[Div:i\d+]] Div [ [[Arg]] [[ConstN1]] ]
90 // CHECK-DAG: Return [ [[Div]] ]
91
92 // CHECK-START: int Main.DivN1(int) instruction_simplifier (after)
93 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
94 // CHECK-DAG: [[Neg:i\d+]] Neg [ [[Arg]] ]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000095 // CHECK-DAG: Return [ [[Neg]] ]
96
Alexandre Rames74417692015-04-09 15:21:41 +010097 // CHECK-START: int Main.DivN1(int) instruction_simplifier (after)
98 // CHECK-NOT: Div
99
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000100 public static int DivN1(int arg) {
101 return arg / -1;
102 }
103
104 // CHECK-START: long Main.Mul1(long) instruction_simplifier (before)
105 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
106 // CHECK-DAG: [[Const1:j\d+]] LongConstant 1
107 // CHECK-DAG: [[Mul:j\d+]] Mul [ [[Arg]] [[Const1]] ]
108 // CHECK-DAG: Return [ [[Mul]] ]
109
110 // CHECK-START: long Main.Mul1(long) instruction_simplifier (after)
111 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000112 // CHECK-DAG: Return [ [[Arg]] ]
113
Alexandre Rames74417692015-04-09 15:21:41 +0100114 // CHECK-START: long Main.Mul1(long) instruction_simplifier (after)
115 // CHECK-NOT: Mul
116
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000117 public static long Mul1(long arg) {
118 return arg * 1;
119 }
120
121 // CHECK-START: int Main.MulN1(int) instruction_simplifier (before)
122 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
123 // CHECK-DAG: [[ConstN1:i\d+]] IntConstant -1
124 // CHECK-DAG: [[Mul:i\d+]] Mul [ [[Arg]] [[ConstN1]] ]
125 // CHECK-DAG: Return [ [[Mul]] ]
126
127 // CHECK-START: int Main.MulN1(int) instruction_simplifier (after)
128 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
129 // CHECK-DAG: [[Neg:i\d+]] Neg [ [[Arg]] ]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000130 // CHECK-DAG: Return [ [[Neg]] ]
131
Alexandre Rames74417692015-04-09 15:21:41 +0100132 // CHECK-START: int Main.MulN1(int) instruction_simplifier (after)
133 // CHECK-NOT: Mul
134
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000135 public static int MulN1(int arg) {
136 return arg * -1;
137 }
138
139 // CHECK-START: long Main.MulPowerOfTwo128(long) instruction_simplifier (before)
140 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
141 // CHECK-DAG: [[Const128:j\d+]] LongConstant 128
142 // CHECK-DAG: [[Mul:j\d+]] Mul [ [[Arg]] [[Const128]] ]
143 // CHECK-DAG: Return [ [[Mul]] ]
144
145 // CHECK-START: long Main.MulPowerOfTwo128(long) instruction_simplifier (after)
146 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
147 // CHECK-DAG: [[Const7:i\d+]] IntConstant 7
148 // CHECK-DAG: [[Shl:j\d+]] Shl [ [[Arg]] [[Const7]] ]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000149 // CHECK-DAG: Return [ [[Shl]] ]
150
Alexandre Rames74417692015-04-09 15:21:41 +0100151 // CHECK-START: long Main.MulPowerOfTwo128(long) instruction_simplifier (after)
152 // CHECK-NOT: Mul
153
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000154 public static long MulPowerOfTwo128(long arg) {
155 return arg * 128;
156 }
157
158 // CHECK-START: int Main.Or0(int) instruction_simplifier (before)
159 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
160 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
161 // CHECK-DAG: [[Or:i\d+]] Or [ [[Arg]] [[Const0]] ]
162 // CHECK-DAG: Return [ [[Or]] ]
163
164 // CHECK-START: int Main.Or0(int) instruction_simplifier (after)
165 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000166 // CHECK-DAG: Return [ [[Arg]] ]
167
Alexandre Rames74417692015-04-09 15:21:41 +0100168 // CHECK-START: int Main.Or0(int) instruction_simplifier (after)
169 // CHECK-NOT: Or
170
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000171 public static int Or0(int arg) {
172 return arg | 0;
173 }
174
175 // CHECK-START: long Main.OrSame(long) instruction_simplifier (before)
176 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
177 // CHECK-DAG: [[Or:j\d+]] Or [ [[Arg]] [[Arg]] ]
178 // CHECK-DAG: Return [ [[Or]] ]
179
180 // CHECK-START: long Main.OrSame(long) instruction_simplifier (after)
181 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000182 // CHECK-DAG: Return [ [[Arg]] ]
183
Alexandre Rames74417692015-04-09 15:21:41 +0100184 // CHECK-START: long Main.OrSame(long) instruction_simplifier (after)
185 // CHECK-NOT: Or
186
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000187 public static long OrSame(long arg) {
188 return arg | arg;
189 }
190
191 // CHECK-START: int Main.Shl0(int) instruction_simplifier (before)
192 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
193 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
194 // CHECK-DAG: [[Shl:i\d+]] Shl [ [[Arg]] [[Const0]] ]
195 // CHECK-DAG: Return [ [[Shl]] ]
196
197 // CHECK-START: int Main.Shl0(int) instruction_simplifier (after)
198 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000199 // CHECK-DAG: Return [ [[Arg]] ]
200
Alexandre Rames74417692015-04-09 15:21:41 +0100201 // CHECK-START: int Main.Shl0(int) instruction_simplifier (after)
202 // CHECK-NOT: Shl
203
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000204 public static int Shl0(int arg) {
205 return arg << 0;
206 }
207
208 // CHECK-START: long Main.Shr0(long) instruction_simplifier (before)
209 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
210 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
211 // CHECK-DAG: [[Shr:j\d+]] Shr [ [[Arg]] [[Const0]] ]
212 // CHECK-DAG: Return [ [[Shr]] ]
213
214 // CHECK-START: long Main.Shr0(long) instruction_simplifier (after)
215 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000216 // CHECK-DAG: Return [ [[Arg]] ]
217
Alexandre Rames74417692015-04-09 15:21:41 +0100218 // CHECK-START: long Main.Shr0(long) instruction_simplifier (after)
219 // CHECK-NOT: Shr
220
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000221 public static long Shr0(long arg) {
222 return arg >> 0;
223 }
224
225 // CHECK-START: long Main.Sub0(long) instruction_simplifier (before)
226 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
227 // CHECK-DAG: [[Const0:j\d+]] LongConstant 0
228 // CHECK-DAG: [[Sub:j\d+]] Sub [ [[Arg]] [[Const0]] ]
229 // CHECK-DAG: Return [ [[Sub]] ]
230
231 // CHECK-START: long Main.Sub0(long) instruction_simplifier (after)
232 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000233 // CHECK-DAG: Return [ [[Arg]] ]
234
Alexandre Rames74417692015-04-09 15:21:41 +0100235 // CHECK-START: long Main.Sub0(long) instruction_simplifier (after)
236 // CHECK-NOT: Sub
237
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000238 public static long Sub0(long arg) {
239 return arg - 0;
240 }
241
242 // CHECK-START: int Main.SubAliasNeg(int) instruction_simplifier (before)
243 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
244 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
245 // CHECK-DAG: [[Sub:i\d+]] Sub [ [[Const0]] [[Arg]] ]
246 // CHECK-DAG: Return [ [[Sub]] ]
247
248 // CHECK-START: int Main.SubAliasNeg(int) instruction_simplifier (after)
249 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
250 // CHECK-DAG: [[Neg:i\d+]] Neg [ [[Arg]] ]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000251 // CHECK-DAG: Return [ [[Neg]] ]
252
Alexandre Rames74417692015-04-09 15:21:41 +0100253 // CHECK-START: int Main.SubAliasNeg(int) instruction_simplifier (after)
254 // CHECK-NOT: Sub
255
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000256 public static int SubAliasNeg(int arg) {
257 return 0 - arg;
258 }
259
260 // CHECK-START: long Main.UShr0(long) instruction_simplifier (before)
261 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
262 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
263 // CHECK-DAG: [[UShr:j\d+]] UShr [ [[Arg]] [[Const0]] ]
264 // CHECK-DAG: Return [ [[UShr]] ]
265
266 // CHECK-START: long Main.UShr0(long) instruction_simplifier (after)
267 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000268 // CHECK-DAG: Return [ [[Arg]] ]
269
Alexandre Rames74417692015-04-09 15:21:41 +0100270 // CHECK-START: long Main.UShr0(long) instruction_simplifier (after)
271 // CHECK-NOT: UShr
272
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000273 public static long UShr0(long arg) {
274 return arg >>> 0;
275 }
276
277 // CHECK-START: int Main.Xor0(int) instruction_simplifier (before)
278 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
279 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
280 // CHECK-DAG: [[Xor:i\d+]] Xor [ [[Arg]] [[Const0]] ]
281 // CHECK-DAG: Return [ [[Xor]] ]
282
283 // CHECK-START: int Main.Xor0(int) instruction_simplifier (after)
284 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000285 // CHECK-DAG: Return [ [[Arg]] ]
286
Alexandre Rames74417692015-04-09 15:21:41 +0100287 // CHECK-START: int Main.Xor0(int) instruction_simplifier (after)
288 // CHECK-NOT: Xor
289
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000290 public static int Xor0(int arg) {
291 return arg ^ 0;
292 }
293
294 // CHECK-START: int Main.XorAllOnes(int) instruction_simplifier (before)
295 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
296 // CHECK-DAG: [[ConstF:i\d+]] IntConstant -1
297 // CHECK-DAG: [[Xor:i\d+]] Xor [ [[Arg]] [[ConstF]] ]
298 // CHECK-DAG: Return [ [[Xor]] ]
299
300 // CHECK-START: int Main.XorAllOnes(int) instruction_simplifier (after)
301 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
302 // CHECK-DAG: [[Not:i\d+]] Not [ [[Arg]] ]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000303 // CHECK-DAG: Return [ [[Not]] ]
304
Alexandre Rames74417692015-04-09 15:21:41 +0100305 // CHECK-START: int Main.XorAllOnes(int) instruction_simplifier (after)
306 // CHECK-NOT: Xor
307
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000308 public static int XorAllOnes(int arg) {
309 return arg ^ -1;
310 }
311
312 public static void main(String[] args) {
313 int arg = 123456;
314
315 assertLongEquals(Add0(arg), arg);
316 assertIntEquals(AndAllOnes(arg), arg);
317 assertLongEquals(Div1(arg), arg);
318 assertIntEquals(DivN1(arg), -arg);
319 assertLongEquals(Mul1(arg), arg);
320 assertIntEquals(MulN1(arg), -arg);
321 assertLongEquals(MulPowerOfTwo128(arg), (128 * arg));
322 assertIntEquals(Or0(arg), arg);
323 assertLongEquals(OrSame(arg), arg);
324 assertIntEquals(Shl0(arg), arg);
325 assertLongEquals(Shr0(arg), arg);
326 assertLongEquals(Sub0(arg), arg);
327 assertIntEquals(SubAliasNeg(arg), -arg);
328 assertLongEquals(UShr0(arg), arg);
329 assertIntEquals(Xor0(arg), arg);
330 assertIntEquals(XorAllOnes(arg), ~arg);
331 }
332}