[TO_BOOL_INT] = { true, INSTR_FMT_IXC00, HAS_EXIT_FLAG | HAS_ESCAPES_FLAG },
[TO_BOOL_LIST] = { true, INSTR_FMT_IXC00, HAS_EXIT_FLAG | HAS_ESCAPES_FLAG },
[TO_BOOL_NONE] = { true, INSTR_FMT_IXC00, HAS_EXIT_FLAG },
- [TO_BOOL_STR] = { true, INSTR_FMT_IXC00, HAS_EXIT_FLAG | HAS_ESCAPES_FLAG },
+ [TO_BOOL_STR] = { true, INSTR_FMT_IXC00, HAS_EXIT_FLAG },
[TRACE_RECORD] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG },
[UNARY_INVERT] = { true, INSTR_FMT_IX, HAS_ERROR_FLAG | HAS_ESCAPES_FLAG },
[UNARY_NEGATIVE] = { true, INSTR_FMT_IX, HAS_ERROR_FLAG | HAS_ESCAPES_FLAG },
[TO_BOOL_INT] = { .nuops = 1, .uops = { { _TO_BOOL_INT, OPARG_SIMPLE, 3 } } },
[TO_BOOL_LIST] = { .nuops = 2, .uops = { { _GUARD_TOS_LIST, OPARG_SIMPLE, 0 }, { _TO_BOOL_LIST, OPARG_SIMPLE, 3 } } },
[TO_BOOL_NONE] = { .nuops = 1, .uops = { { _TO_BOOL_NONE, OPARG_SIMPLE, 3 } } },
- [TO_BOOL_STR] = { .nuops = 2, .uops = { { _GUARD_TOS_UNICODE, OPARG_SIMPLE, 0 }, { _TO_BOOL_STR, OPARG_SIMPLE, 3 } } },
+ [TO_BOOL_STR] = { .nuops = 3, .uops = { { _GUARD_TOS_UNICODE, OPARG_SIMPLE, 0 }, { _TO_BOOL_STR, OPARG_SIMPLE, 3 }, { _POP_TOP_UNICODE, OPARG_SIMPLE, 3 } } },
[UNARY_INVERT] = { .nuops = 1, .uops = { { _UNARY_INVERT, OPARG_SIMPLE, 0 } } },
[UNARY_NEGATIVE] = { .nuops = 1, .uops = { { _UNARY_NEGATIVE, OPARG_SIMPLE, 0 } } },
[UNARY_NOT] = { .nuops = 1, .uops = { { _UNARY_NOT, OPARG_SIMPLE, 0 } } },
#define _TO_BOOL_NONE_r11 1353
#define _TO_BOOL_NONE_r22 1354
#define _TO_BOOL_NONE_r33 1355
-#define _TO_BOOL_STR_r11 1356
-#define _TRACE_RECORD_r00 1357
-#define _UNARY_INVERT_r11 1358
-#define _UNARY_NEGATIVE_r11 1359
-#define _UNARY_NOT_r01 1360
-#define _UNARY_NOT_r11 1361
-#define _UNARY_NOT_r22 1362
-#define _UNARY_NOT_r33 1363
-#define _UNPACK_EX_r10 1364
-#define _UNPACK_SEQUENCE_r10 1365
-#define _UNPACK_SEQUENCE_LIST_r10 1366
-#define _UNPACK_SEQUENCE_TUPLE_r10 1367
-#define _UNPACK_SEQUENCE_TWO_TUPLE_r12 1368
-#define _WITH_EXCEPT_START_r33 1369
-#define _YIELD_VALUE_r11 1370
-#define MAX_UOP_REGS_ID 1370
+#define _TO_BOOL_STR_r02 1356
+#define _TO_BOOL_STR_r12 1357
+#define _TO_BOOL_STR_r23 1358
+#define _TRACE_RECORD_r00 1359
+#define _UNARY_INVERT_r11 1360
+#define _UNARY_NEGATIVE_r11 1361
+#define _UNARY_NOT_r01 1362
+#define _UNARY_NOT_r11 1363
+#define _UNARY_NOT_r22 1364
+#define _UNARY_NOT_r33 1365
+#define _UNPACK_EX_r10 1366
+#define _UNPACK_SEQUENCE_r10 1367
+#define _UNPACK_SEQUENCE_LIST_r10 1368
+#define _UNPACK_SEQUENCE_TUPLE_r10 1369
+#define _UNPACK_SEQUENCE_TWO_TUPLE_r12 1370
+#define _WITH_EXCEPT_START_r33 1371
+#define _YIELD_VALUE_r11 1372
+#define MAX_UOP_REGS_ID 1372
#ifdef __cplusplus
}
[_GUARD_NOS_COMPACT_ASCII] = HAS_EXIT_FLAG,
[_GUARD_NOS_UNICODE] = HAS_EXIT_FLAG,
[_GUARD_TOS_UNICODE] = HAS_EXIT_FLAG,
- [_TO_BOOL_STR] = HAS_ESCAPES_FLAG,
+ [_TO_BOOL_STR] = 0,
[_REPLACE_WITH_TRUE] = 0,
[_UNARY_INVERT] = HAS_ERROR_FLAG | HAS_ESCAPES_FLAG,
[_GUARD_NOS_INT] = HAS_EXIT_FLAG,
},
},
[_TO_BOOL_STR] = {
- .best = { 1, 1, 1, 1 },
+ .best = { 0, 1, 2, 2 },
.entries = {
- { -1, -1, -1 },
- { 1, 1, _TO_BOOL_STR_r11 },
- { -1, -1, -1 },
+ { 2, 0, _TO_BOOL_STR_r02 },
+ { 2, 1, _TO_BOOL_STR_r12 },
+ { 3, 2, _TO_BOOL_STR_r23 },
{ -1, -1, -1 },
},
},
[_GUARD_TOS_UNICODE_r11] = _GUARD_TOS_UNICODE,
[_GUARD_TOS_UNICODE_r22] = _GUARD_TOS_UNICODE,
[_GUARD_TOS_UNICODE_r33] = _GUARD_TOS_UNICODE,
- [_TO_BOOL_STR_r11] = _TO_BOOL_STR,
+ [_TO_BOOL_STR_r02] = _TO_BOOL_STR,
+ [_TO_BOOL_STR_r12] = _TO_BOOL_STR,
+ [_TO_BOOL_STR_r23] = _TO_BOOL_STR,
[_REPLACE_WITH_TRUE_r02] = _REPLACE_WITH_TRUE,
[_REPLACE_WITH_TRUE_r12] = _REPLACE_WITH_TRUE,
[_REPLACE_WITH_TRUE_r23] = _REPLACE_WITH_TRUE,
[_TO_BOOL_NONE_r22] = "_TO_BOOL_NONE_r22",
[_TO_BOOL_NONE_r33] = "_TO_BOOL_NONE_r33",
[_TO_BOOL_STR] = "_TO_BOOL_STR",
- [_TO_BOOL_STR_r11] = "_TO_BOOL_STR_r11",
+ [_TO_BOOL_STR_r02] = "_TO_BOOL_STR_r02",
+ [_TO_BOOL_STR_r12] = "_TO_BOOL_STR_r12",
+ [_TO_BOOL_STR_r23] = "_TO_BOOL_STR_r23",
[_UNARY_INVERT] = "_UNARY_INVERT",
[_UNARY_INVERT_r11] = "_UNARY_INVERT_r11",
[_UNARY_NEGATIVE] = "_UNARY_NEGATIVE",
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 1)
self.assertIn("_POP_TOP_NOP", uops)
+ def test_to_bool_str(self):
+ def f(n):
+ for i in range(n):
+ false = i == TIER2_THRESHOLD
+ empty = "X"[:false]
+ if empty:
+ return 1
+ return 0
+
+ res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
+ self.assertEqual(res, 0)
+ self.assertIsNotNone(ex)
+ uops = get_opnames(ex)
+ self.assertIn("_TO_BOOL_STR", uops)
+ self.assertLessEqual(count_ops(ex, "_POP_TOP"), 3)
+ self.assertIn("_POP_TOP_NOP", uops)
+
def test_to_bool_always_true(self):
def testfunc(n):
class A:
--- /dev/null
+Eliminate redundant refcounting from ``TO_BOOL_STR``.
EXIT_IF(!PyUnicode_CheckExact(value_o));
}
- op(_TO_BOOL_STR, (value -- res)) {
+ op(_TO_BOOL_STR, (value -- res, v)) {
STAT_INC(TO_BOOL, hit);
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
- if (value_o == &_Py_STR(empty)) {
- assert(_Py_IsImmortal(value_o));
- DEAD(value);
- res = PyStackRef_False;
- }
- else {
- assert(Py_SIZE(value_o));
- PyStackRef_CLOSE(value);
- res = PyStackRef_True;
- }
+ res = value_o == &_Py_STR(empty) ? PyStackRef_False : PyStackRef_True;
+ v = value;
+ DEAD(value);
}
macro(TO_BOOL_STR) =
- _GUARD_TOS_UNICODE + unused/1 + unused/2 + _TO_BOOL_STR;
+ _GUARD_TOS_UNICODE + unused/1 + unused/2 + _TO_BOOL_STR + _POP_TOP_UNICODE;
op(_REPLACE_WITH_TRUE, (value -- res, v)) {
res = PyStackRef_True;
break;
}
- case _TO_BOOL_STR_r11: {
+ case _TO_BOOL_STR_r02: {
+ CHECK_CURRENT_CACHED_VALUES(0);
+ assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
+ _PyStackRef value;
+ _PyStackRef res;
+ _PyStackRef v;
+ value = stack_pointer[-1];
+ STAT_INC(TO_BOOL, hit);
+ PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
+ res = value_o == &_Py_STR(empty) ? PyStackRef_False : PyStackRef_True;
+ v = value;
+ _tos_cache1 = v;
+ _tos_cache0 = res;
+ SET_CURRENT_CACHED_VALUES(2);
+ stack_pointer += -1;
+ ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
+ assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
+ break;
+ }
+
+ case _TO_BOOL_STR_r12: {
CHECK_CURRENT_CACHED_VALUES(1);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef value;
_PyStackRef res;
+ _PyStackRef v;
_PyStackRef _stack_item_0 = _tos_cache0;
value = _stack_item_0;
STAT_INC(TO_BOOL, hit);
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
- if (value_o == &_Py_STR(empty)) {
- assert(_Py_IsImmortal(value_o));
- res = PyStackRef_False;
- }
- else {
- assert(Py_SIZE(value_o));
- _PyFrame_SetStackPointer(frame, stack_pointer);
- PyStackRef_CLOSE(value);
- stack_pointer = _PyFrame_GetStackPointer(frame);
- res = PyStackRef_True;
- }
+ res = value_o == &_Py_STR(empty) ? PyStackRef_False : PyStackRef_True;
+ v = value;
+ _tos_cache1 = v;
_tos_cache0 = res;
- _tos_cache1 = PyStackRef_ZERO_BITS;
- _tos_cache2 = PyStackRef_ZERO_BITS;
- SET_CURRENT_CACHED_VALUES(1);
+ SET_CURRENT_CACHED_VALUES(2);
+ assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
+ break;
+ }
+
+ case _TO_BOOL_STR_r23: {
+ CHECK_CURRENT_CACHED_VALUES(2);
+ assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
+ _PyStackRef value;
+ _PyStackRef res;
+ _PyStackRef v;
+ _PyStackRef _stack_item_0 = _tos_cache0;
+ _PyStackRef _stack_item_1 = _tos_cache1;
+ value = _stack_item_1;
+ STAT_INC(TO_BOOL, hit);
+ PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
+ res = value_o == &_Py_STR(empty) ? PyStackRef_False : PyStackRef_True;
+ v = value;
+ _tos_cache2 = v;
+ _tos_cache1 = res;
+ _tos_cache0 = _stack_item_0;
+ SET_CURRENT_CACHED_VALUES(3);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
static_assert(INLINE_CACHE_ENTRIES_TO_BOOL == 3, "incorrect cache size");
_PyStackRef value;
_PyStackRef res;
+ _PyStackRef v;
// _GUARD_TOS_UNICODE
{
value = stack_pointer[-1];
{
STAT_INC(TO_BOOL, hit);
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
- if (value_o == &_Py_STR(empty)) {
- assert(_Py_IsImmortal(value_o));
- res = PyStackRef_False;
- }
- else {
- assert(Py_SIZE(value_o));
- stack_pointer += -1;
- ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
- _PyFrame_SetStackPointer(frame, stack_pointer);
- PyStackRef_CLOSE(value);
- stack_pointer = _PyFrame_GetStackPointer(frame);
- res = PyStackRef_True;
- stack_pointer += 1;
- }
+ res = value_o == &_Py_STR(empty) ? PyStackRef_False : PyStackRef_True;
+ v = value;
+ }
+ // _POP_TOP_UNICODE
+ {
+ value = v;
+ assert(PyUnicode_CheckExact(PyStackRef_AsPyObjectBorrow(value)));
+ PyStackRef_CLOSE_SPECIALIZED(value, _PyUnicode_ExactDealloc);
}
stack_pointer[-1] = res;
DISPATCH();
_PyUOpInstruction *this_instr,
JitOptContext *ctx,
JitOptRef value,
- JitOptRef *result_ptr)
+ JitOptRef *result_ptr,
+ bool insert_mode)
{
if (sym_matches_type(value, &PyBool_Type)) {
REPLACE_OP(this_instr, _NOP, 0, 0);
int truthiness = sym_truthiness(ctx, value);
if (truthiness >= 0) {
PyObject *load = truthiness ? Py_True : Py_False;
- REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load);
+ int opcode = insert_mode ?
+ _INSERT_1_LOAD_CONST_INLINE_BORROW :
+ _POP_TOP_LOAD_CONST_INLINE_BORROW;
+ REPLACE_OP(this_instr, opcode, 0, (uintptr_t)load);
*result_ptr = sym_new_const(ctx, load);
return 1;
}
_PyUOpInstruction *this_instr,
JitOptContext *ctx,
JitOptSymbol *value,
- JitOptSymbol **result_ptr);
+ JitOptSymbol **result_ptr,
+ bool insert_mode);
extern void
eliminate_pop_guard(_PyUOpInstruction *this_instr, bool exit);
}
op(_TO_BOOL, (value -- res)) {
- int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
+ int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
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);
+ int already_bool = optimize_to_bool(this_instr, ctx, value, &value, false);
if (!already_bool) {
sym_set_type(value, &PyBool_Type);
}
}
op(_TO_BOOL_INT, (value -- res)) {
- int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
+ int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
if (!already_bool) {
sym_set_type(value, &PyLong_Type);
res = sym_new_truthiness(ctx, value, true);
}
op(_TO_BOOL_LIST, (value -- res)) {
- int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
+ int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
if (!already_bool) {
res = sym_new_type(ctx, &PyBool_Type);
}
}
op(_TO_BOOL_NONE, (value -- res)) {
- int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
+ int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
if (!already_bool) {
sym_set_const(value, Py_None);
res = sym_new_const(ctx, Py_False);
sym_set_type(value, &PyUnicode_Type);
}
- op(_TO_BOOL_STR, (value -- res)) {
- int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
+ op(_TO_BOOL_STR, (value -- res, v)) {
+ int already_bool = optimize_to_bool(this_instr, ctx, value, &res, true);
+ v = value;
if (!already_bool) {
res = sym_new_truthiness(ctx, value, true);
}
JitOptRef value;
JitOptRef res;
value = stack_pointer[-1];
- int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
+ int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
if (!already_bool) {
res = sym_new_truthiness(ctx, value, true);
}
case _TO_BOOL_BOOL: {
JitOptRef value;
value = stack_pointer[-1];
- int already_bool = optimize_to_bool(this_instr, ctx, value, &value);
+ int already_bool = optimize_to_bool(this_instr, ctx, value, &value, false);
if (!already_bool) {
sym_set_type(value, &PyBool_Type);
}
JitOptRef value;
JitOptRef res;
value = stack_pointer[-1];
- int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
+ int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
if (!already_bool) {
sym_set_type(value, &PyLong_Type);
res = sym_new_truthiness(ctx, value, true);
JitOptRef value;
JitOptRef res;
value = stack_pointer[-1];
- int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
+ int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
if (!already_bool) {
res = sym_new_type(ctx, &PyBool_Type);
}
JitOptRef value;
JitOptRef res;
value = stack_pointer[-1];
- int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
+ int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
if (!already_bool) {
sym_set_const(value, Py_None);
res = sym_new_const(ctx, Py_False);
case _TO_BOOL_STR: {
JitOptRef value;
JitOptRef res;
+ JitOptRef v;
value = stack_pointer[-1];
- int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
+ int already_bool = optimize_to_bool(this_instr, ctx, value, &res, true);
+ v = value;
if (!already_bool) {
res = sym_new_truthiness(ctx, value, true);
}
+ CHECK_STACK_BOUNDS(1);
stack_pointer[-1] = res;
+ stack_pointer[0] = v;
+ stack_pointer += 1;
+ ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
break;
}