-
Notifications
You must be signed in to change notification settings - Fork 52
Closed
Description
These instructions are used for boolean operations, either explicit a or b
or implicit a < b < c
.
We should remove these instructions for a few reasons:
- These two bytecodes represent only 0.2% of all instructions.
- They add quite a lot of complexity to the compiler
- PEP 669 requires an instrumented version of all jumps, so removing them will remove 4 instructions, not just 2.
- They can be trivially replaced:
JUMP_IF_FALSE_OR_POP
becomesCOPY 1; POP_JUMP_IF_FALSE; POP_TOP
.
We can avoid generating much, if any, extra code by better peephole optimization, or more sophisticated code generation.
Possible optimizations
Code generation
If there are no walrus expressions (x := ...
) in the statement, then LOAD_FAST
instructions can be freely re-ordered and duplicated.
a < b < c
can be implemented as (a < b) or (b < c)
without the need to store b
on the stack.
Currently we generate the sequence:
LOAD_FAST 0 (a)
LOAD_FAST 1 (b)
SWAP 2
COPY 2
COMPARE (<)
POP_JUMP_IF_FALSE
LOAD_FAST 2 (c)
COMPARE (<)
POP_JUMP_IF_FALSE
We could generate:
LOAD_FAST 0 (a)
LOAD_FAST 1 (b)
COMPARE (<)
POP_JUMP_IF_FALSE
LOAD_FAST 1 (b)
LOAD_FAST 2 (c)
COMPARE (<)
POP_JUMP_IF_FALSE
CFG optimization
The sequence:
LOAD_FAST 0 (a)
LOAD_FAST 1 (b)
SWAP 2
can be changed to:
LOAD_FAST 1 (b)
LOAD_FAST 0 (a)
The sequence:
LOAD_FAST a
COPY 1
POP_JUMP_IF_FALSE label
POP_TOP
...
label:
...
can be changed to:
LOAD_FAST a
POP_JUMP_IF_FALSE label
...
label:
LOAD_FAST a
...
(Provided that label
has only one predecessor)
Fidget-Spinner, JuliaPoo and santiagoserranomarcosiemens
Metadata
Metadata
Assignees
Labels
No labels