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