argotorg / argotorg/solidity

Redundant DUP1 and POP in CallValueCheck

Open
#15,165 0 comments 0 reactions 0 assignees View on GitHub
bug :bug:
Dominant language
C++
Stars
25.7k
Forks
6.2k
Avg merge
2d 19h
Merged PRs (30d)
29

Description

## Description

https://github.com/ethereum/solidity/blob/b849b327781cb71478709b28c4d0d372492cbdc1/libsolidity/codegen/ContractCompiler.cpp#L141-L146
The call value check will generate the following opcodes:
```
CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP
```
The original value of the first DUP1 is not used; it is not accessed in the revert branch and popped in the other branch. Therefore, the DUP1 operation is redundant. I think we can directly use the original value. The optimized opcodes could be:
```
CALLVALUE ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST
```

## Optimization
https://github.com/ethereum/solidity/blob/b849b327781cb71478709b28c4d0d372492cbdc1/libsolidity/codegen/CompilerContext.cpp#L357-L370
In this case, the function ```revertReasonIfDebug``` returns "revert(0,0)". For this special case, could we simply return the optimized opcodes to eliminate the redundant dup1 and pop?
https://github.com/ethereum/solidity/blob/b849b327781cb71478709b28c4d0d372492cbdc1/libsolidity/codegen/CompilerContext.cpp#L427-L428
Or is it possible to optimize this dup1 and remove the pop in function appendConditionalRevert?

Contributor guide

Open the contributing guide

Research direction

Start in libsolidity/codegen/ContractCompiler.cpp at the call value check, then read CompilerContext.cpp around revertReasonIfDebug and appendConditionalRevert. Inspect the generated opcode sequence and relevant compiler tests if available; done means removing the redundant DUP1 and POP for the described revert(0,0) case without changing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, solidity
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.