]>
Commit | Line | Data |
---|---|---|
8f2e8c07 KB |
1 | /* |
2 | * Optimizations for Tiny Code Generator for QEMU | |
3 | * | |
4 | * Copyright (c) 2010 Samsung Electronics. | |
5 | * Contributed by Kirill Batuzov <batuzovk@ispras.ru> | |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
23 | * THE SOFTWARE. | |
24 | */ | |
25 | ||
26 | #include "config.h" | |
27 | ||
28 | #include <stdlib.h> | |
29 | #include <stdio.h> | |
30 | ||
31 | #include "qemu-common.h" | |
32 | #include "tcg-op.h" | |
33 | ||
8f2e8c07 KB |
34 | #define CASE_OP_32_64(x) \ |
35 | glue(glue(case INDEX_op_, x), _i32): \ | |
36 | glue(glue(case INDEX_op_, x), _i64) | |
8f2e8c07 | 37 | |
22613af4 KB |
38 | typedef enum { |
39 | TCG_TEMP_UNDEF = 0, | |
40 | TCG_TEMP_CONST, | |
41 | TCG_TEMP_COPY, | |
22613af4 KB |
42 | } tcg_temp_state; |
43 | ||
44 | struct tcg_temp_info { | |
45 | tcg_temp_state state; | |
46 | uint16_t prev_copy; | |
47 | uint16_t next_copy; | |
48 | tcg_target_ulong val; | |
49 | }; | |
50 | ||
51 | static struct tcg_temp_info temps[TCG_MAX_TEMPS]; | |
52 | ||
e590d4e6 AJ |
53 | /* Reset TEMP's state to TCG_TEMP_UNDEF. If TEMP only had one copy, remove |
54 | the copy flag from the left temp. */ | |
55 | static void reset_temp(TCGArg temp) | |
22613af4 | 56 | { |
e590d4e6 AJ |
57 | if (temps[temp].state == TCG_TEMP_COPY) { |
58 | if (temps[temp].prev_copy == temps[temp].next_copy) { | |
59 | temps[temps[temp].next_copy].state = TCG_TEMP_UNDEF; | |
60 | } else { | |
61 | temps[temps[temp].next_copy].prev_copy = temps[temp].prev_copy; | |
62 | temps[temps[temp].prev_copy].next_copy = temps[temp].next_copy; | |
22613af4 | 63 | } |
22613af4 | 64 | } |
48b56ce1 | 65 | temps[temp].state = TCG_TEMP_UNDEF; |
22613af4 KB |
66 | } |
67 | ||
fe0de7aa | 68 | static int op_bits(TCGOpcode op) |
22613af4 | 69 | { |
8399ad59 RH |
70 | const TCGOpDef *def = &tcg_op_defs[op]; |
71 | return def->flags & TCG_OPF_64BIT ? 64 : 32; | |
22613af4 KB |
72 | } |
73 | ||
fe0de7aa | 74 | static TCGOpcode op_to_movi(TCGOpcode op) |
22613af4 KB |
75 | { |
76 | switch (op_bits(op)) { | |
77 | case 32: | |
78 | return INDEX_op_movi_i32; | |
22613af4 KB |
79 | case 64: |
80 | return INDEX_op_movi_i64; | |
22613af4 KB |
81 | default: |
82 | fprintf(stderr, "op_to_movi: unexpected return value of " | |
83 | "function op_bits.\n"); | |
84 | tcg_abort(); | |
85 | } | |
86 | } | |
87 | ||
e590d4e6 AJ |
88 | static TCGArg find_better_copy(TCGContext *s, TCGArg temp) |
89 | { | |
90 | TCGArg i; | |
91 | ||
92 | /* If this is already a global, we can't do better. */ | |
93 | if (temp < s->nb_globals) { | |
94 | return temp; | |
95 | } | |
96 | ||
97 | /* Search for a global first. */ | |
98 | for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) { | |
99 | if (i < s->nb_globals) { | |
100 | return i; | |
101 | } | |
102 | } | |
103 | ||
104 | /* If it is a temp, search for a temp local. */ | |
105 | if (!s->temps[temp].temp_local) { | |
106 | for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) { | |
107 | if (s->temps[i].temp_local) { | |
108 | return i; | |
109 | } | |
110 | } | |
111 | } | |
112 | ||
113 | /* Failure to find a better representation, return the same temp. */ | |
114 | return temp; | |
115 | } | |
116 | ||
117 | static bool temps_are_copies(TCGArg arg1, TCGArg arg2) | |
118 | { | |
119 | TCGArg i; | |
120 | ||
121 | if (arg1 == arg2) { | |
122 | return true; | |
123 | } | |
124 | ||
125 | if (temps[arg1].state != TCG_TEMP_COPY | |
126 | || temps[arg2].state != TCG_TEMP_COPY) { | |
127 | return false; | |
128 | } | |
129 | ||
130 | for (i = temps[arg1].next_copy ; i != arg1 ; i = temps[i].next_copy) { | |
131 | if (i == arg2) { | |
132 | return true; | |
133 | } | |
134 | } | |
135 | ||
136 | return false; | |
137 | } | |
138 | ||
b80bb016 AJ |
139 | static void tcg_opt_gen_mov(TCGContext *s, TCGArg *gen_args, |
140 | TCGArg dst, TCGArg src) | |
22613af4 | 141 | { |
e590d4e6 AJ |
142 | reset_temp(dst); |
143 | assert(temps[src].state != TCG_TEMP_CONST); | |
144 | ||
145 | if (s->temps[src].type == s->temps[dst].type) { | |
146 | if (temps[src].state != TCG_TEMP_COPY) { | |
147 | temps[src].state = TCG_TEMP_COPY; | |
22613af4 KB |
148 | temps[src].next_copy = src; |
149 | temps[src].prev_copy = src; | |
150 | } | |
151 | temps[dst].state = TCG_TEMP_COPY; | |
22613af4 KB |
152 | temps[dst].next_copy = temps[src].next_copy; |
153 | temps[dst].prev_copy = src; | |
154 | temps[temps[dst].next_copy].prev_copy = dst; | |
155 | temps[src].next_copy = dst; | |
156 | } | |
e590d4e6 | 157 | |
22613af4 KB |
158 | gen_args[0] = dst; |
159 | gen_args[1] = src; | |
160 | } | |
161 | ||
e590d4e6 | 162 | static void tcg_opt_gen_movi(TCGArg *gen_args, TCGArg dst, TCGArg val) |
22613af4 | 163 | { |
e590d4e6 | 164 | reset_temp(dst); |
22613af4 KB |
165 | temps[dst].state = TCG_TEMP_CONST; |
166 | temps[dst].val = val; | |
167 | gen_args[0] = dst; | |
168 | gen_args[1] = val; | |
169 | } | |
170 | ||
fe0de7aa | 171 | static TCGOpcode op_to_mov(TCGOpcode op) |
53108fb5 KB |
172 | { |
173 | switch (op_bits(op)) { | |
174 | case 32: | |
175 | return INDEX_op_mov_i32; | |
53108fb5 KB |
176 | case 64: |
177 | return INDEX_op_mov_i64; | |
53108fb5 KB |
178 | default: |
179 | fprintf(stderr, "op_to_mov: unexpected return value of " | |
180 | "function op_bits.\n"); | |
181 | tcg_abort(); | |
182 | } | |
183 | } | |
184 | ||
fe0de7aa | 185 | static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y) |
53108fb5 KB |
186 | { |
187 | switch (op) { | |
188 | CASE_OP_32_64(add): | |
189 | return x + y; | |
190 | ||
191 | CASE_OP_32_64(sub): | |
192 | return x - y; | |
193 | ||
194 | CASE_OP_32_64(mul): | |
195 | return x * y; | |
196 | ||
9a81090b KB |
197 | CASE_OP_32_64(and): |
198 | return x & y; | |
199 | ||
200 | CASE_OP_32_64(or): | |
201 | return x | y; | |
202 | ||
203 | CASE_OP_32_64(xor): | |
204 | return x ^ y; | |
205 | ||
55c0975c KB |
206 | case INDEX_op_shl_i32: |
207 | return (uint32_t)x << (uint32_t)y; | |
208 | ||
55c0975c KB |
209 | case INDEX_op_shl_i64: |
210 | return (uint64_t)x << (uint64_t)y; | |
55c0975c KB |
211 | |
212 | case INDEX_op_shr_i32: | |
213 | return (uint32_t)x >> (uint32_t)y; | |
214 | ||
55c0975c KB |
215 | case INDEX_op_shr_i64: |
216 | return (uint64_t)x >> (uint64_t)y; | |
55c0975c KB |
217 | |
218 | case INDEX_op_sar_i32: | |
219 | return (int32_t)x >> (int32_t)y; | |
220 | ||
55c0975c KB |
221 | case INDEX_op_sar_i64: |
222 | return (int64_t)x >> (int64_t)y; | |
55c0975c KB |
223 | |
224 | case INDEX_op_rotr_i32: | |
25c4d9cc | 225 | x = ((uint32_t)x << (32 - y)) | ((uint32_t)x >> y); |
55c0975c KB |
226 | return x; |
227 | ||
55c0975c | 228 | case INDEX_op_rotr_i64: |
25c4d9cc | 229 | x = ((uint64_t)x << (64 - y)) | ((uint64_t)x >> y); |
55c0975c | 230 | return x; |
55c0975c KB |
231 | |
232 | case INDEX_op_rotl_i32: | |
25c4d9cc | 233 | x = ((uint32_t)x << y) | ((uint32_t)x >> (32 - y)); |
55c0975c KB |
234 | return x; |
235 | ||
55c0975c | 236 | case INDEX_op_rotl_i64: |
25c4d9cc | 237 | x = ((uint64_t)x << y) | ((uint64_t)x >> (64 - y)); |
55c0975c | 238 | return x; |
25c4d9cc RH |
239 | |
240 | CASE_OP_32_64(not): | |
a640f031 | 241 | return ~x; |
25c4d9cc | 242 | |
cb25c80a RH |
243 | CASE_OP_32_64(neg): |
244 | return -x; | |
245 | ||
246 | CASE_OP_32_64(andc): | |
247 | return x & ~y; | |
248 | ||
249 | CASE_OP_32_64(orc): | |
250 | return x | ~y; | |
251 | ||
252 | CASE_OP_32_64(eqv): | |
253 | return ~(x ^ y); | |
254 | ||
255 | CASE_OP_32_64(nand): | |
256 | return ~(x & y); | |
257 | ||
258 | CASE_OP_32_64(nor): | |
259 | return ~(x | y); | |
260 | ||
25c4d9cc | 261 | CASE_OP_32_64(ext8s): |
a640f031 | 262 | return (int8_t)x; |
25c4d9cc RH |
263 | |
264 | CASE_OP_32_64(ext16s): | |
a640f031 | 265 | return (int16_t)x; |
25c4d9cc RH |
266 | |
267 | CASE_OP_32_64(ext8u): | |
a640f031 | 268 | return (uint8_t)x; |
25c4d9cc RH |
269 | |
270 | CASE_OP_32_64(ext16u): | |
a640f031 KB |
271 | return (uint16_t)x; |
272 | ||
a640f031 KB |
273 | case INDEX_op_ext32s_i64: |
274 | return (int32_t)x; | |
275 | ||
276 | case INDEX_op_ext32u_i64: | |
277 | return (uint32_t)x; | |
a640f031 | 278 | |
53108fb5 KB |
279 | default: |
280 | fprintf(stderr, | |
281 | "Unrecognized operation %d in do_constant_folding.\n", op); | |
282 | tcg_abort(); | |
283 | } | |
284 | } | |
285 | ||
fe0de7aa | 286 | static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y) |
53108fb5 KB |
287 | { |
288 | TCGArg res = do_constant_folding_2(op, x, y); | |
53108fb5 KB |
289 | if (op_bits(op) == 32) { |
290 | res &= 0xffffffff; | |
291 | } | |
53108fb5 KB |
292 | return res; |
293 | } | |
294 | ||
b336ceb6 AJ |
295 | /* Return 2 if the condition can't be simplified, and the result |
296 | of the condition (0 or 1) if it can */ | |
f8dd19e5 AJ |
297 | static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x, |
298 | TCGArg y, TCGCond c) | |
299 | { | |
b336ceb6 AJ |
300 | if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) { |
301 | switch (op_bits(op)) { | |
302 | case 32: | |
303 | switch (c) { | |
304 | case TCG_COND_EQ: | |
305 | return (uint32_t)temps[x].val == (uint32_t)temps[y].val; | |
306 | case TCG_COND_NE: | |
307 | return (uint32_t)temps[x].val != (uint32_t)temps[y].val; | |
308 | case TCG_COND_LT: | |
309 | return (int32_t)temps[x].val < (int32_t)temps[y].val; | |
310 | case TCG_COND_GE: | |
311 | return (int32_t)temps[x].val >= (int32_t)temps[y].val; | |
312 | case TCG_COND_LE: | |
313 | return (int32_t)temps[x].val <= (int32_t)temps[y].val; | |
314 | case TCG_COND_GT: | |
315 | return (int32_t)temps[x].val > (int32_t)temps[y].val; | |
316 | case TCG_COND_LTU: | |
317 | return (uint32_t)temps[x].val < (uint32_t)temps[y].val; | |
318 | case TCG_COND_GEU: | |
319 | return (uint32_t)temps[x].val >= (uint32_t)temps[y].val; | |
320 | case TCG_COND_LEU: | |
321 | return (uint32_t)temps[x].val <= (uint32_t)temps[y].val; | |
322 | case TCG_COND_GTU: | |
323 | return (uint32_t)temps[x].val > (uint32_t)temps[y].val; | |
0aed257f RH |
324 | default: |
325 | break; | |
b336ceb6 AJ |
326 | } |
327 | break; | |
328 | case 64: | |
329 | switch (c) { | |
330 | case TCG_COND_EQ: | |
331 | return (uint64_t)temps[x].val == (uint64_t)temps[y].val; | |
332 | case TCG_COND_NE: | |
333 | return (uint64_t)temps[x].val != (uint64_t)temps[y].val; | |
334 | case TCG_COND_LT: | |
335 | return (int64_t)temps[x].val < (int64_t)temps[y].val; | |
336 | case TCG_COND_GE: | |
337 | return (int64_t)temps[x].val >= (int64_t)temps[y].val; | |
338 | case TCG_COND_LE: | |
339 | return (int64_t)temps[x].val <= (int64_t)temps[y].val; | |
340 | case TCG_COND_GT: | |
341 | return (int64_t)temps[x].val > (int64_t)temps[y].val; | |
342 | case TCG_COND_LTU: | |
343 | return (uint64_t)temps[x].val < (uint64_t)temps[y].val; | |
344 | case TCG_COND_GEU: | |
345 | return (uint64_t)temps[x].val >= (uint64_t)temps[y].val; | |
346 | case TCG_COND_LEU: | |
347 | return (uint64_t)temps[x].val <= (uint64_t)temps[y].val; | |
348 | case TCG_COND_GTU: | |
349 | return (uint64_t)temps[x].val > (uint64_t)temps[y].val; | |
0aed257f RH |
350 | default: |
351 | break; | |
b336ceb6 AJ |
352 | } |
353 | break; | |
354 | } | |
355 | } else if (temps_are_copies(x, y)) { | |
f8dd19e5 | 356 | switch (c) { |
f8dd19e5 | 357 | case TCG_COND_GT: |
f8dd19e5 | 358 | case TCG_COND_LTU: |
b336ceb6 | 359 | case TCG_COND_LT: |
f8dd19e5 | 360 | case TCG_COND_GTU: |
f8dd19e5 | 361 | case TCG_COND_NE: |
b336ceb6 | 362 | return 0; |
f8dd19e5 | 363 | case TCG_COND_GE: |
b336ceb6 | 364 | case TCG_COND_GEU: |
f8dd19e5 | 365 | case TCG_COND_LE: |
b336ceb6 AJ |
366 | case TCG_COND_LEU: |
367 | case TCG_COND_EQ: | |
368 | return 1; | |
0aed257f RH |
369 | default: |
370 | break; | |
b336ceb6 AJ |
371 | } |
372 | } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) { | |
373 | switch (c) { | |
f8dd19e5 | 374 | case TCG_COND_LTU: |
b336ceb6 | 375 | return 0; |
f8dd19e5 | 376 | case TCG_COND_GEU: |
b336ceb6 AJ |
377 | return 1; |
378 | default: | |
379 | return 2; | |
f8dd19e5 | 380 | } |
b336ceb6 AJ |
381 | } else { |
382 | return 2; | |
f8dd19e5 AJ |
383 | } |
384 | ||
385 | fprintf(stderr, | |
386 | "Unrecognized bitness %d or condition %d in " | |
387 | "do_constant_folding_cond.\n", op_bits(op), c); | |
388 | tcg_abort(); | |
389 | } | |
390 | ||
24c9ae4e RH |
391 | static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2) |
392 | { | |
393 | TCGArg a1 = *p1, a2 = *p2; | |
394 | int sum = 0; | |
395 | sum += temps[a1].state == TCG_TEMP_CONST; | |
396 | sum -= temps[a2].state == TCG_TEMP_CONST; | |
397 | ||
398 | /* Prefer the constant in second argument, and then the form | |
399 | op a, a, b, which is better handled on non-RISC hosts. */ | |
400 | if (sum > 0 || (sum == 0 && dest == a2)) { | |
401 | *p1 = a2; | |
402 | *p2 = a1; | |
403 | return true; | |
404 | } | |
405 | return false; | |
406 | } | |
407 | ||
0bfcb865 RH |
408 | static bool swap_commutative2(TCGArg *p1, TCGArg *p2) |
409 | { | |
410 | int sum = 0; | |
411 | sum += temps[p1[0]].state == TCG_TEMP_CONST; | |
412 | sum += temps[p1[1]].state == TCG_TEMP_CONST; | |
413 | sum -= temps[p2[0]].state == TCG_TEMP_CONST; | |
414 | sum -= temps[p2[1]].state == TCG_TEMP_CONST; | |
415 | if (sum > 0) { | |
416 | TCGArg t; | |
417 | t = p1[0], p1[0] = p2[0], p2[0] = t; | |
418 | t = p1[1], p1[1] = p2[1], p2[1] = t; | |
419 | return true; | |
420 | } | |
421 | return false; | |
422 | } | |
423 | ||
22613af4 | 424 | /* Propagate constants and copies, fold constant expressions. */ |
8f2e8c07 KB |
425 | static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr, |
426 | TCGArg *args, TCGOpDef *tcg_op_defs) | |
427 | { | |
fe0de7aa BS |
428 | int i, nb_ops, op_index, nb_temps, nb_globals, nb_call_args; |
429 | TCGOpcode op; | |
8f2e8c07 KB |
430 | const TCGOpDef *def; |
431 | TCGArg *gen_args; | |
53108fb5 | 432 | TCGArg tmp; |
5d8f5363 | 433 | |
22613af4 KB |
434 | /* Array VALS has an element for each temp. |
435 | If this temp holds a constant then its value is kept in VALS' element. | |
e590d4e6 AJ |
436 | If this temp is a copy of other ones then the other copies are |
437 | available through the doubly linked circular list. */ | |
8f2e8c07 KB |
438 | |
439 | nb_temps = s->nb_temps; | |
440 | nb_globals = s->nb_globals; | |
22613af4 | 441 | memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info)); |
8f2e8c07 KB |
442 | |
443 | nb_ops = tcg_opc_ptr - gen_opc_buf; | |
444 | gen_args = args; | |
445 | for (op_index = 0; op_index < nb_ops; op_index++) { | |
446 | op = gen_opc_buf[op_index]; | |
447 | def = &tcg_op_defs[op]; | |
22613af4 | 448 | /* Do copy propagation */ |
1ff8c541 AJ |
449 | if (op == INDEX_op_call) { |
450 | int nb_oargs = args[0] >> 16; | |
451 | int nb_iargs = args[0] & 0xffff; | |
452 | for (i = nb_oargs + 1; i < nb_oargs + nb_iargs + 1; i++) { | |
453 | if (temps[args[i]].state == TCG_TEMP_COPY) { | |
454 | args[i] = find_better_copy(s, args[i]); | |
455 | } | |
456 | } | |
457 | } else { | |
22613af4 KB |
458 | for (i = def->nb_oargs; i < def->nb_oargs + def->nb_iargs; i++) { |
459 | if (temps[args[i]].state == TCG_TEMP_COPY) { | |
e590d4e6 | 460 | args[i] = find_better_copy(s, args[i]); |
22613af4 KB |
461 | } |
462 | } | |
463 | } | |
464 | ||
53108fb5 KB |
465 | /* For commutative operations make constant second argument */ |
466 | switch (op) { | |
467 | CASE_OP_32_64(add): | |
468 | CASE_OP_32_64(mul): | |
9a81090b KB |
469 | CASE_OP_32_64(and): |
470 | CASE_OP_32_64(or): | |
471 | CASE_OP_32_64(xor): | |
cb25c80a RH |
472 | CASE_OP_32_64(eqv): |
473 | CASE_OP_32_64(nand): | |
474 | CASE_OP_32_64(nor): | |
24c9ae4e | 475 | swap_commutative(args[0], &args[1], &args[2]); |
53108fb5 | 476 | break; |
65a7cce1 | 477 | CASE_OP_32_64(brcond): |
24c9ae4e | 478 | if (swap_commutative(-1, &args[0], &args[1])) { |
65a7cce1 AJ |
479 | args[2] = tcg_swap_cond(args[2]); |
480 | } | |
481 | break; | |
482 | CASE_OP_32_64(setcond): | |
24c9ae4e | 483 | if (swap_commutative(args[0], &args[1], &args[2])) { |
65a7cce1 AJ |
484 | args[3] = tcg_swap_cond(args[3]); |
485 | } | |
486 | break; | |
fa01a208 | 487 | CASE_OP_32_64(movcond): |
24c9ae4e RH |
488 | if (swap_commutative(-1, &args[1], &args[2])) { |
489 | args[5] = tcg_swap_cond(args[5]); | |
5d8f5363 RH |
490 | } |
491 | /* For movcond, we canonicalize the "false" input reg to match | |
492 | the destination reg so that the tcg backend can implement | |
493 | a "move if true" operation. */ | |
24c9ae4e RH |
494 | if (swap_commutative(args[0], &args[4], &args[3])) { |
495 | args[5] = tcg_invert_cond(args[5]); | |
fa01a208 | 496 | } |
1e484e61 RH |
497 | break; |
498 | case INDEX_op_add2_i32: | |
499 | swap_commutative(args[0], &args[2], &args[4]); | |
500 | swap_commutative(args[1], &args[3], &args[5]); | |
501 | break; | |
0bfcb865 RH |
502 | case INDEX_op_brcond2_i32: |
503 | if (swap_commutative2(&args[0], &args[2])) { | |
504 | args[4] = tcg_swap_cond(args[4]); | |
505 | } | |
506 | break; | |
507 | case INDEX_op_setcond2_i32: | |
508 | if (swap_commutative2(&args[1], &args[3])) { | |
509 | args[5] = tcg_swap_cond(args[5]); | |
510 | } | |
511 | break; | |
53108fb5 KB |
512 | default: |
513 | break; | |
514 | } | |
515 | ||
01ee5282 AJ |
516 | /* Simplify expressions for "shift/rot r, 0, a => movi r, 0" */ |
517 | switch (op) { | |
518 | CASE_OP_32_64(shl): | |
519 | CASE_OP_32_64(shr): | |
520 | CASE_OP_32_64(sar): | |
521 | CASE_OP_32_64(rotl): | |
522 | CASE_OP_32_64(rotr): | |
523 | if (temps[args[1]].state == TCG_TEMP_CONST | |
524 | && temps[args[1]].val == 0) { | |
525 | gen_opc_buf[op_index] = op_to_movi(op); | |
e590d4e6 | 526 | tcg_opt_gen_movi(gen_args, args[0], 0); |
01ee5282 AJ |
527 | args += 3; |
528 | gen_args += 2; | |
529 | continue; | |
530 | } | |
531 | break; | |
532 | default: | |
533 | break; | |
534 | } | |
535 | ||
56e49438 | 536 | /* Simplify expression for "op r, a, 0 => mov r, a" cases */ |
53108fb5 KB |
537 | switch (op) { |
538 | CASE_OP_32_64(add): | |
539 | CASE_OP_32_64(sub): | |
55c0975c KB |
540 | CASE_OP_32_64(shl): |
541 | CASE_OP_32_64(shr): | |
542 | CASE_OP_32_64(sar): | |
25c4d9cc RH |
543 | CASE_OP_32_64(rotl): |
544 | CASE_OP_32_64(rotr): | |
38ee188b AJ |
545 | CASE_OP_32_64(or): |
546 | CASE_OP_32_64(xor): | |
53108fb5 KB |
547 | if (temps[args[1]].state == TCG_TEMP_CONST) { |
548 | /* Proceed with possible constant folding. */ | |
549 | break; | |
550 | } | |
551 | if (temps[args[2]].state == TCG_TEMP_CONST | |
552 | && temps[args[2]].val == 0) { | |
e590d4e6 | 553 | if (temps_are_copies(args[0], args[1])) { |
53108fb5 KB |
554 | gen_opc_buf[op_index] = INDEX_op_nop; |
555 | } else { | |
556 | gen_opc_buf[op_index] = op_to_mov(op); | |
b80bb016 | 557 | tcg_opt_gen_mov(s, gen_args, args[0], args[1]); |
53108fb5 | 558 | gen_args += 2; |
53108fb5 | 559 | } |
fedc0da2 | 560 | args += 3; |
53108fb5 KB |
561 | continue; |
562 | } | |
563 | break; | |
56e49438 AJ |
564 | default: |
565 | break; | |
566 | } | |
567 | ||
568 | /* Simplify expression for "op r, a, 0 => movi r, 0" cases */ | |
569 | switch (op) { | |
61251c0c | 570 | CASE_OP_32_64(and): |
53108fb5 KB |
571 | CASE_OP_32_64(mul): |
572 | if ((temps[args[2]].state == TCG_TEMP_CONST | |
573 | && temps[args[2]].val == 0)) { | |
574 | gen_opc_buf[op_index] = op_to_movi(op); | |
e590d4e6 | 575 | tcg_opt_gen_movi(gen_args, args[0], 0); |
53108fb5 KB |
576 | args += 3; |
577 | gen_args += 2; | |
578 | continue; | |
579 | } | |
580 | break; | |
56e49438 AJ |
581 | default: |
582 | break; | |
583 | } | |
584 | ||
585 | /* Simplify expression for "op r, a, a => mov r, a" cases */ | |
586 | switch (op) { | |
9a81090b KB |
587 | CASE_OP_32_64(or): |
588 | CASE_OP_32_64(and): | |
0aba1c73 | 589 | if (temps_are_copies(args[1], args[2])) { |
e590d4e6 | 590 | if (temps_are_copies(args[0], args[1])) { |
9a81090b KB |
591 | gen_opc_buf[op_index] = INDEX_op_nop; |
592 | } else { | |
593 | gen_opc_buf[op_index] = op_to_mov(op); | |
b80bb016 | 594 | tcg_opt_gen_mov(s, gen_args, args[0], args[1]); |
9a81090b | 595 | gen_args += 2; |
9a81090b | 596 | } |
fedc0da2 | 597 | args += 3; |
9a81090b KB |
598 | continue; |
599 | } | |
600 | break; | |
fe0de7aa BS |
601 | default: |
602 | break; | |
53108fb5 KB |
603 | } |
604 | ||
3c94193e AJ |
605 | /* Simplify expression for "op r, a, a => movi r, 0" cases */ |
606 | switch (op) { | |
607 | CASE_OP_32_64(sub): | |
608 | CASE_OP_32_64(xor): | |
609 | if (temps_are_copies(args[1], args[2])) { | |
610 | gen_opc_buf[op_index] = op_to_movi(op); | |
611 | tcg_opt_gen_movi(gen_args, args[0], 0); | |
612 | gen_args += 2; | |
613 | args += 3; | |
614 | continue; | |
615 | } | |
616 | break; | |
617 | default: | |
618 | break; | |
619 | } | |
620 | ||
22613af4 KB |
621 | /* Propagate constants through copy operations and do constant |
622 | folding. Constants will be substituted to arguments by register | |
623 | allocator where needed and possible. Also detect copies. */ | |
8f2e8c07 | 624 | switch (op) { |
22613af4 | 625 | CASE_OP_32_64(mov): |
e590d4e6 | 626 | if (temps_are_copies(args[0], args[1])) { |
22613af4 KB |
627 | args += 2; |
628 | gen_opc_buf[op_index] = INDEX_op_nop; | |
629 | break; | |
630 | } | |
631 | if (temps[args[1]].state != TCG_TEMP_CONST) { | |
b80bb016 | 632 | tcg_opt_gen_mov(s, gen_args, args[0], args[1]); |
22613af4 KB |
633 | gen_args += 2; |
634 | args += 2; | |
635 | break; | |
636 | } | |
637 | /* Source argument is constant. Rewrite the operation and | |
638 | let movi case handle it. */ | |
639 | op = op_to_movi(op); | |
640 | gen_opc_buf[op_index] = op; | |
641 | args[1] = temps[args[1]].val; | |
642 | /* fallthrough */ | |
643 | CASE_OP_32_64(movi): | |
e590d4e6 | 644 | tcg_opt_gen_movi(gen_args, args[0], args[1]); |
22613af4 KB |
645 | gen_args += 2; |
646 | args += 2; | |
647 | break; | |
6e14e91b | 648 | |
a640f031 | 649 | CASE_OP_32_64(not): |
cb25c80a | 650 | CASE_OP_32_64(neg): |
25c4d9cc RH |
651 | CASE_OP_32_64(ext8s): |
652 | CASE_OP_32_64(ext8u): | |
653 | CASE_OP_32_64(ext16s): | |
654 | CASE_OP_32_64(ext16u): | |
a640f031 KB |
655 | case INDEX_op_ext32s_i64: |
656 | case INDEX_op_ext32u_i64: | |
a640f031 KB |
657 | if (temps[args[1]].state == TCG_TEMP_CONST) { |
658 | gen_opc_buf[op_index] = op_to_movi(op); | |
659 | tmp = do_constant_folding(op, temps[args[1]].val, 0); | |
e590d4e6 | 660 | tcg_opt_gen_movi(gen_args, args[0], tmp); |
6e14e91b RH |
661 | gen_args += 2; |
662 | args += 2; | |
663 | break; | |
a640f031 | 664 | } |
6e14e91b RH |
665 | goto do_default; |
666 | ||
53108fb5 KB |
667 | CASE_OP_32_64(add): |
668 | CASE_OP_32_64(sub): | |
669 | CASE_OP_32_64(mul): | |
9a81090b KB |
670 | CASE_OP_32_64(or): |
671 | CASE_OP_32_64(and): | |
672 | CASE_OP_32_64(xor): | |
55c0975c KB |
673 | CASE_OP_32_64(shl): |
674 | CASE_OP_32_64(shr): | |
675 | CASE_OP_32_64(sar): | |
25c4d9cc RH |
676 | CASE_OP_32_64(rotl): |
677 | CASE_OP_32_64(rotr): | |
cb25c80a RH |
678 | CASE_OP_32_64(andc): |
679 | CASE_OP_32_64(orc): | |
680 | CASE_OP_32_64(eqv): | |
681 | CASE_OP_32_64(nand): | |
682 | CASE_OP_32_64(nor): | |
53108fb5 KB |
683 | if (temps[args[1]].state == TCG_TEMP_CONST |
684 | && temps[args[2]].state == TCG_TEMP_CONST) { | |
685 | gen_opc_buf[op_index] = op_to_movi(op); | |
686 | tmp = do_constant_folding(op, temps[args[1]].val, | |
687 | temps[args[2]].val); | |
e590d4e6 | 688 | tcg_opt_gen_movi(gen_args, args[0], tmp); |
53108fb5 | 689 | gen_args += 2; |
6e14e91b RH |
690 | args += 3; |
691 | break; | |
53108fb5 | 692 | } |
6e14e91b RH |
693 | goto do_default; |
694 | ||
7ef55fc9 AJ |
695 | CASE_OP_32_64(deposit): |
696 | if (temps[args[1]].state == TCG_TEMP_CONST | |
697 | && temps[args[2]].state == TCG_TEMP_CONST) { | |
698 | gen_opc_buf[op_index] = op_to_movi(op); | |
699 | tmp = ((1ull << args[4]) - 1); | |
700 | tmp = (temps[args[1]].val & ~(tmp << args[3])) | |
701 | | ((temps[args[2]].val & tmp) << args[3]); | |
702 | tcg_opt_gen_movi(gen_args, args[0], tmp); | |
703 | gen_args += 2; | |
6e14e91b RH |
704 | args += 5; |
705 | break; | |
7ef55fc9 | 706 | } |
6e14e91b RH |
707 | goto do_default; |
708 | ||
f8dd19e5 | 709 | CASE_OP_32_64(setcond): |
b336ceb6 AJ |
710 | tmp = do_constant_folding_cond(op, args[1], args[2], args[3]); |
711 | if (tmp != 2) { | |
f8dd19e5 | 712 | gen_opc_buf[op_index] = op_to_movi(op); |
e590d4e6 | 713 | tcg_opt_gen_movi(gen_args, args[0], tmp); |
f8dd19e5 | 714 | gen_args += 2; |
6e14e91b RH |
715 | args += 4; |
716 | break; | |
f8dd19e5 | 717 | } |
6e14e91b RH |
718 | goto do_default; |
719 | ||
fbeaa26c | 720 | CASE_OP_32_64(brcond): |
b336ceb6 AJ |
721 | tmp = do_constant_folding_cond(op, args[0], args[1], args[2]); |
722 | if (tmp != 2) { | |
723 | if (tmp) { | |
fbeaa26c AJ |
724 | memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info)); |
725 | gen_opc_buf[op_index] = INDEX_op_br; | |
726 | gen_args[0] = args[3]; | |
727 | gen_args += 1; | |
fbeaa26c AJ |
728 | } else { |
729 | gen_opc_buf[op_index] = INDEX_op_nop; | |
fbeaa26c | 730 | } |
6e14e91b RH |
731 | args += 4; |
732 | break; | |
fbeaa26c | 733 | } |
6e14e91b RH |
734 | goto do_default; |
735 | ||
fa01a208 | 736 | CASE_OP_32_64(movcond): |
b336ceb6 AJ |
737 | tmp = do_constant_folding_cond(op, args[1], args[2], args[5]); |
738 | if (tmp != 2) { | |
e590d4e6 | 739 | if (temps_are_copies(args[0], args[4-tmp])) { |
fa01a208 RH |
740 | gen_opc_buf[op_index] = INDEX_op_nop; |
741 | } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) { | |
742 | gen_opc_buf[op_index] = op_to_movi(op); | |
e590d4e6 | 743 | tcg_opt_gen_movi(gen_args, args[0], temps[args[4-tmp]].val); |
fa01a208 RH |
744 | gen_args += 2; |
745 | } else { | |
746 | gen_opc_buf[op_index] = op_to_mov(op); | |
e590d4e6 | 747 | tcg_opt_gen_mov(s, gen_args, args[0], args[4-tmp]); |
fa01a208 RH |
748 | gen_args += 2; |
749 | } | |
6e14e91b RH |
750 | args += 6; |
751 | break; | |
fa01a208 | 752 | } |
6e14e91b RH |
753 | goto do_default; |
754 | ||
bc1473ef RH |
755 | case INDEX_op_brcond2_i32: |
756 | /* Simplify LT/GE comparisons vs zero to a single compare | |
757 | vs the high word of the input. */ | |
758 | if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE) | |
759 | && temps[args[2]].state == TCG_TEMP_CONST | |
760 | && temps[args[3]].state == TCG_TEMP_CONST | |
761 | && temps[args[2]].val == 0 | |
762 | && temps[args[3]].val == 0) { | |
763 | gen_opc_buf[op_index] = INDEX_op_brcond_i32; | |
764 | gen_args[0] = args[1]; | |
765 | gen_args[1] = args[3]; | |
766 | gen_args[2] = args[4]; | |
767 | gen_args[3] = args[5]; | |
768 | gen_args += 4; | |
769 | args += 6; | |
770 | memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info)); | |
771 | break; | |
772 | } | |
773 | goto do_default; | |
774 | ||
775 | case INDEX_op_setcond2_i32: | |
776 | /* Simplify LT/GE comparisons vs zero to a single compare | |
777 | vs the high word of the input. */ | |
778 | if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE) | |
779 | && temps[args[3]].state == TCG_TEMP_CONST | |
780 | && temps[args[4]].state == TCG_TEMP_CONST | |
781 | && temps[args[3]].val == 0 | |
782 | && temps[args[4]].val == 0) { | |
783 | gen_opc_buf[op_index] = INDEX_op_setcond_i32; | |
784 | gen_args[0] = args[0]; | |
785 | gen_args[1] = args[2]; | |
786 | gen_args[2] = args[4]; | |
787 | gen_args[3] = args[5]; | |
788 | gen_args += 4; | |
789 | args += 6; | |
790 | break; | |
791 | } | |
792 | goto do_default; | |
793 | ||
8f2e8c07 | 794 | case INDEX_op_call: |
22613af4 KB |
795 | nb_call_args = (args[0] >> 16) + (args[0] & 0xffff); |
796 | if (!(args[nb_call_args + 1] & (TCG_CALL_CONST | TCG_CALL_PURE))) { | |
797 | for (i = 0; i < nb_globals; i++) { | |
e590d4e6 | 798 | reset_temp(i); |
22613af4 KB |
799 | } |
800 | } | |
801 | for (i = 0; i < (args[0] >> 16); i++) { | |
e590d4e6 | 802 | reset_temp(args[i + 1]); |
22613af4 KB |
803 | } |
804 | i = nb_call_args + 3; | |
8f2e8c07 KB |
805 | while (i) { |
806 | *gen_args = *args; | |
807 | args++; | |
808 | gen_args++; | |
809 | i--; | |
810 | } | |
811 | break; | |
6e14e91b | 812 | |
8f2e8c07 | 813 | default: |
6e14e91b RH |
814 | do_default: |
815 | /* Default case: we know nothing about operation (or were unable | |
816 | to compute the operation result) so no propagation is done. | |
817 | We trash everything if the operation is the end of a basic | |
818 | block, otherwise we only trash the output args. */ | |
a2550660 AJ |
819 | if (def->flags & TCG_OPF_BB_END) { |
820 | memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info)); | |
821 | } else { | |
822 | for (i = 0; i < def->nb_oargs; i++) { | |
e590d4e6 | 823 | reset_temp(args[i]); |
a2550660 | 824 | } |
22613af4 | 825 | } |
8f2e8c07 KB |
826 | for (i = 0; i < def->nb_args; i++) { |
827 | gen_args[i] = args[i]; | |
828 | } | |
829 | args += def->nb_args; | |
830 | gen_args += def->nb_args; | |
831 | break; | |
832 | } | |
833 | } | |
834 | ||
835 | return gen_args; | |
836 | } | |
837 | ||
838 | TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr, | |
839 | TCGArg *args, TCGOpDef *tcg_op_defs) | |
840 | { | |
841 | TCGArg *res; | |
842 | res = tcg_constant_folding(s, tcg_opc_ptr, args, tcg_op_defs); | |
843 | return res; | |
844 | } |