solidity icon indicating copy to clipboard operation
solidity copied to clipboard

Redundant `ISZERO` and `PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND` in covertType

Open molly-ting opened this issue 1 year ago • 0 comments

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?

molly-ting avatar Jun 08 '24 04:06 molly-ting