-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize DUP1, ISZERO and POP in ifStatement #15097
Comments
Another similar case is the external call. Description
The above function will generate the following opcode.
The corresponding assembly code is:
In tag_11, there is a 'POP' just after 'JUMPI', which means the original value of the DUP is not used. Although this value is used in the other branch(tag_12), this can also be optimized. OptimizationSpecifically, moving tag_12 after
solidity/libsolidity/codegen/ExpressionCompiler.cpp Lines 2854 to 2859 in b849b32
solidity/libsolidity/codegen/ExpressionCompiler.cpp Lines 2923 to 2928 in b849b32
In this function, endTag is tag_12 in this example. solidity/libsolidity/codegen/ContractCompiler.cpp Lines 965 to 977 in b849b32
In this function, successTag is tag_15 in this example. Is it possible to move the conditionalJump in this function to the front of the endTag in the function appendExternalFunctionCall so that we can save one duplication and pop? |
Description
The above function will generate the following opcode.
The corresponding assembly code is:
In tag_7, there is a 'POP' just after 'JUMPI', which means the original value of the DUP is not used. Although this value is used in the other branch(tag_11), this can also be optimized.
Optimization
Specifically, moving tag_11 after
iszero tag_12 jumpi
would achieve this optimization. The resulting assembly code is as follows:In this way, for a>b, it will execute
PUSH1 DUP2 DUP4 GT PUSH2 JUMPI JUMPDEST DUP2 DUP4
instead ofPUSH1 DUP2 DUP4 GT DUP1 PUSH2 JUMPI JUMPDEST ISZERO PUSH2 JUMPI DUP2 DUP4
. For a<=b and a>100, it will executePUSH1 DUP2 DUP4 GT PUSH2 JUMPI PUSH1 DUP4 GT ISZERO PUSH2 JUMPI JUMPDEST DUP2 DUP4
instead ofPUSH1 DUP2 DUP4 GT DUP1 PUSH2 JUMPI POP PUSH1 DUP4 GT JUMPDEST ISZERO PUSH2 JUMPI DUP2 DUP4
.solidity/libsolidity/codegen/ExpressionCompiler.cpp
Lines 2367 to 2380 in b849b32
In this function, the endLabel is tag_11 in this example.
solidity/libsolidity/codegen/ContractCompiler.cpp
Lines 1153 to 1172 in b849b32
Is it possible to move the Instruction::ISZERO and conditionalJump in this function to the front of the endLabel in the function appendAndOrOperatorCode?
The text was updated successfully, but these errors were encountered: