summaryrefslogtreecommitdiffstats
path: root/Python/optimizer_analysis.c
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2024-03-05 11:23:46 (GMT)
committerGitHub <noreply@github.com>2024-03-05 11:23:46 (GMT)
commitcbf3d38cbeb6e640d5959549169ec45cdedc1a71 (patch)
tree64f321978cc29420a94c4808118d5577e80cd8bd /Python/optimizer_analysis.c
parenta29998a06bf75264c3faaeeec4584a5f75b45a1f (diff)
downloadcpython-cbf3d38cbeb6e640d5959549169ec45cdedc1a71.zip
cpython-cbf3d38cbeb6e640d5959549169ec45cdedc1a71.tar.gz
cpython-cbf3d38cbeb6e640d5959549169ec45cdedc1a71.tar.bz2
GH-115685: Optimize `TO_BOOL` and variants based on truthiness of input. (GH-116311)
Diffstat (limited to 'Python/optimizer_analysis.c')
-rw-r--r--Python/optimizer_analysis.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/Python/optimizer_analysis.c b/Python/optimizer_analysis.c
index a326e22..1e1d552 100644
--- a/Python/optimizer_analysis.c
+++ b/Python/optimizer_analysis.c
@@ -298,9 +298,31 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
#define sym_set_type _Py_uop_sym_set_type
#define sym_set_const _Py_uop_sym_set_const
#define sym_is_bottom _Py_uop_sym_is_bottom
+#define sym_truthiness _Py_uop_sym_truthiness
#define frame_new _Py_uop_frame_new
#define frame_pop _Py_uop_frame_pop
+static int
+optimize_to_bool(
+ _PyUOpInstruction *this_instr,
+ _Py_UOpsContext *ctx,
+ _Py_UopsSymbol *value,
+ _Py_UopsSymbol **result_ptr)
+{
+ if (sym_matches_type(value, &PyBool_Type)) {
+ REPLACE_OP(this_instr, _NOP, 0, 0);
+ *result_ptr = value;
+ return 1;
+ }
+ int truthiness = sym_truthiness(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);
+ *result_ptr = sym_new_const(ctx, load);
+ return 1;
+ }
+ return 0;
+}
/* 1 for success, 0 for not ready, cannot error at the moment. */
static int