mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
Simplify and future proof NOP counting in the peepholer.
No longer assumes that the input is NOP free.
This commit is contained in:
parent
4a8d851910
commit
099ecfbec9
1 changed files with 6 additions and 13 deletions
|
@ -536,8 +536,7 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
|
||||||
goto exitUnchanged;
|
goto exitUnchanged;
|
||||||
assert(PyList_Check(consts));
|
assert(PyList_Check(consts));
|
||||||
|
|
||||||
for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
|
for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
|
||||||
addrmap[i] = i - nops;
|
|
||||||
opcode = codestr[i];
|
opcode = codestr[i];
|
||||||
|
|
||||||
lastlc = cumlc;
|
lastlc = cumlc;
|
||||||
|
@ -560,7 +559,6 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
|
||||||
SETARG(codestr, i, j);
|
SETARG(codestr, i, j);
|
||||||
codestr[i+3] = POP_TOP;
|
codestr[i+3] = POP_TOP;
|
||||||
codestr[i+4] = NOP;
|
codestr[i+4] = NOP;
|
||||||
nops++;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* not a is b --> a is not b
|
/* not a is b --> a is not b
|
||||||
|
@ -575,7 +573,6 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
|
||||||
continue;
|
continue;
|
||||||
SETARG(codestr, i, (j^1));
|
SETARG(codestr, i, (j^1));
|
||||||
codestr[i+3] = NOP;
|
codestr[i+3] = NOP;
|
||||||
nops++;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* Replace LOAD_GLOBAL/LOAD_NAME None with LOAD_CONST None */
|
/* Replace LOAD_GLOBAL/LOAD_NAME None with LOAD_CONST None */
|
||||||
|
@ -604,7 +601,6 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
|
||||||
!PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
|
!PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
|
||||||
continue;
|
continue;
|
||||||
memset(codestr+i, NOP, 7);
|
memset(codestr+i, NOP, 7);
|
||||||
nops += 7;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* Try to fold tuples of constants.
|
/* Try to fold tuples of constants.
|
||||||
|
@ -619,7 +615,6 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
|
||||||
codestr[h] == LOAD_CONST &&
|
codestr[h] == LOAD_CONST &&
|
||||||
ISBASICBLOCK(blocks, h, 3*(j+1)) &&
|
ISBASICBLOCK(blocks, h, 3*(j+1)) &&
|
||||||
tuple_of_constants(&codestr[h], j, consts)) {
|
tuple_of_constants(&codestr[h], j, consts)) {
|
||||||
nops += 3 * j;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
/* Intentional fallthrough */
|
/* Intentional fallthrough */
|
||||||
|
@ -631,16 +626,13 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
|
||||||
continue;
|
continue;
|
||||||
if (j == 1) {
|
if (j == 1) {
|
||||||
memset(codestr+i, NOP, 6);
|
memset(codestr+i, NOP, 6);
|
||||||
nops += 6;
|
|
||||||
} else if (j == 2) {
|
} else if (j == 2) {
|
||||||
codestr[i] = ROT_TWO;
|
codestr[i] = ROT_TWO;
|
||||||
memset(codestr+i+1, NOP, 5);
|
memset(codestr+i+1, NOP, 5);
|
||||||
nops += 5;
|
|
||||||
} else if (j == 3) {
|
} else if (j == 3) {
|
||||||
codestr[i] = ROT_THREE;
|
codestr[i] = ROT_THREE;
|
||||||
codestr[i+1] = ROT_TWO;
|
codestr[i+1] = ROT_TWO;
|
||||||
memset(codestr+i+2, NOP, 4);
|
memset(codestr+i+2, NOP, 4);
|
||||||
nops += 4;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -704,12 +696,16 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
|
||||||
!ISBASICBLOCK(blocks,i,5))
|
!ISBASICBLOCK(blocks,i,5))
|
||||||
continue;
|
continue;
|
||||||
memset(codestr+i+1, NOP, 4);
|
memset(codestr+i+1, NOP, 4);
|
||||||
nops += 4;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Fixup linenotab */
|
/* Fixup linenotab */
|
||||||
|
for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
|
||||||
|
addrmap[i] = i - nops;
|
||||||
|
if (codestr[i] == NOP)
|
||||||
|
nops++;
|
||||||
|
}
|
||||||
cum_orig_line = 0;
|
cum_orig_line = 0;
|
||||||
last_line = 0;
|
last_line = 0;
|
||||||
for (i=0 ; i < tabsiz ; i+=2) {
|
for (i=0 ; i < tabsiz ; i+=2) {
|
||||||
|
@ -749,9 +745,6 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
|
||||||
while (adj--)
|
while (adj--)
|
||||||
codestr[h++] = codestr[i++];
|
codestr[h++] = codestr[i++];
|
||||||
}
|
}
|
||||||
/* The following assertion detects the presence of NOPs in the input
|
|
||||||
bytecode. The compiler never produces NOPs so far; if one day it
|
|
||||||
does, the way 'nops' is counted above must be changed. */
|
|
||||||
assert(h + nops == codelen);
|
assert(h + nops == codelen);
|
||||||
|
|
||||||
code = PyString_FromStringAndSize((char *)codestr, h);
|
code = PyString_FromStringAndSize((char *)codestr, h);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue