mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
GH-115685: Optimize TO_BOOL
and variants based on truthiness of input. (GH-116311)
This commit is contained in:
parent
a29998a06b
commit
cbf3d38cbe
5 changed files with 150 additions and 55 deletions
|
@ -29,6 +29,14 @@ typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame;
|
|||
#define frame_new _Py_uop_frame_new
|
||||
#define frame_pop _Py_uop_frame_pop
|
||||
|
||||
extern int
|
||||
optimize_to_bool(
|
||||
_PyUOpInstruction *this_instr,
|
||||
_Py_UOpsContext *ctx,
|
||||
_Py_UopsSymbol *value,
|
||||
_Py_UopsSymbol **result_ptr);
|
||||
|
||||
|
||||
static int
|
||||
dummy_func(void) {
|
||||
|
||||
|
@ -271,63 +279,72 @@ dummy_func(void) {
|
|||
}
|
||||
|
||||
op(_TO_BOOL, (value -- res)) {
|
||||
(void)value;
|
||||
res = sym_new_type(ctx, &PyBool_Type);
|
||||
OUT_OF_SPACE_IF_NULL(res);
|
||||
if (optimize_to_bool(this_instr, ctx, value, &res)) {
|
||||
OUT_OF_SPACE_IF_NULL(res);
|
||||
}
|
||||
else {
|
||||
res = sym_new_type(ctx, &PyBool_Type);
|
||||
OUT_OF_SPACE_IF_NULL(res);
|
||||
}
|
||||
}
|
||||
|
||||
op(_TO_BOOL_BOOL, (value -- value)) {
|
||||
if (sym_matches_type(value, &PyBool_Type)) {
|
||||
REPLACE_OP(this_instr, _NOP, 0, 0);
|
||||
op(_TO_BOOL_BOOL, (value -- res)) {
|
||||
if (optimize_to_bool(this_instr, ctx, value, &res)) {
|
||||
OUT_OF_SPACE_IF_NULL(res);
|
||||
}
|
||||
else {
|
||||
if(!sym_set_type(value, &PyBool_Type)) {
|
||||
goto hit_bottom;
|
||||
}
|
||||
res = value;
|
||||
}
|
||||
}
|
||||
|
||||
op(_TO_BOOL_INT, (value -- res)) {
|
||||
if (sym_is_const(value) && sym_matches_type(value, &PyLong_Type)) {
|
||||
PyObject *load = _PyLong_IsZero((PyLongObject *)sym_get_const(value))
|
||||
? Py_False : Py_True;
|
||||
REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load);
|
||||
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, load));
|
||||
if (optimize_to_bool(this_instr, ctx, value, &res)) {
|
||||
OUT_OF_SPACE_IF_NULL(res);
|
||||
}
|
||||
else {
|
||||
if(!sym_set_type(value, &PyLong_Type)) {
|
||||
goto hit_bottom;
|
||||
}
|
||||
OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type));
|
||||
}
|
||||
if(!sym_set_type(value, &PyLong_Type)) {
|
||||
goto hit_bottom;
|
||||
}
|
||||
}
|
||||
|
||||
op(_TO_BOOL_LIST, (value -- res)) {
|
||||
if(!sym_set_type(value, &PyList_Type)) {
|
||||
goto hit_bottom;
|
||||
if (optimize_to_bool(this_instr, ctx, value, &res)) {
|
||||
OUT_OF_SPACE_IF_NULL(res);
|
||||
}
|
||||
else {
|
||||
if(!sym_set_type(value, &PyList_Type)) {
|
||||
goto hit_bottom;
|
||||
}
|
||||
OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type));
|
||||
}
|
||||
OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type));
|
||||
}
|
||||
|
||||
op(_TO_BOOL_NONE, (value -- res)) {
|
||||
if (sym_get_const(value) == Py_None) {
|
||||
REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)Py_False);
|
||||
if (optimize_to_bool(this_instr, ctx, value, &res)) {
|
||||
OUT_OF_SPACE_IF_NULL(res);
|
||||
}
|
||||
else {
|
||||
if (!sym_set_const(value, Py_None)) {
|
||||
goto hit_bottom;
|
||||
}
|
||||
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, Py_False));
|
||||
}
|
||||
sym_set_const(value, Py_None);
|
||||
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, Py_False));
|
||||
}
|
||||
|
||||
op(_TO_BOOL_STR, (value -- res)) {
|
||||
if (sym_is_const(value) && sym_matches_type(value, &PyUnicode_Type)) {
|
||||
PyObject *load = sym_get_const(value) == &_Py_STR(empty) ? Py_False : Py_True;
|
||||
REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load);
|
||||
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, load));
|
||||
if (optimize_to_bool(this_instr, ctx, value, &res)) {
|
||||
OUT_OF_SPACE_IF_NULL(res);
|
||||
}
|
||||
else {
|
||||
OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type));
|
||||
}
|
||||
if(!sym_set_type(value, &PyUnicode_Type)) {
|
||||
goto hit_bottom;
|
||||
if(!sym_set_type(value, &PyUnicode_Type)) {
|
||||
goto hit_bottom;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue