-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ Table Of Contents | |
|
||
usage | ||
api | ||
peephole | ||
changelog | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
****************** | ||
Peephole Optimizer | ||
****************** | ||
|
||
Peephole optimizer based on the peephole optimizer of CPython 3.6 written in C. | ||
|
||
It is implemented with the :class:`BytecodeBlocks` class. | ||
|
||
|
||
Optimizations | ||
============= | ||
|
||
Optimizations implemented in the peephole optimizer: | ||
|
||
* Constant folding | ||
|
||
- unary operations: +a, -a, ~a | ||
- binary operations: | ||
|
||
* a + b, a - b, a * b, a / b, a // b, a % b, a ** b | ||
* a << b, a >> b, a & b, a | b, a ^ b | ||
|
||
- BUILD_TUPLE: convert to a constant | ||
- BUILD_TUPLE + UNPACK_SEQUENCE | ||
- BUILD_LIST + UNPACK_SEQUENCE | ||
- BUILD_LIST + COMPARE_OP(in/not in): convert list to tuple | ||
- BUILD_SET + COMPARE_OP(in/not in): convert set to frozenset | ||
- COMPARE_OP: | ||
|
||
* replace ``not(a is b)`` with ``a is not b`` | ||
* replace ``not(a is not b)`` with ``a is b`` | ||
* replace ``not(a in b)`` with ``a not in b`` | ||
* replace ``not(a not in b)`` with ``a in b`` | ||
|
||
* Dead code elimination | ||
|
||
- Remove code after final operations in the current block: | ||
see :meth:`Instr.is_final` | ||
- Remove unreachable blocks (:class:`Block`) | ||
|
||
* Replace UNARY_NOT+POP_JUMP_IF_FALSE with POP_JUMP_IF_TRUE | ||
|
||
* Optimize jumps | ||
|
||
- Replace unconditional jumps to RETURN_VALUE with RETURN_VALUE | ||
- Replace jumps to unconditional jumps with jumps to the final target | ||
|
||
For tuples, constant folding is only run if the result has 20 items or less. | ||
|
||
By design, only basic optimizations can be implemented. A peephole optimizer | ||
has a narrow view on the bytecode (a few instructions) and only a very limited | ||
knownledge of the code. | ||
|
||
.. note:: | ||
``3 < 5`` or ``(1, 2, 3)[1]`` are not optimized. | ||
|
||
|
||
API | ||
=== | ||
|
||
.. class:: PeepholeOptimizer | ||
|
||
.. method:: optimize(code: types.CodeType) -> types.CodeType | ||
|
||
Optimize a Python code object. | ||
|
||
Return a new optimized Python code object. | ||
|
||
|
||
.. class:: CodeTransformer | ||
|
||
Code transformer for the API of the `PEP 511 | ||
<https://www.python.org/dev/peps/pep-0511/>`_ (API for code transformers). | ||
|
||
.. method:: code_transformer(code, context) | ||
|
||
Run the :class:`PeepholeOptimizer` optimizer on the code. | ||
|
||
Return a new optimized Python code object. |