Redundant `ISZERO` and `PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND` in covertType
- Dominant language
- C++
- Stars
- 25.7k
- Forks
- 6.2k
- Avg merge
- 2d 19h
- Merged PRs (30d)
- 29
Description
## Environment
version: 0.8.25
setting: --optimize-runs: 20000
## Description
```
contract Demo {
struct A {
uint b;
bool c;
address d;
}
A sa;
constructor(A memory p) {
sa = p;
}
function covert() public returns(uint) {
A memory ma = sa;
return ma.b;
}
}
```
The above code can generate the following opcodes:
```
PUSH1 0x0 SWAP1 SLOAD SWAP1 PUSH2 0x100 EXP SWAP1 DIV PUSH1 0xFF AND ISZERO ISZERO ISZERO ISZERO DUP2 MSTORE PUSH1 0x20 ADD PUSH1 0x1 DUP3 ADD PUSH1 0x1 SWAP1 SLOAD SWAP1 PUSH2 0x100 EXP SWAP1 DIV PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND DUP2 MSTORE POP POP
```
There are four consecutive ```ISZERO``` operations. The last two are redundant because they do not change the result.
Similarly, there are three consecutive ```PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND``` operations. The last two are also redundant because they do not change the result.
https://github.com/ethereum/solidity/blob/0f982266663246437cca867ebd3973df99c7512f/libsolidity/codegen/CompilerUtils.cpp#L1171-L1197
https://github.com/ethereum/solidity/blob/0f982266663246437cca867ebd3973df99c7512f/libsolidity/codegen/CompilerUtils.cpp#L231-L236
Two ```ISZERO``` are generated at line 1194 in the function ```convertType```, while the other two are generated at line 233 in the function ```storeInMemoryDynamic```.
https://github.com/ethereum/solidity/blob/0f982266663246437cca867ebd3973df99c7512f/libsolidity/codegen/LValue.cpp#L276-L280
One ```PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND``` is generated at line 279 in the function ```retrieveValue```, another at line 1194 in the function ```convertType```, and the last one at line 233 in the function ```storeInMemoryDynamic```.
Is it possible to remove the two redundant ```ISZERO``` and the two redundant ```PUSH AND```?
Contributor guide
Research direction
Start by compiling the provided Demo contract with Solidity 0.8.25 and --optimize-runs 20000, then inspect CompilerUtils.cpp functions convertType and storeInMemoryDynamic alongside LValue.cpp retrieveValue. The work is done when the generated opcodes no longer contain the redundant consecutive ISZERO operations or repeated PUSH20 mask-and sequences.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, solidity
- Domain
- blockchain, compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100