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