m25p80: Initial implementation of SPI flash device
[qemu-kvm.git] / tcg / optimize.c
blobedb2b0ea90fb68d42bfbe8b5138311bf291a7bd8
1 /*
2 * Optimizations for Tiny Code Generator for QEMU
4 * Copyright (c) 2010 Samsung Electronics.
5 * Contributed by Kirill Batuzov <batuzovk@ispras.ru>
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
26 #include "config.h"
28 #include <stdlib.h>
29 #include <stdio.h>
31 #include "qemu-common.h"
32 #include "tcg-op.h"
34 #define CASE_OP_32_64(x) \
35 glue(glue(case INDEX_op_, x), _i32): \
36 glue(glue(case INDEX_op_, x), _i64)
38 typedef enum {
39 TCG_TEMP_UNDEF = 0,
40 TCG_TEMP_CONST,
41 TCG_TEMP_COPY,
42 } tcg_temp_state;
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;
51 static struct tcg_temp_info temps[TCG_MAX_TEMPS];
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)
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;
65 temps[temp].state = TCG_TEMP_UNDEF;
68 static int op_bits(TCGOpcode op)
70 const TCGOpDef *def = &tcg_op_defs[op];
71 return def->flags & TCG_OPF_64BIT ? 64 : 32;
74 static TCGOpcode op_to_movi(TCGOpcode op)
76 switch (op_bits(op)) {
77 case 32:
78 return INDEX_op_movi_i32;
79 case 64:
80 return INDEX_op_movi_i64;
81 default:
82 fprintf(stderr, "op_to_movi: unexpected return value of "
83 "function op_bits.\n");
84 tcg_abort();
88 static TCGArg find_better_copy(TCGContext *s, TCGArg temp)
90 TCGArg i;
92 /* If this is already a global, we can't do better. */
93 if (temp < s->nb_globals) {
94 return temp;
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;
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;
113 /* Failure to find a better representation, return the same temp. */
114 return temp;
117 static bool temps_are_copies(TCGArg arg1, TCGArg arg2)
119 TCGArg i;
121 if (arg1 == arg2) {
122 return true;
125 if (temps[arg1].state != TCG_TEMP_COPY
126 || temps[arg2].state != TCG_TEMP_COPY) {
127 return false;
130 for (i = temps[arg1].next_copy ; i != arg1 ; i = temps[i].next_copy) {
131 if (i == arg2) {
132 return true;
136 return false;
139 static void tcg_opt_gen_mov(TCGContext *s, TCGArg *gen_args,
140 TCGArg dst, TCGArg src)
142 reset_temp(dst);
143 assert(temps[src].state != TCG_TEMP_CONST);
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;
148 temps[src].next_copy = src;
149 temps[src].prev_copy = src;
151 temps[dst].state = TCG_TEMP_COPY;
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;
158 gen_args[0] = dst;
159 gen_args[1] = src;
162 static void tcg_opt_gen_movi(TCGArg *gen_args, TCGArg dst, TCGArg val)
164 reset_temp(dst);
165 temps[dst].state = TCG_TEMP_CONST;
166 temps[dst].val = val;
167 gen_args[0] = dst;
168 gen_args[1] = val;
171 static TCGOpcode op_to_mov(TCGOpcode op)
173 switch (op_bits(op)) {
174 case 32:
175 return INDEX_op_mov_i32;
176 case 64:
177 return INDEX_op_mov_i64;
178 default:
179 fprintf(stderr, "op_to_mov: unexpected return value of "
180 "function op_bits.\n");
181 tcg_abort();
185 static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)
187 switch (op) {
188 CASE_OP_32_64(add):
189 return x + y;
191 CASE_OP_32_64(sub):
192 return x - y;
194 CASE_OP_32_64(mul):
195 return x * y;
197 CASE_OP_32_64(and):
198 return x & y;
200 CASE_OP_32_64(or):
201 return x | y;
203 CASE_OP_32_64(xor):
204 return x ^ y;
206 case INDEX_op_shl_i32:
207 return (uint32_t)x << (uint32_t)y;
209 case INDEX_op_shl_i64:
210 return (uint64_t)x << (uint64_t)y;
212 case INDEX_op_shr_i32:
213 return (uint32_t)x >> (uint32_t)y;
215 case INDEX_op_shr_i64:
216 return (uint64_t)x >> (uint64_t)y;
218 case INDEX_op_sar_i32:
219 return (int32_t)x >> (int32_t)y;
221 case INDEX_op_sar_i64:
222 return (int64_t)x >> (int64_t)y;
224 case INDEX_op_rotr_i32:
225 x = ((uint32_t)x << (32 - y)) | ((uint32_t)x >> y);
226 return x;
228 case INDEX_op_rotr_i64:
229 x = ((uint64_t)x << (64 - y)) | ((uint64_t)x >> y);
230 return x;
232 case INDEX_op_rotl_i32:
233 x = ((uint32_t)x << y) | ((uint32_t)x >> (32 - y));
234 return x;
236 case INDEX_op_rotl_i64:
237 x = ((uint64_t)x << y) | ((uint64_t)x >> (64 - y));
238 return x;
240 CASE_OP_32_64(not):
241 return ~x;
243 CASE_OP_32_64(neg):
244 return -x;
246 CASE_OP_32_64(andc):
247 return x & ~y;
249 CASE_OP_32_64(orc):
250 return x | ~y;
252 CASE_OP_32_64(eqv):
253 return ~(x ^ y);
255 CASE_OP_32_64(nand):
256 return ~(x & y);
258 CASE_OP_32_64(nor):
259 return ~(x | y);
261 CASE_OP_32_64(ext8s):
262 return (int8_t)x;
264 CASE_OP_32_64(ext16s):
265 return (int16_t)x;
267 CASE_OP_32_64(ext8u):
268 return (uint8_t)x;
270 CASE_OP_32_64(ext16u):
271 return (uint16_t)x;
273 case INDEX_op_ext32s_i64:
274 return (int32_t)x;
276 case INDEX_op_ext32u_i64:
277 return (uint32_t)x;
279 default:
280 fprintf(stderr,
281 "Unrecognized operation %d in do_constant_folding.\n", op);
282 tcg_abort();
286 static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
288 TCGArg res = do_constant_folding_2(op, x, y);
289 if (op_bits(op) == 32) {
290 res &= 0xffffffff;
292 return res;
295 /* Return 2 if the condition can't be simplified, and the result
296 of the condition (0 or 1) if it can */
297 static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
298 TCGArg y, TCGCond c)
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;
324 default:
325 break;
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;
350 default:
351 break;
353 break;
355 } else if (temps_are_copies(x, y)) {
356 switch (c) {
357 case TCG_COND_GT:
358 case TCG_COND_LTU:
359 case TCG_COND_LT:
360 case TCG_COND_GTU:
361 case TCG_COND_NE:
362 return 0;
363 case TCG_COND_GE:
364 case TCG_COND_GEU:
365 case TCG_COND_LE:
366 case TCG_COND_LEU:
367 case TCG_COND_EQ:
368 return 1;
369 default:
370 break;
372 } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
373 switch (c) {
374 case TCG_COND_LTU:
375 return 0;
376 case TCG_COND_GEU:
377 return 1;
378 default:
379 return 2;
381 } else {
382 return 2;
385 fprintf(stderr,
386 "Unrecognized bitness %d or condition %d in "
387 "do_constant_folding_cond.\n", op_bits(op), c);
388 tcg_abort();
391 /* Propagate constants and copies, fold constant expressions. */
392 static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
393 TCGArg *args, TCGOpDef *tcg_op_defs)
395 int i, nb_ops, op_index, nb_temps, nb_globals, nb_call_args;
396 TCGOpcode op;
397 const TCGOpDef *def;
398 TCGArg *gen_args;
399 TCGArg tmp;
400 TCGCond cond;
402 /* Array VALS has an element for each temp.
403 If this temp holds a constant then its value is kept in VALS' element.
404 If this temp is a copy of other ones then the other copies are
405 available through the doubly linked circular list. */
407 nb_temps = s->nb_temps;
408 nb_globals = s->nb_globals;
409 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
411 nb_ops = tcg_opc_ptr - gen_opc_buf;
412 gen_args = args;
413 for (op_index = 0; op_index < nb_ops; op_index++) {
414 op = gen_opc_buf[op_index];
415 def = &tcg_op_defs[op];
416 /* Do copy propagation */
417 if (op == INDEX_op_call) {
418 int nb_oargs = args[0] >> 16;
419 int nb_iargs = args[0] & 0xffff;
420 for (i = nb_oargs + 1; i < nb_oargs + nb_iargs + 1; i++) {
421 if (temps[args[i]].state == TCG_TEMP_COPY) {
422 args[i] = find_better_copy(s, args[i]);
425 } else {
426 for (i = def->nb_oargs; i < def->nb_oargs + def->nb_iargs; i++) {
427 if (temps[args[i]].state == TCG_TEMP_COPY) {
428 args[i] = find_better_copy(s, args[i]);
433 /* For commutative operations make constant second argument */
434 switch (op) {
435 CASE_OP_32_64(add):
436 CASE_OP_32_64(mul):
437 CASE_OP_32_64(and):
438 CASE_OP_32_64(or):
439 CASE_OP_32_64(xor):
440 CASE_OP_32_64(eqv):
441 CASE_OP_32_64(nand):
442 CASE_OP_32_64(nor):
443 /* Prefer the constant in second argument, and then the form
444 op a, a, b, which is better handled on non-RISC hosts. */
445 if (temps[args[1]].state == TCG_TEMP_CONST || (args[0] == args[2]
446 && temps[args[2]].state != TCG_TEMP_CONST)) {
447 tmp = args[1];
448 args[1] = args[2];
449 args[2] = tmp;
451 break;
452 CASE_OP_32_64(brcond):
453 if (temps[args[0]].state == TCG_TEMP_CONST
454 && temps[args[1]].state != TCG_TEMP_CONST) {
455 tmp = args[0];
456 args[0] = args[1];
457 args[1] = tmp;
458 args[2] = tcg_swap_cond(args[2]);
460 break;
461 CASE_OP_32_64(setcond):
462 if (temps[args[1]].state == TCG_TEMP_CONST
463 && temps[args[2]].state != TCG_TEMP_CONST) {
464 tmp = args[1];
465 args[1] = args[2];
466 args[2] = tmp;
467 args[3] = tcg_swap_cond(args[3]);
469 break;
470 CASE_OP_32_64(movcond):
471 cond = args[5];
472 if (temps[args[1]].state == TCG_TEMP_CONST
473 && temps[args[2]].state != TCG_TEMP_CONST) {
474 tmp = args[1];
475 args[1] = args[2];
476 args[2] = tmp;
477 cond = tcg_swap_cond(cond);
479 /* For movcond, we canonicalize the "false" input reg to match
480 the destination reg so that the tcg backend can implement
481 a "move if true" operation. */
482 if (args[0] == args[3]) {
483 tmp = args[3];
484 args[3] = args[4];
485 args[4] = tmp;
486 cond = tcg_invert_cond(cond);
488 args[5] = cond;
489 default:
490 break;
493 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0" */
494 switch (op) {
495 CASE_OP_32_64(shl):
496 CASE_OP_32_64(shr):
497 CASE_OP_32_64(sar):
498 CASE_OP_32_64(rotl):
499 CASE_OP_32_64(rotr):
500 if (temps[args[1]].state == TCG_TEMP_CONST
501 && temps[args[1]].val == 0) {
502 gen_opc_buf[op_index] = op_to_movi(op);
503 tcg_opt_gen_movi(gen_args, args[0], 0);
504 args += 3;
505 gen_args += 2;
506 continue;
508 break;
509 default:
510 break;
513 /* Simplify expression for "op r, a, 0 => mov r, a" cases */
514 switch (op) {
515 CASE_OP_32_64(add):
516 CASE_OP_32_64(sub):
517 CASE_OP_32_64(shl):
518 CASE_OP_32_64(shr):
519 CASE_OP_32_64(sar):
520 CASE_OP_32_64(rotl):
521 CASE_OP_32_64(rotr):
522 CASE_OP_32_64(or):
523 CASE_OP_32_64(xor):
524 if (temps[args[1]].state == TCG_TEMP_CONST) {
525 /* Proceed with possible constant folding. */
526 break;
528 if (temps[args[2]].state == TCG_TEMP_CONST
529 && temps[args[2]].val == 0) {
530 if (temps_are_copies(args[0], args[1])) {
531 gen_opc_buf[op_index] = INDEX_op_nop;
532 } else {
533 gen_opc_buf[op_index] = op_to_mov(op);
534 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
535 gen_args += 2;
537 args += 3;
538 continue;
540 break;
541 default:
542 break;
545 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
546 switch (op) {
547 CASE_OP_32_64(and):
548 CASE_OP_32_64(mul):
549 if ((temps[args[2]].state == TCG_TEMP_CONST
550 && temps[args[2]].val == 0)) {
551 gen_opc_buf[op_index] = op_to_movi(op);
552 tcg_opt_gen_movi(gen_args, args[0], 0);
553 args += 3;
554 gen_args += 2;
555 continue;
557 break;
558 default:
559 break;
562 /* Simplify expression for "op r, a, a => mov r, a" cases */
563 switch (op) {
564 CASE_OP_32_64(or):
565 CASE_OP_32_64(and):
566 if (temps_are_copies(args[1], args[2])) {
567 if (temps_are_copies(args[0], args[1])) {
568 gen_opc_buf[op_index] = INDEX_op_nop;
569 } else {
570 gen_opc_buf[op_index] = op_to_mov(op);
571 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
572 gen_args += 2;
574 args += 3;
575 continue;
577 break;
578 default:
579 break;
582 /* Simplify expression for "op r, a, a => movi r, 0" cases */
583 switch (op) {
584 CASE_OP_32_64(sub):
585 CASE_OP_32_64(xor):
586 if (temps_are_copies(args[1], args[2])) {
587 gen_opc_buf[op_index] = op_to_movi(op);
588 tcg_opt_gen_movi(gen_args, args[0], 0);
589 gen_args += 2;
590 args += 3;
591 continue;
593 break;
594 default:
595 break;
598 /* Propagate constants through copy operations and do constant
599 folding. Constants will be substituted to arguments by register
600 allocator where needed and possible. Also detect copies. */
601 switch (op) {
602 CASE_OP_32_64(mov):
603 if (temps_are_copies(args[0], args[1])) {
604 args += 2;
605 gen_opc_buf[op_index] = INDEX_op_nop;
606 break;
608 if (temps[args[1]].state != TCG_TEMP_CONST) {
609 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
610 gen_args += 2;
611 args += 2;
612 break;
614 /* Source argument is constant. Rewrite the operation and
615 let movi case handle it. */
616 op = op_to_movi(op);
617 gen_opc_buf[op_index] = op;
618 args[1] = temps[args[1]].val;
619 /* fallthrough */
620 CASE_OP_32_64(movi):
621 tcg_opt_gen_movi(gen_args, args[0], args[1]);
622 gen_args += 2;
623 args += 2;
624 break;
625 CASE_OP_32_64(not):
626 CASE_OP_32_64(neg):
627 CASE_OP_32_64(ext8s):
628 CASE_OP_32_64(ext8u):
629 CASE_OP_32_64(ext16s):
630 CASE_OP_32_64(ext16u):
631 case INDEX_op_ext32s_i64:
632 case INDEX_op_ext32u_i64:
633 if (temps[args[1]].state == TCG_TEMP_CONST) {
634 gen_opc_buf[op_index] = op_to_movi(op);
635 tmp = do_constant_folding(op, temps[args[1]].val, 0);
636 tcg_opt_gen_movi(gen_args, args[0], tmp);
637 } else {
638 reset_temp(args[0]);
639 gen_args[0] = args[0];
640 gen_args[1] = args[1];
642 gen_args += 2;
643 args += 2;
644 break;
645 CASE_OP_32_64(add):
646 CASE_OP_32_64(sub):
647 CASE_OP_32_64(mul):
648 CASE_OP_32_64(or):
649 CASE_OP_32_64(and):
650 CASE_OP_32_64(xor):
651 CASE_OP_32_64(shl):
652 CASE_OP_32_64(shr):
653 CASE_OP_32_64(sar):
654 CASE_OP_32_64(rotl):
655 CASE_OP_32_64(rotr):
656 CASE_OP_32_64(andc):
657 CASE_OP_32_64(orc):
658 CASE_OP_32_64(eqv):
659 CASE_OP_32_64(nand):
660 CASE_OP_32_64(nor):
661 if (temps[args[1]].state == TCG_TEMP_CONST
662 && temps[args[2]].state == TCG_TEMP_CONST) {
663 gen_opc_buf[op_index] = op_to_movi(op);
664 tmp = do_constant_folding(op, temps[args[1]].val,
665 temps[args[2]].val);
666 tcg_opt_gen_movi(gen_args, args[0], tmp);
667 gen_args += 2;
668 } else {
669 reset_temp(args[0]);
670 gen_args[0] = args[0];
671 gen_args[1] = args[1];
672 gen_args[2] = args[2];
673 gen_args += 3;
675 args += 3;
676 break;
677 CASE_OP_32_64(deposit):
678 if (temps[args[1]].state == TCG_TEMP_CONST
679 && temps[args[2]].state == TCG_TEMP_CONST) {
680 gen_opc_buf[op_index] = op_to_movi(op);
681 tmp = ((1ull << args[4]) - 1);
682 tmp = (temps[args[1]].val & ~(tmp << args[3]))
683 | ((temps[args[2]].val & tmp) << args[3]);
684 tcg_opt_gen_movi(gen_args, args[0], tmp);
685 gen_args += 2;
686 } else {
687 reset_temp(args[0]);
688 gen_args[0] = args[0];
689 gen_args[1] = args[1];
690 gen_args[2] = args[2];
691 gen_args[3] = args[3];
692 gen_args[4] = args[4];
693 gen_args += 5;
695 args += 5;
696 break;
697 CASE_OP_32_64(setcond):
698 tmp = do_constant_folding_cond(op, args[1], args[2], args[3]);
699 if (tmp != 2) {
700 gen_opc_buf[op_index] = op_to_movi(op);
701 tcg_opt_gen_movi(gen_args, args[0], tmp);
702 gen_args += 2;
703 } else {
704 reset_temp(args[0]);
705 gen_args[0] = args[0];
706 gen_args[1] = args[1];
707 gen_args[2] = args[2];
708 gen_args[3] = args[3];
709 gen_args += 4;
711 args += 4;
712 break;
713 CASE_OP_32_64(brcond):
714 tmp = do_constant_folding_cond(op, args[0], args[1], args[2]);
715 if (tmp != 2) {
716 if (tmp) {
717 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
718 gen_opc_buf[op_index] = INDEX_op_br;
719 gen_args[0] = args[3];
720 gen_args += 1;
721 } else {
722 gen_opc_buf[op_index] = INDEX_op_nop;
724 } else {
725 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
726 reset_temp(args[0]);
727 gen_args[0] = args[0];
728 gen_args[1] = args[1];
729 gen_args[2] = args[2];
730 gen_args[3] = args[3];
731 gen_args += 4;
733 args += 4;
734 break;
735 CASE_OP_32_64(movcond):
736 tmp = do_constant_folding_cond(op, args[1], args[2], args[5]);
737 if (tmp != 2) {
738 if (temps_are_copies(args[0], args[4-tmp])) {
739 gen_opc_buf[op_index] = INDEX_op_nop;
740 } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) {
741 gen_opc_buf[op_index] = op_to_movi(op);
742 tcg_opt_gen_movi(gen_args, args[0], temps[args[4-tmp]].val);
743 gen_args += 2;
744 } else {
745 gen_opc_buf[op_index] = op_to_mov(op);
746 tcg_opt_gen_mov(s, gen_args, args[0], args[4-tmp]);
747 gen_args += 2;
749 } else {
750 reset_temp(args[0]);
751 gen_args[0] = args[0];
752 gen_args[1] = args[1];
753 gen_args[2] = args[2];
754 gen_args[3] = args[3];
755 gen_args[4] = args[4];
756 gen_args[5] = args[5];
757 gen_args += 6;
759 args += 6;
760 break;
761 case INDEX_op_call:
762 nb_call_args = (args[0] >> 16) + (args[0] & 0xffff);
763 if (!(args[nb_call_args + 1] & (TCG_CALL_CONST | TCG_CALL_PURE))) {
764 for (i = 0; i < nb_globals; i++) {
765 reset_temp(i);
768 for (i = 0; i < (args[0] >> 16); i++) {
769 reset_temp(args[i + 1]);
771 i = nb_call_args + 3;
772 while (i) {
773 *gen_args = *args;
774 args++;
775 gen_args++;
776 i--;
778 break;
779 default:
780 /* Default case: we do know nothing about operation so no
781 propagation is done. We trash everything if the operation
782 is the end of a basic block, otherwise we only trash the
783 output args. */
784 if (def->flags & TCG_OPF_BB_END) {
785 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
786 } else {
787 for (i = 0; i < def->nb_oargs; i++) {
788 reset_temp(args[i]);
791 for (i = 0; i < def->nb_args; i++) {
792 gen_args[i] = args[i];
794 args += def->nb_args;
795 gen_args += def->nb_args;
796 break;
800 return gen_args;
803 TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr,
804 TCGArg *args, TCGOpDef *tcg_op_defs)
806 TCGArg *res;
807 res = tcg_constant_folding(s, tcg_opc_ptr, args, tcg_op_defs);
808 return res;