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