@@ -1849,8 +1849,13 @@ def optimizeCFG(self) -> None:
18491849
18501850 self .remove_unused_consts ()
18511851 self .add_checks_for_loads_of_uninitialized_variables ()
1852+ self .insert_superinstructions ()
18521853 self .push_cold_blocks_to_end (except_handlers , optimizer )
18531854
1855+ def insert_superinstructions (self ) -> None :
1856+ # No super instructions on 3.12
1857+ pass
1858+
18541859 def build_cell_fixed_offsets (self ) -> list [int ]:
18551860 nlocals = len (self .varnames )
18561861 ncellvars = len (self .cellvars )
@@ -2770,6 +2775,46 @@ def push_todo_block(block: Block) -> None:
27702775 block .next .except_stack = except_stack
27712776 push_todo_block (block .next )
27722777
2778+ def make_super_instruction (
2779+ self , inst1 : Instruction , inst2 : Instruction , super_op : str
2780+ ) -> None :
2781+ # pyre-ignore[16]: lineno is maybe not defined on AST
2782+ line1 = inst1 .loc .lineno
2783+ # pyre-ignore[16]: lineno is maybe not defined on AST
2784+ line2 = inst2 .loc .lineno
2785+ # Skip if instructions are on different lines
2786+ if line1 >= 0 and line2 >= 0 and line1 != line2 :
2787+ return
2788+
2789+ if inst1 .ioparg >= 16 or inst2 .ioparg >= 16 :
2790+ return
2791+
2792+ inst1 .opname = super_op
2793+ inst1 .ioparg = (inst1 .ioparg << 4 ) | inst2 .ioparg
2794+ inst2 .set_to_nop ()
2795+
2796+ def insert_superinstructions (self ) -> None :
2797+ for block in self .ordered_blocks :
2798+ for i , instr in enumerate (block .insts ):
2799+ if i + 1 == len (block .insts ):
2800+ break
2801+
2802+ next_instr = block .insts [i + 1 ]
2803+ if instr .opname == "LOAD_FAST" :
2804+ if next_instr .opname == "LOAD_FAST" :
2805+ self .make_super_instruction (
2806+ instr , next_instr , "LOAD_FAST_LOAD_FAST"
2807+ )
2808+ elif instr .opname == "STORE_FAST" :
2809+ if next_instr .opname == "LOAD_FAST" :
2810+ self .make_super_instruction (
2811+ instr , next_instr , "STORE_FAST_LOAD_FAST"
2812+ )
2813+ elif next_instr .opname == "STORE_FAST" :
2814+ self .make_super_instruction (
2815+ instr , next_instr , "STORE_FAST_STORE_FAST"
2816+ )
2817+
27732818 _const_opcodes : set [str ] = set (PyFlowGraph312 ._const_opcodes ) | {"LOAD_SMALL_INT" }
27742819
27752820
0 commit comments