blob: ef6428ddd4e5e9e53400601d11bfcc32f2274502 [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
43 // CHECK-NOT: Add
44 // CHECK-DAG: Return [ [[Arg]] ]
45
46 public static long Add0(long arg) {
47 return 0 + arg;
48 }
49
50 // CHECK-START: int Main.AndAllOnes(int) instruction_simplifier (before)
51 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
52 // CHECK-DAG: [[ConstF:i\d+]] IntConstant -1
53 // CHECK-DAG: [[And:i\d+]] And [ [[Arg]] [[ConstF]] ]
54 // CHECK-DAG: Return [ [[And]] ]
55
56 // CHECK-START: int Main.AndAllOnes(int) instruction_simplifier (after)
57 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
58 // CHECK-NOT: And
59 // CHECK-DAG: Return [ [[Arg]] ]
60
61 public static int AndAllOnes(int arg) {
62 return arg & -1;
63 }
64
65 // CHECK-START: long Main.Div1(long) instruction_simplifier (before)
66 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
67 // CHECK-DAG: [[Const1:j\d+]] LongConstant 1
68 // CHECK-DAG: [[Div:j\d+]] Div [ [[Arg]] [[Const1]] ]
69 // CHECK-DAG: Return [ [[Div]] ]
70
71 // CHECK-START: long Main.Div1(long) instruction_simplifier (after)
72 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
73 // CHECK-NOT: Div
74 // CHECK-DAG: Return [ [[Arg]] ]
75
76 public static long Div1(long arg) {
77 return arg / 1;
78 }
79
80 // CHECK-START: int Main.DivN1(int) instruction_simplifier (before)
81 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
82 // CHECK-DAG: [[ConstN1:i\d+]] IntConstant -1
83 // CHECK-DAG: [[Div:i\d+]] Div [ [[Arg]] [[ConstN1]] ]
84 // CHECK-DAG: Return [ [[Div]] ]
85
86 // CHECK-START: int Main.DivN1(int) instruction_simplifier (after)
87 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
88 // CHECK-DAG: [[Neg:i\d+]] Neg [ [[Arg]] ]
89 // CHECK-NOT: Div
90 // CHECK-DAG: Return [ [[Neg]] ]
91
92 public static int DivN1(int arg) {
93 return arg / -1;
94 }
95
96 // CHECK-START: long Main.Mul1(long) instruction_simplifier (before)
97 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
98 // CHECK-DAG: [[Const1:j\d+]] LongConstant 1
99 // CHECK-DAG: [[Mul:j\d+]] Mul [ [[Arg]] [[Const1]] ]
100 // CHECK-DAG: Return [ [[Mul]] ]
101
102 // CHECK-START: long Main.Mul1(long) instruction_simplifier (after)
103 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
104 // CHECK-NOT: Mul
105 // CHECK-DAG: Return [ [[Arg]] ]
106
107 public static long Mul1(long arg) {
108 return arg * 1;
109 }
110
111 // CHECK-START: int Main.MulN1(int) instruction_simplifier (before)
112 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
113 // CHECK-DAG: [[ConstN1:i\d+]] IntConstant -1
114 // CHECK-DAG: [[Mul:i\d+]] Mul [ [[Arg]] [[ConstN1]] ]
115 // CHECK-DAG: Return [ [[Mul]] ]
116
117 // CHECK-START: int Main.MulN1(int) instruction_simplifier (after)
118 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
119 // CHECK-DAG: [[Neg:i\d+]] Neg [ [[Arg]] ]
120 // CHECK-NOT: Mul
121 // CHECK-DAG: Return [ [[Neg]] ]
122
123 public static int MulN1(int arg) {
124 return arg * -1;
125 }
126
127 // CHECK-START: long Main.MulPowerOfTwo128(long) instruction_simplifier (before)
128 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
129 // CHECK-DAG: [[Const128:j\d+]] LongConstant 128
130 // CHECK-DAG: [[Mul:j\d+]] Mul [ [[Arg]] [[Const128]] ]
131 // CHECK-DAG: Return [ [[Mul]] ]
132
133 // CHECK-START: long Main.MulPowerOfTwo128(long) instruction_simplifier (after)
134 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
135 // CHECK-DAG: [[Const7:i\d+]] IntConstant 7
136 // CHECK-DAG: [[Shl:j\d+]] Shl [ [[Arg]] [[Const7]] ]
137 // CHECK-NOT: Mul
138 // CHECK-DAG: Return [ [[Shl]] ]
139
140 public static long MulPowerOfTwo128(long arg) {
141 return arg * 128;
142 }
143
144 // CHECK-START: int Main.Or0(int) instruction_simplifier (before)
145 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
146 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
147 // CHECK-DAG: [[Or:i\d+]] Or [ [[Arg]] [[Const0]] ]
148 // CHECK-DAG: Return [ [[Or]] ]
149
150 // CHECK-START: int Main.Or0(int) instruction_simplifier (after)
151 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
152 // CHECK-NOT: Or
153 // CHECK-DAG: Return [ [[Arg]] ]
154
155 public static int Or0(int arg) {
156 return arg | 0;
157 }
158
159 // CHECK-START: long Main.OrSame(long) instruction_simplifier (before)
160 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
161 // CHECK-DAG: [[Or:j\d+]] Or [ [[Arg]] [[Arg]] ]
162 // CHECK-DAG: Return [ [[Or]] ]
163
164 // CHECK-START: long Main.OrSame(long) instruction_simplifier (after)
165 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
166 // CHECK-NOT: Or
167 // CHECK-DAG: Return [ [[Arg]] ]
168
169 public static long OrSame(long arg) {
170 return arg | arg;
171 }
172
173 // CHECK-START: int Main.Shl0(int) instruction_simplifier (before)
174 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
175 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
176 // CHECK-DAG: [[Shl:i\d+]] Shl [ [[Arg]] [[Const0]] ]
177 // CHECK-DAG: Return [ [[Shl]] ]
178
179 // CHECK-START: int Main.Shl0(int) instruction_simplifier (after)
180 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
181 // CHECK-NOT: Shl
182 // CHECK-DAG: Return [ [[Arg]] ]
183
184 public static int Shl0(int arg) {
185 return arg << 0;
186 }
187
188 // CHECK-START: long Main.Shr0(long) instruction_simplifier (before)
189 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
190 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
191 // CHECK-DAG: [[Shr:j\d+]] Shr [ [[Arg]] [[Const0]] ]
192 // CHECK-DAG: Return [ [[Shr]] ]
193
194 // CHECK-START: long Main.Shr0(long) instruction_simplifier (after)
195 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
196 // CHECK-NOT: Shr
197 // CHECK-DAG: Return [ [[Arg]] ]
198
199 public static long Shr0(long arg) {
200 return arg >> 0;
201 }
202
203 // CHECK-START: long Main.Sub0(long) instruction_simplifier (before)
204 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
205 // CHECK-DAG: [[Const0:j\d+]] LongConstant 0
206 // CHECK-DAG: [[Sub:j\d+]] Sub [ [[Arg]] [[Const0]] ]
207 // CHECK-DAG: Return [ [[Sub]] ]
208
209 // CHECK-START: long Main.Sub0(long) instruction_simplifier (after)
210 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
211 // CHECK-NOT: Sub
212 // CHECK-DAG: Return [ [[Arg]] ]
213
214 public static long Sub0(long arg) {
215 return arg - 0;
216 }
217
218 // CHECK-START: int Main.SubAliasNeg(int) instruction_simplifier (before)
219 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
220 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
221 // CHECK-DAG: [[Sub:i\d+]] Sub [ [[Const0]] [[Arg]] ]
222 // CHECK-DAG: Return [ [[Sub]] ]
223
224 // CHECK-START: int Main.SubAliasNeg(int) instruction_simplifier (after)
225 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
226 // CHECK-DAG: [[Neg:i\d+]] Neg [ [[Arg]] ]
227 // CHECK-NOT: Sub
228 // CHECK-DAG: Return [ [[Neg]] ]
229
230 public static int SubAliasNeg(int arg) {
231 return 0 - arg;
232 }
233
234 // CHECK-START: long Main.UShr0(long) instruction_simplifier (before)
235 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
236 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
237 // CHECK-DAG: [[UShr:j\d+]] UShr [ [[Arg]] [[Const0]] ]
238 // CHECK-DAG: Return [ [[UShr]] ]
239
240 // CHECK-START: long Main.UShr0(long) instruction_simplifier (after)
241 // CHECK-DAG: [[Arg:j\d+]] ParameterValue
242 // CHECK-NOT: UShr
243 // CHECK-DAG: Return [ [[Arg]] ]
244
245 public static long UShr0(long arg) {
246 return arg >>> 0;
247 }
248
249 // CHECK-START: int Main.Xor0(int) instruction_simplifier (before)
250 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
251 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
252 // CHECK-DAG: [[Xor:i\d+]] Xor [ [[Arg]] [[Const0]] ]
253 // CHECK-DAG: Return [ [[Xor]] ]
254
255 // CHECK-START: int Main.Xor0(int) instruction_simplifier (after)
256 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
257 // CHECK-NOT: Xor
258 // CHECK-DAG: Return [ [[Arg]] ]
259
260 public static int Xor0(int arg) {
261 return arg ^ 0;
262 }
263
264 // CHECK-START: int Main.XorAllOnes(int) instruction_simplifier (before)
265 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
266 // CHECK-DAG: [[ConstF:i\d+]] IntConstant -1
267 // CHECK-DAG: [[Xor:i\d+]] Xor [ [[Arg]] [[ConstF]] ]
268 // CHECK-DAG: Return [ [[Xor]] ]
269
270 // CHECK-START: int Main.XorAllOnes(int) instruction_simplifier (after)
271 // CHECK-DAG: [[Arg:i\d+]] ParameterValue
272 // CHECK-DAG: [[Not:i\d+]] Not [ [[Arg]] ]
273 // CHECK-NOT: Xor
274 // CHECK-DAG: Return [ [[Not]] ]
275
276 public static int XorAllOnes(int arg) {
277 return arg ^ -1;
278 }
279
280 public static void main(String[] args) {
281 int arg = 123456;
282
283 assertLongEquals(Add0(arg), arg);
284 assertIntEquals(AndAllOnes(arg), arg);
285 assertLongEquals(Div1(arg), arg);
286 assertIntEquals(DivN1(arg), -arg);
287 assertLongEquals(Mul1(arg), arg);
288 assertIntEquals(MulN1(arg), -arg);
289 assertLongEquals(MulPowerOfTwo128(arg), (128 * arg));
290 assertIntEquals(Or0(arg), arg);
291 assertLongEquals(OrSame(arg), arg);
292 assertIntEquals(Shl0(arg), arg);
293 assertLongEquals(Shr0(arg), arg);
294 assertLongEquals(Sub0(arg), arg);
295 assertIntEquals(SubAliasNeg(arg), -arg);
296 assertLongEquals(UShr0(arg), arg);
297 assertIntEquals(Xor0(arg), arg);
298 assertIntEquals(XorAllOnes(arg), ~arg);
299 }
300}