10000 fix optimize_if_const_unaryop · python/cpython@c1a1be5 · GitHub
[go: up one dir, main page]

Skip to content

Commit c1a1be5

Browse files
committed
fix optimize_if_const_unaryop
1 parent 8358e28 commit c1a1be5

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

Python/flowgraph.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,11 +1762,15 @@ eval_const_unaryop(PyObject *operand, int op, bool unarypos)
17621762
}
17631763

17641764
static void
1765-
remove_redundant_to_bool(basicblock *bb, int start)
1765+
remove_to_bool_sequence(basicblock *bb, int start)
17661766
{
17671767
assert(start >= 0);
17681768
for (;start < bb->b_iused; start++) {
17691769
cfg_instr *curr = &bb->b_instr[start];
1770+
if (curr->i_opcode == NOP) {
1771+
/* should not happen, but just in case */
1772+
continue;
1773+
}
17701774
if (curr->i_opcode == TO_BOOL) {
17711775
INSTR_SET_OP0(curr, NOP);
17721776
continue;
@@ -1799,8 +1803,9 @@ maybe_unary_not_cancel_out(basicblock *bb, int i, int nextop)
17991803
assert(instr->i_opcode == UNARY_NOT);
18001804
if (nextop == UNARY_NOT) {
18011805
INSTR_SET_OP0(instr, NOP);
1802-
assert(i < bb->b_iused);
1803-
cfg_instr *next = &bb->b_instr[i+1];
1806+
int nexi = i + 1;
1807+
assert(nexi >= 0 && nexi < bb->b_iused);
1808+
cfg_instr *next = &bb->b_instr[nexi];
18041809
assert(next->i_opcode == nextop);
18051810
INSTR_SET_OP0(next, NOP);
18061811
return true;
@@ -1829,7 +1834,6 @@ optimize_unary_not_is_contains(basicblock *bb, int i, int nextop)
18291834
{
18301835
cfg_instr *instr = &bb->b_instr[i];
18311836
assert(instr->i_opcode == UNARY_NOT);
1832-
remove_redundant_to_bool(bb, i+1);
18331837
cfg_instr *target = find_unary_not_target(bb, i-1);
18341838
if (target == NULL) {
18351839
return false;
@@ -1864,6 +1868,7 @@ optimize_if_const_unaryop(basicblock *bb, int i, int nextop,
18641868
/* nothing more to do here */
18651869
return SUCCESS;
18661870
}
1871+
remove_to_bool_sequence(bb, i+1);
18671872
if (optimize_unary_not_is_contains(bb, i, nextop)) {
18681873
/* if optimized, no need to continue */
18691874
return SUCCESS;
@@ -1875,6 +1880,10 @@ optimize_if_const_unaryop(basicblock *bb, int i, int nextop,
18751880
assert(PyTuple_Size(seq) == 1);
18761881
PyObject *operand = PyTuple_GET_ITEM(seq, 0);
18771882
PyObject *newconst = eval_const_unaryop(operand, instr->i_opcode, unarypos);
1883+
if (instr->i_opcode == UNARY_NOT) {
1884+
/* we eliminated TO_BOOL's so we must return boolean */
1885+
assert(PyBool_Check(newconst));
1886+
}
18781887
Py_DECREF(seq);
18791888
return make_const(newconst, bb, i, 1, consts, const_cache);
18801889
}

0 commit comments

Comments
 (0)
0