From 87010e85cb37192d63b1a30e5fabba307ad5a3f5 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 18 Dec 2017 15:52:54 +0900 Subject: [PATCH] bpo-29469: peephole: Remove const_stack (GH-4879) Constant folding was moved to AST optimizer. But compiler may emit LOAD_CONSTs + BUILD_TUPLE. For example, default arguments can be constant tuple if all arguments are constant. This commit makes peephole's tuple folding simple. It doesn't support nested tuples because nested tuples are folded by AST optimizer already. --- Python/peephole.c | 102 ++++++++++------------------------------------ 1 file changed, 21 insertions(+), 81 deletions(-) diff --git a/Python/peephole.c b/Python/peephole.c index 49d62a2c342..76a0edbcc2b 100644 --- a/Python/peephole.c +++ b/Python/peephole.c @@ -23,51 +23,6 @@ (blocks[start]==blocks[end]) -#define CONST_STACK_CREATE() { \ - const_stack_size = 256; \ - const_stack = PyMem_New(PyObject *, const_stack_size); \ - if (!const_stack) { \ - PyErr_NoMemory(); \ - goto exitError; \ - } \ - } - -#define CONST_STACK_DELETE() do { \ - if (const_stack) \ - PyMem_Free(const_stack); \ - } while(0) - -#define CONST_STACK_LEN() ((unsigned)(const_stack_top + 1)) - -#define CONST_STACK_PUSH_OP(i) do { \ - PyObject *_x; \ - assert(_Py_OPCODE(codestr[i]) == LOAD_CONST); \ - assert(PyList_GET_SIZE(consts) > (Py_ssize_t)get_arg(codestr, i)); \ - _x = PyList_GET_ITEM(consts, get_arg(codestr, i)); \ - if (++const_stack_top >= const_stack_size) { \ - const_stack_size *= 2; \ - PyMem_Resize(const_stack, PyObject *, const_stack_size); \ - if (!const_stack) { \ - PyErr_NoMemory(); \ - goto exitError; \ - } \ - } \ - const_stack[const_stack_top] = _x; \ - in_consts = 1; \ - } while(0) - -#define CONST_STACK_RESET() do { \ - const_stack_top = -1; \ - } while(0) - -#define CONST_STACK_LASTN(i) \ - &const_stack[CONST_STACK_LEN() - i] - -#define CONST_STACK_POP(i) do { \ - assert(CONST_STACK_LEN() >= i); \ - const_stack_top -= i; \ - } while(0) - /* Scans back N consecutive LOAD_CONST instructions, skipping NOPs, returns index of the Nth last's LOAD_CONST's EXTENDED_ARG prefix. Callers are responsible to check CONST_STACK_LEN beforehand. @@ -88,8 +43,7 @@ lastn_const_start(const _Py_CODEUNIT *codestr, Py_ssize_t i, Py_ssize_t n) } } else { - assert(_Py_OPCODE(codestr[i]) == NOP || - _Py_OPCODE(codestr[i]) == EXTENDED_ARG); + assert(_Py_OPCODE(codestr[i]) == EXTENDED_ARG); } } } @@ -172,39 +126,40 @@ copy_op_arg(_Py_CODEUNIT *codestr, Py_ssize_t i, unsigned char op, The consts table must still be in list form so that the new constant (c1, c2, ... cn) can be appended. Called with codestr pointing to the first LOAD_CONST. - Bails out with no change if one or more of the LOAD_CONSTs is missing. */ static Py_ssize_t fold_tuple_on_constants(_Py_CODEUNIT *codestr, Py_ssize_t c_start, - Py_ssize_t opcode_end, unsigned char opcode, - PyObject *consts, PyObject **objs, int n) + Py_ssize_t opcode_end, PyObject *consts, int n) { - PyObject *newconst, *constant; - Py_ssize_t i, len_consts; - /* Pre-conditions */ assert(PyList_CheckExact(consts)); /* Buildup new tuple of constants */ - newconst = PyTuple_New(n); + PyObject *newconst = PyTuple_New(n); if (newconst == NULL) { return -1; } - for (i=0 ; i 0 && CONST_STACK_LEN() >= j) { + if (j > 0 && lastlc >= j) { h = lastn_const_start(codestr, op_start, j); if (ISBASICBLOCK(blocks, h, op_start)) { - h = fold_tuple_on_constants(codestr, h, i + 1, opcode, - consts, CONST_STACK_LASTN(j), j); - if (h >= 0) { - CONST_STACK_POP(j); - CONST_STACK_PUSH_OP(h); - } + h = fold_tuple_on_constants(codestr, h, i+1, consts, j); break; } } @@ -374,12 +318,10 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, } else if (j == 2) { codestr[op_start] = PACKOPARG(ROT_TWO, 0); fill_nops(codestr, op_start + 1, nexti + 1); - CONST_STACK_RESET(); } else if (j == 3) { codestr[op_start] = PACKOPARG(ROT_THREE, 0); codestr[op_start + 1] = PACKOPARG(ROT_TWO, 0); fill_nops(codestr, op_start + 2, nexti + 1); - CONST_STACK_RESET(); } break; @@ -538,7 +480,6 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, } assert(h + (Py_ssize_t)nops == codelen); - CONST_STACK_DELETE(); PyMem_Free(blocks); code = PyBytes_FromStringAndSize((char *)codestr, h * sizeof(_Py_CODEUNIT)); PyMem_Free(codestr); @@ -549,7 +490,6 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, exitUnchanged: Py_XINCREF(code); - CONST_STACK_DELETE(); PyMem_Free(blocks); PyMem_Free(codestr); return code;