diff --git a/docs/internals/optimizer.rst b/docs/internals/optimizer.rst index 64c44506a819..70896333c2bc 100644 --- a/docs/internals/optimizer.rst +++ b/docs/internals/optimizer.rst @@ -23,10 +23,16 @@ the function calls. Similarly, if a function is side-effect free and its result is multiplied by zero, you can remove the function call completely. -The codegen-based optimizer consists of two steps, the first of which changes the -order of literals in binary operators, moving them to the right. The second one -enables the compiler to utilize unchecked arithmetic when generating code for -incrementing the counter variable in certain ``for`` loops. +The codegen-based optimizer affects the initial low-level code produced from the Solidity input. +In the legacy pipeline, the bytecode is generated immediately and most of the optimizations of this +kind are implicit and not configurable, the only exception being an optimization which changes the +order of literals in binary operations. +The IR-based pipeline takes a different approach and produces Yul IR closely matching the structure +of the Solidity code, with nearly all optimizations deferred to the Yul optimizer module. +In that case codegen-level optimization is done only in very limited cases which are difficult to +handle in Yul IR, but are straightforward with the high-level information from analysis phase at hand. +An example of such an optimization is the bypass of checked arithmetic when incrementing the counter +in certain idiomatic ``for`` loops. Currently, the parameter ``--optimize`` activates the opcode-based optimizer for the generated bytecode and the Yul optimizer for the Yul code generated internally, for example for ABI coder v2. @@ -1401,16 +1407,17 @@ The LiteralRematerialiser should be run before this step. Codegen-Based Optimizer Module ============================== -Currently, the codegen-based optimizer module consists of two distinct steps. +Currently, the codegen-based optimizer module provides two optimizations. -The first step moves literals to the right of commutative binary operators which -helps exploit associativity. +The first one, available in the legacy code generator, moves literals to the right side of +commutative binary operators, which helps exploit their associativity. -The second step uses unchecked arithmetic when generating code for incrementing -the counter variable of specific ``for`` loops. This -avoids wasting gas by identifying some conditions that guarantee -that the counter variable cannot overflow. This eliminates the need to use a verbose -unchecked arithmetic block inside the loop body to increment the counter variable. +The other one, available in the IR-based code generator, enables the use of unchecked arithmetic +when generating code for incrementing the counter variable of certain idiomatic ``for`` loops. +This avoids wasting gas by identifying some conditions that guarantee that the counter variable +cannot overflow. +This eliminates the need to use a verbose unchecked arithmetic block inside the loop body to +increment the counter variable. .. _unchecked-loop-optimizer: