8000 revert old unarynot handing, add contains/is + unarynot folding · python/cpython@258a5b6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 258a5b6

Browse files
committed
revert old unarynot handing, add contains/is + unarynot folding
1 parent 2739fa0 commit 258a5b6

File tree

1 file changed

+19
-93
lines changed

1 file changed

+19
-93
lines changed

Python/flowgraph.c

Lines changed: 19 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,96 +1762,6 @@ eval_const_unaryop(PyObject *operand, int op, bool unarypos)
17621762
return result;
17631763
}
17641764

1765-
static void
1766-
remove_to_bool_sequence(basicblock *bb, int start)
1767-
{
1768-
assert(start >= 0);
1769-
for (;start < bb->b_iused; start++) {
1770-
cfg_instr *curr = &bb->b_instr[start];
1771-
if (curr->i_opcode == NOP) {
1772-
continue;
1773-
}
1774-
if (curr->i_opcode == TO_BOOL) {
1775-
INSTR_SET_OP0(curr, NOP);
1776-
continue;
1777-
}
1778-
break;
1779-
}
1780-
}
1781-
1782-
static cfg_instr *
1783-
find_unary_not_target(basicblock *bb, int start)
1784-
{
1785-
assert(start < bb->b_iused);
1786-
for (; start >= 0; start--) {
1787-
cfg_instr *instr = &bb->b_instr[start];
1788-
if (instr->i_opcode == NOP) {
1789-
continue;
1790-
}
1791-
if (instr->i_opcode == IS_OP || instr->i_opcode == CONTAINS_OP) {
1792-
return instr;
1793-
}
1794-
break;
1795-
}
1796-
return NULL;
1797-
}
1798-
1799-
/* Replace:
1800-
IS_OP(is)
1801-
UNARY_NOT
1802-
with:
1803-
IS_OP(is not)
1804-
NOP
1805-
or vice versa ("is not" to "is").
1806-
1807-
And:
1808-
CONTAINS_OP(in)
1809-
UNARY_NOT
1810-
with:
1811-
CONTAINS_OP(not in)
1812-
NOP
1813-
or vice versa ("not in" to "in").
1814-
*/
1815-
static bool
1816-
optimize_unary_not_is_contains(basicblock *bb, int i, int nextop)
1817-
{
1818-
cfg_instr *instr = &bb->b_instr[i];
1819-
assert(instr->i_opcode == UNARY_NOT);
1820-
cfg_instr *target = find_unary_not_target(bb, i-1);
1821-
if (target == NULL) {
1822-
return false;
1823-
}
1824-
int inverted = target->i_oparg ^ 1;
1825-
assert(inverted == 0 || inverted == 1);
1826-
INSTR_SET_OP1(target, target->i_opcode, inverted);
1827-
INSTR_SET_OP0(instr, NOP);
1828-
return true;
1829-
}
1830-
1831-
static bool
1832-
optimize_unary_not_non_const(basicblock *bb, int i, int nextop)
1833-
{
1834-
assert(i >= 0 && i < bb->b_iused);
1835-
cfg_instr *instr = &bb->b_instr[i];
1836-
assert(instr->i_opcode == UNARY_NOT);
1837-
if (nextop == UNARY_NOT) { /* subsequent UNARY_NOT is no-op */
1838-
INSTR_SET_OP0(instr, NOP);
1839-
int nexti = i + 1;
1840-
assert(nexti >= 1 && nexti < bb->b_iused);
1841-
cfg_instr *next = &bb->b_instr[nexti];
1842-
assert(next->i_opcode == nextop);
1843-
INSTR_SET_OP0(next, NOP);
1844-
if (find_unary_not_target(bb, i-1) != NULL) {
1845-
/* IS_OP & CONTAINS_OP don't need TO_BOOL conversion */
1846-
remove_to_bool_sequence(bb, i+1);
1847-
}
1848-
return true;
1849-
}
1850-
/* single UNARY_NOT doesn't need TO_BOOL conversion */
1851-
remove_to_bool_sequence(bb, i+1);
1852-
return optimize_unary_not_is_contains(bb, i, nextop);
1853-
}
1854-
18551765
static int
18561766
optimize_if_const_unaryop(basicblock *bb, int i, int nextop,
18571767
PyObject *consts, PyObject *const_cache, bool unarypos)
@@ -1870,9 +1780,6 @@ optimize_if_const_unaryop(basicblock *bb, int i, int nextop,
18701780
|| instr->i_opcode == UNARY_NOT
18711781
);
18721782
}
1873-
if (instr->i_opcode == UNARY_NOT && optimize_unary_not_non_const(bb, i, nextop)) {
1874-
return SUCCESS;
1875-
}
18761783
PyObject *seq;
18771784
RETURN_IF_ERROR(get_constant_sequence(bb, i-1, 1, consts, &seq));
18781785
RETURN_IF_NOT_CONST_SEQ(seq);
@@ -2358,6 +2265,13 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
23582265
INSTR_SET_OP1(&bb->b_instr[i + 1], opcode, oparg);
23592266
continue;
23602267
}
2268+
if (nextop == UNARY_NOT) {
2269+
INSTR_SET_OP0(inst, NOP);
2270+
int inverted = oparg ^ 1;
2271+
assert(inverted == 0 || inverted == 1);
2272+
INSTR_SET_OP1(&bb->b_instr[i + 1], opcode, inverted);
2273+
continue;
2274+
}
23612275
break;
23622276
case TO_BOOL:
23632277
if (nextop == TO_BOOL) {
@@ -2368,6 +2282,18 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
23682282
case UNARY_NOT:
23692283
case UNARY_INVERT:
23702284
case UNARY_NEGATIVE:
2285+
if (opcode == UNARY_NOT) {
2286+
if (nextop == TO_BOOL) {
2287+
INSTR_SET_OP0(inst, NOP);
2288+
INSTR_SET_OP0(&bb->b_instr[i + 1], UNARY_NOT);
2289+
continue;
2290+
}
2291+
if (nextop == UNARY_NOT) {
2292+
INSTR_SET_OP0(inst, NOP);
2293+
INSTR_SET_OP0(&bb->b_instr[i + 1], NOP);
2294+
continue;
2295+
}
2296+
}
23712297
RETURN_IF_ERROR(optimize_if_const_unaryop(bb, i, nextop, consts, const_cache, false));
23722298
break;
23732299
case CALL_INTRINSIC_1:

0 commit comments

Comments
 (0)
0