GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cpython/Python/optimizer_bytecodes.c at main · python/cpython Expand file tree/ Copy pathoptimizer_bytecodes.cMore file actions 2777 lines (2526 loc) · 97.1 KB / Copy pathoptimizer_bytecodes.cFile metadata and controls2777 lines (2526 loc) · 97.1 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 #include "Python.h" #include "pycore_call.h" // _Py_METH_CALL_FLAGS #include "pycore_long.h" #include "pycore_opcode_utils.h" #include "pycore_optimizer.h" #include "pycore_stackref.h" #include "pycore_typeobject.h" #include "pycore_uops.h" #include "pycore_uop_ids.h" #include "internal/pycore_moduleobject.h" #define op(name, ...) /* NAME is ignored */ typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame; /* Shortened forms for convenience */ #define sym_is_not_null _Py_uop_sym_is_not_null #define sym_is_const _Py_uop_sym_is_const #define sym_get_const _Py_uop_sym_get_const #define sym_new_unknown _Py_uop_sym_new_unknown #define sym_new_not_null _Py_uop_sym_new_not_null #define sym_new_type _Py_uop_sym_new_type #define sym_is_null _Py_uop_sym_is_null #define sym_new_const _Py_uop_sym_new_const #define sym_new_null _Py_uop_sym_new_null #define sym_matches_type _Py_uop_sym_matches_type #define sym_matches_type_version _Py_uop_sym_matches_type_version #define sym_get_type_version _Py_uop_sym_get_type_version #define sym_get_type _Py_uop_sym_get_type #define sym_has_type _Py_uop_sym_has_type #define sym_set_null(SYM) _Py_uop_sym_set_null(ctx, SYM) #define sym_set_non_null(SYM) _Py_uop_sym_set_non_null(ctx, SYM) #define sym_set_type(SYM, TYPE) _Py_uop_sym_set_type(ctx, SYM, TYPE) #define sym_set_type_version(SYM, VERSION) _Py_uop_sym_set_type_version(ctx, SYM, VERSION) #define sym_set_const(SYM, CNST) _Py_uop_sym_set_const(ctx, SYM, CNST) #define sym_set_compact_int(SYM) _Py_uop_sym_set_compact_int(ctx, SYM) #define sym_is_bottom _Py_uop_sym_is_bottom #define frame_new _Py_uop_frame_new #define frame_new_from_symbol _Py_uop_frame_new_from_symbol #define frame_pop _Py_uop_frame_pop #define sym_new_tuple _Py_uop_sym_new_tuple #define sym_tuple_getitem _Py_uop_sym_tuple_getitem #define sym_tuple_length _Py_uop_sym_tuple_length #define sym_is_immortal _Py_uop_symbol_is_immortal #define sym_new_compact_int _Py_uop_sym_new_compact_int #define sym_is_compact_int _Py_uop_sym_is_compact_int #define sym_new_truthiness _Py_uop_sym_new_truthiness #define sym_new_predicate _Py_uop_sym_new_predicate #define sym_apply_predicate_narrowing _Py_uop_sym_apply_predicate_narrowing #define sym_set_recorded_type(SYM, TYPE) _Py_uop_sym_set_recorded_type(ctx, SYM, TYPE) #define sym_set_recorded_value(SYM, VAL) _Py_uop_sym_set_recorded_value(ctx, SYM, VAL) #define sym_set_recorded_gen_func(SYM, VAL) _Py_uop_sym_set_recorded_gen_func(ctx, SYM, VAL) #define sym_get_probable_func_code _Py_uop_sym_get_probable_func_code #define sym_get_probable_value _Py_uop_sym_get_probable_value #define sym_get_probable_type _Py_uop_sym_get_probable_type #define sym_set_stack_depth(DEPTH, SP) _Py_uop_sym_set_stack_depth(ctx, DEPTH, SP) extern int optimize_to_bool( _PyUOpInstruction *this_instr, JitOptContext *ctx, JitOptSymbol *value, JitOptSymbol **result_ptr, uint16_t prefix, uint16_t suffix); extern void eliminate_pop_guard(_PyUOpInstruction *this_instr, JitOptContext *ctx, bool exit); extern PyCodeObject *get_code(_PyUOpInstruction *op); static int dummy_func(void) { PyCodeObject *co; int oparg; JitOptSymbol *flag; JitOptSymbol *left; JitOptSymbol *right; JitOptSymbol *value; JitOptSymbol *res; JitOptSymbol *iter; JitOptSymbol *top; JitOptSymbol *bottom; _Py_UOpsAbstractFrame *frame; _Py_UOpsAbstractFrame *new_frame; JitOptContext *ctx; _PyUOpInstruction *this_instr; _PyBloomFilter *dependencies; int modified; int curr_space; int max_space; _PyUOpInstruction *first_valid_check_stack; _PyUOpInstruction *corresponding_check_stack; // BEGIN BYTECODES // op(_MAKE_HEAP_SAFE, (value -- value)) { // eliminate _MAKE_HEAP_SAFE when we *know* the value is immortal if (sym_is_immortal(PyJitRef_Unwrap(value))) { ADD_OP(_NOP, 0, 0); } value = PyJitRef_StripBorrowInfo(value); } op(_COPY_FREE_VARS, (--)) { PyCodeObject *co = get_current_code_object(ctx); if (co == NULL) { ctx->done = true; break; } int offset = co->co_nlocalsplus - oparg; for (int i = 0; i < oparg; ++i) { ctx->frame->locals[offset + i] = sym_new_not_null(ctx); } } op(_LOAD_FAST_CHECK, (-- value)) { value = GETLOCAL(oparg); // We guarantee this will error - just bail and don't optimize it. if (sym_is_null(value)) { ctx->done = true; } assert(!PyJitRef_IsUnique(value)); } op(_LOAD_FAST, (-- value)) { value = GETLOCAL(oparg); assert(!PyJitRef_IsUnique(value)); } op(_LOAD_FAST_BORROW, (-- value)) { value = PyJitRef_Borrow(GETLOCAL(oparg)); assert(!PyJitRef_IsUnique(value)); } op(_LOAD_FAST_AND_CLEAR, (-- value)) { value = GETLOCAL(oparg); JitOptRef temp = sym_new_null(ctx); GETLOCAL(oparg) = temp; assert(!PyJitRef_IsUnique(value)); } op(_GUARD_TYPE_VERSION_LOCKED, (type_version/2, owner -- owner)) { assert(type_version); if (sym_matches_type_version(owner, type_version)) { ADD_OP(_NOP, 0, 0); } else { PyTypeObject *probable_type = sym_get_probable_type(owner); if (probable_type != NULL && probable_type->tp_version_tag == type_version) { // Promote the probable type version to a known one. sym_set_type(owner, probable_type); sym_set_type_version(owner, type_version); watch_type(probable_type, dependencies); } else { ctx->contradiction = true; ctx->done = true; break; } } } op(_STORE_ATTR_INSTANCE_VALUE, (offset/1, value, owner -- o)) { (void)offset; (void)value; o = owner; } op(_STORE_ATTR_WITH_HINT, (hint/1, value, owner -- o)) { (void)hint; (void)value; o = owner; } op(_SWAP_FAST, (value -- trash)) { JitOptRef tmp = GETLOCAL(oparg); GETLOCAL(oparg) = PyJitRef_RemoveUnique(value); trash = tmp; } op(_STORE_SUBSCR_LIST_INT, (value, list_st, sub_st -- ls, ss)) { (void)value; ls = list_st; ss = sub_st; } op(_STORE_ATTR_SLOT, (index/1, value, owner -- o)) { (void)index; (void)value; o = owner; } op(_STORE_SUBSCR_DICT, (value, dict_st, sub -- st)) { PyObject *sub_o = sym_get_const(ctx, sub); if (sub_o != NULL) { optimize_dict_known_hash(ctx, dependencies, this_instr, sub_o, _STORE_SUBSCR_DICT_KNOWN_HASH); } (void)value; st = dict_st; } op(_PUSH_NULL, (-- res)) { res = PyJitRef_Borrow(sym_new_null(ctx)); } op(_GUARD_TOS_INT, (value -- value)) { if (sym_is_compact_int(value)) { ADD_OP(_NOP, 0, 0); } else { if (sym_get_type(value) == &PyLong_Type) { ADD_OP(_GUARD_TOS_OVERFLOWED, 0, 0); } sym_set_compact_int(value); } } op(_GUARD_TOS_EXACT_INT, (value -- value)) { if (sym_matches_type(value, &PyLong_Type)) { ADD_OP(_NOP, 0, 0); } sym_set_type(value, &PyLong_Type); } op(_GUARD_NOS_INT, (left, unused -- left, unused)) { if (sym_is_compact_int(left)) { ADD_OP(_NOP, 0, 0); } else { if (sym_get_type(left) == &PyLong_Type) { ADD_OP(_GUARD_NOS_OVERFLOWED, 0, 0); } sym_set_compact_int(left); } } op(_CHECK_ATTR_CLASS, (type_version/2, owner -- owner)) { PyObject *type = sym_get_probable_value(owner); if (type != NULL && ((PyTypeObject *)type)->tp_version_tag == type_version) { if (type == sym_get_const(ctx, owner)) { ADD_OP(_NOP, 0, 0); } else { sym_set_const(owner, type); watch_type((PyTypeObject *)type, dependencies); } } } op(_GUARD_TYPE_VERSION, (type_version/2, owner -- owner)) { assert(type_version); assert(this_instr[-1].opcode == _RECORD_TOS_TYPE || this_instr[-1].opcode == _RECORD_TOS); if (sym_matches_type_version(owner, type_version)) { ADD_OP(_NOP, 0, 0); } else { PyTypeObject *probable_type = sym_get_probable_type(owner); if (probable_type != NULL && probable_type->tp_version_tag == type_version) { sym_set_type(owner, probable_type); sym_set_type_version(owner, type_version); watch_type(probable_type, dependencies); } else { ctx->contradiction = true; ctx->done = true; break; } } } op(_GUARD_TOS_FLOAT, (value -- value)) { if (sym_matches_type(value, &PyFloat_Type)) { ADD_OP(_NOP, 0, 0); } sym_set_type(value, &PyFloat_Type); } op(_GUARD_NOS_FLOAT, (left, unused -- left, unused)) { if (sym_matches_type(left, &PyFloat_Type)) { ADD_OP(_NOP, 0, 0); } sym_set_type(left, &PyFloat_Type); } op(_BINARY_OP, (lhs, rhs -- res, l, r)) { l = lhs; r = rhs; REPLACE_OPCODE_IF_EVALUATES_PURE(lhs, rhs, res); bool lhs_int = sym_matches_type(lhs, &PyLong_Type); bool rhs_int = sym_matches_type(rhs, &PyLong_Type); bool lhs_float = sym_matches_type(lhs, &PyFloat_Type); bool rhs_float = sym_matches_type(rhs, &PyFloat_Type); bool is_truediv = (oparg == NB_TRUE_DIVIDE || oparg == NB_INPLACE_TRUE_DIVIDE); bool is_remainder = (oparg == NB_REMAINDER || oparg == NB_INPLACE_REMAINDER); int emit_op = _BINARY_OP; // Promote probable-float operands to known floats via speculative // guards. _RECORD_TOS_TYPE / _RECORD_NOS_TYPE in the BINARY_OP macro // record the observed operand type during tracing, which // sym_get_probable_type reads here. Applied only to ops where // narrowing unlocks a meaningful downstream win: // - NB_TRUE_DIVIDE: enables the specialized float path below. // - NB_REMAINDER: lets the float result type propagate. // NB_POWER is excluded: speculative guards there regressed // test_power_type_depends_on_input_values (GH-127844). if (is_truediv || is_remainder) { if (!sym_has_type(rhs) && sym_get_probable_type(rhs) == &PyFloat_Type) { ADD_OP(_GUARD_TOS_FLOAT, 0, 0); sym_set_type(rhs, &PyFloat_Type); rhs_float = true; } if (!sym_has_type(lhs) && sym_get_probable_type(lhs) == &PyFloat_Type) { ADD_OP(_GUARD_NOS_FLOAT, 0, 0); sym_set_type(lhs, &PyFloat_Type); lhs_float = true; } } if (is_truediv && lhs_float && rhs_float) { if (PyJitRef_IsUnique(lhs)) { emit_op = _BINARY_OP_TRUEDIV_FLOAT_INPLACE; l = sym_new_null(ctx); r = rhs; } else if (PyJitRef_IsUnique(rhs)) { emit_op = _BINARY_OP_TRUEDIV_FLOAT_INPLACE_RIGHT; l = lhs; r = sym_new_null(ctx); } else { emit_op = _BINARY_OP_TRUEDIV_FLOAT; l = lhs; r = rhs; } res = PyJitRef_MakeUnique(sym_new_type(ctx, &PyFloat_Type)); } else if (is_truediv && (lhs_int || lhs_float) && (rhs_int || rhs_float)) { res = PyJitRef_MakeUnique(sym_new_type(ctx, &PyFloat_Type)); } else if (!((lhs_int || lhs_float) && (rhs_int || rhs_float))) { // There's something other than an int or float involved: res = sym_new_unknown(ctx); } else if (oparg == NB_POWER || oparg == NB_INPLACE_POWER) { // This one's fun... the *type* of the result depends on the // *values* being exponentiated. However, exponents with one // constant part are reasonably common, so it's probably worth // trying to infer some simple cases: // - A: 1 ** 1 -> 1 (int ** int -> int) // - B: 1 ** -1 -> 1.0 (int ** int -> float) // - C: 1.0 ** 1 -> 1.0 (float ** int -> float) // - D: 1 ** 1.0 -> 1.0 (int ** float -> float) // - E: -1 ** 0.5 ~> 1j (int ** float -> complex) // - F: 1.0 ** 1.0 -> 1.0 (float ** float -> float) // - G: -1.0 ** 0.5 ~> 1j (float ** float -> complex) if (rhs_float) { // Case D, E, F, or G... can't know without the sign of the LHS // or whether the RHS is whole, which isn't worth the effort: res = sym_new_unknown(ctx); } else if (lhs_float) { // Case C: res = PyJitRef_MakeUnique(sym_new_type(ctx, &PyFloat_Type)); } else if (!sym_is_const(ctx, rhs)) { // Case A or B... can't know without the sign of the RHS: res = sym_new_unknown(ctx); } else if (_PyLong_IsNegative((PyLongObject *)sym_get_const(ctx, rhs))) { // Case B: res = PyJitRef_MakeUnique(sym_new_type(ctx, &PyFloat_Type)); } else { // Case A: res = sym_new_type(ctx, &PyLong_Type); } } else if (lhs_int && rhs_int) { res = sym_new_type(ctx, &PyLong_Type); } else { res = PyJitRef_MakeUnique(sym_new_type(ctx, &PyFloat_Type)); } ADD_OP(emit_op, oparg, 0); } op(_BINARY_OP_ADD_INT, (left, right -- res, l, r)) { if (PyJitRef_IsUnique(left)) { REPLACE_OP(this_instr, _BINARY_OP_ADD_INT_INPLACE, 0, 0); } else if (PyJitRef_IsUnique(right)) { REPLACE_OP(this_instr, _BINARY_OP_ADD_INT_INPLACE_RIGHT, 0, 0); } // Result may be a unique compact int or a cached small int // at runtime. Mark as unique; inplace ops verify at runtime. res = PyJitRef_MakeUnique(sym_new_compact_int(ctx)); l = left; r = right; REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, res); } op(_BINARY_OP_SUBTRACT_INT, (left, right -- res, l, r)) { if (PyJitRef_IsUnique(left)) { REPLACE_OP(this_instr, _BINARY_OP_SUBTRACT_INT_INPLACE, 0, 0); } else if (PyJitRef_IsUnique(right)) { REPLACE_OP(this_instr, _BINARY_OP_SUBTRACT_INT_INPLACE_RIGHT, 0, 0); } res = PyJitRef_MakeUnique(sym_new_compact_int(ctx)); l = left; r = right; REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, res); } op(_BINARY_OP_MULTIPLY_INT, (left, right -- res, l, r)) { if (PyJitRef_IsUnique(left)) { REPLACE_OP(this_instr, _BINARY_OP_MULTIPLY_INT_INPLACE, 0, 0); } else if (PyJitRef_IsUnique(right)) { REPLACE_OP(this_instr, _BINARY_OP_MULTIPLY_INT_INPLACE_RIGHT, 0, 0); } res = PyJitRef_MakeUnique(sym_new_compact_int(ctx)); l = left; r = right; REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, res); } op(_BINARY_OP_ADD_FLOAT, (left, right -- res, l, r)) { if (PyJitRef_IsUnique(left)) { ADD_OP(_BINARY_OP_ADD_FLOAT_INPLACE, 0, 0); l = PyJitRef_Borrow(sym_new_null(ctx)); r = right; } else if (PyJitRef_IsUnique(right)) { ADD_OP(_BINARY_OP_ADD_FLOAT_INPLACE_RIGHT, 0, 0); l = left; r = PyJitRef_Borrow(sym_new_null(ctx)); } else { l = left; r = right; } res = PyJitRef_MakeUnique(sym_new_type(ctx, &PyFloat_Type)); } op(_BINARY_OP_SUBTRACT_FLOAT, (left, right -- res, l, r)) { if (PyJitRef_IsUnique(left)) { ADD_OP(_BINARY_OP_SUBTRACT_FLOAT_INPLACE, 0, 0); l = PyJitRef_Borrow(sym_new_null(ctx)); r = right; } else if (PyJitRef_IsUnique(right)) { ADD_OP(_BINARY_OP_SUBTRACT_FLOAT_INPLACE_RIGHT, 0, 0); l = left; r = PyJitRef_Borrow(sym_new_null(ctx)); } else { l = left; r = right; } res = PyJitRef_MakeUnique(sym_new_type(ctx, &PyFloat_Type)); } op(_BINARY_OP_MULTIPLY_FLOAT, (left, right -- res, l, r)) { if (PyJitRef_IsUnique(left)) { ADD_OP(_BINARY_OP_MULTIPLY_FLOAT_INPLACE, 0, 0); l = PyJitRef_Borrow(sym_new_null(ctx)); r = right; } else if (PyJitRef_IsUnique(right)) { ADD_OP(_BINARY_OP_MULTIPLY_FLOAT_INPLACE_RIGHT, 0, 0); l = left; r = PyJitRef_Borrow(sym_new_null(ctx)); } else { l = left; r = right; } res = PyJitRef_MakeUnique(sym_new_type(ctx, &PyFloat_Type)); } op(_BINARY_OP_ADD_UNICODE, (left, right -- res, l, r)) { res = sym_new_type(ctx, &PyUnicode_Type); l = left; r = right; } op(_GUARD_BINARY_OP_EXTEND_LHS, (descr/4, left, right -- left, right)) { _PyBinaryOpSpecializationDescr *d = (_PyBinaryOpSpecializationDescr *)descr; assert(d != NULL && d->guard == NULL && d->lhs_type != NULL); if (sym_matches_type(left, d->lhs_type)) { ADD_OP(_NOP, 0, 0); } sym_set_type(left, d->lhs_type); } op(_GUARD_BINARY_OP_EXTEND_RHS, (descr/4, left, right -- left, right)) { _PyBinaryOpSpecializationDescr *d = (_PyBinaryOpSpecializationDescr *)descr; assert(d != NULL && d->guard == NULL && d->rhs_type != NULL); if (sym_matches_type(right, d->rhs_type)) { ADD_OP(_NOP, 0, 0); } sym_set_type(right, d->rhs_type); } op(_GUARD_BINARY_OP_EXTEND, (descr/4, left, right -- left, right)) { _PyBinaryOpSpecializationDescr *d = (_PyBinaryOpSpecializationDescr *)descr; if (d != NULL && d->guard == NULL) { /* guard == NULL means the check is purely a type test against lhs_type/rhs_type, so eliminate it when types are already known. */ assert(d->lhs_type != NULL && d->rhs_type != NULL); bool lhs_known = sym_matches_type(left, d->lhs_type); bool rhs_known = sym_matches_type(right, d->rhs_type); if (lhs_known && rhs_known) { ADD_OP(_NOP, 0, 0); } else if (lhs_known) { ADD_OP(_GUARD_BINARY_OP_EXTEND_RHS, 0, (uintptr_t)d); sym_set_type(right, d->rhs_type); } else if (rhs_known) { ADD_OP(_GUARD_BINARY_OP_EXTEND_LHS, 0, (uintptr_t)d); sym_set_type(left, d->lhs_type); } } } op(_BINARY_OP_EXTEND, (descr/4, left, right -- res, l, r)) { _PyBinaryOpSpecializationDescr *d = (_PyBinaryOpSpecializationDescr *)descr; if (d != NULL && d->result_type != NULL) { res = sym_new_type(ctx, d->result_type); if (d->result_unique) { res = PyJitRef_MakeUnique(res); } } else { res = sym_new_not_null(ctx); } l = left; r = right; } op(_BINARY_OP_INPLACE_ADD_UNICODE, (left, right -- res)) { if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) { assert(PyUnicode_CheckExact(sym_get_const(ctx, left))); assert(PyUnicode_CheckExact(sym_get_const(ctx, right))); PyObject *temp = PyUnicode_Concat(sym_get_const(ctx, left), sym_get_const(ctx, right)); if (temp == NULL) { goto error; } res = sym_new_const(ctx, temp); Py_DECREF(temp); } else { res = sym_new_type(ctx, &PyUnicode_Type); } GETLOCAL(this_instr->operand0) = sym_new_null(ctx); } op(_BINARY_OP_SUBSCR_CHECK_FUNC, (container, unused -- container, unused, getitem)) { getitem = sym_new_not_null(ctx); PyTypeObject *tp = sym_get_type(container); if (tp == NULL) { PyObject *c = sym_get_probable_value(container); if (c != NULL) { tp = Py_TYPE(c); } } if (tp != NULL) { PyObject *getitem_o = ((PyHeapTypeObject *)tp)->_spec_cache.getitem; sym_set_recorded_value(getitem, getitem_o); } } op(_BINARY_OP_SUBSCR_INIT_CALL, (container, sub, getitem -- new_frame)) { _Py_UOpsAbstractFrame *f = frame_new_from_symbol(ctx, getitem, NULL, 0); if (f == NULL) { break; } f->locals[0] = container; f->locals[1] = sub; new_frame = PyJitRef_WrapInvalid(f); } op(_BINARY_OP_SUBSCR_STR_INT, (str_st, sub_st -- res, s, i)) { res = sym_new_type(ctx, &PyUnicode_Type); s = str_st; i = sub_st; } op(_BINARY_OP_SUBSCR_USTR_INT, (str_st, sub_st -- res, s, i)) { res = sym_new_type(ctx, &PyUnicode_Type); s = str_st; i = sub_st; } op(_GUARD_BINARY_OP_SUBSCR_TUPLE_INT_BOUNDS, (tuple_st, sub_st -- tuple_st, sub_st)) { assert(sym_matches_type(tuple_st, &PyTuple_Type)); if (sym_is_const(ctx, sub_st)) { assert(PyLong_CheckExact(sym_get_const(ctx, sub_st))); long index = PyLong_AsLong(sym_get_const(ctx, sub_st)); assert(index >= 0); Py_ssize_t tuple_length = sym_tuple_length(tuple_st); if (tuple_length != -1 && index < tuple_length) { ADD_OP(_NOP, 0, 0); } } } op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res, ts, ss)) { assert(sym_matches_type(tuple_st, &PyTuple_Type)); if (sym_is_const(ctx, sub_st)) { assert(PyLong_CheckExact(sym_get_const(ctx, sub_st))); long index = PyLong_AsLong(sym_get_const(ctx, sub_st)); assert(index >= 0); Py_ssize_t tuple_length = sym_tuple_length(tuple_st); if (tuple_length == -1) { // Unknown length res = sym_new_not_null(ctx); } else { assert(index < tuple_length); res = sym_tuple_getitem(ctx, tuple_st, index); } } else { res = sym_new_not_null(ctx); } ts = tuple_st; ss = sub_st; } op(_BINARY_OP_SUBSCR_DICT, (dict_st, sub_st -- res, ds, ss)) { res = sym_new_not_null(ctx); ds = dict_st; ss = sub_st; PyObject *sub = sym_get_const(ctx, sub_st); if (sym_is_not_container(sub_st) && sym_matches_type(dict_st, &PyFrozenDict_Type)) { REPLACE_OPCODE_IF_EVALUATES_PURE(dict_st, sub_st, res); } else if (sub != NULL) { optimize_dict_known_hash(ctx, dependencies, this_instr, sub, _BINARY_OP_SUBSCR_DICT_KNOWN_HASH); } } op(_BINARY_OP_SUBSCR_LIST_SLICE, (list_st, sub_st -- res, ls, ss)) { res = sym_new_type(ctx, &PyList_Type); ls = list_st; ss = sub_st; } op(_TO_BOOL, (value -- res)) { int already_bool = optimize_to_bool(this_instr, ctx, value, &res, _POP_TOP, _NOP); if (!already_bool) { res = sym_new_truthiness(ctx, value, true); } } op(_TO_BOOL_BOOL, (value -- value)) { int already_bool = optimize_to_bool(this_instr, ctx, value, &value, _POP_TOP, _NOP); if (!already_bool) { sym_set_type(value, &PyBool_Type); } } op(_TO_BOOL_INT, (value -- res, v)) { int already_bool = optimize_to_bool(this_instr, ctx, value, &res, _NOP, _SWAP); if (!already_bool) { sym_set_type(value, &PyLong_Type); res = sym_new_truthiness(ctx, value, true); } v = value; } op(_TO_BOOL_LIST, (value -- res, v)) { int already_bool = optimize_to_bool(this_instr, ctx, value, &res, _NOP, _SWAP); if (!already_bool) { res = sym_new_type(ctx, &PyBool_Type); } v = value; } op(_TO_BOOL_NONE, (value -- res)) { int already_bool = optimize_to_bool(this_instr, ctx, value, &res, _POP_TOP, _NOP); if (!already_bool) { sym_set_const(value, Py_None); res = sym_new_const(ctx, Py_False); } } op(_GUARD_NOS_UNICODE, (nos, unused -- nos, unused)) { if (sym_matches_type(nos, &PyUnicode_Type)) { ADD_OP(_NOP, 0, 0); } sym_set_type(nos, &PyUnicode_Type); } op(_GUARD_NOS_COMPACT_ASCII, (nos, unused -- nos, unused)) { sym_set_type(nos, &PyUnicode_Type); } op(_GUARD_TOS_UNICODE, (value -- value)) { if (sym_matches_type(value, &PyUnicode_Type)) { ADD_OP(_NOP, 0, 0); } sym_set_type(value, &PyUnicode_Type); } op(_TO_BOOL_STR, (value -- res, v)) { int already_bool = optimize_to_bool(this_instr, ctx, value, &res, _NOP, _SWAP); v = value; if (!already_bool) { res = sym_new_truthiness(ctx, value, true); } } op(_UNARY_NOT, (value -- res)) { REPLACE_OPCODE_IF_EVALUATES_PURE(value, res); sym_set_type(value, &PyBool_Type); res = sym_new_truthiness(ctx, value, false); } op(_UNARY_NEGATIVE, (value -- res, v)) { v = value; REPLACE_OPCODE_IF_EVALUATES_PURE(value, res); if (sym_matches_type(value, &PyFloat_Type) && PyJitRef_IsUnique(value)) { ADD_OP(_UNARY_NEGATIVE_FLOAT_INPLACE, 0, 0); v = PyJitRef_Borrow(sym_new_null(ctx)); res = PyJitRef_MakeUnique(sym_new_type(ctx, &PyFloat_Type)); } else if (sym_is_compact_int(value)) { res = sym_new_compact_int(ctx); } else { PyTypeObject *type = sym_get_type(value); if (type == &PyLong_Type || type == &PyFloat_Type) { res = sym_new_type(ctx, type); } else { res = sym_new_not_null(ctx); } } } op(_UNARY_INVERT, (value -- res, v)) { v = value; // Required to avoid a warning due to the deprecation of bitwise inversion of bools if (!sym_matches_type(value, &PyBool_Type)) { REPLACE_OPCODE_IF_EVALUATES_PURE(value, res); } if (sym_matches_type(value, &PyLong_Type)) { res = sym_new_type(ctx, &PyLong_Type); } else { res = sym_new_not_null(ctx); } } op(_COMPARE_OP, (left, right -- res)) { // Comparison between bytes and str or int is not impacted by this optimization as bytes // is not a safe type (due to its ability to raise a warning during comparisons). REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, res); if (oparg & 16) { res = sym_new_type(ctx, &PyBool_Type); } else { res = _Py_uop_sym_new_not_null(ctx); } } op(_COMPARE_OP_INT, (left, right -- res, l, r)) { int cmp_mask = oparg & (COMPARE_LT_MASK | COMPARE_GT_MASK | COMPARE_EQ_MASK); if (cmp_mask == COMPARE_EQ_MASK) { res = sym_new_predicate(ctx, left, right, JIT_PRED_EQ); } else if (cmp_mask == (COMPARE_LT_MASK | COMPARE_GT_MASK)) { res = sym_new_predicate(ctx, left, right, JIT_PRED_NE); } else { res = sym_new_type(ctx, &PyBool_Type); } l = left; r = right; REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, res); } op(_COMPARE_OP_FLOAT, (left, right -- res, l, r)) { int cmp_mask = oparg & (COMPARE_LT_MASK | COMPARE_GT_MASK | COMPARE_EQ_MASK); if (cmp_mask == COMPARE_EQ_MASK) { res = sym_new_predicate(ctx, left, right, JIT_PRED_EQ); } else if (cmp_mask == (COMPARE_LT_MASK | COMPARE_GT_MASK)) { res = sym_new_predicate(ctx, left, right, JIT_PRED_NE); } else { res = sym_new_type(ctx, &PyBool_Type); } l = left; r = right; REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, res); } op(_COMPARE_OP_STR, (left, right -- res, l, r)) { /* Cannot use predicate optimization here, as `a == b` * does not imply that `a` is equivalent to `b`. `a` may be * mortal, while `b` is immortal */ res = sym_new_type(ctx, &PyBool_Type); l = left; r = right; REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, res); } op(_IS_OP, (left, right -- b, l, r)) { b = sym_new_predicate(ctx, left, right, (oparg ? JIT_PRED_IS_NOT : JIT_PRED_IS)); l = left; r = right; } op(_IS_NONE, (value -- b)) { if (sym_is_const(ctx, value)) { PyObject *value_o = sym_get_const(ctx, value); b = sym_new_const(ctx, Py_IsNone(value_o) ? Py_True : Py_False); } else { b = sym_new_type(ctx, &PyBool_Type); } } op(_CONTAINS_OP, (left, right -- b, l, r)) { b = sym_new_type(ctx, &PyBool_Type); l = left; r = right; REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, b); } op(_CONTAINS_OP_SET, (left, right -- b, l, r)) { b = sym_new_type(ctx, &PyBool_Type); l = left; r = right; if (sym_is_not_container(left) && sym_matches_type(right, &PyFrozenSet_Type)) { REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, b); } } op(_CONTAINS_OP_DICT, (left, right -- b, l, r)) { b = sym_new_type(ctx, &PyBool_Type); l = left; r = right; if (sym_is_not_container(left) && sym_matches_type(right, &PyFrozenDict_Type)) { REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, b); } } op(_LOAD_CONST, (-- value)) { PyCodeObject *co = get_current_code_object(ctx); PyObject *val = PyTuple_GET_ITEM(co->co_consts, oparg); ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)val); value = PyJitRef_Borrow(sym_new_const(ctx, val)); } op(_LOAD_COMMON_CONSTANT, (-- value)) { assert(oparg < NUM_COMMON_CONSTANTS); PyObject *val = PyStackRef_AsPyObjectBorrow( _PyInterpreterState_GET()->common_consts[oparg]); assert(_Py_IsImmortal(val)); ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)val); value = PyJitRef_Borrow(sym_new_const(ctx, val)); } op(_LOAD_SMALL_INT, (-- value)) { PyObject *val = PyLong_FromLong(oparg); assert(val); assert(_Py_IsImmortal(val)); ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)val); value = PyJitRef_Borrow(sym_new_const(ctx, val)); } op(_LOAD_CONST_INLINE, (ptr/4 -- value)) { value = sym_new_const(ctx, ptr); } op(_LOAD_CONST_INLINE_BORROW, (ptr/4 -- value)) { value = PyJitRef_Borrow(sym_new_const(ctx, ptr)); } op(_POP_TOP_OPARG, (args[oparg] --)) { for (int i = oparg-1; i >= 0; i--) { optimize_pop_top(ctx, this_instr, args[i]); } } op(_POP_TOP, (value -- )) { optimize_pop_top(ctx, this_instr, value); } op(_POP_TOP_INT, (value --)) { if (PyJitRef_IsBorrowed(value)) { ADD_OP(_POP_TOP_NOP, 0, 0); } } op(_POP_TOP_FLOAT, (value --)) { if (PyJitRef_IsBorrowed(value)) { ADD_OP(_POP_TOP_NOP, 0, 0); } } op(_POP_TOP_UNICODE, (value --)) { if (PyJitRef_IsBorrowed(value)) { ADD_OP(_POP_TOP_NOP, 0, 0); } } op(_COPY, (bottom, unused[oparg-1] -- bottom, unused[oparg-1], top)) { assert(oparg > 0); bottom = PyJitRef_RemoveUnique(bottom); top = bottom; } op(_SWAP, (bottom, unused[oparg-2], top -- bottom, unused[oparg-2], top)) { JitOptRef temp = bottom; bottom = top; top = temp; assert(oparg >= 2); } op(_RROT_3, (bottom, middle, top -- bottom, middle, top)) { JitOptRef temp = top; top = middle; middle = bottom; bottom = temp; } op(_LOAD_ATTR_INSTANCE_VALUE, (offset/1, owner -- attr, o)) { attr = sym_new_not_null(ctx); (void)offset; o = owner; } op(_LOAD_ATTR_MODULE, (dict_version/2, index/1, owner -- attr, o)) { (void)dict_version; (void)index; attr = PyJitRef_NULL; if (sym_is_const(ctx, owner)) { PyModuleObject *mod = (PyModuleObject *)sym_get_const(ctx, owner); if (PyModule_CheckExact(mod)) { PyObject *dict = mod->md_dict; uint64_t watched_mutations = get_mutations(dict); if (watched_mutations < _Py_MAX_ALLOWED_GLOBALS_MODIFICATIONS) { PyDict_Watch(GLOBALS_WATCHER_ID, dict); _Py_BloomFilter_Add(dependencies, dict); PyObject *res = convert_global_to_const(this_instr, dict); if (res == NULL) { attr = sym_new_not_null(ctx); } else { bool immortal = _Py_IsImmortal(res); ADD_OP(immortal ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE, 0, (uintptr_t)res); ADD_OP(_SWAP, 2, 0); attr = sym_new_const(ctx, res); } } } } if (PyJitRef_IsNull(attr)) { /* No conversion made. We don't know what `attr` is. */ attr = sym_new_not_null(ctx); } o = owner; } op (_PUSH_NULL_CONDITIONAL, ( -- null[oparg & 1])) { if (oparg & 1) { ADD_OP(_PUSH_NULL, 0, 0); null[0] = sym_new_null(ctx); } else { ADD_OP(_NOP, 0, 0); } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |