@@ -719,6 +719,80 @@ const uint16_t op_without_pop[MAX_UOP_ID + 1] = {
719719 [_POP_TOP_UNICODE ] = _NOP ,
720720};
721721
722+ static int
723+ previous_non_skip_uop (_PyUOpInstruction * buffer , int pc )
724+ {
725+ while (pc >= 0 && op_skip [buffer [pc ].opcode ]) {
726+ pc -- ;
727+ }
728+ return pc ;
729+ }
730+
731+ /* Remove redundant stack shuffles left by constant-folding rewrites:
732+ * push push _SWAP(2) pop
733+ * push push push _RROT_3 pop pop
734+ * In both forms, only the push closest to the shuffle survives. */
735+ static bool
736+ remove_folded_stack_shuffle (_PyUOpInstruction * buffer , int pc , int arity )
737+ {
738+ int pops [2 ];
739+ int idx = pc ;
740+ for (int i = 0 ; i < arity - 1 ; i ++ ) {
741+ if (idx < 0 || !op_without_pop [buffer [idx ].opcode ]) {
742+ return false;
743+ }
744+ pops [i ] = idx ;
745+ idx = previous_non_skip_uop (buffer , idx - 1 );
746+ }
747+ int shuf = idx ;
748+ if (shuf < 0 ) {
749+ return false;
750+ }
751+ uint16_t shuf_op = buffer [shuf ].opcode ;
752+ bool is_shuffle =
753+ (arity == 2 && shuf_op == _SWAP && buffer [shuf ].oparg == 2 ) ||
754+ (arity == 3 && shuf_op == _RROT_3 );
755+ if (!is_shuffle ) {
756+ return false;
757+ }
758+
759+ int dead_pushes [2 ];
760+ for (int i = 0 ; i < arity ; i ++ ) {
761+ idx = previous_non_skip_uop (buffer , idx - 1 );
762+ if (idx < 0 ) {
763+ return false;
764+ }
765+ uint16_t push_op = buffer [idx ].opcode ;
766+ if (push_op == _COPY || !op_without_push [push_op ]) {
767+ return false;
768+ }
769+ if (i > 0 ) {
770+ dead_pushes [i - 1 ] = idx ;
771+ }
772+ }
773+
774+ for (int i = 0 ; i < arity - 1 ; i ++ ) {
775+ buffer [dead_pushes [i ]].opcode = _NOP ;
776+ }
777+ buffer [shuf ].opcode = _NOP ;
778+ for (int i = 0 ; i < arity - 1 ; i ++ ) {
779+ buffer [pops [i ]].opcode = _NOP ;
780+ }
781+ return true;
782+ }
783+
784+ static bool
785+ remove_adjacent_push_pop (_PyUOpInstruction * buffer , int pc )
786+ {
787+ int last = previous_non_skip_uop (buffer , pc - 1 );
788+ if (last < 0 || !op_without_push [buffer [last ].opcode ]) {
789+ return false;
790+ }
791+ buffer [last ].opcode = _NOP ;
792+ buffer [pc ].opcode = _NOP ;
793+ return true;
794+ }
795+
722796
723797static int
724798remove_unneeded_uops (_PyUOpInstruction * buffer , int buffer_size )
@@ -755,21 +829,14 @@ remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size)
755829 // ...becomes:
756830 // _NOP + _NOP + _POP_TOP + _NOP + _NOP
757831 while (op_without_pop [opcode ]) {
758- _PyUOpInstruction * last = & buffer [pc - 1 ];
759- while (op_skip [last -> opcode ]) {
760- last -- ;
761- }
762- if (op_without_push [last -> opcode ] && op_without_pop [opcode ]) {
763- last -> opcode = op_without_push [last -> opcode ];
764- opcode = buffer [pc ].opcode = op_without_pop [opcode ];
765- if (op_without_pop [last -> opcode ]) {
766- opcode = last -> opcode ;
767- pc = (int )(last - buffer );
768- }
769- }
770- else {
771- break ;
832+ if (remove_folded_stack_shuffle (buffer , pc , 2 ) ||
833+ remove_folded_stack_shuffle (buffer , pc , 3 ) ||
834+ remove_adjacent_push_pop (buffer , pc ))
835+ {
836+ opcode = buffer [pc ].opcode ;
837+ continue ;
772838 }
839+ break ;
773840 }
774841 /* _PUSH_FRAME doesn't escape or error, but it
775842 * does need the IP for the return address */
0 commit comments