]> git.proxmox.com Git - mirror_qemu.git/blame - tcg/optimize.c
tcg/optimize: check types in copy propagation
[mirror_qemu.git] / tcg / optimize.c
CommitLineData
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
38typedef enum {
39 TCG_TEMP_UNDEF = 0,
40 TCG_TEMP_CONST,
41 TCG_TEMP_COPY,
48b56ce1 42 TCG_TEMP_HAS_COPY
22613af4
KB
43} tcg_temp_state;
44
45struct tcg_temp_info {
46 tcg_temp_state state;
47 uint16_t prev_copy;
48 uint16_t next_copy;
49 tcg_target_ulong val;
50};
51
52static struct tcg_temp_info temps[TCG_MAX_TEMPS];
53
48b56ce1 54/* Reset TEMP's state to TCG_TEMP_UNDEF. If TEMP was a representative of some
22613af4
KB
55 class of equivalent temp's, a new representative should be chosen in this
56 class. */
57static void reset_temp(TCGArg temp, int nb_temps, int nb_globals)
58{
59 int i;
60 TCGArg new_base = (TCGArg)-1;
61 if (temps[temp].state == TCG_TEMP_HAS_COPY) {
62 for (i = temps[temp].next_copy; i != temp; i = temps[i].next_copy) {
63 if (i >= nb_globals) {
64 temps[i].state = TCG_TEMP_HAS_COPY;
65 new_base = i;
66 break;
67 }
68 }
69 for (i = temps[temp].next_copy; i != temp; i = temps[i].next_copy) {
70 if (new_base == (TCGArg)-1) {
48b56ce1 71 temps[i].state = TCG_TEMP_UNDEF;
22613af4
KB
72 } else {
73 temps[i].val = new_base;
74 }
75 }
76 temps[temps[temp].next_copy].prev_copy = temps[temp].prev_copy;
77 temps[temps[temp].prev_copy].next_copy = temps[temp].next_copy;
78 } else if (temps[temp].state == TCG_TEMP_COPY) {
79 temps[temps[temp].next_copy].prev_copy = temps[temp].prev_copy;
80 temps[temps[temp].prev_copy].next_copy = temps[temp].next_copy;
81 new_base = temps[temp].val;
82 }
48b56ce1 83 temps[temp].state = TCG_TEMP_UNDEF;
22613af4 84 if (new_base != (TCGArg)-1 && temps[new_base].next_copy == new_base) {
48b56ce1 85 temps[new_base].state = TCG_TEMP_UNDEF;
22613af4
KB
86 }
87}
88
fe0de7aa 89static int op_bits(TCGOpcode op)
22613af4 90{
8399ad59
RH
91 const TCGOpDef *def = &tcg_op_defs[op];
92 return def->flags & TCG_OPF_64BIT ? 64 : 32;
22613af4
KB
93}
94
fe0de7aa 95static TCGOpcode op_to_movi(TCGOpcode op)
22613af4
KB
96{
97 switch (op_bits(op)) {
98 case 32:
99 return INDEX_op_movi_i32;
22613af4
KB
100 case 64:
101 return INDEX_op_movi_i64;
22613af4
KB
102 default:
103 fprintf(stderr, "op_to_movi: unexpected return value of "
104 "function op_bits.\n");
105 tcg_abort();
106 }
107}
108
b80bb016
AJ
109static void tcg_opt_gen_mov(TCGContext *s, TCGArg *gen_args,
110 TCGArg dst, TCGArg src)
22613af4 111{
b80bb016 112 reset_temp(dst, s->nb_temps, s->nb_globals);
22613af4 113 assert(temps[src].state != TCG_TEMP_COPY);
b80bb016
AJ
114 /* Only consider temps with the same type (width) as copies. */
115 if (src >= s->nb_globals && s->temps[dst].type == s->temps[src].type) {
22613af4
KB
116 assert(temps[src].state != TCG_TEMP_CONST);
117 if (temps[src].state != TCG_TEMP_HAS_COPY) {
118 temps[src].state = TCG_TEMP_HAS_COPY;
119 temps[src].next_copy = src;
120 temps[src].prev_copy = src;
121 }
122 temps[dst].state = TCG_TEMP_COPY;
123 temps[dst].val = src;
124 temps[dst].next_copy = temps[src].next_copy;
125 temps[dst].prev_copy = src;
126 temps[temps[dst].next_copy].prev_copy = dst;
127 temps[src].next_copy = dst;
128 }
129 gen_args[0] = dst;
130 gen_args[1] = src;
131}
132
133static void tcg_opt_gen_movi(TCGArg *gen_args, TCGArg dst, TCGArg val,
134 int nb_temps, int nb_globals)
135{
136 reset_temp(dst, nb_temps, nb_globals);
137 temps[dst].state = TCG_TEMP_CONST;
138 temps[dst].val = val;
139 gen_args[0] = dst;
140 gen_args[1] = val;
141}
142
fe0de7aa 143static TCGOpcode op_to_mov(TCGOpcode op)
53108fb5
KB
144{
145 switch (op_bits(op)) {
146 case 32:
147 return INDEX_op_mov_i32;
53108fb5
KB
148 case 64:
149 return INDEX_op_mov_i64;
53108fb5
KB
150 default:
151 fprintf(stderr, "op_to_mov: unexpected return value of "
152 "function op_bits.\n");
153 tcg_abort();
154 }
155}
156
fe0de7aa 157static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)
53108fb5
KB
158{
159 switch (op) {
160 CASE_OP_32_64(add):
161 return x + y;
162
163 CASE_OP_32_64(sub):
164 return x - y;
165
166 CASE_OP_32_64(mul):
167 return x * y;
168
9a81090b
KB
169 CASE_OP_32_64(and):
170 return x & y;
171
172 CASE_OP_32_64(or):
173 return x | y;
174
175 CASE_OP_32_64(xor):
176 return x ^ y;
177
55c0975c
KB
178 case INDEX_op_shl_i32:
179 return (uint32_t)x << (uint32_t)y;
180
55c0975c
KB
181 case INDEX_op_shl_i64:
182 return (uint64_t)x << (uint64_t)y;
55c0975c
KB
183
184 case INDEX_op_shr_i32:
185 return (uint32_t)x >> (uint32_t)y;
186
55c0975c
KB
187 case INDEX_op_shr_i64:
188 return (uint64_t)x >> (uint64_t)y;
55c0975c
KB
189
190 case INDEX_op_sar_i32:
191 return (int32_t)x >> (int32_t)y;
192
55c0975c
KB
193 case INDEX_op_sar_i64:
194 return (int64_t)x >> (int64_t)y;
55c0975c
KB
195
196 case INDEX_op_rotr_i32:
25c4d9cc 197 x = ((uint32_t)x << (32 - y)) | ((uint32_t)x >> y);
55c0975c
KB
198 return x;
199
55c0975c 200 case INDEX_op_rotr_i64:
25c4d9cc 201 x = ((uint64_t)x << (64 - y)) | ((uint64_t)x >> y);
55c0975c 202 return x;
55c0975c
KB
203
204 case INDEX_op_rotl_i32:
25c4d9cc 205 x = ((uint32_t)x << y) | ((uint32_t)x >> (32 - y));
55c0975c
KB
206 return x;
207
55c0975c 208 case INDEX_op_rotl_i64:
25c4d9cc 209 x = ((uint64_t)x << y) | ((uint64_t)x >> (64 - y));
55c0975c 210 return x;
25c4d9cc
RH
211
212 CASE_OP_32_64(not):
a640f031 213 return ~x;
25c4d9cc 214
cb25c80a
RH
215 CASE_OP_32_64(neg):
216 return -x;
217
218 CASE_OP_32_64(andc):
219 return x & ~y;
220
221 CASE_OP_32_64(orc):
222 return x | ~y;
223
224 CASE_OP_32_64(eqv):
225 return ~(x ^ y);
226
227 CASE_OP_32_64(nand):
228 return ~(x & y);
229
230 CASE_OP_32_64(nor):
231 return ~(x | y);
232
25c4d9cc 233 CASE_OP_32_64(ext8s):
a640f031 234 return (int8_t)x;
25c4d9cc
RH
235
236 CASE_OP_32_64(ext16s):
a640f031 237 return (int16_t)x;
25c4d9cc
RH
238
239 CASE_OP_32_64(ext8u):
a640f031 240 return (uint8_t)x;
25c4d9cc
RH
241
242 CASE_OP_32_64(ext16u):
a640f031
KB
243 return (uint16_t)x;
244
a640f031
KB
245 case INDEX_op_ext32s_i64:
246 return (int32_t)x;
247
248 case INDEX_op_ext32u_i64:
249 return (uint32_t)x;
a640f031 250
53108fb5
KB
251 default:
252 fprintf(stderr,
253 "Unrecognized operation %d in do_constant_folding.\n", op);
254 tcg_abort();
255 }
256}
257
fe0de7aa 258static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
53108fb5
KB
259{
260 TCGArg res = do_constant_folding_2(op, x, y);
53108fb5
KB
261 if (op_bits(op) == 32) {
262 res &= 0xffffffff;
263 }
53108fb5
KB
264 return res;
265}
266
f8dd19e5
AJ
267static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
268 TCGArg y, TCGCond c)
269{
270 switch (op_bits(op)) {
271 case 32:
272 switch (c) {
273 case TCG_COND_EQ:
274 return (uint32_t)x == (uint32_t)y;
275 case TCG_COND_NE:
276 return (uint32_t)x != (uint32_t)y;
277 case TCG_COND_LT:
278 return (int32_t)x < (int32_t)y;
279 case TCG_COND_GE:
280 return (int32_t)x >= (int32_t)y;
281 case TCG_COND_LE:
282 return (int32_t)x <= (int32_t)y;
283 case TCG_COND_GT:
284 return (int32_t)x > (int32_t)y;
285 case TCG_COND_LTU:
286 return (uint32_t)x < (uint32_t)y;
287 case TCG_COND_GEU:
288 return (uint32_t)x >= (uint32_t)y;
289 case TCG_COND_LEU:
290 return (uint32_t)x <= (uint32_t)y;
291 case TCG_COND_GTU:
292 return (uint32_t)x > (uint32_t)y;
293 }
294 break;
295 case 64:
296 switch (c) {
297 case TCG_COND_EQ:
298 return (uint64_t)x == (uint64_t)y;
299 case TCG_COND_NE:
300 return (uint64_t)x != (uint64_t)y;
301 case TCG_COND_LT:
302 return (int64_t)x < (int64_t)y;
303 case TCG_COND_GE:
304 return (int64_t)x >= (int64_t)y;
305 case TCG_COND_LE:
306 return (int64_t)x <= (int64_t)y;
307 case TCG_COND_GT:
308 return (int64_t)x > (int64_t)y;
309 case TCG_COND_LTU:
310 return (uint64_t)x < (uint64_t)y;
311 case TCG_COND_GEU:
312 return (uint64_t)x >= (uint64_t)y;
313 case TCG_COND_LEU:
314 return (uint64_t)x <= (uint64_t)y;
315 case TCG_COND_GTU:
316 return (uint64_t)x > (uint64_t)y;
317 }
318 break;
319 }
320
321 fprintf(stderr,
322 "Unrecognized bitness %d or condition %d in "
323 "do_constant_folding_cond.\n", op_bits(op), c);
324 tcg_abort();
325}
326
327
22613af4 328/* Propagate constants and copies, fold constant expressions. */
8f2e8c07
KB
329static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
330 TCGArg *args, TCGOpDef *tcg_op_defs)
331{
fe0de7aa
BS
332 int i, nb_ops, op_index, nb_temps, nb_globals, nb_call_args;
333 TCGOpcode op;
8f2e8c07
KB
334 const TCGOpDef *def;
335 TCGArg *gen_args;
53108fb5 336 TCGArg tmp;
5d8f5363
RH
337 TCGCond cond;
338
22613af4
KB
339 /* Array VALS has an element for each temp.
340 If this temp holds a constant then its value is kept in VALS' element.
341 If this temp is a copy of other ones then this equivalence class'
342 representative is kept in VALS' element.
343 If this temp is neither copy nor constant then corresponding VALS'
344 element is unused. */
8f2e8c07
KB
345
346 nb_temps = s->nb_temps;
347 nb_globals = s->nb_globals;
22613af4 348 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
8f2e8c07
KB
349
350 nb_ops = tcg_opc_ptr - gen_opc_buf;
351 gen_args = args;
352 for (op_index = 0; op_index < nb_ops; op_index++) {
353 op = gen_opc_buf[op_index];
354 def = &tcg_op_defs[op];
22613af4
KB
355 /* Do copy propagation */
356 if (!(def->flags & (TCG_OPF_CALL_CLOBBER | TCG_OPF_SIDE_EFFECTS))) {
357 assert(op != INDEX_op_call);
358 for (i = def->nb_oargs; i < def->nb_oargs + def->nb_iargs; i++) {
359 if (temps[args[i]].state == TCG_TEMP_COPY) {
360 args[i] = temps[args[i]].val;
361 }
362 }
363 }
364
53108fb5
KB
365 /* For commutative operations make constant second argument */
366 switch (op) {
367 CASE_OP_32_64(add):
368 CASE_OP_32_64(mul):
9a81090b
KB
369 CASE_OP_32_64(and):
370 CASE_OP_32_64(or):
371 CASE_OP_32_64(xor):
cb25c80a
RH
372 CASE_OP_32_64(eqv):
373 CASE_OP_32_64(nand):
374 CASE_OP_32_64(nor):
53108fb5
KB
375 if (temps[args[1]].state == TCG_TEMP_CONST) {
376 tmp = args[1];
377 args[1] = args[2];
378 args[2] = tmp;
379 }
380 break;
65a7cce1
AJ
381 CASE_OP_32_64(brcond):
382 if (temps[args[0]].state == TCG_TEMP_CONST
383 && temps[args[1]].state != TCG_TEMP_CONST) {
384 tmp = args[0];
385 args[0] = args[1];
386 args[1] = tmp;
387 args[2] = tcg_swap_cond(args[2]);
388 }
389 break;
390 CASE_OP_32_64(setcond):
391 if (temps[args[1]].state == TCG_TEMP_CONST
392 && temps[args[2]].state != TCG_TEMP_CONST) {
393 tmp = args[1];
394 args[1] = args[2];
395 args[2] = tmp;
396 args[3] = tcg_swap_cond(args[3]);
397 }
398 break;
fa01a208 399 CASE_OP_32_64(movcond):
5d8f5363 400 cond = args[5];
fa01a208
RH
401 if (temps[args[1]].state == TCG_TEMP_CONST
402 && temps[args[2]].state != TCG_TEMP_CONST) {
403 tmp = args[1];
404 args[1] = args[2];
405 args[2] = tmp;
5d8f5363
RH
406 cond = tcg_swap_cond(cond);
407 }
408 /* For movcond, we canonicalize the "false" input reg to match
409 the destination reg so that the tcg backend can implement
410 a "move if true" operation. */
411 if (args[0] == args[3]) {
412 tmp = args[3];
413 args[3] = args[4];
414 args[4] = tmp;
415 cond = tcg_invert_cond(cond);
fa01a208 416 }
5d8f5363 417 args[5] = cond;
53108fb5
KB
418 default:
419 break;
420 }
421
01ee5282
AJ
422 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0" */
423 switch (op) {
424 CASE_OP_32_64(shl):
425 CASE_OP_32_64(shr):
426 CASE_OP_32_64(sar):
427 CASE_OP_32_64(rotl):
428 CASE_OP_32_64(rotr):
429 if (temps[args[1]].state == TCG_TEMP_CONST
430 && temps[args[1]].val == 0) {
431 gen_opc_buf[op_index] = op_to_movi(op);
432 tcg_opt_gen_movi(gen_args, args[0], 0, nb_temps, nb_globals);
433 args += 3;
434 gen_args += 2;
435 continue;
436 }
437 break;
438 default:
439 break;
440 }
441
56e49438 442 /* Simplify expression for "op r, a, 0 => mov r, a" cases */
53108fb5
KB
443 switch (op) {
444 CASE_OP_32_64(add):
445 CASE_OP_32_64(sub):
55c0975c
KB
446 CASE_OP_32_64(shl):
447 CASE_OP_32_64(shr):
448 CASE_OP_32_64(sar):
25c4d9cc
RH
449 CASE_OP_32_64(rotl):
450 CASE_OP_32_64(rotr):
38ee188b
AJ
451 CASE_OP_32_64(or):
452 CASE_OP_32_64(xor):
53108fb5
KB
453 if (temps[args[1]].state == TCG_TEMP_CONST) {
454 /* Proceed with possible constant folding. */
455 break;
456 }
457 if (temps[args[2]].state == TCG_TEMP_CONST
458 && temps[args[2]].val == 0) {
459 if ((temps[args[0]].state == TCG_TEMP_COPY
460 && temps[args[0]].val == args[1])
461 || args[0] == args[1]) {
53108fb5
KB
462 gen_opc_buf[op_index] = INDEX_op_nop;
463 } else {
464 gen_opc_buf[op_index] = op_to_mov(op);
b80bb016 465 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
53108fb5 466 gen_args += 2;
53108fb5 467 }
fedc0da2 468 args += 3;
53108fb5
KB
469 continue;
470 }
471 break;
56e49438
AJ
472 default:
473 break;
474 }
475
476 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
477 switch (op) {
61251c0c 478 CASE_OP_32_64(and):
53108fb5
KB
479 CASE_OP_32_64(mul):
480 if ((temps[args[2]].state == TCG_TEMP_CONST
481 && temps[args[2]].val == 0)) {
482 gen_opc_buf[op_index] = op_to_movi(op);
483 tcg_opt_gen_movi(gen_args, args[0], 0, nb_temps, nb_globals);
484 args += 3;
485 gen_args += 2;
486 continue;
487 }
488 break;
56e49438
AJ
489 default:
490 break;
491 }
492
493 /* Simplify expression for "op r, a, a => mov r, a" cases */
494 switch (op) {
9a81090b
KB
495 CASE_OP_32_64(or):
496 CASE_OP_32_64(and):
497 if (args[1] == args[2]) {
498 if (args[1] == args[0]) {
9a81090b
KB
499 gen_opc_buf[op_index] = INDEX_op_nop;
500 } else {
501 gen_opc_buf[op_index] = op_to_mov(op);
b80bb016 502 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
9a81090b 503 gen_args += 2;
9a81090b 504 }
fedc0da2 505 args += 3;
9a81090b
KB
506 continue;
507 }
508 break;
fe0de7aa
BS
509 default:
510 break;
53108fb5
KB
511 }
512
22613af4
KB
513 /* Propagate constants through copy operations and do constant
514 folding. Constants will be substituted to arguments by register
515 allocator where needed and possible. Also detect copies. */
8f2e8c07 516 switch (op) {
22613af4
KB
517 CASE_OP_32_64(mov):
518 if ((temps[args[1]].state == TCG_TEMP_COPY
519 && temps[args[1]].val == args[0])
520 || args[0] == args[1]) {
521 args += 2;
522 gen_opc_buf[op_index] = INDEX_op_nop;
523 break;
524 }
525 if (temps[args[1]].state != TCG_TEMP_CONST) {
b80bb016 526 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
22613af4
KB
527 gen_args += 2;
528 args += 2;
529 break;
530 }
531 /* Source argument is constant. Rewrite the operation and
532 let movi case handle it. */
533 op = op_to_movi(op);
534 gen_opc_buf[op_index] = op;
535 args[1] = temps[args[1]].val;
536 /* fallthrough */
537 CASE_OP_32_64(movi):
538 tcg_opt_gen_movi(gen_args, args[0], args[1], nb_temps, nb_globals);
539 gen_args += 2;
540 args += 2;
541 break;
a640f031 542 CASE_OP_32_64(not):
cb25c80a 543 CASE_OP_32_64(neg):
25c4d9cc
RH
544 CASE_OP_32_64(ext8s):
545 CASE_OP_32_64(ext8u):
546 CASE_OP_32_64(ext16s):
547 CASE_OP_32_64(ext16u):
a640f031
KB
548 case INDEX_op_ext32s_i64:
549 case INDEX_op_ext32u_i64:
a640f031
KB
550 if (temps[args[1]].state == TCG_TEMP_CONST) {
551 gen_opc_buf[op_index] = op_to_movi(op);
552 tmp = do_constant_folding(op, temps[args[1]].val, 0);
553 tcg_opt_gen_movi(gen_args, args[0], tmp, nb_temps, nb_globals);
a640f031
KB
554 } else {
555 reset_temp(args[0], nb_temps, nb_globals);
556 gen_args[0] = args[0];
557 gen_args[1] = args[1];
a640f031 558 }
fedc0da2
AJ
559 gen_args += 2;
560 args += 2;
561 break;
53108fb5
KB
562 CASE_OP_32_64(add):
563 CASE_OP_32_64(sub):
564 CASE_OP_32_64(mul):
9a81090b
KB
565 CASE_OP_32_64(or):
566 CASE_OP_32_64(and):
567 CASE_OP_32_64(xor):
55c0975c
KB
568 CASE_OP_32_64(shl):
569 CASE_OP_32_64(shr):
570 CASE_OP_32_64(sar):
25c4d9cc
RH
571 CASE_OP_32_64(rotl):
572 CASE_OP_32_64(rotr):
cb25c80a
RH
573 CASE_OP_32_64(andc):
574 CASE_OP_32_64(orc):
575 CASE_OP_32_64(eqv):
576 CASE_OP_32_64(nand):
577 CASE_OP_32_64(nor):
53108fb5
KB
578 if (temps[args[1]].state == TCG_TEMP_CONST
579 && temps[args[2]].state == TCG_TEMP_CONST) {
580 gen_opc_buf[op_index] = op_to_movi(op);
581 tmp = do_constant_folding(op, temps[args[1]].val,
582 temps[args[2]].val);
583 tcg_opt_gen_movi(gen_args, args[0], tmp, nb_temps, nb_globals);
584 gen_args += 2;
53108fb5
KB
585 } else {
586 reset_temp(args[0], nb_temps, nb_globals);
587 gen_args[0] = args[0];
588 gen_args[1] = args[1];
589 gen_args[2] = args[2];
590 gen_args += 3;
53108fb5 591 }
fedc0da2
AJ
592 args += 3;
593 break;
f8dd19e5
AJ
594 CASE_OP_32_64(setcond):
595 if (temps[args[1]].state == TCG_TEMP_CONST
596 && temps[args[2]].state == TCG_TEMP_CONST) {
597 gen_opc_buf[op_index] = op_to_movi(op);
598 tmp = do_constant_folding_cond(op, temps[args[1]].val,
599 temps[args[2]].val, args[3]);
600 tcg_opt_gen_movi(gen_args, args[0], tmp, nb_temps, nb_globals);
601 gen_args += 2;
f8dd19e5
AJ
602 } else {
603 reset_temp(args[0], nb_temps, nb_globals);
604 gen_args[0] = args[0];
605 gen_args[1] = args[1];
606 gen_args[2] = args[2];
607 gen_args[3] = args[3];
608 gen_args += 4;
f8dd19e5 609 }
fedc0da2
AJ
610 args += 4;
611 break;
fbeaa26c
AJ
612 CASE_OP_32_64(brcond):
613 if (temps[args[0]].state == TCG_TEMP_CONST
614 && temps[args[1]].state == TCG_TEMP_CONST) {
615 if (do_constant_folding_cond(op, temps[args[0]].val,
616 temps[args[1]].val, args[2])) {
617 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
618 gen_opc_buf[op_index] = INDEX_op_br;
619 gen_args[0] = args[3];
620 gen_args += 1;
fbeaa26c
AJ
621 } else {
622 gen_opc_buf[op_index] = INDEX_op_nop;
fbeaa26c 623 }
fbeaa26c
AJ
624 } else {
625 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
626 reset_temp(args[0], nb_temps, nb_globals);
627 gen_args[0] = args[0];
628 gen_args[1] = args[1];
629 gen_args[2] = args[2];
630 gen_args[3] = args[3];
631 gen_args += 4;
fbeaa26c 632 }
fedc0da2
AJ
633 args += 4;
634 break;
fa01a208
RH
635 CASE_OP_32_64(movcond):
636 if (temps[args[1]].state == TCG_TEMP_CONST
637 && temps[args[2]].state == TCG_TEMP_CONST) {
638 tmp = do_constant_folding_cond(op, temps[args[1]].val,
639 temps[args[2]].val, args[5]);
640 if (args[0] == args[4-tmp]
641 || (temps[args[4-tmp]].state == TCG_TEMP_COPY
642 && temps[args[4-tmp]].val == args[0])) {
643 gen_opc_buf[op_index] = INDEX_op_nop;
644 } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) {
645 gen_opc_buf[op_index] = op_to_movi(op);
646 tcg_opt_gen_movi(gen_args, args[0], temps[args[4-tmp]].val,
647 nb_temps, nb_globals);
648 gen_args += 2;
649 } else {
650 gen_opc_buf[op_index] = op_to_mov(op);
651 tcg_opt_gen_mov(gen_args, args[0], args[4-tmp],
652 nb_temps, nb_globals);
653 gen_args += 2;
654 }
655 } else {
656 reset_temp(args[0], nb_temps, nb_globals);
657 gen_args[0] = args[0];
658 gen_args[1] = args[1];
659 gen_args[2] = args[2];
660 gen_args[3] = args[3];
661 gen_args[4] = args[4];
662 gen_args[5] = args[5];
663 gen_args += 6;
664 }
665 args += 6;
666 break;
8f2e8c07 667 case INDEX_op_call:
22613af4
KB
668 nb_call_args = (args[0] >> 16) + (args[0] & 0xffff);
669 if (!(args[nb_call_args + 1] & (TCG_CALL_CONST | TCG_CALL_PURE))) {
670 for (i = 0; i < nb_globals; i++) {
671 reset_temp(i, nb_temps, nb_globals);
672 }
673 }
674 for (i = 0; i < (args[0] >> 16); i++) {
675 reset_temp(args[i + 1], nb_temps, nb_globals);
676 }
677 i = nb_call_args + 3;
8f2e8c07
KB
678 while (i) {
679 *gen_args = *args;
680 args++;
681 gen_args++;
682 i--;
683 }
684 break;
8f2e8c07 685 default:
22613af4 686 /* Default case: we do know nothing about operation so no
a2550660
AJ
687 propagation is done. We trash everything if the operation
688 is the end of a basic block, otherwise we only trash the
689 output args. */
690 if (def->flags & TCG_OPF_BB_END) {
691 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
692 } else {
693 for (i = 0; i < def->nb_oargs; i++) {
694 reset_temp(args[i], nb_temps, nb_globals);
695 }
22613af4 696 }
8f2e8c07
KB
697 for (i = 0; i < def->nb_args; i++) {
698 gen_args[i] = args[i];
699 }
700 args += def->nb_args;
701 gen_args += def->nb_args;
702 break;
703 }
704 }
705
706 return gen_args;
707}
708
709TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr,
710 TCGArg *args, TCGOpDef *tcg_op_defs)
711{
712 TCGArg *res;
713 res = tcg_constant_folding(s, tcg_opc_ptr, args, tcg_op_defs);
714 return res;
715}