@@ -1762,11 +1762,15 @@ eval_const_unaryop(PyObject *operand, int op, bool unarypos)
1762
1762
}
1763
1763
1764
1764
static void
1765
- remove_redundant_to_bool (basicblock * bb , int start )
1765
+ remove_to_bool_sequence (basicblock * bb , int start )
1766
1766
{
1767
1767
assert (start >= 0 );
1768
1768
for (;start < bb -> b_iused ; start ++ ) {
1769
1769
cfg_instr * curr = & bb -> b_instr [start ];
1770
+ if (curr -> i_opcode == NOP ) {
1771
+ /* should not happen, but just in case */
1772
+ continue ;
1773
+ }
1770
1774
if (curr -> i_opcode == TO_BOOL ) {
1771
1775
INSTR_SET_OP0 (curr , NOP );
1772
1776
continue ;
@@ -1799,8 +1803,9 @@ maybe_unary_not_cancel_out(basicblock *bb, int i, int nextop)
1799
1803
assert (instr -> i_opcode == UNARY_NOT );
1800
1804
if (nextop == UNARY_NOT ) {
1801
1805
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 ];
1804
1809
assert (next -> i_opcode == nextop );
1805
1810
INSTR_SET_OP0 (next , NOP );
1806
1811
return true;
@@ -1829,7 +1834,6 @@ optimize_unary_not_is_contains(basicblock *bb, int i, int nextop)
1829
1834
{
1830
1835
cfg_instr * instr = & bb -> b_instr [i ];
1831
1836
assert (instr -> i_opcode == UNARY_NOT );
1832
- remove_redundant_to_bool (bb , i + 1 );
1833
1837
cfg_instr * target = find_unary_not_target (bb , i - 1 );
1834
1838
if (target == NULL ) {
1835
1839
return false;
@@ -1864,6 +1868,7 @@ optimize_if_const_unaryop(basicblock *bb, int i, int nextop,
1864
1868
/* nothing more to do here */
1865
1869
return SUCCESS ;
1866
1870
}
1871
+ remove_to_bool_sequence (bb , i + 1 );
1867
1872
if (optimize_unary_not_is_contains (bb , i , nextop )) {
1868
1873
/* if optimized, no need to continue */
1869
1874
return SUCCESS ;
@@ -1875,6 +1880,10 @@ optimize_if_const_unaryop(basicblock *bb, int i, int nextop,
1875
1880
assert (PyTuple_Size (seq ) == 1 );
1876
1881
PyObject * operand = PyTuple_GET_ITEM (seq , 0 );
1877
1882
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
+ }
1878
1887
Py_DECREF (seq );
1879
1888
return make_const (newconst , bb , i , 1 , consts , const_cache );
1880
1889
}
0 commit comments