@@ -1762,96 +1762,6 @@ eval_const_unaryop(PyObject *operand, int op, bool unarypos)
1762
1762
return result ;
1763
1763
}
1764
1764
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
-
1855
1765
static int
1856
1766
optimize_if_const_unaryop (basicblock * bb , int i , int nextop ,
1857
1767
PyObject * consts , PyObject * const_cache , bool unarypos )
@@ -1870,9 +1780,6 @@ optimize_if_const_unaryop(basicblock *bb, int i, int nextop,
1870
1780
|| instr -> i_opcode == UNARY_NOT
1871
1781
);
1872
1782
}
1873
- if (instr -> i_opcode == UNARY_NOT && optimize_unary_not_non_const (bb , i , nextop )) {
1874
- return SUCCESS ;
1875
- }
1876
1783
PyObject * seq ;
1877
1784
RETURN_IF_ERROR (get_constant_sequence (bb , i - 1 , 1 , consts , & seq ));
1878
1785
RETURN_IF_NOT_CONST_SEQ (seq );
@@ -2358,6 +2265,13 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
2358
2265
INSTR_SET_OP1 (& bb -> b_instr [i + 1 ], opcode , oparg );
2359
2266
continue ;
2360
2267
}
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
+ }
2361
2275
break ;
2362
2276
case TO_BOOL :
2363
2277
if (nextop == TO_BOOL ) {
@@ -2368,6 +2282,18 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
2368
2282
case UNARY_NOT :
2369
2283
case UNARY_INVERT :
2370
2284
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
+ }
2371
2297
RETURN_IF_ERROR (optimize_if_const_unaryop (bb , i , nextop , consts , const_cache , false));
2372
2298
break ;
2373
2299
case CALL_INTRINSIC_1 :
0 commit comments