Skip to content
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

Redundant ISZERO and PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND in covertType #15190

Open
molly-ting opened this issue Jun 8, 2024 · 0 comments
Labels

Comments

@molly-ting
Copy link

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.

case DataLocation::Memory:
// Copy the array to a free position in memory, unless it is already in memory.
switch (typeOnStack.location())
{
case DataLocation::Storage:
{
auto conversionImpl =
[typeOnStack = &typeOnStack, targetType = &targetType](CompilerContext& _context)
{
CompilerUtils utils(_context);
// stack: <source ref>
utils.allocateMemory(typeOnStack->memoryDataSize());
_context << Instruction::SWAP1 << Instruction::DUP2;
// stack: <memory ptr> <source ref> <memory ptr>
for (auto const& member: typeOnStack->members(nullptr))
{
solAssert(!member.type->containsNestedMapping());
std::pair<u256, unsigned> const& offsets = typeOnStack->storageOffsetsOfMember(member.name);
_context << offsets.first << Instruction::DUP3 << Instruction::ADD;
_context << u256(offsets.second);
StorageItem(_context, *member.type).retrieveValue(SourceLocation(), true);
Type const* targetMemberType = targetType->memberType(member.name);
solAssert(!!targetMemberType, "Member not found in target type.");
utils.convertType(*member.type, *targetMemberType, true);
utils.storeInMemoryDynamic(*targetMemberType, true);
}
_context << Instruction::POP << Instruction::POP;

else if (_type.isValueType())
{
unsigned numBytes = prepareMemoryStore(_type, _padToWordBoundaries, _cleanup);
m_context << Instruction::DUP2 << Instruction::MSTORE;
m_context << u256(numBytes) << Instruction::ADD;
}

Two ISZERO are generated at line 1194 in the function convertType, while the other two are generated at line 233 in the function storeInMemoryDynamic.
if (!cleaned)
{
solAssert(type->sizeOnStack() == 1, "");
m_context << ((u256(0x1) << (8 * type->storageBytes())) - 1) << Instruction::AND;
}

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant