Skip to content

Commit 9b0f10c

Browse files
committed
remove simple constant folding from peephole
1 parent a4edbc3 commit 9b0f10c

File tree

1 file changed

+0
-193
lines changed

1 file changed

+0
-193
lines changed

Python/peephole.c

Lines changed: 0 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -218,153 +218,6 @@ fold_tuple_on_constants(_Py_CODEUNIT *codestr, Py_ssize_t c_start,
218218
return copy_op_arg(codestr, c_start, LOAD_CONST, len_consts, opcode_end);
219219
}
220220

221-
/* Replace LOAD_CONST c1, LOAD_CONST c2, BINOP
222-
with LOAD_CONST binop(c1,c2)
223-
The consts table must still be in list form so that the
224-
new constant can be appended.
225-
Called with codestr pointing to the BINOP.
226-
Abandons the transformation if the folding fails (i.e. 1+'a').
227-
If the new constant is a sequence, only folds when the size
228-
is below a threshold value. That keeps pyc files from
229-
becoming large in the presence of code like: (None,)*1000.
230-
*/
231-
static Py_ssize_t
232-
fold_binops_on_constants(_Py_CODEUNIT *codestr, Py_ssize_t c_start,
233-
Py_ssize_t opcode_end, unsigned char opcode,
234-
PyObject *consts, PyObject **objs)
235-
{
236-
PyObject *newconst, *v, *w;
237-
Py_ssize_t len_consts, size;
238-
239-
/* Pre-conditions */
240-
assert(PyList_CheckExact(consts));
241-
len_consts = PyList_GET_SIZE(consts);
242-
243-
/* Create new constant */
244-
v = objs[0];
245-
w = objs[1];
246-
switch (opcode) {
247-
case BINARY_POWER:
248-
newconst = PyNumber_Power(v, w, Py_None);
249-
break;
250-
case BINARY_MULTIPLY:
251-
newconst = PyNumber_Multiply(v, w);
252-
break;
253-
case BINARY_TRUE_DIVIDE:
254-
newconst = PyNumber_TrueDivide(v, w);
255-
break;
256-
case BINARY_FLOOR_DIVIDE:
257-
newconst = PyNumber_FloorDivide(v, w);
258-
break;
259-
case BINARY_MODULO:
260-
newconst = PyNumber_Remainder(v, w);
261-
break;
262-
case BINARY_ADD:
263-
newconst = PyNumber_Add(v, w);
264-
break;
265-
case BINARY_SUBTRACT:
266-
newconst = PyNumber_Subtract(v, w);
267-
break;
268-
case BINARY_SUBSCR:
269-
newconst = PyObject_GetItem(v, w);
270-
break;
271-
case BINARY_LSHIFT:
272-
newconst = PyNumber_Lshift(v, w);
273-
break;
274-
case BINARY_RSHIFT:
275-
newconst = PyNumber_Rshift(v, w);
276-
break;
277-
case BINARY_AND:
278-
newconst = PyNumber_And(v, w);
279-
break;
280-
case BINARY_XOR:
281-
newconst = PyNumber_Xor(v, w);
282-
break;
283-
case BINARY_OR:
284-
newconst = PyNumber_Or(v, w);
285-
break;
286-
default:
287-
/* Called with an unknown opcode */
288-
PyErr_Format(PyExc_SystemError,
289-
"unexpected binary operation %d on a constant",
290-
opcode);
291-
return -1;
292-
}
293-
if (newconst == NULL) {
294-
if(!PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) {
295-
PyErr_Clear();
296-
}
297-
return -1;
298-
}
299-
size = PyObject_Size(newconst);
300-
if (size == -1) {
301-
if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) {
302-
return -1;
303-
}
304-
PyErr_Clear();
305-
} else if (size > 20) {
306-
Py_DECREF(newconst);
307-
return -1;
308-
}
309-
310-
/* Append folded constant into consts table */
311-
if (PyList_Append(consts, newconst)) {
312-
Py_DECREF(newconst);
313-
return -1;
314-
}
315-
Py_DECREF(newconst);
316-
317-
return copy_op_arg(codestr, c_start, LOAD_CONST, len_consts, opcode_end);
318-
}
319-
320-
static Py_ssize_t
321-
fold_unaryops_on_constants(_Py_CODEUNIT *codestr, Py_ssize_t c_start,
322-
Py_ssize_t opcode_end, unsigned char opcode,
323-
PyObject *consts, PyObject *v)
324-
{
325-
PyObject *newconst;
326-
Py_ssize_t len_consts;
327-
328-
/* Pre-conditions */
329-
assert(PyList_CheckExact(consts));
330-
len_consts = PyList_GET_SIZE(consts);
331-
332-
/* Create new constant */
333-
switch (opcode) {
334-
case UNARY_NEGATIVE:
335-
newconst = PyNumber_Negative(v);
336-
break;
337-
case UNARY_INVERT:
338-
newconst = PyNumber_Invert(v);
339-
break;
340-
case UNARY_POSITIVE:
341-
newconst = PyNumber_Positive(v);
342-
break;
343-
default:
344-
/* Called with an unknown opcode */
345-
PyErr_Format(PyExc_SystemError,
346-
"unexpected unary operation %d on a constant",
347-
opcode);
348-
return -1;
349-
}
350-
if (newconst == NULL) {
351-
if(!PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) {
352-
PyErr_Clear();
353-
}
354-
return -1;
355-
}
356-
357-
/* Append folded constant into consts table */
358-
if (PyList_Append(consts, newconst)) {
359-
Py_DECREF(newconst);
360-
PyErr_Clear();
361-
return -1;
362-
}
363-
Py_DECREF(newconst);
364-
365-
return copy_op_arg(codestr, c_start, LOAD_CONST, len_consts, opcode_end);
366-
}
367-
368221
static unsigned int *
369222
markblocks(_Py_CODEUNIT *code, Py_ssize_t len)
370223
{
@@ -566,52 +419,6 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
566419
}
567420
break;
568421

569-
/* Fold binary ops on constants.
570-
LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */
571-
case BINARY_POWER:
572-
case BINARY_MULTIPLY:
573-
case BINARY_TRUE_DIVIDE:
574-
case BINARY_FLOOR_DIVIDE:
575-
case BINARY_MODULO:
576-
case BINARY_ADD:
577-
case BINARY_SUBTRACT:
578-
case BINARY_SUBSCR:
579-
case BINARY_LSHIFT:
580-
case BINARY_RSHIFT:
581-
case BINARY_AND:
582-
case BINARY_XOR:
583-
case BINARY_OR:
584-
if (CONST_STACK_LEN() < 2)
585-
break;
586-
h = lastn_const_start(codestr, op_start, 2);
587-
if (ISBASICBLOCK(blocks, h, op_start)) {
588-
h = fold_binops_on_constants(codestr, h, i + 1, opcode,
589-
consts, CONST_STACK_LASTN(2));
590-
if (h >= 0) {
591-
CONST_STACK_POP(2);
592-
CONST_STACK_PUSH_OP(h);
593-
}
594-
}
595-
break;
596-
597-
/* Fold unary ops on constants.
598-
LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */
599-
case UNARY_NEGATIVE:
600-
case UNARY_INVERT:
601-
case UNARY_POSITIVE:
602-
if (CONST_STACK_LEN() < 1)
603-
break;
604-
h = lastn_const_start(codestr, op_start, 1);
605-
if (ISBASICBLOCK(blocks, h, op_start)) {
606-
h = fold_unaryops_on_constants(codestr, h, i + 1, opcode,
607-
consts, *CONST_STACK_LASTN(1));
608-
if (h >= 0) {
609-
CONST_STACK_POP(1);
610-
CONST_STACK_PUSH_OP(h);
611-
}
612-
}
613-
break;
614-
615422
/* Simplify conditional jump to conditional jump where the
616423
result of the first test implies the success of a similar
617424
test or the failure of the opposite test.

0 commit comments

Comments
 (0)