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 "qemu/osdep.h" 27 #include "qemu/int128.h" 28 #include "qemu/interval-tree.h" 29 #include "tcg/tcg-op-common.h" 30 #include "tcg-internal.h" 31 #include "tcg-has.h" 32 33 #define CASE_OP_32_64(x) \ 34 glue(glue(case INDEX_op_, x), _i32): \ 35 glue(glue(case INDEX_op_, x), _i64) 36 37 #define CASE_OP_32_64_VEC(x) \ 38 glue(glue(case INDEX_op_, x), _i32): \ 39 glue(glue(case INDEX_op_, x), _i64): \ 40 glue(glue(case INDEX_op_, x), _vec) 41 42 typedef struct MemCopyInfo { 43 IntervalTreeNode itree; 44 QSIMPLEQ_ENTRY (MemCopyInfo) next; 45 TCGTemp *ts; 46 TCGType type; 47 } MemCopyInfo; 48 49 typedef struct TempOptInfo { 50 bool is_const; 51 TCGTemp *prev_copy; 52 TCGTemp *next_copy; 53 QSIMPLEQ_HEAD(, MemCopyInfo) mem_copy; 54 uint64_t val; 55 uint64_t z_mask; /* mask bit is 0 if and only if value bit is 0 */ 56 uint64_t s_mask; /* mask bit is 1 if value bit matches msb */ 57 } TempOptInfo; 58 59 typedef struct OptContext { 60 TCGContext *tcg; 61 TCGOp *prev_mb; 62 TCGTempSet temps_used; 63 64 IntervalTreeRoot mem_copy; 65 QSIMPLEQ_HEAD(, MemCopyInfo) mem_free; 66 67 /* In flight values from optimization. */ 68 TCGType type; 69 } OptContext; 70 71 static inline TempOptInfo *ts_info(TCGTemp *ts) 72 { 73 return ts->state_ptr; 74 } 75 76 static inline TempOptInfo *arg_info(TCGArg arg) 77 { 78 return ts_info(arg_temp(arg)); 79 } 80 81 static inline bool ti_is_const(TempOptInfo *ti) 82 { 83 return ti->is_const; 84 } 85 86 static inline uint64_t ti_const_val(TempOptInfo *ti) 87 { 88 return ti->val; 89 } 90 91 static inline bool ti_is_const_val(TempOptInfo *ti, uint64_t val) 92 { 93 return ti_is_const(ti) && ti_const_val(ti) == val; 94 } 95 96 static inline bool ts_is_const(TCGTemp *ts) 97 { 98 return ti_is_const(ts_info(ts)); 99 } 100 101 static inline bool ts_is_const_val(TCGTemp *ts, uint64_t val) 102 { 103 return ti_is_const_val(ts_info(ts), val); 104 } 105 106 static inline bool arg_is_const(TCGArg arg) 107 { 108 return ts_is_const(arg_temp(arg)); 109 } 110 111 static inline bool arg_is_const_val(TCGArg arg, uint64_t val) 112 { 113 return ts_is_const_val(arg_temp(arg), val); 114 } 115 116 static inline bool ts_is_copy(TCGTemp *ts) 117 { 118 return ts_info(ts)->next_copy != ts; 119 } 120 121 static TCGTemp *cmp_better_copy(TCGTemp *a, TCGTemp *b) 122 { 123 return a->kind < b->kind ? b : a; 124 } 125 126 /* Initialize and activate a temporary. */ 127 static void init_ts_info(OptContext *ctx, TCGTemp *ts) 128 { 129 size_t idx = temp_idx(ts); 130 TempOptInfo *ti; 131 132 if (test_bit(idx, ctx->temps_used.l)) { 133 return; 134 } 135 set_bit(idx, ctx->temps_used.l); 136 137 ti = ts->state_ptr; 138 if (ti == NULL) { 139 ti = tcg_malloc(sizeof(TempOptInfo)); 140 ts->state_ptr = ti; 141 } 142 143 ti->next_copy = ts; 144 ti->prev_copy = ts; 145 QSIMPLEQ_INIT(&ti->mem_copy); 146 if (ts->kind == TEMP_CONST) { 147 ti->is_const = true; 148 ti->val = ts->val; 149 ti->z_mask = ts->val; 150 ti->s_mask = INT64_MIN >> clrsb64(ts->val); 151 } else { 152 ti->is_const = false; 153 ti->z_mask = -1; 154 ti->s_mask = 0; 155 } 156 } 157 158 static MemCopyInfo *mem_copy_first(OptContext *ctx, intptr_t s, intptr_t l) 159 { 160 IntervalTreeNode *r = interval_tree_iter_first(&ctx->mem_copy, s, l); 161 return r ? container_of(r, MemCopyInfo, itree) : NULL; 162 } 163 164 static MemCopyInfo *mem_copy_next(MemCopyInfo *mem, intptr_t s, intptr_t l) 165 { 166 IntervalTreeNode *r = interval_tree_iter_next(&mem->itree, s, l); 167 return r ? container_of(r, MemCopyInfo, itree) : NULL; 168 } 169 170 static void remove_mem_copy(OptContext *ctx, MemCopyInfo *mc) 171 { 172 TCGTemp *ts = mc->ts; 173 TempOptInfo *ti = ts_info(ts); 174 175 interval_tree_remove(&mc->itree, &ctx->mem_copy); 176 QSIMPLEQ_REMOVE(&ti->mem_copy, mc, MemCopyInfo, next); 177 QSIMPLEQ_INSERT_TAIL(&ctx->mem_free, mc, next); 178 } 179 180 static void remove_mem_copy_in(OptContext *ctx, intptr_t s, intptr_t l) 181 { 182 while (true) { 183 MemCopyInfo *mc = mem_copy_first(ctx, s, l); 184 if (!mc) { 185 break; 186 } 187 remove_mem_copy(ctx, mc); 188 } 189 } 190 191 static void remove_mem_copy_all(OptContext *ctx) 192 { 193 remove_mem_copy_in(ctx, 0, -1); 194 tcg_debug_assert(interval_tree_is_empty(&ctx->mem_copy)); 195 } 196 197 static TCGTemp *find_better_copy(TCGTemp *ts) 198 { 199 TCGTemp *i, *ret; 200 201 /* If this is already readonly, we can't do better. */ 202 if (temp_readonly(ts)) { 203 return ts; 204 } 205 206 ret = ts; 207 for (i = ts_info(ts)->next_copy; i != ts; i = ts_info(i)->next_copy) { 208 ret = cmp_better_copy(ret, i); 209 } 210 return ret; 211 } 212 213 static void move_mem_copies(TCGTemp *dst_ts, TCGTemp *src_ts) 214 { 215 TempOptInfo *si = ts_info(src_ts); 216 TempOptInfo *di = ts_info(dst_ts); 217 MemCopyInfo *mc; 218 219 QSIMPLEQ_FOREACH(mc, &si->mem_copy, next) { 220 tcg_debug_assert(mc->ts == src_ts); 221 mc->ts = dst_ts; 222 } 223 QSIMPLEQ_CONCAT(&di->mem_copy, &si->mem_copy); 224 } 225 226 /* Reset TEMP's state, possibly removing the temp for the list of copies. */ 227 static void reset_ts(OptContext *ctx, TCGTemp *ts) 228 { 229 TempOptInfo *ti = ts_info(ts); 230 TCGTemp *pts = ti->prev_copy; 231 TCGTemp *nts = ti->next_copy; 232 TempOptInfo *pi = ts_info(pts); 233 TempOptInfo *ni = ts_info(nts); 234 235 ni->prev_copy = ti->prev_copy; 236 pi->next_copy = ti->next_copy; 237 ti->next_copy = ts; 238 ti->prev_copy = ts; 239 ti->is_const = false; 240 ti->z_mask = -1; 241 ti->s_mask = 0; 242 243 if (!QSIMPLEQ_EMPTY(&ti->mem_copy)) { 244 if (ts == nts) { 245 /* Last temp copy being removed, the mem copies die. */ 246 MemCopyInfo *mc; 247 QSIMPLEQ_FOREACH(mc, &ti->mem_copy, next) { 248 interval_tree_remove(&mc->itree, &ctx->mem_copy); 249 } 250 QSIMPLEQ_CONCAT(&ctx->mem_free, &ti->mem_copy); 251 } else { 252 move_mem_copies(find_better_copy(nts), ts); 253 } 254 } 255 } 256 257 static void reset_temp(OptContext *ctx, TCGArg arg) 258 { 259 reset_ts(ctx, arg_temp(arg)); 260 } 261 262 static void record_mem_copy(OptContext *ctx, TCGType type, 263 TCGTemp *ts, intptr_t start, intptr_t last) 264 { 265 MemCopyInfo *mc; 266 TempOptInfo *ti; 267 268 mc = QSIMPLEQ_FIRST(&ctx->mem_free); 269 if (mc) { 270 QSIMPLEQ_REMOVE_HEAD(&ctx->mem_free, next); 271 } else { 272 mc = tcg_malloc(sizeof(*mc)); 273 } 274 275 memset(mc, 0, sizeof(*mc)); 276 mc->itree.start = start; 277 mc->itree.last = last; 278 mc->type = type; 279 interval_tree_insert(&mc->itree, &ctx->mem_copy); 280 281 ts = find_better_copy(ts); 282 ti = ts_info(ts); 283 mc->ts = ts; 284 QSIMPLEQ_INSERT_TAIL(&ti->mem_copy, mc, next); 285 } 286 287 static bool ts_are_copies(TCGTemp *ts1, TCGTemp *ts2) 288 { 289 TCGTemp *i; 290 291 if (ts1 == ts2) { 292 return true; 293 } 294 295 if (!ts_is_copy(ts1) || !ts_is_copy(ts2)) { 296 return false; 297 } 298 299 for (i = ts_info(ts1)->next_copy; i != ts1; i = ts_info(i)->next_copy) { 300 if (i == ts2) { 301 return true; 302 } 303 } 304 305 return false; 306 } 307 308 static bool args_are_copies(TCGArg arg1, TCGArg arg2) 309 { 310 return ts_are_copies(arg_temp(arg1), arg_temp(arg2)); 311 } 312 313 static TCGTemp *find_mem_copy_for(OptContext *ctx, TCGType type, intptr_t s) 314 { 315 MemCopyInfo *mc; 316 317 for (mc = mem_copy_first(ctx, s, s); mc; mc = mem_copy_next(mc, s, s)) { 318 if (mc->itree.start == s && mc->type == type) { 319 return find_better_copy(mc->ts); 320 } 321 } 322 return NULL; 323 } 324 325 static TCGArg arg_new_constant(OptContext *ctx, uint64_t val) 326 { 327 TCGType type = ctx->type; 328 TCGTemp *ts; 329 330 if (type == TCG_TYPE_I32) { 331 val = (int32_t)val; 332 } 333 334 ts = tcg_constant_internal(type, val); 335 init_ts_info(ctx, ts); 336 337 return temp_arg(ts); 338 } 339 340 static TCGArg arg_new_temp(OptContext *ctx) 341 { 342 TCGTemp *ts = tcg_temp_new_internal(ctx->type, TEMP_EBB); 343 init_ts_info(ctx, ts); 344 return temp_arg(ts); 345 } 346 347 static TCGOp *opt_insert_after(OptContext *ctx, TCGOp *op, 348 TCGOpcode opc, unsigned narg) 349 { 350 return tcg_op_insert_after(ctx->tcg, op, opc, ctx->type, narg); 351 } 352 353 static TCGOp *opt_insert_before(OptContext *ctx, TCGOp *op, 354 TCGOpcode opc, unsigned narg) 355 { 356 return tcg_op_insert_before(ctx->tcg, op, opc, ctx->type, narg); 357 } 358 359 static bool tcg_opt_gen_mov(OptContext *ctx, TCGOp *op, TCGArg dst, TCGArg src) 360 { 361 TCGTemp *dst_ts = arg_temp(dst); 362 TCGTemp *src_ts = arg_temp(src); 363 TempOptInfo *di; 364 TempOptInfo *si; 365 TCGOpcode new_op; 366 367 if (ts_are_copies(dst_ts, src_ts)) { 368 tcg_op_remove(ctx->tcg, op); 369 return true; 370 } 371 372 reset_ts(ctx, dst_ts); 373 di = ts_info(dst_ts); 374 si = ts_info(src_ts); 375 376 switch (ctx->type) { 377 case TCG_TYPE_I32: 378 case TCG_TYPE_I64: 379 new_op = INDEX_op_mov; 380 break; 381 case TCG_TYPE_V64: 382 case TCG_TYPE_V128: 383 case TCG_TYPE_V256: 384 /* TCGOP_TYPE and TCGOP_VECE remain unchanged. */ 385 new_op = INDEX_op_mov_vec; 386 break; 387 default: 388 g_assert_not_reached(); 389 } 390 op->opc = new_op; 391 op->args[0] = dst; 392 op->args[1] = src; 393 394 di->z_mask = si->z_mask; 395 di->s_mask = si->s_mask; 396 397 if (src_ts->type == dst_ts->type) { 398 TempOptInfo *ni = ts_info(si->next_copy); 399 400 di->next_copy = si->next_copy; 401 di->prev_copy = src_ts; 402 ni->prev_copy = dst_ts; 403 si->next_copy = dst_ts; 404 di->is_const = si->is_const; 405 di->val = si->val; 406 407 if (!QSIMPLEQ_EMPTY(&si->mem_copy) 408 && cmp_better_copy(src_ts, dst_ts) == dst_ts) { 409 move_mem_copies(dst_ts, src_ts); 410 } 411 } 412 return true; 413 } 414 415 static bool tcg_opt_gen_movi(OptContext *ctx, TCGOp *op, 416 TCGArg dst, uint64_t val) 417 { 418 /* Convert movi to mov with constant temp. */ 419 return tcg_opt_gen_mov(ctx, op, dst, arg_new_constant(ctx, val)); 420 } 421 422 static uint64_t do_constant_folding_2(TCGOpcode op, uint64_t x, uint64_t y) 423 { 424 uint64_t l64, h64; 425 426 switch (op) { 427 case INDEX_op_add: 428 return x + y; 429 430 CASE_OP_32_64(sub): 431 return x - y; 432 433 CASE_OP_32_64(mul): 434 return x * y; 435 436 case INDEX_op_and: 437 case INDEX_op_and_vec: 438 return x & y; 439 440 case INDEX_op_or: 441 case INDEX_op_or_vec: 442 return x | y; 443 444 case INDEX_op_xor: 445 case INDEX_op_xor_vec: 446 return x ^ y; 447 448 case INDEX_op_shl_i32: 449 return (uint32_t)x << (y & 31); 450 451 case INDEX_op_shl_i64: 452 return (uint64_t)x << (y & 63); 453 454 case INDEX_op_shr_i32: 455 return (uint32_t)x >> (y & 31); 456 457 case INDEX_op_shr_i64: 458 return (uint64_t)x >> (y & 63); 459 460 case INDEX_op_sar_i32: 461 return (int32_t)x >> (y & 31); 462 463 case INDEX_op_sar_i64: 464 return (int64_t)x >> (y & 63); 465 466 case INDEX_op_rotr_i32: 467 return ror32(x, y & 31); 468 469 case INDEX_op_rotr_i64: 470 return ror64(x, y & 63); 471 472 case INDEX_op_rotl_i32: 473 return rol32(x, y & 31); 474 475 case INDEX_op_rotl_i64: 476 return rol64(x, y & 63); 477 478 CASE_OP_32_64_VEC(not): 479 return ~x; 480 481 CASE_OP_32_64(neg): 482 return -x; 483 484 case INDEX_op_andc: 485 case INDEX_op_andc_vec: 486 return x & ~y; 487 488 case INDEX_op_orc: 489 case INDEX_op_orc_vec: 490 return x | ~y; 491 492 case INDEX_op_eqv: 493 case INDEX_op_eqv_vec: 494 return ~(x ^ y); 495 496 case INDEX_op_nand: 497 case INDEX_op_nand_vec: 498 return ~(x & y); 499 500 CASE_OP_32_64_VEC(nor): 501 return ~(x | y); 502 503 case INDEX_op_clz_i32: 504 return (uint32_t)x ? clz32(x) : y; 505 506 case INDEX_op_clz_i64: 507 return x ? clz64(x) : y; 508 509 case INDEX_op_ctz_i32: 510 return (uint32_t)x ? ctz32(x) : y; 511 512 case INDEX_op_ctz_i64: 513 return x ? ctz64(x) : y; 514 515 case INDEX_op_ctpop_i32: 516 return ctpop32(x); 517 518 case INDEX_op_ctpop_i64: 519 return ctpop64(x); 520 521 CASE_OP_32_64(bswap16): 522 x = bswap16(x); 523 return y & TCG_BSWAP_OS ? (int16_t)x : x; 524 525 CASE_OP_32_64(bswap32): 526 x = bswap32(x); 527 return y & TCG_BSWAP_OS ? (int32_t)x : x; 528 529 case INDEX_op_bswap64_i64: 530 return bswap64(x); 531 532 case INDEX_op_ext_i32_i64: 533 return (int32_t)x; 534 535 case INDEX_op_extu_i32_i64: 536 case INDEX_op_extrl_i64_i32: 537 return (uint32_t)x; 538 539 case INDEX_op_extrh_i64_i32: 540 return (uint64_t)x >> 32; 541 542 case INDEX_op_muluh_i32: 543 return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32; 544 case INDEX_op_mulsh_i32: 545 return ((int64_t)(int32_t)x * (int32_t)y) >> 32; 546 547 case INDEX_op_muluh_i64: 548 mulu64(&l64, &h64, x, y); 549 return h64; 550 case INDEX_op_mulsh_i64: 551 muls64(&l64, &h64, x, y); 552 return h64; 553 554 case INDEX_op_div_i32: 555 /* Avoid crashing on divide by zero, otherwise undefined. */ 556 return (int32_t)x / ((int32_t)y ? : 1); 557 case INDEX_op_divu_i32: 558 return (uint32_t)x / ((uint32_t)y ? : 1); 559 case INDEX_op_div_i64: 560 return (int64_t)x / ((int64_t)y ? : 1); 561 case INDEX_op_divu_i64: 562 return (uint64_t)x / ((uint64_t)y ? : 1); 563 564 case INDEX_op_rem_i32: 565 return (int32_t)x % ((int32_t)y ? : 1); 566 case INDEX_op_remu_i32: 567 return (uint32_t)x % ((uint32_t)y ? : 1); 568 case INDEX_op_rem_i64: 569 return (int64_t)x % ((int64_t)y ? : 1); 570 case INDEX_op_remu_i64: 571 return (uint64_t)x % ((uint64_t)y ? : 1); 572 573 default: 574 g_assert_not_reached(); 575 } 576 } 577 578 static uint64_t do_constant_folding(TCGOpcode op, TCGType type, 579 uint64_t x, uint64_t y) 580 { 581 uint64_t res = do_constant_folding_2(op, x, y); 582 if (type == TCG_TYPE_I32) { 583 res = (int32_t)res; 584 } 585 return res; 586 } 587 588 static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c) 589 { 590 switch (c) { 591 case TCG_COND_EQ: 592 return x == y; 593 case TCG_COND_NE: 594 return x != y; 595 case TCG_COND_LT: 596 return (int32_t)x < (int32_t)y; 597 case TCG_COND_GE: 598 return (int32_t)x >= (int32_t)y; 599 case TCG_COND_LE: 600 return (int32_t)x <= (int32_t)y; 601 case TCG_COND_GT: 602 return (int32_t)x > (int32_t)y; 603 case TCG_COND_LTU: 604 return x < y; 605 case TCG_COND_GEU: 606 return x >= y; 607 case TCG_COND_LEU: 608 return x <= y; 609 case TCG_COND_GTU: 610 return x > y; 611 case TCG_COND_TSTEQ: 612 return (x & y) == 0; 613 case TCG_COND_TSTNE: 614 return (x & y) != 0; 615 case TCG_COND_ALWAYS: 616 case TCG_COND_NEVER: 617 break; 618 } 619 g_assert_not_reached(); 620 } 621 622 static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c) 623 { 624 switch (c) { 625 case TCG_COND_EQ: 626 return x == y; 627 case TCG_COND_NE: 628 return x != y; 629 case TCG_COND_LT: 630 return (int64_t)x < (int64_t)y; 631 case TCG_COND_GE: 632 return (int64_t)x >= (int64_t)y; 633 case TCG_COND_LE: 634 return (int64_t)x <= (int64_t)y; 635 case TCG_COND_GT: 636 return (int64_t)x > (int64_t)y; 637 case TCG_COND_LTU: 638 return x < y; 639 case TCG_COND_GEU: 640 return x >= y; 641 case TCG_COND_LEU: 642 return x <= y; 643 case TCG_COND_GTU: 644 return x > y; 645 case TCG_COND_TSTEQ: 646 return (x & y) == 0; 647 case TCG_COND_TSTNE: 648 return (x & y) != 0; 649 case TCG_COND_ALWAYS: 650 case TCG_COND_NEVER: 651 break; 652 } 653 g_assert_not_reached(); 654 } 655 656 static int do_constant_folding_cond_eq(TCGCond c) 657 { 658 switch (c) { 659 case TCG_COND_GT: 660 case TCG_COND_LTU: 661 case TCG_COND_LT: 662 case TCG_COND_GTU: 663 case TCG_COND_NE: 664 return 0; 665 case TCG_COND_GE: 666 case TCG_COND_GEU: 667 case TCG_COND_LE: 668 case TCG_COND_LEU: 669 case TCG_COND_EQ: 670 return 1; 671 case TCG_COND_TSTEQ: 672 case TCG_COND_TSTNE: 673 return -1; 674 case TCG_COND_ALWAYS: 675 case TCG_COND_NEVER: 676 break; 677 } 678 g_assert_not_reached(); 679 } 680 681 /* 682 * Return -1 if the condition can't be simplified, 683 * and the result of the condition (0 or 1) if it can. 684 */ 685 static int do_constant_folding_cond(TCGType type, TCGArg x, 686 TCGArg y, TCGCond c) 687 { 688 if (arg_is_const(x) && arg_is_const(y)) { 689 uint64_t xv = arg_info(x)->val; 690 uint64_t yv = arg_info(y)->val; 691 692 switch (type) { 693 case TCG_TYPE_I32: 694 return do_constant_folding_cond_32(xv, yv, c); 695 case TCG_TYPE_I64: 696 return do_constant_folding_cond_64(xv, yv, c); 697 default: 698 /* Only scalar comparisons are optimizable */ 699 return -1; 700 } 701 } else if (args_are_copies(x, y)) { 702 return do_constant_folding_cond_eq(c); 703 } else if (arg_is_const_val(y, 0)) { 704 switch (c) { 705 case TCG_COND_LTU: 706 case TCG_COND_TSTNE: 707 return 0; 708 case TCG_COND_GEU: 709 case TCG_COND_TSTEQ: 710 return 1; 711 default: 712 return -1; 713 } 714 } 715 return -1; 716 } 717 718 /** 719 * swap_commutative: 720 * @dest: TCGArg of the destination argument, or NO_DEST. 721 * @p1: first paired argument 722 * @p2: second paired argument 723 * 724 * If *@p1 is a constant and *@p2 is not, swap. 725 * If *@p2 matches @dest, swap. 726 * Return true if a swap was performed. 727 */ 728 729 #define NO_DEST temp_arg(NULL) 730 731 static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2) 732 { 733 TCGArg a1 = *p1, a2 = *p2; 734 int sum = 0; 735 sum += arg_is_const(a1); 736 sum -= arg_is_const(a2); 737 738 /* Prefer the constant in second argument, and then the form 739 op a, a, b, which is better handled on non-RISC hosts. */ 740 if (sum > 0 || (sum == 0 && dest == a2)) { 741 *p1 = a2; 742 *p2 = a1; 743 return true; 744 } 745 return false; 746 } 747 748 static bool swap_commutative2(TCGArg *p1, TCGArg *p2) 749 { 750 int sum = 0; 751 sum += arg_is_const(p1[0]); 752 sum += arg_is_const(p1[1]); 753 sum -= arg_is_const(p2[0]); 754 sum -= arg_is_const(p2[1]); 755 if (sum > 0) { 756 TCGArg t; 757 t = p1[0], p1[0] = p2[0], p2[0] = t; 758 t = p1[1], p1[1] = p2[1], p2[1] = t; 759 return true; 760 } 761 return false; 762 } 763 764 /* 765 * Return -1 if the condition can't be simplified, 766 * and the result of the condition (0 or 1) if it can. 767 */ 768 static int do_constant_folding_cond1(OptContext *ctx, TCGOp *op, TCGArg dest, 769 TCGArg *p1, TCGArg *p2, TCGArg *pcond) 770 { 771 TCGCond cond; 772 TempOptInfo *i1; 773 bool swap; 774 int r; 775 776 swap = swap_commutative(dest, p1, p2); 777 cond = *pcond; 778 if (swap) { 779 *pcond = cond = tcg_swap_cond(cond); 780 } 781 782 r = do_constant_folding_cond(ctx->type, *p1, *p2, cond); 783 if (r >= 0) { 784 return r; 785 } 786 if (!is_tst_cond(cond)) { 787 return -1; 788 } 789 790 i1 = arg_info(*p1); 791 792 /* 793 * TSTNE x,x -> NE x,0 794 * TSTNE x,i -> NE x,0 if i includes all nonzero bits of x 795 */ 796 if (args_are_copies(*p1, *p2) || 797 (arg_is_const(*p2) && (i1->z_mask & ~arg_info(*p2)->val) == 0)) { 798 *p2 = arg_new_constant(ctx, 0); 799 *pcond = tcg_tst_eqne_cond(cond); 800 return -1; 801 } 802 803 /* TSTNE x,i -> LT x,0 if i only includes sign bit copies */ 804 if (arg_is_const(*p2) && (arg_info(*p2)->val & ~i1->s_mask) == 0) { 805 *p2 = arg_new_constant(ctx, 0); 806 *pcond = tcg_tst_ltge_cond(cond); 807 return -1; 808 } 809 810 /* Expand to AND with a temporary if no backend support. */ 811 if (!TCG_TARGET_HAS_tst) { 812 TCGOp *op2 = opt_insert_before(ctx, op, INDEX_op_and, 3); 813 TCGArg tmp = arg_new_temp(ctx); 814 815 op2->args[0] = tmp; 816 op2->args[1] = *p1; 817 op2->args[2] = *p2; 818 819 *p1 = tmp; 820 *p2 = arg_new_constant(ctx, 0); 821 *pcond = tcg_tst_eqne_cond(cond); 822 } 823 return -1; 824 } 825 826 static int do_constant_folding_cond2(OptContext *ctx, TCGOp *op, TCGArg *args) 827 { 828 TCGArg al, ah, bl, bh; 829 TCGCond c; 830 bool swap; 831 int r; 832 833 swap = swap_commutative2(args, args + 2); 834 c = args[4]; 835 if (swap) { 836 args[4] = c = tcg_swap_cond(c); 837 } 838 839 al = args[0]; 840 ah = args[1]; 841 bl = args[2]; 842 bh = args[3]; 843 844 if (arg_is_const(bl) && arg_is_const(bh)) { 845 tcg_target_ulong blv = arg_info(bl)->val; 846 tcg_target_ulong bhv = arg_info(bh)->val; 847 uint64_t b = deposit64(blv, 32, 32, bhv); 848 849 if (arg_is_const(al) && arg_is_const(ah)) { 850 tcg_target_ulong alv = arg_info(al)->val; 851 tcg_target_ulong ahv = arg_info(ah)->val; 852 uint64_t a = deposit64(alv, 32, 32, ahv); 853 854 r = do_constant_folding_cond_64(a, b, c); 855 if (r >= 0) { 856 return r; 857 } 858 } 859 860 if (b == 0) { 861 switch (c) { 862 case TCG_COND_LTU: 863 case TCG_COND_TSTNE: 864 return 0; 865 case TCG_COND_GEU: 866 case TCG_COND_TSTEQ: 867 return 1; 868 default: 869 break; 870 } 871 } 872 873 /* TSTNE x,-1 -> NE x,0 */ 874 if (b == -1 && is_tst_cond(c)) { 875 args[3] = args[2] = arg_new_constant(ctx, 0); 876 args[4] = tcg_tst_eqne_cond(c); 877 return -1; 878 } 879 880 /* TSTNE x,sign -> LT x,0 */ 881 if (b == INT64_MIN && is_tst_cond(c)) { 882 /* bl must be 0, so copy that to bh */ 883 args[3] = bl; 884 args[4] = tcg_tst_ltge_cond(c); 885 return -1; 886 } 887 } 888 889 if (args_are_copies(al, bl) && args_are_copies(ah, bh)) { 890 r = do_constant_folding_cond_eq(c); 891 if (r >= 0) { 892 return r; 893 } 894 895 /* TSTNE x,x -> NE x,0 */ 896 if (is_tst_cond(c)) { 897 args[3] = args[2] = arg_new_constant(ctx, 0); 898 args[4] = tcg_tst_eqne_cond(c); 899 return -1; 900 } 901 } 902 903 /* Expand to AND with a temporary if no backend support. */ 904 if (!TCG_TARGET_HAS_tst && is_tst_cond(c)) { 905 TCGOp *op1 = opt_insert_before(ctx, op, INDEX_op_and, 3); 906 TCGOp *op2 = opt_insert_before(ctx, op, INDEX_op_and, 3); 907 TCGArg t1 = arg_new_temp(ctx); 908 TCGArg t2 = arg_new_temp(ctx); 909 910 op1->args[0] = t1; 911 op1->args[1] = al; 912 op1->args[2] = bl; 913 op2->args[0] = t2; 914 op2->args[1] = ah; 915 op2->args[2] = bh; 916 917 args[0] = t1; 918 args[1] = t2; 919 args[3] = args[2] = arg_new_constant(ctx, 0); 920 args[4] = tcg_tst_eqne_cond(c); 921 } 922 return -1; 923 } 924 925 static void init_arguments(OptContext *ctx, TCGOp *op, int nb_args) 926 { 927 for (int i = 0; i < nb_args; i++) { 928 TCGTemp *ts = arg_temp(op->args[i]); 929 init_ts_info(ctx, ts); 930 } 931 } 932 933 static void copy_propagate(OptContext *ctx, TCGOp *op, 934 int nb_oargs, int nb_iargs) 935 { 936 for (int i = nb_oargs; i < nb_oargs + nb_iargs; i++) { 937 TCGTemp *ts = arg_temp(op->args[i]); 938 if (ts_is_copy(ts)) { 939 op->args[i] = temp_arg(find_better_copy(ts)); 940 } 941 } 942 } 943 944 static void finish_bb(OptContext *ctx) 945 { 946 /* We only optimize memory barriers across basic blocks. */ 947 ctx->prev_mb = NULL; 948 } 949 950 static void finish_ebb(OptContext *ctx) 951 { 952 finish_bb(ctx); 953 /* We only optimize across extended basic blocks. */ 954 memset(&ctx->temps_used, 0, sizeof(ctx->temps_used)); 955 remove_mem_copy_all(ctx); 956 } 957 958 static bool finish_folding(OptContext *ctx, TCGOp *op) 959 { 960 const TCGOpDef *def = &tcg_op_defs[op->opc]; 961 int i, nb_oargs; 962 963 nb_oargs = def->nb_oargs; 964 for (i = 0; i < nb_oargs; i++) { 965 TCGTemp *ts = arg_temp(op->args[i]); 966 reset_ts(ctx, ts); 967 } 968 return true; 969 } 970 971 /* 972 * The fold_* functions return true when processing is complete, 973 * usually by folding the operation to a constant or to a copy, 974 * and calling tcg_opt_gen_{mov,movi}. They may do other things, 975 * like collect information about the value produced, for use in 976 * optimizing a subsequent operation. 977 * 978 * These first fold_* functions are all helpers, used by other 979 * folders for more specific operations. 980 */ 981 982 static bool fold_const1(OptContext *ctx, TCGOp *op) 983 { 984 if (arg_is_const(op->args[1])) { 985 uint64_t t; 986 987 t = arg_info(op->args[1])->val; 988 t = do_constant_folding(op->opc, ctx->type, t, 0); 989 return tcg_opt_gen_movi(ctx, op, op->args[0], t); 990 } 991 return false; 992 } 993 994 static bool fold_const2(OptContext *ctx, TCGOp *op) 995 { 996 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) { 997 uint64_t t1 = arg_info(op->args[1])->val; 998 uint64_t t2 = arg_info(op->args[2])->val; 999 1000 t1 = do_constant_folding(op->opc, ctx->type, t1, t2); 1001 return tcg_opt_gen_movi(ctx, op, op->args[0], t1); 1002 } 1003 return false; 1004 } 1005 1006 static bool fold_commutative(OptContext *ctx, TCGOp *op) 1007 { 1008 swap_commutative(op->args[0], &op->args[1], &op->args[2]); 1009 return false; 1010 } 1011 1012 static bool fold_const2_commutative(OptContext *ctx, TCGOp *op) 1013 { 1014 swap_commutative(op->args[0], &op->args[1], &op->args[2]); 1015 return fold_const2(ctx, op); 1016 } 1017 1018 /* 1019 * Record "zero" and "sign" masks for the single output of @op. 1020 * See TempOptInfo definition of z_mask and s_mask. 1021 * If z_mask allows, fold the output to constant zero. 1022 * The passed s_mask may be augmented by z_mask. 1023 */ 1024 static bool fold_masks_zs(OptContext *ctx, TCGOp *op, 1025 uint64_t z_mask, int64_t s_mask) 1026 { 1027 const TCGOpDef *def = &tcg_op_defs[op->opc]; 1028 TCGTemp *ts; 1029 TempOptInfo *ti; 1030 int rep; 1031 1032 /* Only single-output opcodes are supported here. */ 1033 tcg_debug_assert(def->nb_oargs == 1); 1034 1035 /* 1036 * 32-bit ops generate 32-bit results, which for the purpose of 1037 * simplifying tcg are sign-extended. Certainly that's how we 1038 * represent our constants elsewhere. Note that the bits will 1039 * be reset properly for a 64-bit value when encountering the 1040 * type changing opcodes. 1041 */ 1042 if (ctx->type == TCG_TYPE_I32) { 1043 z_mask = (int32_t)z_mask; 1044 s_mask |= INT32_MIN; 1045 } 1046 1047 if (z_mask == 0) { 1048 return tcg_opt_gen_movi(ctx, op, op->args[0], 0); 1049 } 1050 1051 ts = arg_temp(op->args[0]); 1052 reset_ts(ctx, ts); 1053 1054 ti = ts_info(ts); 1055 ti->z_mask = z_mask; 1056 1057 /* Canonicalize s_mask and incorporate data from z_mask. */ 1058 rep = clz64(~s_mask); 1059 rep = MAX(rep, clz64(z_mask)); 1060 rep = MAX(rep - 1, 0); 1061 ti->s_mask = INT64_MIN >> rep; 1062 1063 return true; 1064 } 1065 1066 static bool fold_masks_z(OptContext *ctx, TCGOp *op, uint64_t z_mask) 1067 { 1068 return fold_masks_zs(ctx, op, z_mask, 0); 1069 } 1070 1071 static bool fold_masks_s(OptContext *ctx, TCGOp *op, uint64_t s_mask) 1072 { 1073 return fold_masks_zs(ctx, op, -1, s_mask); 1074 } 1075 1076 /* 1077 * An "affected" mask bit is 0 if and only if the result is identical 1078 * to the first input. Thus if the entire mask is 0, the operation 1079 * is equivalent to a copy. 1080 */ 1081 static bool fold_affected_mask(OptContext *ctx, TCGOp *op, uint64_t a_mask) 1082 { 1083 if (ctx->type == TCG_TYPE_I32) { 1084 a_mask = (uint32_t)a_mask; 1085 } 1086 if (a_mask == 0) { 1087 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); 1088 } 1089 return false; 1090 } 1091 1092 /* 1093 * Convert @op to NOT, if NOT is supported by the host. 1094 * Return true f the conversion is successful, which will still 1095 * indicate that the processing is complete. 1096 */ 1097 static bool fold_not(OptContext *ctx, TCGOp *op); 1098 static bool fold_to_not(OptContext *ctx, TCGOp *op, int idx) 1099 { 1100 TCGOpcode not_op; 1101 bool have_not; 1102 1103 switch (ctx->type) { 1104 case TCG_TYPE_I32: 1105 not_op = INDEX_op_not_i32; 1106 have_not = TCG_TARGET_HAS_not_i32; 1107 break; 1108 case TCG_TYPE_I64: 1109 not_op = INDEX_op_not_i64; 1110 have_not = TCG_TARGET_HAS_not_i64; 1111 break; 1112 case TCG_TYPE_V64: 1113 case TCG_TYPE_V128: 1114 case TCG_TYPE_V256: 1115 not_op = INDEX_op_not_vec; 1116 have_not = TCG_TARGET_HAS_not_vec; 1117 break; 1118 default: 1119 g_assert_not_reached(); 1120 } 1121 if (have_not) { 1122 op->opc = not_op; 1123 op->args[1] = op->args[idx]; 1124 return fold_not(ctx, op); 1125 } 1126 return false; 1127 } 1128 1129 /* If the binary operation has first argument @i, fold to @i. */ 1130 static bool fold_ix_to_i(OptContext *ctx, TCGOp *op, uint64_t i) 1131 { 1132 if (arg_is_const_val(op->args[1], i)) { 1133 return tcg_opt_gen_movi(ctx, op, op->args[0], i); 1134 } 1135 return false; 1136 } 1137 1138 /* If the binary operation has first argument @i, fold to NOT. */ 1139 static bool fold_ix_to_not(OptContext *ctx, TCGOp *op, uint64_t i) 1140 { 1141 if (arg_is_const_val(op->args[1], i)) { 1142 return fold_to_not(ctx, op, 2); 1143 } 1144 return false; 1145 } 1146 1147 /* If the binary operation has second argument @i, fold to @i. */ 1148 static bool fold_xi_to_i(OptContext *ctx, TCGOp *op, uint64_t i) 1149 { 1150 if (arg_is_const_val(op->args[2], i)) { 1151 return tcg_opt_gen_movi(ctx, op, op->args[0], i); 1152 } 1153 return false; 1154 } 1155 1156 /* If the binary operation has second argument @i, fold to identity. */ 1157 static bool fold_xi_to_x(OptContext *ctx, TCGOp *op, uint64_t i) 1158 { 1159 if (arg_is_const_val(op->args[2], i)) { 1160 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); 1161 } 1162 return false; 1163 } 1164 1165 /* If the binary operation has second argument @i, fold to NOT. */ 1166 static bool fold_xi_to_not(OptContext *ctx, TCGOp *op, uint64_t i) 1167 { 1168 if (arg_is_const_val(op->args[2], i)) { 1169 return fold_to_not(ctx, op, 1); 1170 } 1171 return false; 1172 } 1173 1174 /* If the binary operation has both arguments equal, fold to @i. */ 1175 static bool fold_xx_to_i(OptContext *ctx, TCGOp *op, uint64_t i) 1176 { 1177 if (args_are_copies(op->args[1], op->args[2])) { 1178 return tcg_opt_gen_movi(ctx, op, op->args[0], i); 1179 } 1180 return false; 1181 } 1182 1183 /* If the binary operation has both arguments equal, fold to identity. */ 1184 static bool fold_xx_to_x(OptContext *ctx, TCGOp *op) 1185 { 1186 if (args_are_copies(op->args[1], op->args[2])) { 1187 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); 1188 } 1189 return false; 1190 } 1191 1192 /* 1193 * These outermost fold_<op> functions are sorted alphabetically. 1194 * 1195 * The ordering of the transformations should be: 1196 * 1) those that produce a constant 1197 * 2) those that produce a copy 1198 * 3) those that produce information about the result value. 1199 */ 1200 1201 static bool fold_or(OptContext *ctx, TCGOp *op); 1202 static bool fold_orc(OptContext *ctx, TCGOp *op); 1203 static bool fold_xor(OptContext *ctx, TCGOp *op); 1204 1205 static bool fold_add(OptContext *ctx, TCGOp *op) 1206 { 1207 if (fold_const2_commutative(ctx, op) || 1208 fold_xi_to_x(ctx, op, 0)) { 1209 return true; 1210 } 1211 return finish_folding(ctx, op); 1212 } 1213 1214 /* We cannot as yet do_constant_folding with vectors. */ 1215 static bool fold_add_vec(OptContext *ctx, TCGOp *op) 1216 { 1217 if (fold_commutative(ctx, op) || 1218 fold_xi_to_x(ctx, op, 0)) { 1219 return true; 1220 } 1221 return finish_folding(ctx, op); 1222 } 1223 1224 static bool fold_addsub2(OptContext *ctx, TCGOp *op, bool add) 1225 { 1226 bool a_const = arg_is_const(op->args[2]) && arg_is_const(op->args[3]); 1227 bool b_const = arg_is_const(op->args[4]) && arg_is_const(op->args[5]); 1228 1229 if (a_const && b_const) { 1230 uint64_t al = arg_info(op->args[2])->val; 1231 uint64_t ah = arg_info(op->args[3])->val; 1232 uint64_t bl = arg_info(op->args[4])->val; 1233 uint64_t bh = arg_info(op->args[5])->val; 1234 TCGArg rl, rh; 1235 TCGOp *op2; 1236 1237 if (ctx->type == TCG_TYPE_I32) { 1238 uint64_t a = deposit64(al, 32, 32, ah); 1239 uint64_t b = deposit64(bl, 32, 32, bh); 1240 1241 if (add) { 1242 a += b; 1243 } else { 1244 a -= b; 1245 } 1246 1247 al = sextract64(a, 0, 32); 1248 ah = sextract64(a, 32, 32); 1249 } else { 1250 Int128 a = int128_make128(al, ah); 1251 Int128 b = int128_make128(bl, bh); 1252 1253 if (add) { 1254 a = int128_add(a, b); 1255 } else { 1256 a = int128_sub(a, b); 1257 } 1258 1259 al = int128_getlo(a); 1260 ah = int128_gethi(a); 1261 } 1262 1263 rl = op->args[0]; 1264 rh = op->args[1]; 1265 1266 /* The proper opcode is supplied by tcg_opt_gen_mov. */ 1267 op2 = opt_insert_before(ctx, op, 0, 2); 1268 1269 tcg_opt_gen_movi(ctx, op, rl, al); 1270 tcg_opt_gen_movi(ctx, op2, rh, ah); 1271 return true; 1272 } 1273 1274 /* Fold sub2 r,x,i to add2 r,x,-i */ 1275 if (!add && b_const) { 1276 uint64_t bl = arg_info(op->args[4])->val; 1277 uint64_t bh = arg_info(op->args[5])->val; 1278 1279 /* Negate the two parts without assembling and disassembling. */ 1280 bl = -bl; 1281 bh = ~bh + !bl; 1282 1283 op->opc = (ctx->type == TCG_TYPE_I32 1284 ? INDEX_op_add2_i32 : INDEX_op_add2_i64); 1285 op->args[4] = arg_new_constant(ctx, bl); 1286 op->args[5] = arg_new_constant(ctx, bh); 1287 } 1288 return finish_folding(ctx, op); 1289 } 1290 1291 static bool fold_add2(OptContext *ctx, TCGOp *op) 1292 { 1293 /* Note that the high and low parts may be independently swapped. */ 1294 swap_commutative(op->args[0], &op->args[2], &op->args[4]); 1295 swap_commutative(op->args[1], &op->args[3], &op->args[5]); 1296 1297 return fold_addsub2(ctx, op, true); 1298 } 1299 1300 static bool fold_and(OptContext *ctx, TCGOp *op) 1301 { 1302 uint64_t z1, z2, z_mask, s_mask; 1303 TempOptInfo *t1, *t2; 1304 1305 if (fold_const2_commutative(ctx, op) || 1306 fold_xi_to_i(ctx, op, 0) || 1307 fold_xi_to_x(ctx, op, -1) || 1308 fold_xx_to_x(ctx, op)) { 1309 return true; 1310 } 1311 1312 t1 = arg_info(op->args[1]); 1313 t2 = arg_info(op->args[2]); 1314 z1 = t1->z_mask; 1315 z2 = t2->z_mask; 1316 1317 /* 1318 * Known-zeros does not imply known-ones. Therefore unless 1319 * arg2 is constant, we can't infer affected bits from it. 1320 */ 1321 if (ti_is_const(t2) && fold_affected_mask(ctx, op, z1 & ~z2)) { 1322 return true; 1323 } 1324 1325 z_mask = z1 & z2; 1326 1327 /* 1328 * Sign repetitions are perforce all identical, whether they are 1 or 0. 1329 * Bitwise operations preserve the relative quantity of the repetitions. 1330 */ 1331 s_mask = t1->s_mask & t2->s_mask; 1332 1333 return fold_masks_zs(ctx, op, z_mask, s_mask); 1334 } 1335 1336 static bool fold_andc(OptContext *ctx, TCGOp *op) 1337 { 1338 uint64_t z_mask, s_mask; 1339 TempOptInfo *t1, *t2; 1340 1341 if (fold_const2(ctx, op) || 1342 fold_xx_to_i(ctx, op, 0) || 1343 fold_xi_to_x(ctx, op, 0) || 1344 fold_ix_to_not(ctx, op, -1)) { 1345 return true; 1346 } 1347 1348 t1 = arg_info(op->args[1]); 1349 t2 = arg_info(op->args[2]); 1350 z_mask = t1->z_mask; 1351 1352 if (ti_is_const(t2)) { 1353 /* Fold andc r,x,i to and r,x,~i. */ 1354 switch (ctx->type) { 1355 case TCG_TYPE_I32: 1356 case TCG_TYPE_I64: 1357 op->opc = INDEX_op_and; 1358 break; 1359 case TCG_TYPE_V64: 1360 case TCG_TYPE_V128: 1361 case TCG_TYPE_V256: 1362 op->opc = INDEX_op_and_vec; 1363 break; 1364 default: 1365 g_assert_not_reached(); 1366 } 1367 op->args[2] = arg_new_constant(ctx, ~ti_const_val(t2)); 1368 return fold_and(ctx, op); 1369 } 1370 1371 /* 1372 * Known-zeros does not imply known-ones. Therefore unless 1373 * arg2 is constant, we can't infer anything from it. 1374 */ 1375 if (ti_is_const(t2)) { 1376 uint64_t v2 = ti_const_val(t2); 1377 if (fold_affected_mask(ctx, op, z_mask & v2)) { 1378 return true; 1379 } 1380 z_mask &= ~v2; 1381 } 1382 1383 s_mask = t1->s_mask & t2->s_mask; 1384 return fold_masks_zs(ctx, op, z_mask, s_mask); 1385 } 1386 1387 static bool fold_bitsel_vec(OptContext *ctx, TCGOp *op) 1388 { 1389 /* If true and false values are the same, eliminate the cmp. */ 1390 if (args_are_copies(op->args[2], op->args[3])) { 1391 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]); 1392 } 1393 1394 if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) { 1395 uint64_t tv = arg_info(op->args[2])->val; 1396 uint64_t fv = arg_info(op->args[3])->val; 1397 1398 if (tv == -1 && fv == 0) { 1399 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); 1400 } 1401 if (tv == 0 && fv == -1) { 1402 if (TCG_TARGET_HAS_not_vec) { 1403 op->opc = INDEX_op_not_vec; 1404 return fold_not(ctx, op); 1405 } else { 1406 op->opc = INDEX_op_xor_vec; 1407 op->args[2] = arg_new_constant(ctx, -1); 1408 return fold_xor(ctx, op); 1409 } 1410 } 1411 } 1412 if (arg_is_const(op->args[2])) { 1413 uint64_t tv = arg_info(op->args[2])->val; 1414 if (tv == -1) { 1415 op->opc = INDEX_op_or_vec; 1416 op->args[2] = op->args[3]; 1417 return fold_or(ctx, op); 1418 } 1419 if (tv == 0 && TCG_TARGET_HAS_andc_vec) { 1420 op->opc = INDEX_op_andc_vec; 1421 op->args[2] = op->args[1]; 1422 op->args[1] = op->args[3]; 1423 return fold_andc(ctx, op); 1424 } 1425 } 1426 if (arg_is_const(op->args[3])) { 1427 uint64_t fv = arg_info(op->args[3])->val; 1428 if (fv == 0) { 1429 op->opc = INDEX_op_and_vec; 1430 return fold_and(ctx, op); 1431 } 1432 if (fv == -1 && TCG_TARGET_HAS_orc_vec) { 1433 op->opc = INDEX_op_orc_vec; 1434 op->args[2] = op->args[1]; 1435 op->args[1] = op->args[3]; 1436 return fold_orc(ctx, op); 1437 } 1438 } 1439 return finish_folding(ctx, op); 1440 } 1441 1442 static bool fold_brcond(OptContext *ctx, TCGOp *op) 1443 { 1444 int i = do_constant_folding_cond1(ctx, op, NO_DEST, &op->args[0], 1445 &op->args[1], &op->args[2]); 1446 if (i == 0) { 1447 tcg_op_remove(ctx->tcg, op); 1448 return true; 1449 } 1450 if (i > 0) { 1451 op->opc = INDEX_op_br; 1452 op->args[0] = op->args[3]; 1453 finish_ebb(ctx); 1454 } else { 1455 finish_bb(ctx); 1456 } 1457 return true; 1458 } 1459 1460 static bool fold_brcond2(OptContext *ctx, TCGOp *op) 1461 { 1462 TCGCond cond; 1463 TCGArg label; 1464 int i, inv = 0; 1465 1466 i = do_constant_folding_cond2(ctx, op, &op->args[0]); 1467 cond = op->args[4]; 1468 label = op->args[5]; 1469 if (i >= 0) { 1470 goto do_brcond_const; 1471 } 1472 1473 switch (cond) { 1474 case TCG_COND_LT: 1475 case TCG_COND_GE: 1476 /* 1477 * Simplify LT/GE comparisons vs zero to a single compare 1478 * vs the high word of the input. 1479 */ 1480 if (arg_is_const_val(op->args[2], 0) && 1481 arg_is_const_val(op->args[3], 0)) { 1482 goto do_brcond_high; 1483 } 1484 break; 1485 1486 case TCG_COND_NE: 1487 inv = 1; 1488 QEMU_FALLTHROUGH; 1489 case TCG_COND_EQ: 1490 /* 1491 * Simplify EQ/NE comparisons where one of the pairs 1492 * can be simplified. 1493 */ 1494 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[0], 1495 op->args[2], cond); 1496 switch (i ^ inv) { 1497 case 0: 1498 goto do_brcond_const; 1499 case 1: 1500 goto do_brcond_high; 1501 } 1502 1503 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1], 1504 op->args[3], cond); 1505 switch (i ^ inv) { 1506 case 0: 1507 goto do_brcond_const; 1508 case 1: 1509 goto do_brcond_low; 1510 } 1511 break; 1512 1513 case TCG_COND_TSTEQ: 1514 case TCG_COND_TSTNE: 1515 if (arg_is_const_val(op->args[2], 0)) { 1516 goto do_brcond_high; 1517 } 1518 if (arg_is_const_val(op->args[3], 0)) { 1519 goto do_brcond_low; 1520 } 1521 break; 1522 1523 default: 1524 break; 1525 1526 do_brcond_low: 1527 op->opc = INDEX_op_brcond_i32; 1528 op->args[1] = op->args[2]; 1529 op->args[2] = cond; 1530 op->args[3] = label; 1531 return fold_brcond(ctx, op); 1532 1533 do_brcond_high: 1534 op->opc = INDEX_op_brcond_i32; 1535 op->args[0] = op->args[1]; 1536 op->args[1] = op->args[3]; 1537 op->args[2] = cond; 1538 op->args[3] = label; 1539 return fold_brcond(ctx, op); 1540 1541 do_brcond_const: 1542 if (i == 0) { 1543 tcg_op_remove(ctx->tcg, op); 1544 return true; 1545 } 1546 op->opc = INDEX_op_br; 1547 op->args[0] = label; 1548 finish_ebb(ctx); 1549 return true; 1550 } 1551 1552 finish_bb(ctx); 1553 return true; 1554 } 1555 1556 static bool fold_bswap(OptContext *ctx, TCGOp *op) 1557 { 1558 uint64_t z_mask, s_mask, sign; 1559 TempOptInfo *t1 = arg_info(op->args[1]); 1560 1561 if (ti_is_const(t1)) { 1562 return tcg_opt_gen_movi(ctx, op, op->args[0], 1563 do_constant_folding(op->opc, ctx->type, 1564 ti_const_val(t1), 1565 op->args[2])); 1566 } 1567 1568 z_mask = t1->z_mask; 1569 switch (op->opc) { 1570 case INDEX_op_bswap16_i32: 1571 case INDEX_op_bswap16_i64: 1572 z_mask = bswap16(z_mask); 1573 sign = INT16_MIN; 1574 break; 1575 case INDEX_op_bswap32_i32: 1576 case INDEX_op_bswap32_i64: 1577 z_mask = bswap32(z_mask); 1578 sign = INT32_MIN; 1579 break; 1580 case INDEX_op_bswap64_i64: 1581 z_mask = bswap64(z_mask); 1582 sign = INT64_MIN; 1583 break; 1584 default: 1585 g_assert_not_reached(); 1586 } 1587 1588 s_mask = 0; 1589 switch (op->args[2] & (TCG_BSWAP_OZ | TCG_BSWAP_OS)) { 1590 case TCG_BSWAP_OZ: 1591 break; 1592 case TCG_BSWAP_OS: 1593 /* If the sign bit may be 1, force all the bits above to 1. */ 1594 if (z_mask & sign) { 1595 z_mask |= sign; 1596 } 1597 /* The value and therefore s_mask is explicitly sign-extended. */ 1598 s_mask = sign; 1599 break; 1600 default: 1601 /* The high bits are undefined: force all bits above the sign to 1. */ 1602 z_mask |= sign << 1; 1603 break; 1604 } 1605 1606 return fold_masks_zs(ctx, op, z_mask, s_mask); 1607 } 1608 1609 static bool fold_call(OptContext *ctx, TCGOp *op) 1610 { 1611 TCGContext *s = ctx->tcg; 1612 int nb_oargs = TCGOP_CALLO(op); 1613 int nb_iargs = TCGOP_CALLI(op); 1614 int flags, i; 1615 1616 init_arguments(ctx, op, nb_oargs + nb_iargs); 1617 copy_propagate(ctx, op, nb_oargs, nb_iargs); 1618 1619 /* If the function reads or writes globals, reset temp data. */ 1620 flags = tcg_call_flags(op); 1621 if (!(flags & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) { 1622 int nb_globals = s->nb_globals; 1623 1624 for (i = 0; i < nb_globals; i++) { 1625 if (test_bit(i, ctx->temps_used.l)) { 1626 reset_ts(ctx, &ctx->tcg->temps[i]); 1627 } 1628 } 1629 } 1630 1631 /* If the function has side effects, reset mem data. */ 1632 if (!(flags & TCG_CALL_NO_SIDE_EFFECTS)) { 1633 remove_mem_copy_all(ctx); 1634 } 1635 1636 /* Reset temp data for outputs. */ 1637 for (i = 0; i < nb_oargs; i++) { 1638 reset_temp(ctx, op->args[i]); 1639 } 1640 1641 /* Stop optimizing MB across calls. */ 1642 ctx->prev_mb = NULL; 1643 return true; 1644 } 1645 1646 static bool fold_cmp_vec(OptContext *ctx, TCGOp *op) 1647 { 1648 /* Canonicalize the comparison to put immediate second. */ 1649 if (swap_commutative(NO_DEST, &op->args[1], &op->args[2])) { 1650 op->args[3] = tcg_swap_cond(op->args[3]); 1651 } 1652 return finish_folding(ctx, op); 1653 } 1654 1655 static bool fold_cmpsel_vec(OptContext *ctx, TCGOp *op) 1656 { 1657 /* If true and false values are the same, eliminate the cmp. */ 1658 if (args_are_copies(op->args[3], op->args[4])) { 1659 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[3]); 1660 } 1661 1662 /* Canonicalize the comparison to put immediate second. */ 1663 if (swap_commutative(NO_DEST, &op->args[1], &op->args[2])) { 1664 op->args[5] = tcg_swap_cond(op->args[5]); 1665 } 1666 /* 1667 * Canonicalize the "false" input reg to match the destination, 1668 * so that the tcg backend can implement "move if true". 1669 */ 1670 if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) { 1671 op->args[5] = tcg_invert_cond(op->args[5]); 1672 } 1673 return finish_folding(ctx, op); 1674 } 1675 1676 static bool fold_count_zeros(OptContext *ctx, TCGOp *op) 1677 { 1678 uint64_t z_mask, s_mask; 1679 TempOptInfo *t1 = arg_info(op->args[1]); 1680 TempOptInfo *t2 = arg_info(op->args[2]); 1681 1682 if (ti_is_const(t1)) { 1683 uint64_t t = ti_const_val(t1); 1684 1685 if (t != 0) { 1686 t = do_constant_folding(op->opc, ctx->type, t, 0); 1687 return tcg_opt_gen_movi(ctx, op, op->args[0], t); 1688 } 1689 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]); 1690 } 1691 1692 switch (ctx->type) { 1693 case TCG_TYPE_I32: 1694 z_mask = 31; 1695 break; 1696 case TCG_TYPE_I64: 1697 z_mask = 63; 1698 break; 1699 default: 1700 g_assert_not_reached(); 1701 } 1702 s_mask = ~z_mask; 1703 z_mask |= t2->z_mask; 1704 s_mask &= t2->s_mask; 1705 1706 return fold_masks_zs(ctx, op, z_mask, s_mask); 1707 } 1708 1709 static bool fold_ctpop(OptContext *ctx, TCGOp *op) 1710 { 1711 uint64_t z_mask; 1712 1713 if (fold_const1(ctx, op)) { 1714 return true; 1715 } 1716 1717 switch (ctx->type) { 1718 case TCG_TYPE_I32: 1719 z_mask = 32 | 31; 1720 break; 1721 case TCG_TYPE_I64: 1722 z_mask = 64 | 63; 1723 break; 1724 default: 1725 g_assert_not_reached(); 1726 } 1727 return fold_masks_z(ctx, op, z_mask); 1728 } 1729 1730 static bool fold_deposit(OptContext *ctx, TCGOp *op) 1731 { 1732 TempOptInfo *t1 = arg_info(op->args[1]); 1733 TempOptInfo *t2 = arg_info(op->args[2]); 1734 int ofs = op->args[3]; 1735 int len = op->args[4]; 1736 int width = 8 * tcg_type_size(ctx->type); 1737 uint64_t z_mask, s_mask; 1738 1739 if (ti_is_const(t1) && ti_is_const(t2)) { 1740 return tcg_opt_gen_movi(ctx, op, op->args[0], 1741 deposit64(ti_const_val(t1), ofs, len, 1742 ti_const_val(t2))); 1743 } 1744 1745 /* Inserting a value into zero at offset 0. */ 1746 if (ti_is_const_val(t1, 0) && ofs == 0) { 1747 uint64_t mask = MAKE_64BIT_MASK(0, len); 1748 1749 op->opc = INDEX_op_and; 1750 op->args[1] = op->args[2]; 1751 op->args[2] = arg_new_constant(ctx, mask); 1752 return fold_and(ctx, op); 1753 } 1754 1755 /* Inserting zero into a value. */ 1756 if (ti_is_const_val(t2, 0)) { 1757 uint64_t mask = deposit64(-1, ofs, len, 0); 1758 1759 op->opc = INDEX_op_and; 1760 op->args[2] = arg_new_constant(ctx, mask); 1761 return fold_and(ctx, op); 1762 } 1763 1764 /* The s_mask from the top portion of the deposit is still valid. */ 1765 if (ofs + len == width) { 1766 s_mask = t2->s_mask << ofs; 1767 } else { 1768 s_mask = t1->s_mask & ~MAKE_64BIT_MASK(0, ofs + len); 1769 } 1770 1771 z_mask = deposit64(t1->z_mask, ofs, len, t2->z_mask); 1772 return fold_masks_zs(ctx, op, z_mask, s_mask); 1773 } 1774 1775 static bool fold_divide(OptContext *ctx, TCGOp *op) 1776 { 1777 if (fold_const2(ctx, op) || 1778 fold_xi_to_x(ctx, op, 1)) { 1779 return true; 1780 } 1781 return finish_folding(ctx, op); 1782 } 1783 1784 static bool fold_dup(OptContext *ctx, TCGOp *op) 1785 { 1786 if (arg_is_const(op->args[1])) { 1787 uint64_t t = arg_info(op->args[1])->val; 1788 t = dup_const(TCGOP_VECE(op), t); 1789 return tcg_opt_gen_movi(ctx, op, op->args[0], t); 1790 } 1791 return finish_folding(ctx, op); 1792 } 1793 1794 static bool fold_dup2(OptContext *ctx, TCGOp *op) 1795 { 1796 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) { 1797 uint64_t t = deposit64(arg_info(op->args[1])->val, 32, 32, 1798 arg_info(op->args[2])->val); 1799 return tcg_opt_gen_movi(ctx, op, op->args[0], t); 1800 } 1801 1802 if (args_are_copies(op->args[1], op->args[2])) { 1803 op->opc = INDEX_op_dup_vec; 1804 TCGOP_VECE(op) = MO_32; 1805 } 1806 return finish_folding(ctx, op); 1807 } 1808 1809 static bool fold_eqv(OptContext *ctx, TCGOp *op) 1810 { 1811 uint64_t s_mask; 1812 TempOptInfo *t1, *t2; 1813 1814 if (fold_const2_commutative(ctx, op) || 1815 fold_xi_to_x(ctx, op, -1) || 1816 fold_xi_to_not(ctx, op, 0)) { 1817 return true; 1818 } 1819 1820 t2 = arg_info(op->args[2]); 1821 if (ti_is_const(t2)) { 1822 /* Fold eqv r,x,i to xor r,x,~i. */ 1823 switch (ctx->type) { 1824 case TCG_TYPE_I32: 1825 case TCG_TYPE_I64: 1826 op->opc = INDEX_op_xor; 1827 break; 1828 case TCG_TYPE_V64: 1829 case TCG_TYPE_V128: 1830 case TCG_TYPE_V256: 1831 op->opc = INDEX_op_xor_vec; 1832 break; 1833 default: 1834 g_assert_not_reached(); 1835 } 1836 op->args[2] = arg_new_constant(ctx, ~ti_const_val(t2)); 1837 return fold_xor(ctx, op); 1838 } 1839 1840 t1 = arg_info(op->args[1]); 1841 s_mask = t1->s_mask & t2->s_mask; 1842 return fold_masks_s(ctx, op, s_mask); 1843 } 1844 1845 static bool fold_extract(OptContext *ctx, TCGOp *op) 1846 { 1847 uint64_t z_mask_old, z_mask; 1848 TempOptInfo *t1 = arg_info(op->args[1]); 1849 int pos = op->args[2]; 1850 int len = op->args[3]; 1851 1852 if (ti_is_const(t1)) { 1853 return tcg_opt_gen_movi(ctx, op, op->args[0], 1854 extract64(ti_const_val(t1), pos, len)); 1855 } 1856 1857 z_mask_old = t1->z_mask; 1858 z_mask = extract64(z_mask_old, pos, len); 1859 if (pos == 0 && fold_affected_mask(ctx, op, z_mask_old ^ z_mask)) { 1860 return true; 1861 } 1862 1863 return fold_masks_z(ctx, op, z_mask); 1864 } 1865 1866 static bool fold_extract2(OptContext *ctx, TCGOp *op) 1867 { 1868 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) { 1869 uint64_t v1 = arg_info(op->args[1])->val; 1870 uint64_t v2 = arg_info(op->args[2])->val; 1871 int shr = op->args[3]; 1872 1873 if (op->opc == INDEX_op_extract2_i64) { 1874 v1 >>= shr; 1875 v2 <<= 64 - shr; 1876 } else { 1877 v1 = (uint32_t)v1 >> shr; 1878 v2 = (uint64_t)((int32_t)v2 << (32 - shr)); 1879 } 1880 return tcg_opt_gen_movi(ctx, op, op->args[0], v1 | v2); 1881 } 1882 return finish_folding(ctx, op); 1883 } 1884 1885 static bool fold_exts(OptContext *ctx, TCGOp *op) 1886 { 1887 uint64_t s_mask, z_mask; 1888 TempOptInfo *t1; 1889 1890 if (fold_const1(ctx, op)) { 1891 return true; 1892 } 1893 1894 t1 = arg_info(op->args[1]); 1895 z_mask = t1->z_mask; 1896 s_mask = t1->s_mask; 1897 1898 switch (op->opc) { 1899 case INDEX_op_ext_i32_i64: 1900 s_mask |= INT32_MIN; 1901 z_mask = (int32_t)z_mask; 1902 break; 1903 default: 1904 g_assert_not_reached(); 1905 } 1906 return fold_masks_zs(ctx, op, z_mask, s_mask); 1907 } 1908 1909 static bool fold_extu(OptContext *ctx, TCGOp *op) 1910 { 1911 uint64_t z_mask; 1912 1913 if (fold_const1(ctx, op)) { 1914 return true; 1915 } 1916 1917 z_mask = arg_info(op->args[1])->z_mask; 1918 switch (op->opc) { 1919 case INDEX_op_extrl_i64_i32: 1920 case INDEX_op_extu_i32_i64: 1921 z_mask = (uint32_t)z_mask; 1922 break; 1923 case INDEX_op_extrh_i64_i32: 1924 z_mask >>= 32; 1925 break; 1926 default: 1927 g_assert_not_reached(); 1928 } 1929 return fold_masks_z(ctx, op, z_mask); 1930 } 1931 1932 static bool fold_mb(OptContext *ctx, TCGOp *op) 1933 { 1934 /* Eliminate duplicate and redundant fence instructions. */ 1935 if (ctx->prev_mb) { 1936 /* 1937 * Merge two barriers of the same type into one, 1938 * or a weaker barrier into a stronger one, 1939 * or two weaker barriers into a stronger one. 1940 * mb X; mb Y => mb X|Y 1941 * mb; strl => mb; st 1942 * ldaq; mb => ld; mb 1943 * ldaq; strl => ld; mb; st 1944 * Other combinations are also merged into a strong 1945 * barrier. This is stricter than specified but for 1946 * the purposes of TCG is better than not optimizing. 1947 */ 1948 ctx->prev_mb->args[0] |= op->args[0]; 1949 tcg_op_remove(ctx->tcg, op); 1950 } else { 1951 ctx->prev_mb = op; 1952 } 1953 return true; 1954 } 1955 1956 static bool fold_mov(OptContext *ctx, TCGOp *op) 1957 { 1958 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); 1959 } 1960 1961 static bool fold_movcond(OptContext *ctx, TCGOp *op) 1962 { 1963 uint64_t z_mask, s_mask; 1964 TempOptInfo *tt, *ft; 1965 int i; 1966 1967 /* If true and false values are the same, eliminate the cmp. */ 1968 if (args_are_copies(op->args[3], op->args[4])) { 1969 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[3]); 1970 } 1971 1972 /* 1973 * Canonicalize the "false" input reg to match the destination reg so 1974 * that the tcg backend can implement a "move if true" operation. 1975 */ 1976 if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) { 1977 op->args[5] = tcg_invert_cond(op->args[5]); 1978 } 1979 1980 i = do_constant_folding_cond1(ctx, op, NO_DEST, &op->args[1], 1981 &op->args[2], &op->args[5]); 1982 if (i >= 0) { 1983 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[4 - i]); 1984 } 1985 1986 tt = arg_info(op->args[3]); 1987 ft = arg_info(op->args[4]); 1988 z_mask = tt->z_mask | ft->z_mask; 1989 s_mask = tt->s_mask & ft->s_mask; 1990 1991 if (ti_is_const(tt) && ti_is_const(ft)) { 1992 uint64_t tv = ti_const_val(tt); 1993 uint64_t fv = ti_const_val(ft); 1994 TCGOpcode opc, negopc = 0; 1995 TCGCond cond = op->args[5]; 1996 1997 switch (ctx->type) { 1998 case TCG_TYPE_I32: 1999 opc = INDEX_op_setcond_i32; 2000 if (TCG_TARGET_HAS_negsetcond_i32) { 2001 negopc = INDEX_op_negsetcond_i32; 2002 } 2003 tv = (int32_t)tv; 2004 fv = (int32_t)fv; 2005 break; 2006 case TCG_TYPE_I64: 2007 opc = INDEX_op_setcond_i64; 2008 if (TCG_TARGET_HAS_negsetcond_i64) { 2009 negopc = INDEX_op_negsetcond_i64; 2010 } 2011 break; 2012 default: 2013 g_assert_not_reached(); 2014 } 2015 2016 if (tv == 1 && fv == 0) { 2017 op->opc = opc; 2018 op->args[3] = cond; 2019 } else if (fv == 1 && tv == 0) { 2020 op->opc = opc; 2021 op->args[3] = tcg_invert_cond(cond); 2022 } else if (negopc) { 2023 if (tv == -1 && fv == 0) { 2024 op->opc = negopc; 2025 op->args[3] = cond; 2026 } else if (fv == -1 && tv == 0) { 2027 op->opc = negopc; 2028 op->args[3] = tcg_invert_cond(cond); 2029 } 2030 } 2031 } 2032 2033 return fold_masks_zs(ctx, op, z_mask, s_mask); 2034 } 2035 2036 static bool fold_mul(OptContext *ctx, TCGOp *op) 2037 { 2038 if (fold_const2(ctx, op) || 2039 fold_xi_to_i(ctx, op, 0) || 2040 fold_xi_to_x(ctx, op, 1)) { 2041 return true; 2042 } 2043 return finish_folding(ctx, op); 2044 } 2045 2046 static bool fold_mul_highpart(OptContext *ctx, TCGOp *op) 2047 { 2048 if (fold_const2_commutative(ctx, op) || 2049 fold_xi_to_i(ctx, op, 0)) { 2050 return true; 2051 } 2052 return finish_folding(ctx, op); 2053 } 2054 2055 static bool fold_multiply2(OptContext *ctx, TCGOp *op) 2056 { 2057 swap_commutative(op->args[0], &op->args[2], &op->args[3]); 2058 2059 if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) { 2060 uint64_t a = arg_info(op->args[2])->val; 2061 uint64_t b = arg_info(op->args[3])->val; 2062 uint64_t h, l; 2063 TCGArg rl, rh; 2064 TCGOp *op2; 2065 2066 switch (op->opc) { 2067 case INDEX_op_mulu2_i32: 2068 l = (uint64_t)(uint32_t)a * (uint32_t)b; 2069 h = (int32_t)(l >> 32); 2070 l = (int32_t)l; 2071 break; 2072 case INDEX_op_muls2_i32: 2073 l = (int64_t)(int32_t)a * (int32_t)b; 2074 h = l >> 32; 2075 l = (int32_t)l; 2076 break; 2077 case INDEX_op_mulu2_i64: 2078 mulu64(&l, &h, a, b); 2079 break; 2080 case INDEX_op_muls2_i64: 2081 muls64(&l, &h, a, b); 2082 break; 2083 default: 2084 g_assert_not_reached(); 2085 } 2086 2087 rl = op->args[0]; 2088 rh = op->args[1]; 2089 2090 /* The proper opcode is supplied by tcg_opt_gen_mov. */ 2091 op2 = opt_insert_before(ctx, op, 0, 2); 2092 2093 tcg_opt_gen_movi(ctx, op, rl, l); 2094 tcg_opt_gen_movi(ctx, op2, rh, h); 2095 return true; 2096 } 2097 return finish_folding(ctx, op); 2098 } 2099 2100 static bool fold_nand(OptContext *ctx, TCGOp *op) 2101 { 2102 uint64_t s_mask; 2103 2104 if (fold_const2_commutative(ctx, op) || 2105 fold_xi_to_not(ctx, op, -1)) { 2106 return true; 2107 } 2108 2109 s_mask = arg_info(op->args[1])->s_mask 2110 & arg_info(op->args[2])->s_mask; 2111 return fold_masks_s(ctx, op, s_mask); 2112 } 2113 2114 static bool fold_neg_no_const(OptContext *ctx, TCGOp *op) 2115 { 2116 /* Set to 1 all bits to the left of the rightmost. */ 2117 uint64_t z_mask = arg_info(op->args[1])->z_mask; 2118 z_mask = -(z_mask & -z_mask); 2119 2120 return fold_masks_z(ctx, op, z_mask); 2121 } 2122 2123 static bool fold_neg(OptContext *ctx, TCGOp *op) 2124 { 2125 return fold_const1(ctx, op) || fold_neg_no_const(ctx, op); 2126 } 2127 2128 static bool fold_nor(OptContext *ctx, TCGOp *op) 2129 { 2130 uint64_t s_mask; 2131 2132 if (fold_const2_commutative(ctx, op) || 2133 fold_xi_to_not(ctx, op, 0)) { 2134 return true; 2135 } 2136 2137 s_mask = arg_info(op->args[1])->s_mask 2138 & arg_info(op->args[2])->s_mask; 2139 return fold_masks_s(ctx, op, s_mask); 2140 } 2141 2142 static bool fold_not(OptContext *ctx, TCGOp *op) 2143 { 2144 if (fold_const1(ctx, op)) { 2145 return true; 2146 } 2147 return fold_masks_s(ctx, op, arg_info(op->args[1])->s_mask); 2148 } 2149 2150 static bool fold_or(OptContext *ctx, TCGOp *op) 2151 { 2152 uint64_t z_mask, s_mask; 2153 TempOptInfo *t1, *t2; 2154 2155 if (fold_const2_commutative(ctx, op) || 2156 fold_xi_to_x(ctx, op, 0) || 2157 fold_xx_to_x(ctx, op)) { 2158 return true; 2159 } 2160 2161 t1 = arg_info(op->args[1]); 2162 t2 = arg_info(op->args[2]); 2163 z_mask = t1->z_mask | t2->z_mask; 2164 s_mask = t1->s_mask & t2->s_mask; 2165 return fold_masks_zs(ctx, op, z_mask, s_mask); 2166 } 2167 2168 static bool fold_orc(OptContext *ctx, TCGOp *op) 2169 { 2170 uint64_t s_mask; 2171 TempOptInfo *t1, *t2; 2172 2173 if (fold_const2(ctx, op) || 2174 fold_xx_to_i(ctx, op, -1) || 2175 fold_xi_to_x(ctx, op, -1) || 2176 fold_ix_to_not(ctx, op, 0)) { 2177 return true; 2178 } 2179 2180 t2 = arg_info(op->args[2]); 2181 if (ti_is_const(t2)) { 2182 /* Fold orc r,x,i to or r,x,~i. */ 2183 switch (ctx->type) { 2184 case TCG_TYPE_I32: 2185 case TCG_TYPE_I64: 2186 op->opc = INDEX_op_or; 2187 break; 2188 case TCG_TYPE_V64: 2189 case TCG_TYPE_V128: 2190 case TCG_TYPE_V256: 2191 op->opc = INDEX_op_or_vec; 2192 break; 2193 default: 2194 g_assert_not_reached(); 2195 } 2196 op->args[2] = arg_new_constant(ctx, ~ti_const_val(t2)); 2197 return fold_or(ctx, op); 2198 } 2199 2200 t1 = arg_info(op->args[1]); 2201 s_mask = t1->s_mask & t2->s_mask; 2202 return fold_masks_s(ctx, op, s_mask); 2203 } 2204 2205 static bool fold_qemu_ld_1reg(OptContext *ctx, TCGOp *op) 2206 { 2207 const TCGOpDef *def = &tcg_op_defs[op->opc]; 2208 MemOpIdx oi = op->args[def->nb_oargs + def->nb_iargs]; 2209 MemOp mop = get_memop(oi); 2210 int width = 8 * memop_size(mop); 2211 uint64_t z_mask = -1, s_mask = 0; 2212 2213 if (width < 64) { 2214 if (mop & MO_SIGN) { 2215 s_mask = MAKE_64BIT_MASK(width - 1, 64 - (width - 1)); 2216 } else { 2217 z_mask = MAKE_64BIT_MASK(0, width); 2218 } 2219 } 2220 2221 /* Opcodes that touch guest memory stop the mb optimization. */ 2222 ctx->prev_mb = NULL; 2223 2224 return fold_masks_zs(ctx, op, z_mask, s_mask); 2225 } 2226 2227 static bool fold_qemu_ld_2reg(OptContext *ctx, TCGOp *op) 2228 { 2229 /* Opcodes that touch guest memory stop the mb optimization. */ 2230 ctx->prev_mb = NULL; 2231 return finish_folding(ctx, op); 2232 } 2233 2234 static bool fold_qemu_st(OptContext *ctx, TCGOp *op) 2235 { 2236 /* Opcodes that touch guest memory stop the mb optimization. */ 2237 ctx->prev_mb = NULL; 2238 return true; 2239 } 2240 2241 static bool fold_remainder(OptContext *ctx, TCGOp *op) 2242 { 2243 if (fold_const2(ctx, op) || 2244 fold_xx_to_i(ctx, op, 0)) { 2245 return true; 2246 } 2247 return finish_folding(ctx, op); 2248 } 2249 2250 /* Return 1 if finished, -1 if simplified, 0 if unchanged. */ 2251 static int fold_setcond_zmask(OptContext *ctx, TCGOp *op, bool neg) 2252 { 2253 uint64_t a_zmask, b_val; 2254 TCGCond cond; 2255 2256 if (!arg_is_const(op->args[2])) { 2257 return false; 2258 } 2259 2260 a_zmask = arg_info(op->args[1])->z_mask; 2261 b_val = arg_info(op->args[2])->val; 2262 cond = op->args[3]; 2263 2264 if (ctx->type == TCG_TYPE_I32) { 2265 a_zmask = (uint32_t)a_zmask; 2266 b_val = (uint32_t)b_val; 2267 } 2268 2269 /* 2270 * A with only low bits set vs B with high bits set means that A < B. 2271 */ 2272 if (a_zmask < b_val) { 2273 bool inv = false; 2274 2275 switch (cond) { 2276 case TCG_COND_NE: 2277 case TCG_COND_LEU: 2278 case TCG_COND_LTU: 2279 inv = true; 2280 /* fall through */ 2281 case TCG_COND_GTU: 2282 case TCG_COND_GEU: 2283 case TCG_COND_EQ: 2284 return tcg_opt_gen_movi(ctx, op, op->args[0], neg ? -inv : inv); 2285 default: 2286 break; 2287 } 2288 } 2289 2290 /* 2291 * A with only lsb set is already boolean. 2292 */ 2293 if (a_zmask <= 1) { 2294 bool convert = false; 2295 bool inv = false; 2296 2297 switch (cond) { 2298 case TCG_COND_EQ: 2299 inv = true; 2300 /* fall through */ 2301 case TCG_COND_NE: 2302 convert = (b_val == 0); 2303 break; 2304 case TCG_COND_LTU: 2305 case TCG_COND_TSTEQ: 2306 inv = true; 2307 /* fall through */ 2308 case TCG_COND_GEU: 2309 case TCG_COND_TSTNE: 2310 convert = (b_val == 1); 2311 break; 2312 default: 2313 break; 2314 } 2315 if (convert) { 2316 TCGOpcode neg_opc; 2317 2318 if (!inv && !neg) { 2319 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]); 2320 } 2321 2322 switch (ctx->type) { 2323 case TCG_TYPE_I32: 2324 neg_opc = INDEX_op_neg_i32; 2325 break; 2326 case TCG_TYPE_I64: 2327 neg_opc = INDEX_op_neg_i64; 2328 break; 2329 default: 2330 g_assert_not_reached(); 2331 } 2332 2333 if (!inv) { 2334 op->opc = neg_opc; 2335 } else if (neg) { 2336 op->opc = INDEX_op_add; 2337 op->args[2] = arg_new_constant(ctx, -1); 2338 } else { 2339 op->opc = INDEX_op_xor; 2340 op->args[2] = arg_new_constant(ctx, 1); 2341 } 2342 return -1; 2343 } 2344 } 2345 return 0; 2346 } 2347 2348 static void fold_setcond_tst_pow2(OptContext *ctx, TCGOp *op, bool neg) 2349 { 2350 TCGOpcode neg_opc, shr_opc; 2351 TCGOpcode uext_opc = 0, sext_opc = 0; 2352 TCGCond cond = op->args[3]; 2353 TCGArg ret, src1, src2; 2354 TCGOp *op2; 2355 uint64_t val; 2356 int sh; 2357 bool inv; 2358 2359 if (!is_tst_cond(cond) || !arg_is_const(op->args[2])) { 2360 return; 2361 } 2362 2363 src2 = op->args[2]; 2364 val = arg_info(src2)->val; 2365 if (!is_power_of_2(val)) { 2366 return; 2367 } 2368 sh = ctz64(val); 2369 2370 switch (ctx->type) { 2371 case TCG_TYPE_I32: 2372 shr_opc = INDEX_op_shr_i32; 2373 neg_opc = INDEX_op_neg_i32; 2374 if (TCG_TARGET_extract_valid(TCG_TYPE_I32, sh, 1)) { 2375 uext_opc = INDEX_op_extract_i32; 2376 } 2377 if (TCG_TARGET_sextract_valid(TCG_TYPE_I32, sh, 1)) { 2378 sext_opc = INDEX_op_sextract_i32; 2379 } 2380 break; 2381 case TCG_TYPE_I64: 2382 shr_opc = INDEX_op_shr_i64; 2383 neg_opc = INDEX_op_neg_i64; 2384 if (TCG_TARGET_extract_valid(TCG_TYPE_I64, sh, 1)) { 2385 uext_opc = INDEX_op_extract_i64; 2386 } 2387 if (TCG_TARGET_sextract_valid(TCG_TYPE_I64, sh, 1)) { 2388 sext_opc = INDEX_op_sextract_i64; 2389 } 2390 break; 2391 default: 2392 g_assert_not_reached(); 2393 } 2394 2395 ret = op->args[0]; 2396 src1 = op->args[1]; 2397 inv = cond == TCG_COND_TSTEQ; 2398 2399 if (sh && sext_opc && neg && !inv) { 2400 op->opc = sext_opc; 2401 op->args[1] = src1; 2402 op->args[2] = sh; 2403 op->args[3] = 1; 2404 return; 2405 } else if (sh && uext_opc) { 2406 op->opc = uext_opc; 2407 op->args[1] = src1; 2408 op->args[2] = sh; 2409 op->args[3] = 1; 2410 } else { 2411 if (sh) { 2412 op2 = opt_insert_before(ctx, op, shr_opc, 3); 2413 op2->args[0] = ret; 2414 op2->args[1] = src1; 2415 op2->args[2] = arg_new_constant(ctx, sh); 2416 src1 = ret; 2417 } 2418 op->opc = INDEX_op_and; 2419 op->args[1] = src1; 2420 op->args[2] = arg_new_constant(ctx, 1); 2421 } 2422 2423 if (neg && inv) { 2424 op2 = opt_insert_after(ctx, op, INDEX_op_add, 3); 2425 op2->args[0] = ret; 2426 op2->args[1] = ret; 2427 op2->args[2] = arg_new_constant(ctx, -1); 2428 } else if (inv) { 2429 op2 = opt_insert_after(ctx, op, INDEX_op_xor, 3); 2430 op2->args[0] = ret; 2431 op2->args[1] = ret; 2432 op2->args[2] = arg_new_constant(ctx, 1); 2433 } else if (neg) { 2434 op2 = opt_insert_after(ctx, op, neg_opc, 2); 2435 op2->args[0] = ret; 2436 op2->args[1] = ret; 2437 } 2438 } 2439 2440 static bool fold_setcond(OptContext *ctx, TCGOp *op) 2441 { 2442 int i = do_constant_folding_cond1(ctx, op, op->args[0], &op->args[1], 2443 &op->args[2], &op->args[3]); 2444 if (i >= 0) { 2445 return tcg_opt_gen_movi(ctx, op, op->args[0], i); 2446 } 2447 2448 i = fold_setcond_zmask(ctx, op, false); 2449 if (i > 0) { 2450 return true; 2451 } 2452 if (i == 0) { 2453 fold_setcond_tst_pow2(ctx, op, false); 2454 } 2455 2456 return fold_masks_z(ctx, op, 1); 2457 } 2458 2459 static bool fold_negsetcond(OptContext *ctx, TCGOp *op) 2460 { 2461 int i = do_constant_folding_cond1(ctx, op, op->args[0], &op->args[1], 2462 &op->args[2], &op->args[3]); 2463 if (i >= 0) { 2464 return tcg_opt_gen_movi(ctx, op, op->args[0], -i); 2465 } 2466 2467 i = fold_setcond_zmask(ctx, op, true); 2468 if (i > 0) { 2469 return true; 2470 } 2471 if (i == 0) { 2472 fold_setcond_tst_pow2(ctx, op, true); 2473 } 2474 2475 /* Value is {0,-1} so all bits are repetitions of the sign. */ 2476 return fold_masks_s(ctx, op, -1); 2477 } 2478 2479 static bool fold_setcond2(OptContext *ctx, TCGOp *op) 2480 { 2481 TCGCond cond; 2482 int i, inv = 0; 2483 2484 i = do_constant_folding_cond2(ctx, op, &op->args[1]); 2485 cond = op->args[5]; 2486 if (i >= 0) { 2487 goto do_setcond_const; 2488 } 2489 2490 switch (cond) { 2491 case TCG_COND_LT: 2492 case TCG_COND_GE: 2493 /* 2494 * Simplify LT/GE comparisons vs zero to a single compare 2495 * vs the high word of the input. 2496 */ 2497 if (arg_is_const_val(op->args[3], 0) && 2498 arg_is_const_val(op->args[4], 0)) { 2499 goto do_setcond_high; 2500 } 2501 break; 2502 2503 case TCG_COND_NE: 2504 inv = 1; 2505 QEMU_FALLTHROUGH; 2506 case TCG_COND_EQ: 2507 /* 2508 * Simplify EQ/NE comparisons where one of the pairs 2509 * can be simplified. 2510 */ 2511 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1], 2512 op->args[3], cond); 2513 switch (i ^ inv) { 2514 case 0: 2515 goto do_setcond_const; 2516 case 1: 2517 goto do_setcond_high; 2518 } 2519 2520 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[2], 2521 op->args[4], cond); 2522 switch (i ^ inv) { 2523 case 0: 2524 goto do_setcond_const; 2525 case 1: 2526 goto do_setcond_low; 2527 } 2528 break; 2529 2530 case TCG_COND_TSTEQ: 2531 case TCG_COND_TSTNE: 2532 if (arg_is_const_val(op->args[3], 0)) { 2533 goto do_setcond_high; 2534 } 2535 if (arg_is_const_val(op->args[4], 0)) { 2536 goto do_setcond_low; 2537 } 2538 break; 2539 2540 default: 2541 break; 2542 2543 do_setcond_low: 2544 op->args[2] = op->args[3]; 2545 op->args[3] = cond; 2546 op->opc = INDEX_op_setcond_i32; 2547 return fold_setcond(ctx, op); 2548 2549 do_setcond_high: 2550 op->args[1] = op->args[2]; 2551 op->args[2] = op->args[4]; 2552 op->args[3] = cond; 2553 op->opc = INDEX_op_setcond_i32; 2554 return fold_setcond(ctx, op); 2555 } 2556 2557 return fold_masks_z(ctx, op, 1); 2558 2559 do_setcond_const: 2560 return tcg_opt_gen_movi(ctx, op, op->args[0], i); 2561 } 2562 2563 static bool fold_sextract(OptContext *ctx, TCGOp *op) 2564 { 2565 uint64_t z_mask, s_mask, s_mask_old; 2566 TempOptInfo *t1 = arg_info(op->args[1]); 2567 int pos = op->args[2]; 2568 int len = op->args[3]; 2569 2570 if (ti_is_const(t1)) { 2571 return tcg_opt_gen_movi(ctx, op, op->args[0], 2572 sextract64(ti_const_val(t1), pos, len)); 2573 } 2574 2575 s_mask_old = t1->s_mask; 2576 s_mask = s_mask_old >> pos; 2577 s_mask |= -1ull << (len - 1); 2578 2579 if (pos == 0 && fold_affected_mask(ctx, op, s_mask & ~s_mask_old)) { 2580 return true; 2581 } 2582 2583 z_mask = sextract64(t1->z_mask, pos, len); 2584 return fold_masks_zs(ctx, op, z_mask, s_mask); 2585 } 2586 2587 static bool fold_shift(OptContext *ctx, TCGOp *op) 2588 { 2589 uint64_t s_mask, z_mask; 2590 TempOptInfo *t1, *t2; 2591 2592 if (fold_const2(ctx, op) || 2593 fold_ix_to_i(ctx, op, 0) || 2594 fold_xi_to_x(ctx, op, 0)) { 2595 return true; 2596 } 2597 2598 t1 = arg_info(op->args[1]); 2599 t2 = arg_info(op->args[2]); 2600 s_mask = t1->s_mask; 2601 z_mask = t1->z_mask; 2602 2603 if (ti_is_const(t2)) { 2604 int sh = ti_const_val(t2); 2605 2606 z_mask = do_constant_folding(op->opc, ctx->type, z_mask, sh); 2607 s_mask = do_constant_folding(op->opc, ctx->type, s_mask, sh); 2608 2609 return fold_masks_zs(ctx, op, z_mask, s_mask); 2610 } 2611 2612 switch (op->opc) { 2613 CASE_OP_32_64(sar): 2614 /* 2615 * Arithmetic right shift will not reduce the number of 2616 * input sign repetitions. 2617 */ 2618 return fold_masks_s(ctx, op, s_mask); 2619 CASE_OP_32_64(shr): 2620 /* 2621 * If the sign bit is known zero, then logical right shift 2622 * will not reduce the number of input sign repetitions. 2623 */ 2624 if (~z_mask & -s_mask) { 2625 return fold_masks_s(ctx, op, s_mask); 2626 } 2627 break; 2628 default: 2629 break; 2630 } 2631 2632 return finish_folding(ctx, op); 2633 } 2634 2635 static bool fold_sub_to_neg(OptContext *ctx, TCGOp *op) 2636 { 2637 TCGOpcode neg_op; 2638 bool have_neg; 2639 2640 if (!arg_is_const(op->args[1]) || arg_info(op->args[1])->val != 0) { 2641 return false; 2642 } 2643 2644 switch (ctx->type) { 2645 case TCG_TYPE_I32: 2646 neg_op = INDEX_op_neg_i32; 2647 have_neg = true; 2648 break; 2649 case TCG_TYPE_I64: 2650 neg_op = INDEX_op_neg_i64; 2651 have_neg = true; 2652 break; 2653 case TCG_TYPE_V64: 2654 case TCG_TYPE_V128: 2655 case TCG_TYPE_V256: 2656 neg_op = INDEX_op_neg_vec; 2657 have_neg = (TCG_TARGET_HAS_neg_vec && 2658 tcg_can_emit_vec_op(neg_op, ctx->type, TCGOP_VECE(op)) > 0); 2659 break; 2660 default: 2661 g_assert_not_reached(); 2662 } 2663 if (have_neg) { 2664 op->opc = neg_op; 2665 op->args[1] = op->args[2]; 2666 return fold_neg_no_const(ctx, op); 2667 } 2668 return false; 2669 } 2670 2671 /* We cannot as yet do_constant_folding with vectors. */ 2672 static bool fold_sub_vec(OptContext *ctx, TCGOp *op) 2673 { 2674 if (fold_xx_to_i(ctx, op, 0) || 2675 fold_xi_to_x(ctx, op, 0) || 2676 fold_sub_to_neg(ctx, op)) { 2677 return true; 2678 } 2679 return finish_folding(ctx, op); 2680 } 2681 2682 static bool fold_sub(OptContext *ctx, TCGOp *op) 2683 { 2684 if (fold_const2(ctx, op) || 2685 fold_xx_to_i(ctx, op, 0) || 2686 fold_xi_to_x(ctx, op, 0) || 2687 fold_sub_to_neg(ctx, op)) { 2688 return true; 2689 } 2690 2691 /* Fold sub r,x,i to add r,x,-i */ 2692 if (arg_is_const(op->args[2])) { 2693 uint64_t val = arg_info(op->args[2])->val; 2694 2695 op->opc = INDEX_op_add; 2696 op->args[2] = arg_new_constant(ctx, -val); 2697 } 2698 return finish_folding(ctx, op); 2699 } 2700 2701 static bool fold_sub2(OptContext *ctx, TCGOp *op) 2702 { 2703 return fold_addsub2(ctx, op, false); 2704 } 2705 2706 static bool fold_tcg_ld(OptContext *ctx, TCGOp *op) 2707 { 2708 uint64_t z_mask = -1, s_mask = 0; 2709 2710 /* We can't do any folding with a load, but we can record bits. */ 2711 switch (op->opc) { 2712 CASE_OP_32_64(ld8s): 2713 s_mask = INT8_MIN; 2714 break; 2715 CASE_OP_32_64(ld8u): 2716 z_mask = MAKE_64BIT_MASK(0, 8); 2717 break; 2718 CASE_OP_32_64(ld16s): 2719 s_mask = INT16_MIN; 2720 break; 2721 CASE_OP_32_64(ld16u): 2722 z_mask = MAKE_64BIT_MASK(0, 16); 2723 break; 2724 case INDEX_op_ld32s_i64: 2725 s_mask = INT32_MIN; 2726 break; 2727 case INDEX_op_ld32u_i64: 2728 z_mask = MAKE_64BIT_MASK(0, 32); 2729 break; 2730 default: 2731 g_assert_not_reached(); 2732 } 2733 return fold_masks_zs(ctx, op, z_mask, s_mask); 2734 } 2735 2736 static bool fold_tcg_ld_memcopy(OptContext *ctx, TCGOp *op) 2737 { 2738 TCGTemp *dst, *src; 2739 intptr_t ofs; 2740 TCGType type; 2741 2742 if (op->args[1] != tcgv_ptr_arg(tcg_env)) { 2743 return finish_folding(ctx, op); 2744 } 2745 2746 type = ctx->type; 2747 ofs = op->args[2]; 2748 dst = arg_temp(op->args[0]); 2749 src = find_mem_copy_for(ctx, type, ofs); 2750 if (src && src->base_type == type) { 2751 return tcg_opt_gen_mov(ctx, op, temp_arg(dst), temp_arg(src)); 2752 } 2753 2754 reset_ts(ctx, dst); 2755 record_mem_copy(ctx, type, dst, ofs, ofs + tcg_type_size(type) - 1); 2756 return true; 2757 } 2758 2759 static bool fold_tcg_st(OptContext *ctx, TCGOp *op) 2760 { 2761 intptr_t ofs = op->args[2]; 2762 intptr_t lm1; 2763 2764 if (op->args[1] != tcgv_ptr_arg(tcg_env)) { 2765 remove_mem_copy_all(ctx); 2766 return true; 2767 } 2768 2769 switch (op->opc) { 2770 CASE_OP_32_64(st8): 2771 lm1 = 0; 2772 break; 2773 CASE_OP_32_64(st16): 2774 lm1 = 1; 2775 break; 2776 case INDEX_op_st32_i64: 2777 case INDEX_op_st_i32: 2778 lm1 = 3; 2779 break; 2780 case INDEX_op_st_i64: 2781 lm1 = 7; 2782 break; 2783 case INDEX_op_st_vec: 2784 lm1 = tcg_type_size(ctx->type) - 1; 2785 break; 2786 default: 2787 g_assert_not_reached(); 2788 } 2789 remove_mem_copy_in(ctx, ofs, ofs + lm1); 2790 return true; 2791 } 2792 2793 static bool fold_tcg_st_memcopy(OptContext *ctx, TCGOp *op) 2794 { 2795 TCGTemp *src; 2796 intptr_t ofs, last; 2797 TCGType type; 2798 2799 if (op->args[1] != tcgv_ptr_arg(tcg_env)) { 2800 return fold_tcg_st(ctx, op); 2801 } 2802 2803 src = arg_temp(op->args[0]); 2804 ofs = op->args[2]; 2805 type = ctx->type; 2806 2807 /* 2808 * Eliminate duplicate stores of a constant. 2809 * This happens frequently when the target ISA zero-extends. 2810 */ 2811 if (ts_is_const(src)) { 2812 TCGTemp *prev = find_mem_copy_for(ctx, type, ofs); 2813 if (src == prev) { 2814 tcg_op_remove(ctx->tcg, op); 2815 return true; 2816 } 2817 } 2818 2819 last = ofs + tcg_type_size(type) - 1; 2820 remove_mem_copy_in(ctx, ofs, last); 2821 record_mem_copy(ctx, type, src, ofs, last); 2822 return true; 2823 } 2824 2825 static bool fold_xor(OptContext *ctx, TCGOp *op) 2826 { 2827 uint64_t z_mask, s_mask; 2828 TempOptInfo *t1, *t2; 2829 2830 if (fold_const2_commutative(ctx, op) || 2831 fold_xx_to_i(ctx, op, 0) || 2832 fold_xi_to_x(ctx, op, 0) || 2833 fold_xi_to_not(ctx, op, -1)) { 2834 return true; 2835 } 2836 2837 t1 = arg_info(op->args[1]); 2838 t2 = arg_info(op->args[2]); 2839 z_mask = t1->z_mask | t2->z_mask; 2840 s_mask = t1->s_mask & t2->s_mask; 2841 return fold_masks_zs(ctx, op, z_mask, s_mask); 2842 } 2843 2844 /* Propagate constants and copies, fold constant expressions. */ 2845 void tcg_optimize(TCGContext *s) 2846 { 2847 int nb_temps, i; 2848 TCGOp *op, *op_next; 2849 OptContext ctx = { .tcg = s }; 2850 2851 QSIMPLEQ_INIT(&ctx.mem_free); 2852 2853 /* Array VALS has an element for each temp. 2854 If this temp holds a constant then its value is kept in VALS' element. 2855 If this temp is a copy of other ones then the other copies are 2856 available through the doubly linked circular list. */ 2857 2858 nb_temps = s->nb_temps; 2859 for (i = 0; i < nb_temps; ++i) { 2860 s->temps[i].state_ptr = NULL; 2861 } 2862 2863 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) { 2864 TCGOpcode opc = op->opc; 2865 const TCGOpDef *def; 2866 bool done = false; 2867 2868 /* Calls are special. */ 2869 if (opc == INDEX_op_call) { 2870 fold_call(&ctx, op); 2871 continue; 2872 } 2873 2874 def = &tcg_op_defs[opc]; 2875 init_arguments(&ctx, op, def->nb_oargs + def->nb_iargs); 2876 copy_propagate(&ctx, op, def->nb_oargs, def->nb_iargs); 2877 2878 /* Pre-compute the type of the operation. */ 2879 ctx.type = TCGOP_TYPE(op); 2880 2881 /* 2882 * Process each opcode. 2883 * Sorted alphabetically by opcode as much as possible. 2884 */ 2885 switch (opc) { 2886 case INDEX_op_add: 2887 done = fold_add(&ctx, op); 2888 break; 2889 case INDEX_op_add_vec: 2890 done = fold_add_vec(&ctx, op); 2891 break; 2892 CASE_OP_32_64(add2): 2893 done = fold_add2(&ctx, op); 2894 break; 2895 case INDEX_op_and: 2896 case INDEX_op_and_vec: 2897 done = fold_and(&ctx, op); 2898 break; 2899 case INDEX_op_andc: 2900 case INDEX_op_andc_vec: 2901 done = fold_andc(&ctx, op); 2902 break; 2903 CASE_OP_32_64(brcond): 2904 done = fold_brcond(&ctx, op); 2905 break; 2906 case INDEX_op_brcond2_i32: 2907 done = fold_brcond2(&ctx, op); 2908 break; 2909 CASE_OP_32_64(bswap16): 2910 CASE_OP_32_64(bswap32): 2911 case INDEX_op_bswap64_i64: 2912 done = fold_bswap(&ctx, op); 2913 break; 2914 CASE_OP_32_64(clz): 2915 CASE_OP_32_64(ctz): 2916 done = fold_count_zeros(&ctx, op); 2917 break; 2918 CASE_OP_32_64(ctpop): 2919 done = fold_ctpop(&ctx, op); 2920 break; 2921 CASE_OP_32_64(deposit): 2922 done = fold_deposit(&ctx, op); 2923 break; 2924 CASE_OP_32_64(div): 2925 CASE_OP_32_64(divu): 2926 done = fold_divide(&ctx, op); 2927 break; 2928 case INDEX_op_dup_vec: 2929 done = fold_dup(&ctx, op); 2930 break; 2931 case INDEX_op_dup2_vec: 2932 done = fold_dup2(&ctx, op); 2933 break; 2934 case INDEX_op_eqv: 2935 case INDEX_op_eqv_vec: 2936 done = fold_eqv(&ctx, op); 2937 break; 2938 CASE_OP_32_64(extract): 2939 done = fold_extract(&ctx, op); 2940 break; 2941 CASE_OP_32_64(extract2): 2942 done = fold_extract2(&ctx, op); 2943 break; 2944 case INDEX_op_ext_i32_i64: 2945 done = fold_exts(&ctx, op); 2946 break; 2947 case INDEX_op_extu_i32_i64: 2948 case INDEX_op_extrl_i64_i32: 2949 case INDEX_op_extrh_i64_i32: 2950 done = fold_extu(&ctx, op); 2951 break; 2952 CASE_OP_32_64(ld8s): 2953 CASE_OP_32_64(ld8u): 2954 CASE_OP_32_64(ld16s): 2955 CASE_OP_32_64(ld16u): 2956 case INDEX_op_ld32s_i64: 2957 case INDEX_op_ld32u_i64: 2958 done = fold_tcg_ld(&ctx, op); 2959 break; 2960 case INDEX_op_ld_i32: 2961 case INDEX_op_ld_i64: 2962 case INDEX_op_ld_vec: 2963 done = fold_tcg_ld_memcopy(&ctx, op); 2964 break; 2965 CASE_OP_32_64(st8): 2966 CASE_OP_32_64(st16): 2967 case INDEX_op_st32_i64: 2968 done = fold_tcg_st(&ctx, op); 2969 break; 2970 case INDEX_op_st_i32: 2971 case INDEX_op_st_i64: 2972 case INDEX_op_st_vec: 2973 done = fold_tcg_st_memcopy(&ctx, op); 2974 break; 2975 case INDEX_op_mb: 2976 done = fold_mb(&ctx, op); 2977 break; 2978 case INDEX_op_mov: 2979 case INDEX_op_mov_vec: 2980 done = fold_mov(&ctx, op); 2981 break; 2982 CASE_OP_32_64(movcond): 2983 done = fold_movcond(&ctx, op); 2984 break; 2985 CASE_OP_32_64(mul): 2986 done = fold_mul(&ctx, op); 2987 break; 2988 CASE_OP_32_64(mulsh): 2989 CASE_OP_32_64(muluh): 2990 done = fold_mul_highpart(&ctx, op); 2991 break; 2992 CASE_OP_32_64(muls2): 2993 CASE_OP_32_64(mulu2): 2994 done = fold_multiply2(&ctx, op); 2995 break; 2996 case INDEX_op_nand: 2997 case INDEX_op_nand_vec: 2998 done = fold_nand(&ctx, op); 2999 break; 3000 CASE_OP_32_64(neg): 3001 done = fold_neg(&ctx, op); 3002 break; 3003 CASE_OP_32_64_VEC(nor): 3004 done = fold_nor(&ctx, op); 3005 break; 3006 CASE_OP_32_64_VEC(not): 3007 done = fold_not(&ctx, op); 3008 break; 3009 case INDEX_op_or: 3010 case INDEX_op_or_vec: 3011 done = fold_or(&ctx, op); 3012 break; 3013 case INDEX_op_orc: 3014 case INDEX_op_orc_vec: 3015 done = fold_orc(&ctx, op); 3016 break; 3017 case INDEX_op_qemu_ld_i32: 3018 done = fold_qemu_ld_1reg(&ctx, op); 3019 break; 3020 case INDEX_op_qemu_ld_i64: 3021 if (TCG_TARGET_REG_BITS == 64) { 3022 done = fold_qemu_ld_1reg(&ctx, op); 3023 break; 3024 } 3025 QEMU_FALLTHROUGH; 3026 case INDEX_op_qemu_ld_i128: 3027 done = fold_qemu_ld_2reg(&ctx, op); 3028 break; 3029 case INDEX_op_qemu_st8_i32: 3030 case INDEX_op_qemu_st_i32: 3031 case INDEX_op_qemu_st_i64: 3032 case INDEX_op_qemu_st_i128: 3033 done = fold_qemu_st(&ctx, op); 3034 break; 3035 CASE_OP_32_64(rem): 3036 CASE_OP_32_64(remu): 3037 done = fold_remainder(&ctx, op); 3038 break; 3039 CASE_OP_32_64(rotl): 3040 CASE_OP_32_64(rotr): 3041 CASE_OP_32_64(sar): 3042 CASE_OP_32_64(shl): 3043 CASE_OP_32_64(shr): 3044 done = fold_shift(&ctx, op); 3045 break; 3046 CASE_OP_32_64(setcond): 3047 done = fold_setcond(&ctx, op); 3048 break; 3049 CASE_OP_32_64(negsetcond): 3050 done = fold_negsetcond(&ctx, op); 3051 break; 3052 case INDEX_op_setcond2_i32: 3053 done = fold_setcond2(&ctx, op); 3054 break; 3055 case INDEX_op_cmp_vec: 3056 done = fold_cmp_vec(&ctx, op); 3057 break; 3058 case INDEX_op_cmpsel_vec: 3059 done = fold_cmpsel_vec(&ctx, op); 3060 break; 3061 case INDEX_op_bitsel_vec: 3062 done = fold_bitsel_vec(&ctx, op); 3063 break; 3064 CASE_OP_32_64(sextract): 3065 done = fold_sextract(&ctx, op); 3066 break; 3067 CASE_OP_32_64(sub): 3068 done = fold_sub(&ctx, op); 3069 break; 3070 case INDEX_op_sub_vec: 3071 done = fold_sub_vec(&ctx, op); 3072 break; 3073 CASE_OP_32_64(sub2): 3074 done = fold_sub2(&ctx, op); 3075 break; 3076 case INDEX_op_xor: 3077 case INDEX_op_xor_vec: 3078 done = fold_xor(&ctx, op); 3079 break; 3080 case INDEX_op_set_label: 3081 case INDEX_op_br: 3082 case INDEX_op_exit_tb: 3083 case INDEX_op_goto_tb: 3084 case INDEX_op_goto_ptr: 3085 finish_ebb(&ctx); 3086 done = true; 3087 break; 3088 default: 3089 done = finish_folding(&ctx, op); 3090 break; 3091 } 3092 tcg_debug_assert(done); 3093 } 3094 } 3095