]>
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; | |
3a9d8b17 | 49 | tcg_target_ulong mask; |
22613af4 KB |
50 | }; |
51 | ||
52 | static struct tcg_temp_info temps[TCG_MAX_TEMPS]; | |
53 | ||
e590d4e6 AJ |
54 | /* Reset TEMP's state to TCG_TEMP_UNDEF. If TEMP only had one copy, remove |
55 | the copy flag from the left temp. */ | |
56 | static void reset_temp(TCGArg temp) | |
22613af4 | 57 | { |
e590d4e6 AJ |
58 | if (temps[temp].state == TCG_TEMP_COPY) { |
59 | if (temps[temp].prev_copy == temps[temp].next_copy) { | |
60 | temps[temps[temp].next_copy].state = TCG_TEMP_UNDEF; | |
61 | } else { | |
62 | temps[temps[temp].next_copy].prev_copy = temps[temp].prev_copy; | |
63 | temps[temps[temp].prev_copy].next_copy = temps[temp].next_copy; | |
22613af4 | 64 | } |
22613af4 | 65 | } |
48b56ce1 | 66 | temps[temp].state = TCG_TEMP_UNDEF; |
3a9d8b17 | 67 | temps[temp].mask = -1; |
22613af4 KB |
68 | } |
69 | ||
d193a14a PB |
70 | /* Reset all temporaries, given that there are NB_TEMPS of them. */ |
71 | static void reset_all_temps(int nb_temps) | |
72 | { | |
73 | int i; | |
74 | for (i = 0; i < nb_temps; i++) { | |
75 | temps[i].state = TCG_TEMP_UNDEF; | |
3a9d8b17 | 76 | temps[i].mask = -1; |
d193a14a PB |
77 | } |
78 | } | |
79 | ||
fe0de7aa | 80 | static int op_bits(TCGOpcode op) |
22613af4 | 81 | { |
8399ad59 RH |
82 | const TCGOpDef *def = &tcg_op_defs[op]; |
83 | return def->flags & TCG_OPF_64BIT ? 64 : 32; | |
22613af4 KB |
84 | } |
85 | ||
a62f6f56 RH |
86 | static TCGOpcode op_to_mov(TCGOpcode op) |
87 | { | |
88 | switch (op_bits(op)) { | |
89 | case 32: | |
90 | return INDEX_op_mov_i32; | |
91 | case 64: | |
92 | return INDEX_op_mov_i64; | |
93 | default: | |
94 | fprintf(stderr, "op_to_mov: unexpected return value of " | |
95 | "function op_bits.\n"); | |
96 | tcg_abort(); | |
97 | } | |
98 | } | |
99 | ||
fe0de7aa | 100 | static TCGOpcode op_to_movi(TCGOpcode op) |
22613af4 KB |
101 | { |
102 | switch (op_bits(op)) { | |
103 | case 32: | |
104 | return INDEX_op_movi_i32; | |
22613af4 KB |
105 | case 64: |
106 | return INDEX_op_movi_i64; | |
22613af4 KB |
107 | default: |
108 | fprintf(stderr, "op_to_movi: unexpected return value of " | |
109 | "function op_bits.\n"); | |
110 | tcg_abort(); | |
111 | } | |
112 | } | |
113 | ||
e590d4e6 AJ |
114 | static TCGArg find_better_copy(TCGContext *s, TCGArg temp) |
115 | { | |
116 | TCGArg i; | |
117 | ||
118 | /* If this is already a global, we can't do better. */ | |
119 | if (temp < s->nb_globals) { | |
120 | return temp; | |
121 | } | |
122 | ||
123 | /* Search for a global first. */ | |
124 | for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) { | |
125 | if (i < s->nb_globals) { | |
126 | return i; | |
127 | } | |
128 | } | |
129 | ||
130 | /* If it is a temp, search for a temp local. */ | |
131 | if (!s->temps[temp].temp_local) { | |
132 | for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) { | |
133 | if (s->temps[i].temp_local) { | |
134 | return i; | |
135 | } | |
136 | } | |
137 | } | |
138 | ||
139 | /* Failure to find a better representation, return the same temp. */ | |
140 | return temp; | |
141 | } | |
142 | ||
143 | static bool temps_are_copies(TCGArg arg1, TCGArg arg2) | |
144 | { | |
145 | TCGArg i; | |
146 | ||
147 | if (arg1 == arg2) { | |
148 | return true; | |
149 | } | |
150 | ||
151 | if (temps[arg1].state != TCG_TEMP_COPY | |
152 | || temps[arg2].state != TCG_TEMP_COPY) { | |
153 | return false; | |
154 | } | |
155 | ||
156 | for (i = temps[arg1].next_copy ; i != arg1 ; i = temps[i].next_copy) { | |
157 | if (i == arg2) { | |
158 | return true; | |
159 | } | |
160 | } | |
161 | ||
162 | return false; | |
163 | } | |
164 | ||
c45cb8bb | 165 | static void tcg_opt_gen_mov(TCGContext *s, TCGOp *op, TCGArg *args, |
a62f6f56 | 166 | TCGOpcode old_op, TCGArg dst, TCGArg src) |
22613af4 | 167 | { |
a62f6f56 | 168 | TCGOpcode new_op = op_to_mov(old_op); |
24666baf | 169 | tcg_target_ulong mask; |
a62f6f56 | 170 | |
c45cb8bb | 171 | op->opc = new_op; |
a62f6f56 | 172 | |
3a9d8b17 | 173 | reset_temp(dst); |
24666baf RH |
174 | mask = temps[src].mask; |
175 | if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_mov_i32) { | |
176 | /* High bits of the destination are now garbage. */ | |
177 | mask |= ~0xffffffffull; | |
178 | } | |
179 | temps[dst].mask = mask; | |
180 | ||
3a9d8b17 PB |
181 | assert(temps[src].state != TCG_TEMP_CONST); |
182 | ||
183 | if (s->temps[src].type == s->temps[dst].type) { | |
184 | if (temps[src].state != TCG_TEMP_COPY) { | |
185 | temps[src].state = TCG_TEMP_COPY; | |
186 | temps[src].next_copy = src; | |
187 | temps[src].prev_copy = src; | |
22613af4 | 188 | } |
3a9d8b17 PB |
189 | temps[dst].state = TCG_TEMP_COPY; |
190 | temps[dst].next_copy = temps[src].next_copy; | |
191 | temps[dst].prev_copy = src; | |
192 | temps[temps[dst].next_copy].prev_copy = dst; | |
193 | temps[src].next_copy = dst; | |
194 | } | |
e590d4e6 | 195 | |
c45cb8bb RH |
196 | args[0] = dst; |
197 | args[1] = src; | |
22613af4 KB |
198 | } |
199 | ||
c45cb8bb | 200 | static void tcg_opt_gen_movi(TCGContext *s, TCGOp *op, TCGArg *args, |
a62f6f56 | 201 | TCGOpcode old_op, TCGArg dst, TCGArg val) |
22613af4 | 202 | { |
a62f6f56 | 203 | TCGOpcode new_op = op_to_movi(old_op); |
24666baf | 204 | tcg_target_ulong mask; |
a62f6f56 | 205 | |
c45cb8bb | 206 | op->opc = new_op; |
a62f6f56 | 207 | |
3a9d8b17 PB |
208 | reset_temp(dst); |
209 | temps[dst].state = TCG_TEMP_CONST; | |
210 | temps[dst].val = val; | |
24666baf RH |
211 | mask = val; |
212 | if (TCG_TARGET_REG_BITS > 32 && new_op == INDEX_op_mov_i32) { | |
213 | /* High bits of the destination are now garbage. */ | |
214 | mask |= ~0xffffffffull; | |
215 | } | |
216 | temps[dst].mask = mask; | |
217 | ||
c45cb8bb RH |
218 | args[0] = dst; |
219 | args[1] = val; | |
22613af4 KB |
220 | } |
221 | ||
fe0de7aa | 222 | static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y) |
53108fb5 | 223 | { |
03271524 RH |
224 | uint64_t l64, h64; |
225 | ||
53108fb5 KB |
226 | switch (op) { |
227 | CASE_OP_32_64(add): | |
228 | return x + y; | |
229 | ||
230 | CASE_OP_32_64(sub): | |
231 | return x - y; | |
232 | ||
233 | CASE_OP_32_64(mul): | |
234 | return x * y; | |
235 | ||
9a81090b KB |
236 | CASE_OP_32_64(and): |
237 | return x & y; | |
238 | ||
239 | CASE_OP_32_64(or): | |
240 | return x | y; | |
241 | ||
242 | CASE_OP_32_64(xor): | |
243 | return x ^ y; | |
244 | ||
55c0975c | 245 | case INDEX_op_shl_i32: |
50c5c4d1 | 246 | return (uint32_t)x << (y & 31); |
55c0975c | 247 | |
55c0975c | 248 | case INDEX_op_shl_i64: |
50c5c4d1 | 249 | return (uint64_t)x << (y & 63); |
55c0975c KB |
250 | |
251 | case INDEX_op_shr_i32: | |
50c5c4d1 | 252 | return (uint32_t)x >> (y & 31); |
55c0975c | 253 | |
4bb7a41e | 254 | case INDEX_op_trunc_shr_i32: |
55c0975c | 255 | case INDEX_op_shr_i64: |
50c5c4d1 | 256 | return (uint64_t)x >> (y & 63); |
55c0975c KB |
257 | |
258 | case INDEX_op_sar_i32: | |
50c5c4d1 | 259 | return (int32_t)x >> (y & 31); |
55c0975c | 260 | |
55c0975c | 261 | case INDEX_op_sar_i64: |
50c5c4d1 | 262 | return (int64_t)x >> (y & 63); |
55c0975c KB |
263 | |
264 | case INDEX_op_rotr_i32: | |
50c5c4d1 | 265 | return ror32(x, y & 31); |
55c0975c | 266 | |
55c0975c | 267 | case INDEX_op_rotr_i64: |
50c5c4d1 | 268 | return ror64(x, y & 63); |
55c0975c KB |
269 | |
270 | case INDEX_op_rotl_i32: | |
50c5c4d1 | 271 | return rol32(x, y & 31); |
55c0975c | 272 | |
55c0975c | 273 | case INDEX_op_rotl_i64: |
50c5c4d1 | 274 | return rol64(x, y & 63); |
25c4d9cc RH |
275 | |
276 | CASE_OP_32_64(not): | |
a640f031 | 277 | return ~x; |
25c4d9cc | 278 | |
cb25c80a RH |
279 | CASE_OP_32_64(neg): |
280 | return -x; | |
281 | ||
282 | CASE_OP_32_64(andc): | |
283 | return x & ~y; | |
284 | ||
285 | CASE_OP_32_64(orc): | |
286 | return x | ~y; | |
287 | ||
288 | CASE_OP_32_64(eqv): | |
289 | return ~(x ^ y); | |
290 | ||
291 | CASE_OP_32_64(nand): | |
292 | return ~(x & y); | |
293 | ||
294 | CASE_OP_32_64(nor): | |
295 | return ~(x | y); | |
296 | ||
25c4d9cc | 297 | CASE_OP_32_64(ext8s): |
a640f031 | 298 | return (int8_t)x; |
25c4d9cc RH |
299 | |
300 | CASE_OP_32_64(ext16s): | |
a640f031 | 301 | return (int16_t)x; |
25c4d9cc RH |
302 | |
303 | CASE_OP_32_64(ext8u): | |
a640f031 | 304 | return (uint8_t)x; |
25c4d9cc RH |
305 | |
306 | CASE_OP_32_64(ext16u): | |
a640f031 KB |
307 | return (uint16_t)x; |
308 | ||
a640f031 KB |
309 | case INDEX_op_ext32s_i64: |
310 | return (int32_t)x; | |
311 | ||
312 | case INDEX_op_ext32u_i64: | |
313 | return (uint32_t)x; | |
a640f031 | 314 | |
03271524 RH |
315 | case INDEX_op_muluh_i32: |
316 | return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32; | |
317 | case INDEX_op_mulsh_i32: | |
318 | return ((int64_t)(int32_t)x * (int32_t)y) >> 32; | |
319 | ||
320 | case INDEX_op_muluh_i64: | |
321 | mulu64(&l64, &h64, x, y); | |
322 | return h64; | |
323 | case INDEX_op_mulsh_i64: | |
324 | muls64(&l64, &h64, x, y); | |
325 | return h64; | |
326 | ||
01547f7f RH |
327 | case INDEX_op_div_i32: |
328 | /* Avoid crashing on divide by zero, otherwise undefined. */ | |
329 | return (int32_t)x / ((int32_t)y ? : 1); | |
330 | case INDEX_op_divu_i32: | |
331 | return (uint32_t)x / ((uint32_t)y ? : 1); | |
332 | case INDEX_op_div_i64: | |
333 | return (int64_t)x / ((int64_t)y ? : 1); | |
334 | case INDEX_op_divu_i64: | |
335 | return (uint64_t)x / ((uint64_t)y ? : 1); | |
336 | ||
337 | case INDEX_op_rem_i32: | |
338 | return (int32_t)x % ((int32_t)y ? : 1); | |
339 | case INDEX_op_remu_i32: | |
340 | return (uint32_t)x % ((uint32_t)y ? : 1); | |
341 | case INDEX_op_rem_i64: | |
342 | return (int64_t)x % ((int64_t)y ? : 1); | |
343 | case INDEX_op_remu_i64: | |
344 | return (uint64_t)x % ((uint64_t)y ? : 1); | |
345 | ||
53108fb5 KB |
346 | default: |
347 | fprintf(stderr, | |
348 | "Unrecognized operation %d in do_constant_folding.\n", op); | |
349 | tcg_abort(); | |
350 | } | |
351 | } | |
352 | ||
fe0de7aa | 353 | static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y) |
53108fb5 KB |
354 | { |
355 | TCGArg res = do_constant_folding_2(op, x, y); | |
53108fb5 KB |
356 | if (op_bits(op) == 32) { |
357 | res &= 0xffffffff; | |
358 | } | |
53108fb5 KB |
359 | return res; |
360 | } | |
361 | ||
9519da7e RH |
362 | static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c) |
363 | { | |
364 | switch (c) { | |
365 | case TCG_COND_EQ: | |
366 | return x == y; | |
367 | case TCG_COND_NE: | |
368 | return x != y; | |
369 | case TCG_COND_LT: | |
370 | return (int32_t)x < (int32_t)y; | |
371 | case TCG_COND_GE: | |
372 | return (int32_t)x >= (int32_t)y; | |
373 | case TCG_COND_LE: | |
374 | return (int32_t)x <= (int32_t)y; | |
375 | case TCG_COND_GT: | |
376 | return (int32_t)x > (int32_t)y; | |
377 | case TCG_COND_LTU: | |
378 | return x < y; | |
379 | case TCG_COND_GEU: | |
380 | return x >= y; | |
381 | case TCG_COND_LEU: | |
382 | return x <= y; | |
383 | case TCG_COND_GTU: | |
384 | return x > y; | |
385 | default: | |
386 | tcg_abort(); | |
387 | } | |
388 | } | |
389 | ||
390 | static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c) | |
391 | { | |
392 | switch (c) { | |
393 | case TCG_COND_EQ: | |
394 | return x == y; | |
395 | case TCG_COND_NE: | |
396 | return x != y; | |
397 | case TCG_COND_LT: | |
398 | return (int64_t)x < (int64_t)y; | |
399 | case TCG_COND_GE: | |
400 | return (int64_t)x >= (int64_t)y; | |
401 | case TCG_COND_LE: | |
402 | return (int64_t)x <= (int64_t)y; | |
403 | case TCG_COND_GT: | |
404 | return (int64_t)x > (int64_t)y; | |
405 | case TCG_COND_LTU: | |
406 | return x < y; | |
407 | case TCG_COND_GEU: | |
408 | return x >= y; | |
409 | case TCG_COND_LEU: | |
410 | return x <= y; | |
411 | case TCG_COND_GTU: | |
412 | return x > y; | |
413 | default: | |
414 | tcg_abort(); | |
415 | } | |
416 | } | |
417 | ||
418 | static bool do_constant_folding_cond_eq(TCGCond c) | |
419 | { | |
420 | switch (c) { | |
421 | case TCG_COND_GT: | |
422 | case TCG_COND_LTU: | |
423 | case TCG_COND_LT: | |
424 | case TCG_COND_GTU: | |
425 | case TCG_COND_NE: | |
426 | return 0; | |
427 | case TCG_COND_GE: | |
428 | case TCG_COND_GEU: | |
429 | case TCG_COND_LE: | |
430 | case TCG_COND_LEU: | |
431 | case TCG_COND_EQ: | |
432 | return 1; | |
433 | default: | |
434 | tcg_abort(); | |
435 | } | |
436 | } | |
437 | ||
b336ceb6 AJ |
438 | /* Return 2 if the condition can't be simplified, and the result |
439 | of the condition (0 or 1) if it can */ | |
f8dd19e5 AJ |
440 | static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x, |
441 | TCGArg y, TCGCond c) | |
442 | { | |
b336ceb6 AJ |
443 | if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) { |
444 | switch (op_bits(op)) { | |
445 | case 32: | |
9519da7e | 446 | return do_constant_folding_cond_32(temps[x].val, temps[y].val, c); |
b336ceb6 | 447 | case 64: |
9519da7e | 448 | return do_constant_folding_cond_64(temps[x].val, temps[y].val, c); |
0aed257f | 449 | default: |
9519da7e | 450 | tcg_abort(); |
b336ceb6 | 451 | } |
9519da7e RH |
452 | } else if (temps_are_copies(x, y)) { |
453 | return do_constant_folding_cond_eq(c); | |
b336ceb6 AJ |
454 | } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) { |
455 | switch (c) { | |
f8dd19e5 | 456 | case TCG_COND_LTU: |
b336ceb6 | 457 | return 0; |
f8dd19e5 | 458 | case TCG_COND_GEU: |
b336ceb6 AJ |
459 | return 1; |
460 | default: | |
461 | return 2; | |
f8dd19e5 | 462 | } |
b336ceb6 AJ |
463 | } else { |
464 | return 2; | |
f8dd19e5 | 465 | } |
f8dd19e5 AJ |
466 | } |
467 | ||
6c4382f8 RH |
468 | /* Return 2 if the condition can't be simplified, and the result |
469 | of the condition (0 or 1) if it can */ | |
470 | static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c) | |
471 | { | |
472 | TCGArg al = p1[0], ah = p1[1]; | |
473 | TCGArg bl = p2[0], bh = p2[1]; | |
474 | ||
475 | if (temps[bl].state == TCG_TEMP_CONST | |
476 | && temps[bh].state == TCG_TEMP_CONST) { | |
477 | uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val; | |
478 | ||
479 | if (temps[al].state == TCG_TEMP_CONST | |
480 | && temps[ah].state == TCG_TEMP_CONST) { | |
481 | uint64_t a; | |
482 | a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val; | |
483 | return do_constant_folding_cond_64(a, b, c); | |
484 | } | |
485 | if (b == 0) { | |
486 | switch (c) { | |
487 | case TCG_COND_LTU: | |
488 | return 0; | |
489 | case TCG_COND_GEU: | |
490 | return 1; | |
491 | default: | |
492 | break; | |
493 | } | |
494 | } | |
495 | } | |
496 | if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) { | |
497 | return do_constant_folding_cond_eq(c); | |
498 | } | |
499 | return 2; | |
500 | } | |
501 | ||
24c9ae4e RH |
502 | static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2) |
503 | { | |
504 | TCGArg a1 = *p1, a2 = *p2; | |
505 | int sum = 0; | |
506 | sum += temps[a1].state == TCG_TEMP_CONST; | |
507 | sum -= temps[a2].state == TCG_TEMP_CONST; | |
508 | ||
509 | /* Prefer the constant in second argument, and then the form | |
510 | op a, a, b, which is better handled on non-RISC hosts. */ | |
511 | if (sum > 0 || (sum == 0 && dest == a2)) { | |
512 | *p1 = a2; | |
513 | *p2 = a1; | |
514 | return true; | |
515 | } | |
516 | return false; | |
517 | } | |
518 | ||
0bfcb865 RH |
519 | static bool swap_commutative2(TCGArg *p1, TCGArg *p2) |
520 | { | |
521 | int sum = 0; | |
522 | sum += temps[p1[0]].state == TCG_TEMP_CONST; | |
523 | sum += temps[p1[1]].state == TCG_TEMP_CONST; | |
524 | sum -= temps[p2[0]].state == TCG_TEMP_CONST; | |
525 | sum -= temps[p2[1]].state == TCG_TEMP_CONST; | |
526 | if (sum > 0) { | |
527 | TCGArg t; | |
528 | t = p1[0], p1[0] = p2[0], p2[0] = t; | |
529 | t = p1[1], p1[1] = p2[1], p2[1] = t; | |
530 | return true; | |
531 | } | |
532 | return false; | |
533 | } | |
534 | ||
22613af4 | 535 | /* Propagate constants and copies, fold constant expressions. */ |
c45cb8bb | 536 | static void tcg_constant_folding(TCGContext *s) |
8f2e8c07 | 537 | { |
c45cb8bb | 538 | int oi, oi_next, nb_temps, nb_globals; |
5d8f5363 | 539 | |
22613af4 KB |
540 | /* Array VALS has an element for each temp. |
541 | If this temp holds a constant then its value is kept in VALS' element. | |
e590d4e6 AJ |
542 | If this temp is a copy of other ones then the other copies are |
543 | available through the doubly linked circular list. */ | |
8f2e8c07 KB |
544 | |
545 | nb_temps = s->nb_temps; | |
546 | nb_globals = s->nb_globals; | |
d193a14a | 547 | reset_all_temps(nb_temps); |
8f2e8c07 | 548 | |
c45cb8bb | 549 | for (oi = s->gen_first_op_idx; oi >= 0; oi = oi_next) { |
24666baf | 550 | tcg_target_ulong mask, partmask, affected; |
c45cb8bb | 551 | int nb_oargs, nb_iargs, i; |
cf066674 RH |
552 | TCGArg tmp; |
553 | ||
c45cb8bb RH |
554 | TCGOp * const op = &s->gen_op_buf[oi]; |
555 | TCGArg * const args = &s->gen_opparam_buf[op->args]; | |
556 | TCGOpcode opc = op->opc; | |
557 | const TCGOpDef *def = &tcg_op_defs[opc]; | |
558 | ||
559 | oi_next = op->next; | |
560 | if (opc == INDEX_op_call) { | |
561 | nb_oargs = op->callo; | |
562 | nb_iargs = op->calli; | |
1ff8c541 | 563 | } else { |
cf066674 RH |
564 | nb_oargs = def->nb_oargs; |
565 | nb_iargs = def->nb_iargs; | |
cf066674 RH |
566 | } |
567 | ||
568 | /* Do copy propagation */ | |
569 | for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) { | |
570 | if (temps[args[i]].state == TCG_TEMP_COPY) { | |
571 | args[i] = find_better_copy(s, args[i]); | |
22613af4 KB |
572 | } |
573 | } | |
574 | ||
53108fb5 | 575 | /* For commutative operations make constant second argument */ |
c45cb8bb | 576 | switch (opc) { |
53108fb5 KB |
577 | CASE_OP_32_64(add): |
578 | CASE_OP_32_64(mul): | |
9a81090b KB |
579 | CASE_OP_32_64(and): |
580 | CASE_OP_32_64(or): | |
581 | CASE_OP_32_64(xor): | |
cb25c80a RH |
582 | CASE_OP_32_64(eqv): |
583 | CASE_OP_32_64(nand): | |
584 | CASE_OP_32_64(nor): | |
03271524 RH |
585 | CASE_OP_32_64(muluh): |
586 | CASE_OP_32_64(mulsh): | |
24c9ae4e | 587 | swap_commutative(args[0], &args[1], &args[2]); |
53108fb5 | 588 | break; |
65a7cce1 | 589 | CASE_OP_32_64(brcond): |
24c9ae4e | 590 | if (swap_commutative(-1, &args[0], &args[1])) { |
65a7cce1 AJ |
591 | args[2] = tcg_swap_cond(args[2]); |
592 | } | |
593 | break; | |
594 | CASE_OP_32_64(setcond): | |
24c9ae4e | 595 | if (swap_commutative(args[0], &args[1], &args[2])) { |
65a7cce1 AJ |
596 | args[3] = tcg_swap_cond(args[3]); |
597 | } | |
598 | break; | |
fa01a208 | 599 | CASE_OP_32_64(movcond): |
24c9ae4e RH |
600 | if (swap_commutative(-1, &args[1], &args[2])) { |
601 | args[5] = tcg_swap_cond(args[5]); | |
5d8f5363 RH |
602 | } |
603 | /* For movcond, we canonicalize the "false" input reg to match | |
604 | the destination reg so that the tcg backend can implement | |
605 | a "move if true" operation. */ | |
24c9ae4e RH |
606 | if (swap_commutative(args[0], &args[4], &args[3])) { |
607 | args[5] = tcg_invert_cond(args[5]); | |
fa01a208 | 608 | } |
1e484e61 | 609 | break; |
d7156f7c | 610 | CASE_OP_32_64(add2): |
1e484e61 RH |
611 | swap_commutative(args[0], &args[2], &args[4]); |
612 | swap_commutative(args[1], &args[3], &args[5]); | |
613 | break; | |
d7156f7c | 614 | CASE_OP_32_64(mulu2): |
4d3203fd | 615 | CASE_OP_32_64(muls2): |
1414968a RH |
616 | swap_commutative(args[0], &args[2], &args[3]); |
617 | break; | |
0bfcb865 RH |
618 | case INDEX_op_brcond2_i32: |
619 | if (swap_commutative2(&args[0], &args[2])) { | |
620 | args[4] = tcg_swap_cond(args[4]); | |
621 | } | |
622 | break; | |
623 | case INDEX_op_setcond2_i32: | |
624 | if (swap_commutative2(&args[1], &args[3])) { | |
625 | args[5] = tcg_swap_cond(args[5]); | |
626 | } | |
627 | break; | |
53108fb5 KB |
628 | default: |
629 | break; | |
630 | } | |
631 | ||
2d497542 RH |
632 | /* Simplify expressions for "shift/rot r, 0, a => movi r, 0", |
633 | and "sub r, 0, a => neg r, a" case. */ | |
c45cb8bb | 634 | switch (opc) { |
01ee5282 AJ |
635 | CASE_OP_32_64(shl): |
636 | CASE_OP_32_64(shr): | |
637 | CASE_OP_32_64(sar): | |
638 | CASE_OP_32_64(rotl): | |
639 | CASE_OP_32_64(rotr): | |
640 | if (temps[args[1]].state == TCG_TEMP_CONST | |
641 | && temps[args[1]].val == 0) { | |
c45cb8bb | 642 | tcg_opt_gen_movi(s, op, args, opc, args[0], 0); |
01ee5282 AJ |
643 | continue; |
644 | } | |
645 | break; | |
2d497542 RH |
646 | CASE_OP_32_64(sub): |
647 | { | |
648 | TCGOpcode neg_op; | |
649 | bool have_neg; | |
650 | ||
651 | if (temps[args[2]].state == TCG_TEMP_CONST) { | |
652 | /* Proceed with possible constant folding. */ | |
653 | break; | |
654 | } | |
c45cb8bb | 655 | if (opc == INDEX_op_sub_i32) { |
2d497542 RH |
656 | neg_op = INDEX_op_neg_i32; |
657 | have_neg = TCG_TARGET_HAS_neg_i32; | |
658 | } else { | |
659 | neg_op = INDEX_op_neg_i64; | |
660 | have_neg = TCG_TARGET_HAS_neg_i64; | |
661 | } | |
662 | if (!have_neg) { | |
663 | break; | |
664 | } | |
665 | if (temps[args[1]].state == TCG_TEMP_CONST | |
666 | && temps[args[1]].val == 0) { | |
c45cb8bb | 667 | op->opc = neg_op; |
2d497542 | 668 | reset_temp(args[0]); |
c45cb8bb | 669 | args[1] = args[2]; |
2d497542 RH |
670 | continue; |
671 | } | |
672 | } | |
673 | break; | |
e201b564 RH |
674 | CASE_OP_32_64(xor): |
675 | CASE_OP_32_64(nand): | |
676 | if (temps[args[1]].state != TCG_TEMP_CONST | |
677 | && temps[args[2]].state == TCG_TEMP_CONST | |
678 | && temps[args[2]].val == -1) { | |
679 | i = 1; | |
680 | goto try_not; | |
681 | } | |
682 | break; | |
683 | CASE_OP_32_64(nor): | |
684 | if (temps[args[1]].state != TCG_TEMP_CONST | |
685 | && temps[args[2]].state == TCG_TEMP_CONST | |
686 | && temps[args[2]].val == 0) { | |
687 | i = 1; | |
688 | goto try_not; | |
689 | } | |
690 | break; | |
691 | CASE_OP_32_64(andc): | |
692 | if (temps[args[2]].state != TCG_TEMP_CONST | |
693 | && temps[args[1]].state == TCG_TEMP_CONST | |
694 | && temps[args[1]].val == -1) { | |
695 | i = 2; | |
696 | goto try_not; | |
697 | } | |
698 | break; | |
699 | CASE_OP_32_64(orc): | |
700 | CASE_OP_32_64(eqv): | |
701 | if (temps[args[2]].state != TCG_TEMP_CONST | |
702 | && temps[args[1]].state == TCG_TEMP_CONST | |
703 | && temps[args[1]].val == 0) { | |
704 | i = 2; | |
705 | goto try_not; | |
706 | } | |
707 | break; | |
708 | try_not: | |
709 | { | |
710 | TCGOpcode not_op; | |
711 | bool have_not; | |
712 | ||
713 | if (def->flags & TCG_OPF_64BIT) { | |
714 | not_op = INDEX_op_not_i64; | |
715 | have_not = TCG_TARGET_HAS_not_i64; | |
716 | } else { | |
717 | not_op = INDEX_op_not_i32; | |
718 | have_not = TCG_TARGET_HAS_not_i32; | |
719 | } | |
720 | if (!have_not) { | |
721 | break; | |
722 | } | |
c45cb8bb | 723 | op->opc = not_op; |
e201b564 | 724 | reset_temp(args[0]); |
c45cb8bb | 725 | args[1] = args[i]; |
e201b564 RH |
726 | continue; |
727 | } | |
01ee5282 AJ |
728 | default: |
729 | break; | |
730 | } | |
731 | ||
464a1441 | 732 | /* Simplify expression for "op r, a, const => mov r, a" cases */ |
c45cb8bb | 733 | switch (opc) { |
53108fb5 KB |
734 | CASE_OP_32_64(add): |
735 | CASE_OP_32_64(sub): | |
55c0975c KB |
736 | CASE_OP_32_64(shl): |
737 | CASE_OP_32_64(shr): | |
738 | CASE_OP_32_64(sar): | |
25c4d9cc RH |
739 | CASE_OP_32_64(rotl): |
740 | CASE_OP_32_64(rotr): | |
38ee188b AJ |
741 | CASE_OP_32_64(or): |
742 | CASE_OP_32_64(xor): | |
464a1441 RH |
743 | CASE_OP_32_64(andc): |
744 | if (temps[args[1]].state != TCG_TEMP_CONST | |
745 | && temps[args[2]].state == TCG_TEMP_CONST | |
53108fb5 | 746 | && temps[args[2]].val == 0) { |
464a1441 | 747 | goto do_mov3; |
53108fb5 KB |
748 | } |
749 | break; | |
464a1441 RH |
750 | CASE_OP_32_64(and): |
751 | CASE_OP_32_64(orc): | |
752 | CASE_OP_32_64(eqv): | |
753 | if (temps[args[1]].state != TCG_TEMP_CONST | |
754 | && temps[args[2]].state == TCG_TEMP_CONST | |
755 | && temps[args[2]].val == -1) { | |
756 | goto do_mov3; | |
757 | } | |
758 | break; | |
759 | do_mov3: | |
760 | if (temps_are_copies(args[0], args[1])) { | |
c45cb8bb | 761 | op->opc = INDEX_op_nop; |
464a1441 | 762 | } else { |
c45cb8bb | 763 | tcg_opt_gen_mov(s, op, args, opc, args[0], args[1]); |
464a1441 | 764 | } |
464a1441 | 765 | continue; |
56e49438 AJ |
766 | default: |
767 | break; | |
768 | } | |
769 | ||
3031244b AJ |
770 | /* Simplify using known-zero bits. Currently only ops with a single |
771 | output argument is supported. */ | |
3a9d8b17 | 772 | mask = -1; |
633f6502 | 773 | affected = -1; |
c45cb8bb | 774 | switch (opc) { |
3a9d8b17 PB |
775 | CASE_OP_32_64(ext8s): |
776 | if ((temps[args[1]].mask & 0x80) != 0) { | |
777 | break; | |
778 | } | |
779 | CASE_OP_32_64(ext8u): | |
780 | mask = 0xff; | |
781 | goto and_const; | |
782 | CASE_OP_32_64(ext16s): | |
783 | if ((temps[args[1]].mask & 0x8000) != 0) { | |
784 | break; | |
785 | } | |
786 | CASE_OP_32_64(ext16u): | |
787 | mask = 0xffff; | |
788 | goto and_const; | |
789 | case INDEX_op_ext32s_i64: | |
790 | if ((temps[args[1]].mask & 0x80000000) != 0) { | |
791 | break; | |
792 | } | |
793 | case INDEX_op_ext32u_i64: | |
794 | mask = 0xffffffffU; | |
795 | goto and_const; | |
796 | ||
797 | CASE_OP_32_64(and): | |
798 | mask = temps[args[2]].mask; | |
799 | if (temps[args[2]].state == TCG_TEMP_CONST) { | |
800 | and_const: | |
633f6502 | 801 | affected = temps[args[1]].mask & ~mask; |
3a9d8b17 PB |
802 | } |
803 | mask = temps[args[1]].mask & mask; | |
804 | break; | |
805 | ||
23ec69ed RH |
806 | CASE_OP_32_64(andc): |
807 | /* Known-zeros does not imply known-ones. Therefore unless | |
808 | args[2] is constant, we can't infer anything from it. */ | |
809 | if (temps[args[2]].state == TCG_TEMP_CONST) { | |
810 | mask = ~temps[args[2]].mask; | |
811 | goto and_const; | |
812 | } | |
813 | /* But we certainly know nothing outside args[1] may be set. */ | |
814 | mask = temps[args[1]].mask; | |
815 | break; | |
816 | ||
e46b225a AJ |
817 | case INDEX_op_sar_i32: |
818 | if (temps[args[2]].state == TCG_TEMP_CONST) { | |
50c5c4d1 RH |
819 | tmp = temps[args[2]].val & 31; |
820 | mask = (int32_t)temps[args[1]].mask >> tmp; | |
e46b225a AJ |
821 | } |
822 | break; | |
823 | case INDEX_op_sar_i64: | |
3a9d8b17 | 824 | if (temps[args[2]].state == TCG_TEMP_CONST) { |
50c5c4d1 RH |
825 | tmp = temps[args[2]].val & 63; |
826 | mask = (int64_t)temps[args[1]].mask >> tmp; | |
3a9d8b17 PB |
827 | } |
828 | break; | |
829 | ||
e46b225a AJ |
830 | case INDEX_op_shr_i32: |
831 | if (temps[args[2]].state == TCG_TEMP_CONST) { | |
50c5c4d1 RH |
832 | tmp = temps[args[2]].val & 31; |
833 | mask = (uint32_t)temps[args[1]].mask >> tmp; | |
e46b225a AJ |
834 | } |
835 | break; | |
836 | case INDEX_op_shr_i64: | |
3a9d8b17 | 837 | if (temps[args[2]].state == TCG_TEMP_CONST) { |
50c5c4d1 RH |
838 | tmp = temps[args[2]].val & 63; |
839 | mask = (uint64_t)temps[args[1]].mask >> tmp; | |
3a9d8b17 PB |
840 | } |
841 | break; | |
842 | ||
4bb7a41e RH |
843 | case INDEX_op_trunc_shr_i32: |
844 | mask = (uint64_t)temps[args[1]].mask >> args[2]; | |
845 | break; | |
846 | ||
3a9d8b17 PB |
847 | CASE_OP_32_64(shl): |
848 | if (temps[args[2]].state == TCG_TEMP_CONST) { | |
50c5c4d1 RH |
849 | tmp = temps[args[2]].val & (TCG_TARGET_REG_BITS - 1); |
850 | mask = temps[args[1]].mask << tmp; | |
3a9d8b17 PB |
851 | } |
852 | break; | |
853 | ||
854 | CASE_OP_32_64(neg): | |
855 | /* Set to 1 all bits to the left of the rightmost. */ | |
856 | mask = -(temps[args[1]].mask & -temps[args[1]].mask); | |
857 | break; | |
858 | ||
859 | CASE_OP_32_64(deposit): | |
d998e555 RH |
860 | mask = deposit64(temps[args[1]].mask, args[3], args[4], |
861 | temps[args[2]].mask); | |
3a9d8b17 PB |
862 | break; |
863 | ||
864 | CASE_OP_32_64(or): | |
865 | CASE_OP_32_64(xor): | |
866 | mask = temps[args[1]].mask | temps[args[2]].mask; | |
867 | break; | |
868 | ||
869 | CASE_OP_32_64(setcond): | |
a763551a | 870 | case INDEX_op_setcond2_i32: |
3a9d8b17 PB |
871 | mask = 1; |
872 | break; | |
873 | ||
874 | CASE_OP_32_64(movcond): | |
875 | mask = temps[args[3]].mask | temps[args[4]].mask; | |
876 | break; | |
877 | ||
c8d70272 | 878 | CASE_OP_32_64(ld8u): |
c8d70272 AJ |
879 | mask = 0xff; |
880 | break; | |
881 | CASE_OP_32_64(ld16u): | |
c8d70272 AJ |
882 | mask = 0xffff; |
883 | break; | |
884 | case INDEX_op_ld32u_i64: | |
c8d70272 AJ |
885 | mask = 0xffffffffu; |
886 | break; | |
887 | ||
888 | CASE_OP_32_64(qemu_ld): | |
889 | { | |
cf066674 | 890 | TCGMemOp mop = args[nb_oargs + nb_iargs]; |
c8d70272 AJ |
891 | if (!(mop & MO_SIGN)) { |
892 | mask = (2ULL << ((8 << (mop & MO_SIZE)) - 1)) - 1; | |
893 | } | |
894 | } | |
895 | break; | |
896 | ||
3a9d8b17 PB |
897 | default: |
898 | break; | |
899 | } | |
900 | ||
bc8d688f RH |
901 | /* 32-bit ops generate 32-bit results. For the result is zero test |
902 | below, we can ignore high bits, but for further optimizations we | |
903 | need to record that the high bits contain garbage. */ | |
24666baf | 904 | partmask = mask; |
bc8d688f | 905 | if (!(def->flags & TCG_OPF_64BIT)) { |
24666baf RH |
906 | mask |= ~(tcg_target_ulong)0xffffffffu; |
907 | partmask &= 0xffffffffu; | |
908 | affected &= 0xffffffffu; | |
f096dc96 AJ |
909 | } |
910 | ||
24666baf | 911 | if (partmask == 0) { |
cf066674 | 912 | assert(nb_oargs == 1); |
c45cb8bb | 913 | tcg_opt_gen_movi(s, op, args, opc, args[0], 0); |
633f6502 PB |
914 | continue; |
915 | } | |
916 | if (affected == 0) { | |
cf066674 | 917 | assert(nb_oargs == 1); |
633f6502 | 918 | if (temps_are_copies(args[0], args[1])) { |
c45cb8bb | 919 | op->opc = INDEX_op_nop; |
633f6502 | 920 | } else if (temps[args[1]].state != TCG_TEMP_CONST) { |
c45cb8bb | 921 | tcg_opt_gen_mov(s, op, args, opc, args[0], args[1]); |
633f6502 | 922 | } else { |
c45cb8bb | 923 | tcg_opt_gen_movi(s, op, args, opc, |
a62f6f56 | 924 | args[0], temps[args[1]].val); |
633f6502 | 925 | } |
633f6502 PB |
926 | continue; |
927 | } | |
928 | ||
56e49438 | 929 | /* Simplify expression for "op r, a, 0 => movi r, 0" cases */ |
c45cb8bb | 930 | switch (opc) { |
61251c0c | 931 | CASE_OP_32_64(and): |
53108fb5 | 932 | CASE_OP_32_64(mul): |
03271524 RH |
933 | CASE_OP_32_64(muluh): |
934 | CASE_OP_32_64(mulsh): | |
53108fb5 KB |
935 | if ((temps[args[2]].state == TCG_TEMP_CONST |
936 | && temps[args[2]].val == 0)) { | |
c45cb8bb | 937 | tcg_opt_gen_movi(s, op, args, opc, args[0], 0); |
53108fb5 KB |
938 | continue; |
939 | } | |
940 | break; | |
56e49438 AJ |
941 | default: |
942 | break; | |
943 | } | |
944 | ||
945 | /* Simplify expression for "op r, a, a => mov r, a" cases */ | |
c45cb8bb | 946 | switch (opc) { |
9a81090b KB |
947 | CASE_OP_32_64(or): |
948 | CASE_OP_32_64(and): | |
0aba1c73 | 949 | if (temps_are_copies(args[1], args[2])) { |
e590d4e6 | 950 | if (temps_are_copies(args[0], args[1])) { |
c45cb8bb | 951 | op->opc = INDEX_op_nop; |
9a81090b | 952 | } else { |
c45cb8bb | 953 | tcg_opt_gen_mov(s, op, args, opc, args[0], args[1]); |
9a81090b KB |
954 | } |
955 | continue; | |
956 | } | |
957 | break; | |
fe0de7aa BS |
958 | default: |
959 | break; | |
53108fb5 KB |
960 | } |
961 | ||
3c94193e | 962 | /* Simplify expression for "op r, a, a => movi r, 0" cases */ |
c45cb8bb | 963 | switch (opc) { |
e64e958e | 964 | CASE_OP_32_64(andc): |
3c94193e AJ |
965 | CASE_OP_32_64(sub): |
966 | CASE_OP_32_64(xor): | |
967 | if (temps_are_copies(args[1], args[2])) { | |
c45cb8bb | 968 | tcg_opt_gen_movi(s, op, args, opc, args[0], 0); |
3c94193e AJ |
969 | continue; |
970 | } | |
971 | break; | |
972 | default: | |
973 | break; | |
974 | } | |
975 | ||
22613af4 KB |
976 | /* Propagate constants through copy operations and do constant |
977 | folding. Constants will be substituted to arguments by register | |
978 | allocator where needed and possible. Also detect copies. */ | |
c45cb8bb | 979 | switch (opc) { |
22613af4 | 980 | CASE_OP_32_64(mov): |
e590d4e6 | 981 | if (temps_are_copies(args[0], args[1])) { |
c45cb8bb | 982 | op->opc = INDEX_op_nop; |
22613af4 KB |
983 | break; |
984 | } | |
985 | if (temps[args[1]].state != TCG_TEMP_CONST) { | |
c45cb8bb | 986 | tcg_opt_gen_mov(s, op, args, opc, args[0], args[1]); |
22613af4 KB |
987 | break; |
988 | } | |
989 | /* Source argument is constant. Rewrite the operation and | |
990 | let movi case handle it. */ | |
22613af4 KB |
991 | args[1] = temps[args[1]].val; |
992 | /* fallthrough */ | |
993 | CASE_OP_32_64(movi): | |
c45cb8bb | 994 | tcg_opt_gen_movi(s, op, args, opc, args[0], args[1]); |
22613af4 | 995 | break; |
6e14e91b | 996 | |
a640f031 | 997 | CASE_OP_32_64(not): |
cb25c80a | 998 | CASE_OP_32_64(neg): |
25c4d9cc RH |
999 | CASE_OP_32_64(ext8s): |
1000 | CASE_OP_32_64(ext8u): | |
1001 | CASE_OP_32_64(ext16s): | |
1002 | CASE_OP_32_64(ext16u): | |
a640f031 KB |
1003 | case INDEX_op_ext32s_i64: |
1004 | case INDEX_op_ext32u_i64: | |
a640f031 | 1005 | if (temps[args[1]].state == TCG_TEMP_CONST) { |
c45cb8bb RH |
1006 | tmp = do_constant_folding(opc, temps[args[1]].val, 0); |
1007 | tcg_opt_gen_movi(s, op, args, opc, args[0], tmp); | |
6e14e91b | 1008 | break; |
a640f031 | 1009 | } |
6e14e91b RH |
1010 | goto do_default; |
1011 | ||
4bb7a41e RH |
1012 | case INDEX_op_trunc_shr_i32: |
1013 | if (temps[args[1]].state == TCG_TEMP_CONST) { | |
c45cb8bb RH |
1014 | tmp = do_constant_folding(opc, temps[args[1]].val, args[2]); |
1015 | tcg_opt_gen_movi(s, op, args, opc, args[0], tmp); | |
4bb7a41e RH |
1016 | break; |
1017 | } | |
1018 | goto do_default; | |
1019 | ||
53108fb5 KB |
1020 | CASE_OP_32_64(add): |
1021 | CASE_OP_32_64(sub): | |
1022 | CASE_OP_32_64(mul): | |
9a81090b KB |
1023 | CASE_OP_32_64(or): |
1024 | CASE_OP_32_64(and): | |
1025 | CASE_OP_32_64(xor): | |
55c0975c KB |
1026 | CASE_OP_32_64(shl): |
1027 | CASE_OP_32_64(shr): | |
1028 | CASE_OP_32_64(sar): | |
25c4d9cc RH |
1029 | CASE_OP_32_64(rotl): |
1030 | CASE_OP_32_64(rotr): | |
cb25c80a RH |
1031 | CASE_OP_32_64(andc): |
1032 | CASE_OP_32_64(orc): | |
1033 | CASE_OP_32_64(eqv): | |
1034 | CASE_OP_32_64(nand): | |
1035 | CASE_OP_32_64(nor): | |
03271524 RH |
1036 | CASE_OP_32_64(muluh): |
1037 | CASE_OP_32_64(mulsh): | |
01547f7f RH |
1038 | CASE_OP_32_64(div): |
1039 | CASE_OP_32_64(divu): | |
1040 | CASE_OP_32_64(rem): | |
1041 | CASE_OP_32_64(remu): | |
53108fb5 KB |
1042 | if (temps[args[1]].state == TCG_TEMP_CONST |
1043 | && temps[args[2]].state == TCG_TEMP_CONST) { | |
c45cb8bb | 1044 | tmp = do_constant_folding(opc, temps[args[1]].val, |
53108fb5 | 1045 | temps[args[2]].val); |
c45cb8bb | 1046 | tcg_opt_gen_movi(s, op, args, opc, args[0], tmp); |
6e14e91b | 1047 | break; |
53108fb5 | 1048 | } |
6e14e91b RH |
1049 | goto do_default; |
1050 | ||
7ef55fc9 AJ |
1051 | CASE_OP_32_64(deposit): |
1052 | if (temps[args[1]].state == TCG_TEMP_CONST | |
1053 | && temps[args[2]].state == TCG_TEMP_CONST) { | |
d998e555 RH |
1054 | tmp = deposit64(temps[args[1]].val, args[3], args[4], |
1055 | temps[args[2]].val); | |
c45cb8bb | 1056 | tcg_opt_gen_movi(s, op, args, opc, args[0], tmp); |
6e14e91b | 1057 | break; |
7ef55fc9 | 1058 | } |
6e14e91b RH |
1059 | goto do_default; |
1060 | ||
f8dd19e5 | 1061 | CASE_OP_32_64(setcond): |
c45cb8bb | 1062 | tmp = do_constant_folding_cond(opc, args[1], args[2], args[3]); |
b336ceb6 | 1063 | if (tmp != 2) { |
c45cb8bb | 1064 | tcg_opt_gen_movi(s, op, args, opc, args[0], tmp); |
6e14e91b | 1065 | break; |
f8dd19e5 | 1066 | } |
6e14e91b RH |
1067 | goto do_default; |
1068 | ||
fbeaa26c | 1069 | CASE_OP_32_64(brcond): |
c45cb8bb | 1070 | tmp = do_constant_folding_cond(opc, args[0], args[1], args[2]); |
b336ceb6 AJ |
1071 | if (tmp != 2) { |
1072 | if (tmp) { | |
d193a14a | 1073 | reset_all_temps(nb_temps); |
c45cb8bb RH |
1074 | op->opc = INDEX_op_br; |
1075 | args[0] = args[3]; | |
fbeaa26c | 1076 | } else { |
c45cb8bb | 1077 | op->opc = INDEX_op_nop; |
fbeaa26c | 1078 | } |
6e14e91b | 1079 | break; |
fbeaa26c | 1080 | } |
6e14e91b RH |
1081 | goto do_default; |
1082 | ||
fa01a208 | 1083 | CASE_OP_32_64(movcond): |
c45cb8bb | 1084 | tmp = do_constant_folding_cond(opc, args[1], args[2], args[5]); |
b336ceb6 | 1085 | if (tmp != 2) { |
e590d4e6 | 1086 | if (temps_are_copies(args[0], args[4-tmp])) { |
c45cb8bb | 1087 | op->opc = INDEX_op_nop; |
fa01a208 | 1088 | } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) { |
c45cb8bb | 1089 | tcg_opt_gen_movi(s, op, args, opc, |
a62f6f56 | 1090 | args[0], temps[args[4-tmp]].val); |
fa01a208 | 1091 | } else { |
c45cb8bb | 1092 | tcg_opt_gen_mov(s, op, args, opc, args[0], args[4-tmp]); |
fa01a208 | 1093 | } |
6e14e91b | 1094 | break; |
fa01a208 | 1095 | } |
6e14e91b | 1096 | goto do_default; |
212c328d RH |
1097 | |
1098 | case INDEX_op_add2_i32: | |
1099 | case INDEX_op_sub2_i32: | |
1100 | if (temps[args[2]].state == TCG_TEMP_CONST | |
1101 | && temps[args[3]].state == TCG_TEMP_CONST | |
1102 | && temps[args[4]].state == TCG_TEMP_CONST | |
1103 | && temps[args[5]].state == TCG_TEMP_CONST) { | |
1104 | uint32_t al = temps[args[2]].val; | |
1105 | uint32_t ah = temps[args[3]].val; | |
1106 | uint32_t bl = temps[args[4]].val; | |
1107 | uint32_t bh = temps[args[5]].val; | |
1108 | uint64_t a = ((uint64_t)ah << 32) | al; | |
1109 | uint64_t b = ((uint64_t)bh << 32) | bl; | |
1110 | TCGArg rl, rh; | |
c45cb8bb RH |
1111 | TCGOp *op2; |
1112 | TCGArg *args2; | |
212c328d | 1113 | |
c45cb8bb | 1114 | if (opc == INDEX_op_add2_i32) { |
212c328d RH |
1115 | a += b; |
1116 | } else { | |
1117 | a -= b; | |
1118 | } | |
1119 | ||
1120 | /* We emit the extra nop when we emit the add2/sub2. */ | |
c45cb8bb RH |
1121 | op2 = &s->gen_op_buf[oi_next]; |
1122 | assert(op2->opc == INDEX_op_nop); | |
1123 | ||
1124 | /* But we still have to allocate args for the op. */ | |
1125 | op2->args = s->gen_next_parm_idx; | |
1126 | s->gen_next_parm_idx += 2; | |
1127 | args2 = &s->gen_opparam_buf[op2->args]; | |
212c328d RH |
1128 | |
1129 | rl = args[0]; | |
1130 | rh = args[1]; | |
c45cb8bb RH |
1131 | tcg_opt_gen_movi(s, op, args, opc, rl, (uint32_t)a); |
1132 | tcg_opt_gen_movi(s, op2, args2, opc, rh, (uint32_t)(a >> 32)); | |
1133 | ||
1134 | /* We've done all we need to do with the movi. Skip it. */ | |
1135 | oi_next = op2->next; | |
212c328d RH |
1136 | break; |
1137 | } | |
1138 | goto do_default; | |
1414968a RH |
1139 | |
1140 | case INDEX_op_mulu2_i32: | |
1141 | if (temps[args[2]].state == TCG_TEMP_CONST | |
1142 | && temps[args[3]].state == TCG_TEMP_CONST) { | |
1143 | uint32_t a = temps[args[2]].val; | |
1144 | uint32_t b = temps[args[3]].val; | |
1145 | uint64_t r = (uint64_t)a * b; | |
1146 | TCGArg rl, rh; | |
c45cb8bb RH |
1147 | TCGOp *op2; |
1148 | TCGArg *args2; | |
1414968a RH |
1149 | |
1150 | /* We emit the extra nop when we emit the mulu2. */ | |
c45cb8bb RH |
1151 | op2 = &s->gen_op_buf[oi_next]; |
1152 | assert(op2->opc == INDEX_op_nop); | |
1153 | ||
1154 | /* But we still have to allocate args for the op. */ | |
1155 | op2->args = s->gen_next_parm_idx; | |
1156 | s->gen_next_parm_idx += 2; | |
1157 | args2 = &s->gen_opparam_buf[op2->args]; | |
1414968a RH |
1158 | |
1159 | rl = args[0]; | |
1160 | rh = args[1]; | |
c45cb8bb RH |
1161 | tcg_opt_gen_movi(s, op, args, opc, rl, (uint32_t)r); |
1162 | tcg_opt_gen_movi(s, op2, args2, opc, rh, (uint32_t)(r >> 32)); | |
1163 | ||
1164 | /* We've done all we need to do with the movi. Skip it. */ | |
1165 | oi_next = op2->next; | |
1414968a RH |
1166 | break; |
1167 | } | |
1168 | goto do_default; | |
6e14e91b | 1169 | |
bc1473ef | 1170 | case INDEX_op_brcond2_i32: |
6c4382f8 RH |
1171 | tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]); |
1172 | if (tmp != 2) { | |
1173 | if (tmp) { | |
a763551a | 1174 | do_brcond_true: |
d193a14a | 1175 | reset_all_temps(nb_temps); |
c45cb8bb RH |
1176 | op->opc = INDEX_op_br; |
1177 | args[0] = args[5]; | |
6c4382f8 | 1178 | } else { |
a763551a | 1179 | do_brcond_false: |
c45cb8bb | 1180 | op->opc = INDEX_op_nop; |
6c4382f8 RH |
1181 | } |
1182 | } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE) | |
1183 | && temps[args[2]].state == TCG_TEMP_CONST | |
1184 | && temps[args[3]].state == TCG_TEMP_CONST | |
1185 | && temps[args[2]].val == 0 | |
1186 | && temps[args[3]].val == 0) { | |
1187 | /* Simplify LT/GE comparisons vs zero to a single compare | |
1188 | vs the high word of the input. */ | |
a763551a | 1189 | do_brcond_high: |
d193a14a | 1190 | reset_all_temps(nb_temps); |
c45cb8bb RH |
1191 | op->opc = INDEX_op_brcond_i32; |
1192 | args[0] = args[1]; | |
1193 | args[1] = args[3]; | |
1194 | args[2] = args[4]; | |
1195 | args[3] = args[5]; | |
a763551a RH |
1196 | } else if (args[4] == TCG_COND_EQ) { |
1197 | /* Simplify EQ comparisons where one of the pairs | |
1198 | can be simplified. */ | |
1199 | tmp = do_constant_folding_cond(INDEX_op_brcond_i32, | |
1200 | args[0], args[2], TCG_COND_EQ); | |
1201 | if (tmp == 0) { | |
1202 | goto do_brcond_false; | |
1203 | } else if (tmp == 1) { | |
1204 | goto do_brcond_high; | |
1205 | } | |
1206 | tmp = do_constant_folding_cond(INDEX_op_brcond_i32, | |
1207 | args[1], args[3], TCG_COND_EQ); | |
1208 | if (tmp == 0) { | |
1209 | goto do_brcond_false; | |
1210 | } else if (tmp != 1) { | |
1211 | goto do_default; | |
1212 | } | |
1213 | do_brcond_low: | |
1214 | reset_all_temps(nb_temps); | |
c45cb8bb RH |
1215 | op->opc = INDEX_op_brcond_i32; |
1216 | args[1] = args[2]; | |
1217 | args[2] = args[4]; | |
1218 | args[3] = args[5]; | |
a763551a RH |
1219 | } else if (args[4] == TCG_COND_NE) { |
1220 | /* Simplify NE comparisons where one of the pairs | |
1221 | can be simplified. */ | |
1222 | tmp = do_constant_folding_cond(INDEX_op_brcond_i32, | |
1223 | args[0], args[2], TCG_COND_NE); | |
1224 | if (tmp == 0) { | |
1225 | goto do_brcond_high; | |
1226 | } else if (tmp == 1) { | |
1227 | goto do_brcond_true; | |
1228 | } | |
1229 | tmp = do_constant_folding_cond(INDEX_op_brcond_i32, | |
1230 | args[1], args[3], TCG_COND_NE); | |
1231 | if (tmp == 0) { | |
1232 | goto do_brcond_low; | |
1233 | } else if (tmp == 1) { | |
1234 | goto do_brcond_true; | |
1235 | } | |
1236 | goto do_default; | |
6c4382f8 RH |
1237 | } else { |
1238 | goto do_default; | |
bc1473ef | 1239 | } |
6c4382f8 | 1240 | break; |
bc1473ef RH |
1241 | |
1242 | case INDEX_op_setcond2_i32: | |
6c4382f8 RH |
1243 | tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]); |
1244 | if (tmp != 2) { | |
a763551a | 1245 | do_setcond_const: |
c45cb8bb | 1246 | tcg_opt_gen_movi(s, op, args, opc, args[0], tmp); |
6c4382f8 RH |
1247 | } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE) |
1248 | && temps[args[3]].state == TCG_TEMP_CONST | |
1249 | && temps[args[4]].state == TCG_TEMP_CONST | |
1250 | && temps[args[3]].val == 0 | |
1251 | && temps[args[4]].val == 0) { | |
1252 | /* Simplify LT/GE comparisons vs zero to a single compare | |
1253 | vs the high word of the input. */ | |
a763551a | 1254 | do_setcond_high: |
66e61b55 | 1255 | reset_temp(args[0]); |
a763551a | 1256 | temps[args[0]].mask = 1; |
c45cb8bb RH |
1257 | op->opc = INDEX_op_setcond_i32; |
1258 | args[1] = args[2]; | |
1259 | args[2] = args[4]; | |
1260 | args[3] = args[5]; | |
a763551a RH |
1261 | } else if (args[5] == TCG_COND_EQ) { |
1262 | /* Simplify EQ comparisons where one of the pairs | |
1263 | can be simplified. */ | |
1264 | tmp = do_constant_folding_cond(INDEX_op_setcond_i32, | |
1265 | args[1], args[3], TCG_COND_EQ); | |
1266 | if (tmp == 0) { | |
1267 | goto do_setcond_const; | |
1268 | } else if (tmp == 1) { | |
1269 | goto do_setcond_high; | |
1270 | } | |
1271 | tmp = do_constant_folding_cond(INDEX_op_setcond_i32, | |
1272 | args[2], args[4], TCG_COND_EQ); | |
1273 | if (tmp == 0) { | |
1274 | goto do_setcond_high; | |
1275 | } else if (tmp != 1) { | |
1276 | goto do_default; | |
1277 | } | |
1278 | do_setcond_low: | |
1279 | reset_temp(args[0]); | |
1280 | temps[args[0]].mask = 1; | |
c45cb8bb RH |
1281 | op->opc = INDEX_op_setcond_i32; |
1282 | args[2] = args[3]; | |
1283 | args[3] = args[5]; | |
a763551a RH |
1284 | } else if (args[5] == TCG_COND_NE) { |
1285 | /* Simplify NE comparisons where one of the pairs | |
1286 | can be simplified. */ | |
1287 | tmp = do_constant_folding_cond(INDEX_op_setcond_i32, | |
1288 | args[1], args[3], TCG_COND_NE); | |
1289 | if (tmp == 0) { | |
1290 | goto do_setcond_high; | |
1291 | } else if (tmp == 1) { | |
1292 | goto do_setcond_const; | |
1293 | } | |
1294 | tmp = do_constant_folding_cond(INDEX_op_setcond_i32, | |
1295 | args[2], args[4], TCG_COND_NE); | |
1296 | if (tmp == 0) { | |
1297 | goto do_setcond_low; | |
1298 | } else if (tmp == 1) { | |
1299 | goto do_setcond_const; | |
1300 | } | |
1301 | goto do_default; | |
6c4382f8 RH |
1302 | } else { |
1303 | goto do_default; | |
bc1473ef | 1304 | } |
6c4382f8 | 1305 | break; |
bc1473ef | 1306 | |
8f2e8c07 | 1307 | case INDEX_op_call: |
cf066674 RH |
1308 | if (!(args[nb_oargs + nb_iargs + 1] |
1309 | & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) { | |
22613af4 | 1310 | for (i = 0; i < nb_globals; i++) { |
e590d4e6 | 1311 | reset_temp(i); |
22613af4 KB |
1312 | } |
1313 | } | |
cf066674 | 1314 | goto do_reset_output; |
6e14e91b | 1315 | |
8f2e8c07 | 1316 | default: |
6e14e91b RH |
1317 | do_default: |
1318 | /* Default case: we know nothing about operation (or were unable | |
1319 | to compute the operation result) so no propagation is done. | |
1320 | We trash everything if the operation is the end of a basic | |
3a9d8b17 PB |
1321 | block, otherwise we only trash the output args. "mask" is |
1322 | the non-zero bits mask for the first output arg. */ | |
a2550660 | 1323 | if (def->flags & TCG_OPF_BB_END) { |
d193a14a | 1324 | reset_all_temps(nb_temps); |
a2550660 | 1325 | } else { |
cf066674 RH |
1326 | do_reset_output: |
1327 | for (i = 0; i < nb_oargs; i++) { | |
e590d4e6 | 1328 | reset_temp(args[i]); |
3031244b AJ |
1329 | /* Save the corresponding known-zero bits mask for the |
1330 | first output argument (only one supported so far). */ | |
1331 | if (i == 0) { | |
1332 | temps[args[i]].mask = mask; | |
1333 | } | |
a2550660 | 1334 | } |
22613af4 | 1335 | } |
8f2e8c07 KB |
1336 | break; |
1337 | } | |
1338 | } | |
8f2e8c07 KB |
1339 | } |
1340 | ||
c45cb8bb | 1341 | void tcg_optimize(TCGContext *s) |
8f2e8c07 | 1342 | { |
c45cb8bb | 1343 | tcg_constant_folding(s); |
8f2e8c07 | 1344 | } |