Skip to content

Commit 6751652

Browse files
committed
simplify: add IsTruthy known-object path (PyObject_IsTrue fold)
Constant-folds PyObject_IsTrue for trusted immutable types: Bool, Float, Long, FrozenSet, Slice, Tuple, Unicode, NoneType. If the object is a known constant of these types, replaces IsTruthy with CBool LoadConst.
1 parent 3b23771 commit 6751652

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

Python/jit/hir/simplify_c.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,22 @@ void *simplify_is_truthy_c(SimplifyEnv *env, const void *instr) {
218218
void *input = hir_c_get_operand(instr, 0);
219219
HirType input_type = hir_register_type(input);
220220

221+
/* Known immutable object: constant-fold PyObject_IsTrue */
222+
PyObject *obj = hir_type_as_object(&input_type);
223+
if (obj != NULL) {
224+
PyTypeObject *tp = Py_TYPE(obj);
225+
if (tp == &PyBool_Type || tp == &PyFloat_Type ||
226+
tp == &PyLong_Type || tp == &PyFrozenSet_Type ||
227+
tp == &PySlice_Type || tp == &PyTuple_Type ||
228+
tp == &PyUnicode_Type || tp == Py_TYPE(Py_None)) {
229+
int res = PyObject_IsTrue(obj);
230+
if (res >= 0) {
231+
simplify_env_emit_use_type(env, input, input_type);
232+
return simplify_env_emit_load_const(env, make_cbool_type(res));
233+
}
234+
}
235+
}
236+
221237
/* TBool: compare with Py_True */
222238
HirType t_bool = HIR_TYPE_BOOL;
223239
if (hir_type_is_subtype(input_type, t_bool)) {

0 commit comments

Comments
 (0)