Comments (2)
Another similar case is the external call.
Description
function extcall(uint256 a) public returns(uint256) {
uint256 res = 0;
try l.lib(a) returns (uint256 tmp) {
res=tmp*2;
} catch {
res = 1;
}
return res;
}
The above function will generate the following opcode.
DUP1 ISZERO PUSH2 0xF5 JUMPI POP PUSH1 0x40 MLOAD ... JUMP JUMPDEST PUSH1 0x1 JUMPDEST PUSH2 0x102 JUMPI PUSH1 0x1 SWAP1 POP
The corresponding assembly code is:
...
tag_11:
...
gas
call
swap3
pop
pop
pop
dup1
iszero
tag_12
jumpi
pop
mload(0x40)
…
jump // in
tag_13:
0x01
tag_12:
/* "extcall.sol":481:589 try l.lib(a) returns (uint256 tmp) {... */
tag_15
jumpi
/* "extcall.sol":577:578 1 */
0x01
/* "extcall.sol":571:578 res = 1 */
swap1
pop
/* "extcall.sol":481:589 try l.lib(a) returns (uint256 tmp) {... */
jump(tag_19)
...
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.
Optimization
Specifically, moving tag_12 after tag_15 jumpi
would achieve this optimization. The resulting assembly code is as follows:
...
tag_11:
...
gas
call
swap3
pop
pop
pop
iszero
tag_12
jumpi
mload(0x40)
…
jump // in
tag_13:
0x01
/* "extcall.sol":481:589 try l.lib(a) returns (uint256 tmp) {... */
tag_15
jumpi
tag_12:
/* "extcall.sol":577:578 1 */
0x01
/* "extcall.sol":571:578 res = 1 */
swap1
pop
/* "extcall.sol":481:589 try l.lib(a) returns (uint256 tmp) {... */
jump(tag_19)
...
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?
from solidity.
Could someone take a look at this? I believe it could reduce gas consumption
from solidity.
Related Issues (20)
- Parser fails when decoding with abi.encode, thus it doesn't allow unpacking to variables that are declared differently (e.g. statement-declared and the ones declared in advance) HOT 5
- Optimized PUSH0 and POP in clearStorageLoop HOT 2
- Optimize resizeDynamicArray
- Segmentation fault in solc in function solidity::frontend::experimental::Analysis::annotationContainer, file libsolidity/experimental/analysis/Analysis.cpp HOT 6
- Deduplicate bytecode dependencies used by both creation and deployed object HOT 2
- Reuse optimized IR/bytecode for bytecode dependencies
- SMTChecker: Underflow and overflow targets not checked or reported on the same expression for BMC engine HOT 1
- Redundant `ISZERO` and `PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND` in covertType
- [Yul EVM code transform] Stack too deep in standalone Yul test case
- ICE in `Z3Interface::fromZ3Expr()` when running tests on Arch Linux HOT 8
- Seems like an incorrect type inference... HOT 3
- Update Z3 to 4.13.0 or later HOT 1
- Consider changing linker on Linux from `ld.gold` to `ld`
- Division by zero is checked in Unchecked blocks HOT 4
- Copyright year is 2016-2023 instead of 2016-2024 HOT 1
- Where can I find Test Sepolia so I can deploy a contract on a testnet? HOT 1
- Segfault in AsmParser for debug-compiled solc
- Copyright years are 2016-2023, should to be 2016-2024 HOT 1
- Support for CodeSection type for linking externally written/compiled bytecode with EOF supported Solidity contracts. HOT 1
- require() to accept custom errors instead of a message HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from solidity.