]> git.proxmox.com Git - qemu.git/blob - tcg/optimize.c
tcg: Add signed multiword multiplication operations
[qemu.git] / tcg / optimize.c
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
34 #define CASE_OP_32_64(x) \
35 glue(glue(case INDEX_op_, x), _i32): \
36 glue(glue(case INDEX_op_, x), _i64)
37
38 typedef enum {
39 TCG_TEMP_UNDEF = 0,
40 TCG_TEMP_CONST,
41 TCG_TEMP_COPY,
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 tcg_target_ulong mask;
50 };
51
52 static struct tcg_temp_info temps[TCG_MAX_TEMPS];
53
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)
57 {
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;
64 }
65 }
66 temps[temp].state = TCG_TEMP_UNDEF;
67 temps[temp].mask = -1;
68 }
69
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;
76 temps[i].mask = -1;
77 }
78 }
79
80 static int op_bits(TCGOpcode op)
81 {
82 const TCGOpDef *def = &tcg_op_defs[op];
83 return def->flags & TCG_OPF_64BIT ? 64 : 32;
84 }
85
86 static TCGOpcode op_to_movi(TCGOpcode op)
87 {
88 switch (op_bits(op)) {
89 case 32:
90 return INDEX_op_movi_i32;
91 case 64:
92 return INDEX_op_movi_i64;
93 default:
94 fprintf(stderr, "op_to_movi: unexpected return value of "
95 "function op_bits.\n");
96 tcg_abort();
97 }
98 }
99
100 static TCGArg find_better_copy(TCGContext *s, TCGArg temp)
101 {
102 TCGArg i;
103
104 /* If this is already a global, we can't do better. */
105 if (temp < s->nb_globals) {
106 return temp;
107 }
108
109 /* Search for a global first. */
110 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
111 if (i < s->nb_globals) {
112 return i;
113 }
114 }
115
116 /* If it is a temp, search for a temp local. */
117 if (!s->temps[temp].temp_local) {
118 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
119 if (s->temps[i].temp_local) {
120 return i;
121 }
122 }
123 }
124
125 /* Failure to find a better representation, return the same temp. */
126 return temp;
127 }
128
129 static bool temps_are_copies(TCGArg arg1, TCGArg arg2)
130 {
131 TCGArg i;
132
133 if (arg1 == arg2) {
134 return true;
135 }
136
137 if (temps[arg1].state != TCG_TEMP_COPY
138 || temps[arg2].state != TCG_TEMP_COPY) {
139 return false;
140 }
141
142 for (i = temps[arg1].next_copy ; i != arg1 ; i = temps[i].next_copy) {
143 if (i == arg2) {
144 return true;
145 }
146 }
147
148 return false;
149 }
150
151 static void tcg_opt_gen_mov(TCGContext *s, TCGArg *gen_args,
152 TCGArg dst, TCGArg src)
153 {
154 reset_temp(dst);
155 temps[dst].mask = temps[src].mask;
156 assert(temps[src].state != TCG_TEMP_CONST);
157
158 if (s->temps[src].type == s->temps[dst].type) {
159 if (temps[src].state != TCG_TEMP_COPY) {
160 temps[src].state = TCG_TEMP_COPY;
161 temps[src].next_copy = src;
162 temps[src].prev_copy = src;
163 }
164 temps[dst].state = TCG_TEMP_COPY;
165 temps[dst].next_copy = temps[src].next_copy;
166 temps[dst].prev_copy = src;
167 temps[temps[dst].next_copy].prev_copy = dst;
168 temps[src].next_copy = dst;
169 }
170
171 gen_args[0] = dst;
172 gen_args[1] = src;
173 }
174
175 static void tcg_opt_gen_movi(TCGArg *gen_args, TCGArg dst, TCGArg val)
176 {
177 reset_temp(dst);
178 temps[dst].state = TCG_TEMP_CONST;
179 temps[dst].val = val;
180 temps[dst].mask = val;
181 gen_args[0] = dst;
182 gen_args[1] = val;
183 }
184
185 static TCGOpcode op_to_mov(TCGOpcode op)
186 {
187 switch (op_bits(op)) {
188 case 32:
189 return INDEX_op_mov_i32;
190 case 64:
191 return INDEX_op_mov_i64;
192 default:
193 fprintf(stderr, "op_to_mov: unexpected return value of "
194 "function op_bits.\n");
195 tcg_abort();
196 }
197 }
198
199 static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)
200 {
201 switch (op) {
202 CASE_OP_32_64(add):
203 return x + y;
204
205 CASE_OP_32_64(sub):
206 return x - y;
207
208 CASE_OP_32_64(mul):
209 return x * y;
210
211 CASE_OP_32_64(and):
212 return x & y;
213
214 CASE_OP_32_64(or):
215 return x | y;
216
217 CASE_OP_32_64(xor):
218 return x ^ y;
219
220 case INDEX_op_shl_i32:
221 return (uint32_t)x << (uint32_t)y;
222
223 case INDEX_op_shl_i64:
224 return (uint64_t)x << (uint64_t)y;
225
226 case INDEX_op_shr_i32:
227 return (uint32_t)x >> (uint32_t)y;
228
229 case INDEX_op_shr_i64:
230 return (uint64_t)x >> (uint64_t)y;
231
232 case INDEX_op_sar_i32:
233 return (int32_t)x >> (int32_t)y;
234
235 case INDEX_op_sar_i64:
236 return (int64_t)x >> (int64_t)y;
237
238 case INDEX_op_rotr_i32:
239 x = ((uint32_t)x << (32 - y)) | ((uint32_t)x >> y);
240 return x;
241
242 case INDEX_op_rotr_i64:
243 x = ((uint64_t)x << (64 - y)) | ((uint64_t)x >> y);
244 return x;
245
246 case INDEX_op_rotl_i32:
247 x = ((uint32_t)x << y) | ((uint32_t)x >> (32 - y));
248 return x;
249
250 case INDEX_op_rotl_i64:
251 x = ((uint64_t)x << y) | ((uint64_t)x >> (64 - y));
252 return x;
253
254 CASE_OP_32_64(not):
255 return ~x;
256
257 CASE_OP_32_64(neg):
258 return -x;
259
260 CASE_OP_32_64(andc):
261 return x & ~y;
262
263 CASE_OP_32_64(orc):
264 return x | ~y;
265
266 CASE_OP_32_64(eqv):
267 return ~(x ^ y);
268
269 CASE_OP_32_64(nand):
270 return ~(x & y);
271
272 CASE_OP_32_64(nor):
273 return ~(x | y);
274
275 CASE_OP_32_64(ext8s):
276 return (int8_t)x;
277
278 CASE_OP_32_64(ext16s):
279 return (int16_t)x;
280
281 CASE_OP_32_64(ext8u):
282 return (uint8_t)x;
283
284 CASE_OP_32_64(ext16u):
285 return (uint16_t)x;
286
287 case INDEX_op_ext32s_i64:
288 return (int32_t)x;
289
290 case INDEX_op_ext32u_i64:
291 return (uint32_t)x;
292
293 default:
294 fprintf(stderr,
295 "Unrecognized operation %d in do_constant_folding.\n", op);
296 tcg_abort();
297 }
298 }
299
300 static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
301 {
302 TCGArg res = do_constant_folding_2(op, x, y);
303 if (op_bits(op) == 32) {
304 res &= 0xffffffff;
305 }
306 return res;
307 }
308
309 static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
310 {
311 switch (c) {
312 case TCG_COND_EQ:
313 return x == y;
314 case TCG_COND_NE:
315 return x != y;
316 case TCG_COND_LT:
317 return (int32_t)x < (int32_t)y;
318 case TCG_COND_GE:
319 return (int32_t)x >= (int32_t)y;
320 case TCG_COND_LE:
321 return (int32_t)x <= (int32_t)y;
322 case TCG_COND_GT:
323 return (int32_t)x > (int32_t)y;
324 case TCG_COND_LTU:
325 return x < y;
326 case TCG_COND_GEU:
327 return x >= y;
328 case TCG_COND_LEU:
329 return x <= y;
330 case TCG_COND_GTU:
331 return x > y;
332 default:
333 tcg_abort();
334 }
335 }
336
337 static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
338 {
339 switch (c) {
340 case TCG_COND_EQ:
341 return x == y;
342 case TCG_COND_NE:
343 return x != y;
344 case TCG_COND_LT:
345 return (int64_t)x < (int64_t)y;
346 case TCG_COND_GE:
347 return (int64_t)x >= (int64_t)y;
348 case TCG_COND_LE:
349 return (int64_t)x <= (int64_t)y;
350 case TCG_COND_GT:
351 return (int64_t)x > (int64_t)y;
352 case TCG_COND_LTU:
353 return x < y;
354 case TCG_COND_GEU:
355 return x >= y;
356 case TCG_COND_LEU:
357 return x <= y;
358 case TCG_COND_GTU:
359 return x > y;
360 default:
361 tcg_abort();
362 }
363 }
364
365 static bool do_constant_folding_cond_eq(TCGCond c)
366 {
367 switch (c) {
368 case TCG_COND_GT:
369 case TCG_COND_LTU:
370 case TCG_COND_LT:
371 case TCG_COND_GTU:
372 case TCG_COND_NE:
373 return 0;
374 case TCG_COND_GE:
375 case TCG_COND_GEU:
376 case TCG_COND_LE:
377 case TCG_COND_LEU:
378 case TCG_COND_EQ:
379 return 1;
380 default:
381 tcg_abort();
382 }
383 }
384
385 /* Return 2 if the condition can't be simplified, and the result
386 of the condition (0 or 1) if it can */
387 static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
388 TCGArg y, TCGCond c)
389 {
390 if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) {
391 switch (op_bits(op)) {
392 case 32:
393 return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
394 case 64:
395 return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
396 default:
397 tcg_abort();
398 }
399 } else if (temps_are_copies(x, y)) {
400 return do_constant_folding_cond_eq(c);
401 } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
402 switch (c) {
403 case TCG_COND_LTU:
404 return 0;
405 case TCG_COND_GEU:
406 return 1;
407 default:
408 return 2;
409 }
410 } else {
411 return 2;
412 }
413 }
414
415 /* Return 2 if the condition can't be simplified, and the result
416 of the condition (0 or 1) if it can */
417 static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
418 {
419 TCGArg al = p1[0], ah = p1[1];
420 TCGArg bl = p2[0], bh = p2[1];
421
422 if (temps[bl].state == TCG_TEMP_CONST
423 && temps[bh].state == TCG_TEMP_CONST) {
424 uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
425
426 if (temps[al].state == TCG_TEMP_CONST
427 && temps[ah].state == TCG_TEMP_CONST) {
428 uint64_t a;
429 a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
430 return do_constant_folding_cond_64(a, b, c);
431 }
432 if (b == 0) {
433 switch (c) {
434 case TCG_COND_LTU:
435 return 0;
436 case TCG_COND_GEU:
437 return 1;
438 default:
439 break;
440 }
441 }
442 }
443 if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
444 return do_constant_folding_cond_eq(c);
445 }
446 return 2;
447 }
448
449 static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
450 {
451 TCGArg a1 = *p1, a2 = *p2;
452 int sum = 0;
453 sum += temps[a1].state == TCG_TEMP_CONST;
454 sum -= temps[a2].state == TCG_TEMP_CONST;
455
456 /* Prefer the constant in second argument, and then the form
457 op a, a, b, which is better handled on non-RISC hosts. */
458 if (sum > 0 || (sum == 0 && dest == a2)) {
459 *p1 = a2;
460 *p2 = a1;
461 return true;
462 }
463 return false;
464 }
465
466 static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
467 {
468 int sum = 0;
469 sum += temps[p1[0]].state == TCG_TEMP_CONST;
470 sum += temps[p1[1]].state == TCG_TEMP_CONST;
471 sum -= temps[p2[0]].state == TCG_TEMP_CONST;
472 sum -= temps[p2[1]].state == TCG_TEMP_CONST;
473 if (sum > 0) {
474 TCGArg t;
475 t = p1[0], p1[0] = p2[0], p2[0] = t;
476 t = p1[1], p1[1] = p2[1], p2[1] = t;
477 return true;
478 }
479 return false;
480 }
481
482 /* Propagate constants and copies, fold constant expressions. */
483 static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
484 TCGArg *args, TCGOpDef *tcg_op_defs)
485 {
486 int i, nb_ops, op_index, nb_temps, nb_globals, nb_call_args;
487 tcg_target_ulong mask, affected;
488 TCGOpcode op;
489 const TCGOpDef *def;
490 TCGArg *gen_args;
491 TCGArg tmp;
492
493 /* Array VALS has an element for each temp.
494 If this temp holds a constant then its value is kept in VALS' element.
495 If this temp is a copy of other ones then the other copies are
496 available through the doubly linked circular list. */
497
498 nb_temps = s->nb_temps;
499 nb_globals = s->nb_globals;
500 reset_all_temps(nb_temps);
501
502 nb_ops = tcg_opc_ptr - s->gen_opc_buf;
503 gen_args = args;
504 for (op_index = 0; op_index < nb_ops; op_index++) {
505 op = s->gen_opc_buf[op_index];
506 def = &tcg_op_defs[op];
507 /* Do copy propagation */
508 if (op == INDEX_op_call) {
509 int nb_oargs = args[0] >> 16;
510 int nb_iargs = args[0] & 0xffff;
511 for (i = nb_oargs + 1; i < nb_oargs + nb_iargs + 1; i++) {
512 if (temps[args[i]].state == TCG_TEMP_COPY) {
513 args[i] = find_better_copy(s, args[i]);
514 }
515 }
516 } else {
517 for (i = def->nb_oargs; i < def->nb_oargs + def->nb_iargs; i++) {
518 if (temps[args[i]].state == TCG_TEMP_COPY) {
519 args[i] = find_better_copy(s, args[i]);
520 }
521 }
522 }
523
524 /* For commutative operations make constant second argument */
525 switch (op) {
526 CASE_OP_32_64(add):
527 CASE_OP_32_64(mul):
528 CASE_OP_32_64(and):
529 CASE_OP_32_64(or):
530 CASE_OP_32_64(xor):
531 CASE_OP_32_64(eqv):
532 CASE_OP_32_64(nand):
533 CASE_OP_32_64(nor):
534 swap_commutative(args[0], &args[1], &args[2]);
535 break;
536 CASE_OP_32_64(brcond):
537 if (swap_commutative(-1, &args[0], &args[1])) {
538 args[2] = tcg_swap_cond(args[2]);
539 }
540 break;
541 CASE_OP_32_64(setcond):
542 if (swap_commutative(args[0], &args[1], &args[2])) {
543 args[3] = tcg_swap_cond(args[3]);
544 }
545 break;
546 CASE_OP_32_64(movcond):
547 if (swap_commutative(-1, &args[1], &args[2])) {
548 args[5] = tcg_swap_cond(args[5]);
549 }
550 /* For movcond, we canonicalize the "false" input reg to match
551 the destination reg so that the tcg backend can implement
552 a "move if true" operation. */
553 if (swap_commutative(args[0], &args[4], &args[3])) {
554 args[5] = tcg_invert_cond(args[5]);
555 }
556 break;
557 CASE_OP_32_64(add2):
558 swap_commutative(args[0], &args[2], &args[4]);
559 swap_commutative(args[1], &args[3], &args[5]);
560 break;
561 CASE_OP_32_64(mulu2):
562 CASE_OP_32_64(muls2):
563 swap_commutative(args[0], &args[2], &args[3]);
564 break;
565 case INDEX_op_brcond2_i32:
566 if (swap_commutative2(&args[0], &args[2])) {
567 args[4] = tcg_swap_cond(args[4]);
568 }
569 break;
570 case INDEX_op_setcond2_i32:
571 if (swap_commutative2(&args[1], &args[3])) {
572 args[5] = tcg_swap_cond(args[5]);
573 }
574 break;
575 default:
576 break;
577 }
578
579 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0" */
580 switch (op) {
581 CASE_OP_32_64(shl):
582 CASE_OP_32_64(shr):
583 CASE_OP_32_64(sar):
584 CASE_OP_32_64(rotl):
585 CASE_OP_32_64(rotr):
586 if (temps[args[1]].state == TCG_TEMP_CONST
587 && temps[args[1]].val == 0) {
588 s->gen_opc_buf[op_index] = op_to_movi(op);
589 tcg_opt_gen_movi(gen_args, args[0], 0);
590 args += 3;
591 gen_args += 2;
592 continue;
593 }
594 break;
595 default:
596 break;
597 }
598
599 /* Simplify expression for "op r, a, 0 => mov r, a" cases */
600 switch (op) {
601 CASE_OP_32_64(add):
602 CASE_OP_32_64(sub):
603 CASE_OP_32_64(shl):
604 CASE_OP_32_64(shr):
605 CASE_OP_32_64(sar):
606 CASE_OP_32_64(rotl):
607 CASE_OP_32_64(rotr):
608 CASE_OP_32_64(or):
609 CASE_OP_32_64(xor):
610 if (temps[args[1]].state == TCG_TEMP_CONST) {
611 /* Proceed with possible constant folding. */
612 break;
613 }
614 if (temps[args[2]].state == TCG_TEMP_CONST
615 && temps[args[2]].val == 0) {
616 if (temps_are_copies(args[0], args[1])) {
617 s->gen_opc_buf[op_index] = INDEX_op_nop;
618 } else {
619 s->gen_opc_buf[op_index] = op_to_mov(op);
620 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
621 gen_args += 2;
622 }
623 args += 3;
624 continue;
625 }
626 break;
627 default:
628 break;
629 }
630
631 /* Simplify using known-zero bits */
632 mask = -1;
633 affected = -1;
634 switch (op) {
635 CASE_OP_32_64(ext8s):
636 if ((temps[args[1]].mask & 0x80) != 0) {
637 break;
638 }
639 CASE_OP_32_64(ext8u):
640 mask = 0xff;
641 goto and_const;
642 CASE_OP_32_64(ext16s):
643 if ((temps[args[1]].mask & 0x8000) != 0) {
644 break;
645 }
646 CASE_OP_32_64(ext16u):
647 mask = 0xffff;
648 goto and_const;
649 case INDEX_op_ext32s_i64:
650 if ((temps[args[1]].mask & 0x80000000) != 0) {
651 break;
652 }
653 case INDEX_op_ext32u_i64:
654 mask = 0xffffffffU;
655 goto and_const;
656
657 CASE_OP_32_64(and):
658 mask = temps[args[2]].mask;
659 if (temps[args[2]].state == TCG_TEMP_CONST) {
660 and_const:
661 affected = temps[args[1]].mask & ~mask;
662 }
663 mask = temps[args[1]].mask & mask;
664 break;
665
666 CASE_OP_32_64(sar):
667 if (temps[args[2]].state == TCG_TEMP_CONST) {
668 mask = ((tcg_target_long)temps[args[1]].mask
669 >> temps[args[2]].val);
670 }
671 break;
672
673 CASE_OP_32_64(shr):
674 if (temps[args[2]].state == TCG_TEMP_CONST) {
675 mask = temps[args[1]].mask >> temps[args[2]].val;
676 }
677 break;
678
679 CASE_OP_32_64(shl):
680 if (temps[args[2]].state == TCG_TEMP_CONST) {
681 mask = temps[args[1]].mask << temps[args[2]].val;
682 }
683 break;
684
685 CASE_OP_32_64(neg):
686 /* Set to 1 all bits to the left of the rightmost. */
687 mask = -(temps[args[1]].mask & -temps[args[1]].mask);
688 break;
689
690 CASE_OP_32_64(deposit):
691 tmp = ((1ull << args[4]) - 1);
692 mask = ((temps[args[1]].mask & ~(tmp << args[3]))
693 | ((temps[args[2]].mask & tmp) << args[3]));
694 break;
695
696 CASE_OP_32_64(or):
697 CASE_OP_32_64(xor):
698 mask = temps[args[1]].mask | temps[args[2]].mask;
699 break;
700
701 CASE_OP_32_64(setcond):
702 mask = 1;
703 break;
704
705 CASE_OP_32_64(movcond):
706 mask = temps[args[3]].mask | temps[args[4]].mask;
707 break;
708
709 default:
710 break;
711 }
712
713 if (mask == 0) {
714 assert(def->nb_oargs == 1);
715 s->gen_opc_buf[op_index] = op_to_movi(op);
716 tcg_opt_gen_movi(gen_args, args[0], 0);
717 args += def->nb_oargs + def->nb_iargs + def->nb_cargs;
718 gen_args += 2;
719 continue;
720 }
721 if (affected == 0) {
722 assert(def->nb_oargs == 1);
723 if (temps_are_copies(args[0], args[1])) {
724 s->gen_opc_buf[op_index] = INDEX_op_nop;
725 } else if (temps[args[1]].state != TCG_TEMP_CONST) {
726 s->gen_opc_buf[op_index] = op_to_mov(op);
727 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
728 gen_args += 2;
729 } else {
730 s->gen_opc_buf[op_index] = op_to_movi(op);
731 tcg_opt_gen_movi(gen_args, args[0], temps[args[1]].val);
732 gen_args += 2;
733 }
734 args += def->nb_iargs + 1;
735 continue;
736 }
737
738 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
739 switch (op) {
740 CASE_OP_32_64(and):
741 CASE_OP_32_64(mul):
742 if ((temps[args[2]].state == TCG_TEMP_CONST
743 && temps[args[2]].val == 0)) {
744 s->gen_opc_buf[op_index] = op_to_movi(op);
745 tcg_opt_gen_movi(gen_args, args[0], 0);
746 args += 3;
747 gen_args += 2;
748 continue;
749 }
750 break;
751 default:
752 break;
753 }
754
755 /* Simplify expression for "op r, a, a => mov r, a" cases */
756 switch (op) {
757 CASE_OP_32_64(or):
758 CASE_OP_32_64(and):
759 if (temps_are_copies(args[1], args[2])) {
760 if (temps_are_copies(args[0], args[1])) {
761 s->gen_opc_buf[op_index] = INDEX_op_nop;
762 } else {
763 s->gen_opc_buf[op_index] = op_to_mov(op);
764 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
765 gen_args += 2;
766 }
767 args += 3;
768 continue;
769 }
770 break;
771 default:
772 break;
773 }
774
775 /* Simplify expression for "op r, a, a => movi r, 0" cases */
776 switch (op) {
777 CASE_OP_32_64(sub):
778 CASE_OP_32_64(xor):
779 if (temps_are_copies(args[1], args[2])) {
780 s->gen_opc_buf[op_index] = op_to_movi(op);
781 tcg_opt_gen_movi(gen_args, args[0], 0);
782 gen_args += 2;
783 args += 3;
784 continue;
785 }
786 break;
787 default:
788 break;
789 }
790
791 /* Propagate constants through copy operations and do constant
792 folding. Constants will be substituted to arguments by register
793 allocator where needed and possible. Also detect copies. */
794 switch (op) {
795 CASE_OP_32_64(mov):
796 if (temps_are_copies(args[0], args[1])) {
797 args += 2;
798 s->gen_opc_buf[op_index] = INDEX_op_nop;
799 break;
800 }
801 if (temps[args[1]].state != TCG_TEMP_CONST) {
802 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
803 gen_args += 2;
804 args += 2;
805 break;
806 }
807 /* Source argument is constant. Rewrite the operation and
808 let movi case handle it. */
809 op = op_to_movi(op);
810 s->gen_opc_buf[op_index] = op;
811 args[1] = temps[args[1]].val;
812 /* fallthrough */
813 CASE_OP_32_64(movi):
814 tcg_opt_gen_movi(gen_args, args[0], args[1]);
815 gen_args += 2;
816 args += 2;
817 break;
818
819 CASE_OP_32_64(not):
820 CASE_OP_32_64(neg):
821 CASE_OP_32_64(ext8s):
822 CASE_OP_32_64(ext8u):
823 CASE_OP_32_64(ext16s):
824 CASE_OP_32_64(ext16u):
825 case INDEX_op_ext32s_i64:
826 case INDEX_op_ext32u_i64:
827 if (temps[args[1]].state == TCG_TEMP_CONST) {
828 s->gen_opc_buf[op_index] = op_to_movi(op);
829 tmp = do_constant_folding(op, temps[args[1]].val, 0);
830 tcg_opt_gen_movi(gen_args, args[0], tmp);
831 gen_args += 2;
832 args += 2;
833 break;
834 }
835 goto do_default;
836
837 CASE_OP_32_64(add):
838 CASE_OP_32_64(sub):
839 CASE_OP_32_64(mul):
840 CASE_OP_32_64(or):
841 CASE_OP_32_64(and):
842 CASE_OP_32_64(xor):
843 CASE_OP_32_64(shl):
844 CASE_OP_32_64(shr):
845 CASE_OP_32_64(sar):
846 CASE_OP_32_64(rotl):
847 CASE_OP_32_64(rotr):
848 CASE_OP_32_64(andc):
849 CASE_OP_32_64(orc):
850 CASE_OP_32_64(eqv):
851 CASE_OP_32_64(nand):
852 CASE_OP_32_64(nor):
853 if (temps[args[1]].state == TCG_TEMP_CONST
854 && temps[args[2]].state == TCG_TEMP_CONST) {
855 s->gen_opc_buf[op_index] = op_to_movi(op);
856 tmp = do_constant_folding(op, temps[args[1]].val,
857 temps[args[2]].val);
858 tcg_opt_gen_movi(gen_args, args[0], tmp);
859 gen_args += 2;
860 args += 3;
861 break;
862 }
863 goto do_default;
864
865 CASE_OP_32_64(deposit):
866 if (temps[args[1]].state == TCG_TEMP_CONST
867 && temps[args[2]].state == TCG_TEMP_CONST) {
868 s->gen_opc_buf[op_index] = op_to_movi(op);
869 tmp = ((1ull << args[4]) - 1);
870 tmp = (temps[args[1]].val & ~(tmp << args[3]))
871 | ((temps[args[2]].val & tmp) << args[3]);
872 tcg_opt_gen_movi(gen_args, args[0], tmp);
873 gen_args += 2;
874 args += 5;
875 break;
876 }
877 goto do_default;
878
879 CASE_OP_32_64(setcond):
880 tmp = do_constant_folding_cond(op, args[1], args[2], args[3]);
881 if (tmp != 2) {
882 s->gen_opc_buf[op_index] = op_to_movi(op);
883 tcg_opt_gen_movi(gen_args, args[0], tmp);
884 gen_args += 2;
885 args += 4;
886 break;
887 }
888 goto do_default;
889
890 CASE_OP_32_64(brcond):
891 tmp = do_constant_folding_cond(op, args[0], args[1], args[2]);
892 if (tmp != 2) {
893 if (tmp) {
894 reset_all_temps(nb_temps);
895 s->gen_opc_buf[op_index] = INDEX_op_br;
896 gen_args[0] = args[3];
897 gen_args += 1;
898 } else {
899 s->gen_opc_buf[op_index] = INDEX_op_nop;
900 }
901 args += 4;
902 break;
903 }
904 goto do_default;
905
906 CASE_OP_32_64(movcond):
907 tmp = do_constant_folding_cond(op, args[1], args[2], args[5]);
908 if (tmp != 2) {
909 if (temps_are_copies(args[0], args[4-tmp])) {
910 s->gen_opc_buf[op_index] = INDEX_op_nop;
911 } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) {
912 s->gen_opc_buf[op_index] = op_to_movi(op);
913 tcg_opt_gen_movi(gen_args, args[0], temps[args[4-tmp]].val);
914 gen_args += 2;
915 } else {
916 s->gen_opc_buf[op_index] = op_to_mov(op);
917 tcg_opt_gen_mov(s, gen_args, args[0], args[4-tmp]);
918 gen_args += 2;
919 }
920 args += 6;
921 break;
922 }
923 goto do_default;
924
925 case INDEX_op_add2_i32:
926 case INDEX_op_sub2_i32:
927 if (temps[args[2]].state == TCG_TEMP_CONST
928 && temps[args[3]].state == TCG_TEMP_CONST
929 && temps[args[4]].state == TCG_TEMP_CONST
930 && temps[args[5]].state == TCG_TEMP_CONST) {
931 uint32_t al = temps[args[2]].val;
932 uint32_t ah = temps[args[3]].val;
933 uint32_t bl = temps[args[4]].val;
934 uint32_t bh = temps[args[5]].val;
935 uint64_t a = ((uint64_t)ah << 32) | al;
936 uint64_t b = ((uint64_t)bh << 32) | bl;
937 TCGArg rl, rh;
938
939 if (op == INDEX_op_add2_i32) {
940 a += b;
941 } else {
942 a -= b;
943 }
944
945 /* We emit the extra nop when we emit the add2/sub2. */
946 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
947
948 rl = args[0];
949 rh = args[1];
950 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
951 s->gen_opc_buf[++op_index] = INDEX_op_movi_i32;
952 tcg_opt_gen_movi(&gen_args[0], rl, (uint32_t)a);
953 tcg_opt_gen_movi(&gen_args[2], rh, (uint32_t)(a >> 32));
954 gen_args += 4;
955 args += 6;
956 break;
957 }
958 goto do_default;
959
960 case INDEX_op_mulu2_i32:
961 if (temps[args[2]].state == TCG_TEMP_CONST
962 && temps[args[3]].state == TCG_TEMP_CONST) {
963 uint32_t a = temps[args[2]].val;
964 uint32_t b = temps[args[3]].val;
965 uint64_t r = (uint64_t)a * b;
966 TCGArg rl, rh;
967
968 /* We emit the extra nop when we emit the mulu2. */
969 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
970
971 rl = args[0];
972 rh = args[1];
973 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
974 s->gen_opc_buf[++op_index] = INDEX_op_movi_i32;
975 tcg_opt_gen_movi(&gen_args[0], rl, (uint32_t)r);
976 tcg_opt_gen_movi(&gen_args[2], rh, (uint32_t)(r >> 32));
977 gen_args += 4;
978 args += 4;
979 break;
980 }
981 goto do_default;
982
983 case INDEX_op_brcond2_i32:
984 tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
985 if (tmp != 2) {
986 if (tmp) {
987 reset_all_temps(nb_temps);
988 s->gen_opc_buf[op_index] = INDEX_op_br;
989 gen_args[0] = args[5];
990 gen_args += 1;
991 } else {
992 s->gen_opc_buf[op_index] = INDEX_op_nop;
993 }
994 } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
995 && temps[args[2]].state == TCG_TEMP_CONST
996 && temps[args[3]].state == TCG_TEMP_CONST
997 && temps[args[2]].val == 0
998 && temps[args[3]].val == 0) {
999 /* Simplify LT/GE comparisons vs zero to a single compare
1000 vs the high word of the input. */
1001 reset_all_temps(nb_temps);
1002 s->gen_opc_buf[op_index] = INDEX_op_brcond_i32;
1003 gen_args[0] = args[1];
1004 gen_args[1] = args[3];
1005 gen_args[2] = args[4];
1006 gen_args[3] = args[5];
1007 gen_args += 4;
1008 } else {
1009 goto do_default;
1010 }
1011 args += 6;
1012 break;
1013
1014 case INDEX_op_setcond2_i32:
1015 tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
1016 if (tmp != 2) {
1017 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
1018 tcg_opt_gen_movi(gen_args, args[0], tmp);
1019 gen_args += 2;
1020 } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
1021 && temps[args[3]].state == TCG_TEMP_CONST
1022 && temps[args[4]].state == TCG_TEMP_CONST
1023 && temps[args[3]].val == 0
1024 && temps[args[4]].val == 0) {
1025 /* Simplify LT/GE comparisons vs zero to a single compare
1026 vs the high word of the input. */
1027 s->gen_opc_buf[op_index] = INDEX_op_setcond_i32;
1028 gen_args[0] = args[0];
1029 gen_args[1] = args[2];
1030 gen_args[2] = args[4];
1031 gen_args[3] = args[5];
1032 gen_args += 4;
1033 } else {
1034 goto do_default;
1035 }
1036 args += 6;
1037 break;
1038
1039 case INDEX_op_call:
1040 nb_call_args = (args[0] >> 16) + (args[0] & 0xffff);
1041 if (!(args[nb_call_args + 1] & (TCG_CALL_NO_READ_GLOBALS |
1042 TCG_CALL_NO_WRITE_GLOBALS))) {
1043 for (i = 0; i < nb_globals; i++) {
1044 reset_temp(i);
1045 }
1046 }
1047 for (i = 0; i < (args[0] >> 16); i++) {
1048 reset_temp(args[i + 1]);
1049 }
1050 i = nb_call_args + 3;
1051 while (i) {
1052 *gen_args = *args;
1053 args++;
1054 gen_args++;
1055 i--;
1056 }
1057 break;
1058
1059 default:
1060 do_default:
1061 /* Default case: we know nothing about operation (or were unable
1062 to compute the operation result) so no propagation is done.
1063 We trash everything if the operation is the end of a basic
1064 block, otherwise we only trash the output args. "mask" is
1065 the non-zero bits mask for the first output arg. */
1066 if (def->flags & TCG_OPF_BB_END) {
1067 reset_all_temps(nb_temps);
1068 } else {
1069 for (i = 0; i < def->nb_oargs; i++) {
1070 reset_temp(args[i]);
1071 }
1072 }
1073 for (i = 0; i < def->nb_args; i++) {
1074 gen_args[i] = args[i];
1075 }
1076 args += def->nb_args;
1077 gen_args += def->nb_args;
1078 break;
1079 }
1080 }
1081
1082 return gen_args;
1083 }
1084
1085 TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr,
1086 TCGArg *args, TCGOpDef *tcg_op_defs)
1087 {
1088 TCGArg *res;
1089 res = tcg_constant_folding(s, tcg_opc_ptr, args, tcg_op_defs);
1090 return res;
1091 }