Skip to content

Commit 0a857d1

Browse files
DinoVfacebook-github-bot
authored andcommitted
Optimize CONTAINS_OP and IS_OP
Summary: More optimizations around `TO_BOOL` - this time contains is known to return a bool so the conversion can be removed, and UNARY_NOT can be inverted and removed. Reviewed By: alexmalyshev Differential Revision: D80987788 fbshipit-source-id: 49559b84dd28a1c5fd9b32564c0122f2ed1b8c60
1 parent 59ab86f commit 0a857d1

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

cinderx/PythonLib/cinderx/compiler/flow_graph_optimizer.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,13 +964,36 @@ def opt_store_fast(
964964
):
965965
instr.set_to_nop()
966966

967+
def optimize_contains_is_op(
968+
self: FlowGraphOptimizer,
969+
instr_index: int,
970+
instr: Instruction,
971+
next_instr: Instruction | None,
972+
target: Instruction | None,
973+
block: Block,
974+
) -> int | None:
975+
if next_instr is None:
976+
return
977+
if next_instr.opname == "TO_BOOL":
978+
next_instr.opname = instr.opname
979+
next_instr.oparg = instr.oparg
980+
next_instr.ioparg = instr.ioparg
981+
instr.set_to_nop()
982+
elif next_instr.opname == "UNARY_NOT":
983+
next_instr.opname = instr.opname
984+
next_instr.oparg = instr.oparg
985+
next_instr.ioparg = instr.ioparg ^ 1
986+
instr.set_to_nop()
987+
967988
handlers: dict[str, Handler] = {
968989
**FlowGraphOptimizer312.handlers,
969990
"JUMP_IF_FALSE": opt_jump_if,
970991
"JUMP_IF_TRUE": opt_jump_if,
971992
"BUILD_LIST": optimize_lists_and_sets,
972993
"BUILD_SET": optimize_lists_and_sets,
973994
"COMPARE_OP": optimize_compare_op,
995+
"CONTAINS_OP": optimize_contains_is_op,
996+
"IS_OP": optimize_contains_is_op,
974997
"LOAD_GLOBAL": optimize_load_global,
975998
"JUMP_NO_INTERRUPT": opt_jump_no_interrupt,
976999
"STORE_FAST": opt_store_fast,

0 commit comments

Comments
 (0)