diff options
author | Ken Jin <kenjin@python.org> | 2024-02-15 06:02:18 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-15 06:02:18 (GMT) |
commit | 4ebf8fbdab1c64041ff0ea54b3d15624f6e01511 (patch) | |
tree | 513bb5ca87d1adbe7fd086de0e45479e0ec92054 /Python/tier2_redundancy_eliminator_bytecodes.c | |
parent | ed23839dc5ce21ea9ca087fac170fa1412005210 (diff) | |
download | cpython-4ebf8fbdab1c64041ff0ea54b3d15624f6e01511.zip cpython-4ebf8fbdab1c64041ff0ea54b3d15624f6e01511.tar.gz cpython-4ebf8fbdab1c64041ff0ea54b3d15624f6e01511.tar.bz2 |
gh-115480: Type and constant propagation for int BINARY_OPs (GH-115478)
Diffstat (limited to 'Python/tier2_redundancy_eliminator_bytecodes.c')
-rw-r--r-- | Python/tier2_redundancy_eliminator_bytecodes.c | 62 |
1 files changed, 56 insertions, 6 deletions
diff --git a/Python/tier2_redundancy_eliminator_bytecodes.c b/Python/tier2_redundancy_eliminator_bytecodes.c index 3272b18..39ea0ee 100644 --- a/Python/tier2_redundancy_eliminator_bytecodes.c +++ b/Python/tier2_redundancy_eliminator_bytecodes.c @@ -81,12 +81,62 @@ dummy_func(void) { op(_BINARY_OP_ADD_INT, (left, right -- res)) { - // TODO constant propagation - (void)left; - (void)right; - res = sym_new_known_type(ctx, &PyLong_Type); - if (res == NULL) { - goto out_of_space; + if (is_const(left) && is_const(right)) { + assert(PyLong_CheckExact(get_const(left))); + assert(PyLong_CheckExact(get_const(right))); + PyObject *temp = _PyLong_Add((PyLongObject *)get_const(left), + (PyLongObject *)get_const(right)); + if (temp == NULL) { + goto error; + } + res = sym_new_const(ctx, temp); + // TODO replace opcode with constant propagated one and add tests! + } + else { + res = sym_new_known_type(ctx, &PyLong_Type); + if (res == NULL) { + goto out_of_space; + } + } + } + + op(_BINARY_OP_SUBTRACT_INT, (left, right -- res)) { + if (is_const(left) && is_const(right)) { + assert(PyLong_CheckExact(get_const(left))); + assert(PyLong_CheckExact(get_const(right))); + PyObject *temp = _PyLong_Subtract((PyLongObject *)get_const(left), + (PyLongObject *)get_const(right)); + if (temp == NULL) { + goto error; + } + res = sym_new_const(ctx, temp); + // TODO replace opcode with constant propagated one and add tests! + } + else { + res = sym_new_known_type(ctx, &PyLong_Type); + if (res == NULL) { + goto out_of_space; + } + } + } + + op(_BINARY_OP_MULTIPLY_INT, (left, right -- res)) { + if (is_const(left) && is_const(right)) { + assert(PyLong_CheckExact(get_const(left))); + assert(PyLong_CheckExact(get_const(right))); + PyObject *temp = _PyLong_Multiply((PyLongObject *)get_const(left), + (PyLongObject *)get_const(right)); + if (temp == NULL) { + goto error; + } + res = sym_new_const(ctx, temp); + // TODO replace opcode with constant propagated one and add tests! + } + else { + res = sym_new_known_type(ctx, &PyLong_Type); + if (res == NULL) { + goto out_of_space; + } } } |