mirror of
https://github.com/python/cpython.git
synced 2025-08-22 17:55:18 +00:00
gh-115480: Type and constant propagation for int BINARY_OPs (GH-115478)
This commit is contained in:
parent
ed23839dc5
commit
4ebf8fbdab
3 changed files with 126 additions and 16 deletions
|
@ -341,6 +341,18 @@ sym_new_const(_Py_UOpsAbstractInterpContext *ctx, PyObject *const_val)
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline bool
|
||||||
|
is_const(_Py_UOpsSymType *sym)
|
||||||
|
{
|
||||||
|
return sym->const_val != NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline PyObject *
|
||||||
|
get_const(_Py_UOpsSymType *sym)
|
||||||
|
{
|
||||||
|
return sym->const_val;
|
||||||
|
}
|
||||||
|
|
||||||
static _Py_UOpsSymType*
|
static _Py_UOpsSymType*
|
||||||
sym_new_null(_Py_UOpsAbstractInterpContext *ctx)
|
sym_new_null(_Py_UOpsAbstractInterpContext *ctx)
|
||||||
{
|
{
|
||||||
|
|
|
@ -81,12 +81,62 @@ dummy_func(void) {
|
||||||
|
|
||||||
|
|
||||||
op(_BINARY_OP_ADD_INT, (left, right -- res)) {
|
op(_BINARY_OP_ADD_INT, (left, right -- res)) {
|
||||||
// TODO constant propagation
|
if (is_const(left) && is_const(right)) {
|
||||||
(void)left;
|
assert(PyLong_CheckExact(get_const(left)));
|
||||||
(void)right;
|
assert(PyLong_CheckExact(get_const(right)));
|
||||||
res = sym_new_known_type(ctx, &PyLong_Type);
|
PyObject *temp = _PyLong_Add((PyLongObject *)get_const(left),
|
||||||
if (res == NULL) {
|
(PyLongObject *)get_const(right));
|
||||||
goto out_of_space;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -180,9 +180,28 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
case _BINARY_OP_MULTIPLY_INT: {
|
case _BINARY_OP_MULTIPLY_INT: {
|
||||||
|
_Py_UOpsSymType *right;
|
||||||
|
_Py_UOpsSymType *left;
|
||||||
_Py_UOpsSymType *res;
|
_Py_UOpsSymType *res;
|
||||||
res = sym_new_unknown(ctx);
|
right = stack_pointer[-1];
|
||||||
if (res == NULL) goto out_of_space;
|
left = stack_pointer[-2];
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
stack_pointer[-2] = res;
|
stack_pointer[-2] = res;
|
||||||
stack_pointer += -1;
|
stack_pointer += -1;
|
||||||
break;
|
break;
|
||||||
|
@ -194,12 +213,22 @@
|
||||||
_Py_UOpsSymType *res;
|
_Py_UOpsSymType *res;
|
||||||
right = stack_pointer[-1];
|
right = stack_pointer[-1];
|
||||||
left = stack_pointer[-2];
|
left = stack_pointer[-2];
|
||||||
// TODO constant propagation
|
if (is_const(left) && is_const(right)) {
|
||||||
(void)left;
|
assert(PyLong_CheckExact(get_const(left)));
|
||||||
(void)right;
|
assert(PyLong_CheckExact(get_const(right)));
|
||||||
res = sym_new_known_type(ctx, &PyLong_Type);
|
PyObject *temp = _PyLong_Add((PyLongObject *)get_const(left),
|
||||||
if (res == NULL) {
|
(PyLongObject *)get_const(right));
|
||||||
goto out_of_space;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
stack_pointer[-2] = res;
|
stack_pointer[-2] = res;
|
||||||
stack_pointer += -1;
|
stack_pointer += -1;
|
||||||
|
@ -207,9 +236,28 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
case _BINARY_OP_SUBTRACT_INT: {
|
case _BINARY_OP_SUBTRACT_INT: {
|
||||||
|
_Py_UOpsSymType *right;
|
||||||
|
_Py_UOpsSymType *left;
|
||||||
_Py_UOpsSymType *res;
|
_Py_UOpsSymType *res;
|
||||||
res = sym_new_unknown(ctx);
|
right = stack_pointer[-1];
|
||||||
if (res == NULL) goto out_of_space;
|
left = stack_pointer[-2];
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
stack_pointer[-2] = res;
|
stack_pointer[-2] = res;
|
||||||
stack_pointer += -1;
|
stack_pointer += -1;
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue