some more work

This commit is contained in:
chriseth 2022-11-25 14:20:38 +01:00
parent bd7676873e
commit ddbcea047b
4 changed files with 395 additions and 339 deletions

View File

@ -47,6 +47,9 @@ void UnusedAssignEliminator::run(OptimiserStepContext& _context, Block& _ast)
set<Statement const*> toRemove; set<Statement const*> toRemove;
for (Statement const* unusedStore: rae.m_allStores - rae.m_usedStores) for (Statement const* unusedStore: rae.m_allStores - rae.m_usedStores)
// TODO this should also use user function side effects.
// Then we have to modify the multi-assign test (or verify that it is fine after all
// by adding a test where one var is used but not the other)
if (SideEffectsCollector{_context.dialect, *std::get<Assignment>(*unusedStore).value}.movable()) if (SideEffectsCollector{_context.dialect, *std::get<Assignment>(*unusedStore).value}.movable())
toRemove.insert(unusedStore); toRemove.insert(unusedStore);
else else
@ -111,8 +114,6 @@ void UnusedAssignEliminator::visit(Statement const& _statement)
if (auto const* assignment = get_if<Assignment>(&_statement)) if (auto const* assignment = get_if<Assignment>(&_statement))
{ {
// TODO is it OK to do this for multi-assignments? I guess so because it is enough if
// one of them is used.
m_allStores.insert(&_statement); m_allStores.insert(&_statement);
for (auto const& var: assignment->variableNames) for (auto const& var: assignment->variableNames)
m_activeStores[var.name] = {&_statement}; m_activeStores[var.name] = {&_statement};

View File

@ -52,122 +52,124 @@ static string const thirtyTwo{"@ 32"};
void UnusedStoreEliminator::run(OptimiserStepContext& /*_context*/, Block& /*_ast*/) void UnusedStoreEliminator::run(OptimiserStepContext& /*_context*/, Block& /*_ast*/)
{ {
// map<YulString, SideEffects> functionSideEffects = SideEffectsPropagator::sideEffects( map<YulString, SideEffects> functionSideEffects = SideEffectsPropagator::sideEffects(
// _context.dialect, _context.dialect,
// CallGraphGenerator::callGraph(_ast) CallGraphGenerator::callGraph(_ast)
// ); );
// SSAValueTracker ssaValues; SSAValueTracker ssaValues;
// ssaValues(_ast); ssaValues(_ast);
// map<YulString, AssignedValue> values; map<YulString, AssignedValue> values;
// for (auto const& [name, expression]: ssaValues.values()) for (auto const& [name, expression]: ssaValues.values())
// values[name] = AssignedValue{expression, {}}; values[name] = AssignedValue{expression, {}};
// Expression const zeroLiteral{Literal{{}, LiteralKind::Number, YulString{"0"}, {}}}; Expression const zeroLiteral{Literal{{}, LiteralKind::Number, YulString{"0"}, {}}};
// Expression const oneLiteral{Literal{{}, LiteralKind::Number, YulString{"1"}, {}}}; Expression const oneLiteral{Literal{{}, LiteralKind::Number, YulString{"1"}, {}}};
// Expression const thirtyTwoLiteral{Literal{{}, LiteralKind::Number, YulString{"32"}, {}}}; Expression const thirtyTwoLiteral{Literal{{}, LiteralKind::Number, YulString{"32"}, {}}};
// values[YulString{zero}] = AssignedValue{&zeroLiteral, {}}; values[YulString{zero}] = AssignedValue{&zeroLiteral, {}};
// values[YulString{one}] = AssignedValue{&oneLiteral, {}}; values[YulString{one}] = AssignedValue{&oneLiteral, {}};
// values[YulString{thirtyTwo}] = AssignedValue{&thirtyTwoLiteral, {}}; values[YulString{thirtyTwo}] = AssignedValue{&thirtyTwoLiteral, {}};
// bool const ignoreMemory = MSizeFinder::containsMSize(_context.dialect, _ast); bool const ignoreMemory = MSizeFinder::containsMSize(_context.dialect, _ast);
// UnusedStoreEliminator rse{ UnusedStoreEliminator rse{
// _context.dialect, _context.dialect,
// functionSideEffects, functionSideEffects,
// ControlFlowSideEffectsCollector{_context.dialect, _ast}.functionSideEffectsNamed(), ControlFlowSideEffectsCollector{_context.dialect, _ast}.functionSideEffectsNamed(),
// values, values,
// ignoreMemory ignoreMemory
// }; };
// rse(_ast); rse(_ast);
// if ( if (
// auto evmDialect = dynamic_cast<EVMDialect const*>(&_context.dialect); auto evmDialect = dynamic_cast<EVMDialect const*>(&_context.dialect);
// evmDialect && evmDialect->providesObjectAccess() evmDialect && evmDialect->providesObjectAccess()
// ) )
// rse.changeUndecidedTo(State::Unused, Location::Memory); {
// else }
// rse.changeUndecidedTo(State::Used, Location::Memory); else
// rse.changeUndecidedTo(State::Used, Location::Storage); rse.markActiveAsUsed(Location::Memory);
// rse.scheduleUnusedForDeletion(); rse.markActiveAsUsed(Location::Storage);
rse.scheduleUnusedForDeletion();
// StatementRemover remover(rse.m_pendingRemovals); StatementRemover remover(rse.m_pendingRemovals);
// remover(_ast); remover(_ast);
} }
//void UnusedStoreEliminator::operator()(FunctionCall const& _functionCall) void UnusedStoreEliminator::operator()(FunctionCall const& _functionCall)
//{ {
// UnusedStoreBase::operator()(_functionCall); UnusedStoreBase::operator()(_functionCall);
// for (Operation const& op: operationsFromFunctionCall(_functionCall)) for (Operation const& op: operationsFromFunctionCall(_functionCall))
// applyOperation(op); applyOperation(op);
// ControlFlowSideEffects sideEffects; ControlFlowSideEffects sideEffects;
// if (auto builtin = m_dialect.builtin(_functionCall.functionName.name)) if (auto builtin = m_dialect.builtin(_functionCall.functionName.name))
// sideEffects = builtin->controlFlowSideEffects; sideEffects = builtin->controlFlowSideEffects;
// else else
// sideEffects = m_controlFlowSideEffects.at(_functionCall.functionName.name); sideEffects = m_controlFlowSideEffects.at(_functionCall.functionName.name);
// if (sideEffects.canTerminate) if (sideEffects.canTerminate)
// changeUndecidedTo(State::Used, Location::Storage); markActiveAsUsed(Location::Storage);
// if (!sideEffects.canContinue) if (!sideEffects.canContinue)
// { {
// changeUndecidedTo(State::Unused, Location::Memory); clearActive(Location::Memory);
// if (!sideEffects.canTerminate) if (!sideEffects.canTerminate)
// changeUndecidedTo(State::Unused, Location::Storage); clearActive(Location::Storage);
// } }
//} }
//void UnusedStoreEliminator::operator()(FunctionDefinition const& _functionDefinition) void UnusedStoreEliminator::operator()(FunctionDefinition const& _functionDefinition)
//{ {
// ScopedSaveAndRestore storeOperations(m_storeOperations, {}); ScopedSaveAndRestore storeOperations(m_storeOperations, {});
// UnusedStoreBase::operator()(_functionDefinition); UnusedStoreBase::operator()(_functionDefinition);
//} }
//void UnusedStoreEliminator::operator()(Leave const&) void UnusedStoreEliminator::operator()(Leave const&)
//{ {
// changeUndecidedTo(State::Used); markActiveAsUsed();
//} }
//void UnusedStoreEliminator::visit(Statement const& _statement) void UnusedStoreEliminator::visit(Statement const& _statement)
//{ {
// using evmasm::Instruction; using evmasm::Instruction;
// UnusedStoreBase::visit(_statement); UnusedStoreBase::visit(_statement);
// auto const* exprStatement = get_if<ExpressionStatement>(&_statement); auto const* exprStatement = get_if<ExpressionStatement>(&_statement);
// if (!exprStatement) if (!exprStatement)
// return; return;
// FunctionCall const* funCall = get_if<FunctionCall>(&exprStatement->expression); FunctionCall const* funCall = get_if<FunctionCall>(&exprStatement->expression);
// yulAssert(funCall); yulAssert(funCall);
// optional<Instruction> instruction = toEVMInstruction(m_dialect, funCall->functionName.name); optional<Instruction> instruction = toEVMInstruction(m_dialect, funCall->functionName.name);
// if (!instruction) if (!instruction)
// return; return;
// if (!ranges::all_of(funCall->arguments, [](Expression const& _expr) -> bool { if (!ranges::all_of(funCall->arguments, [](Expression const& _expr) -> bool {
// return get_if<Identifier>(&_expr) || get_if<Literal>(&_expr); return get_if<Identifier>(&_expr) || get_if<Literal>(&_expr);
// })) }))
// return; return;
// // We determine if this is a store instruction without additional side-effects // We determine if this is a store instruction without additional side-effects
// // both by querying a combination of semantic information and by listing the instructions. // both by querying a combination of semantic information and by listing the instructions.
// // This way the assert below should be triggered on any change. // This way the assert below should be triggered on any change.
// using evmasm::SemanticInformation; using evmasm::SemanticInformation;
// bool isStorageWrite = (*instruction == Instruction::SSTORE); bool isStorageWrite = (*instruction == Instruction::SSTORE);
// bool isMemoryWrite = bool isMemoryWrite =
// *instruction == Instruction::EXTCODECOPY || *instruction == Instruction::EXTCODECOPY ||
// *instruction == Instruction::CODECOPY || *instruction == Instruction::CODECOPY ||
// *instruction == Instruction::CALLDATACOPY || *instruction == Instruction::CALLDATACOPY ||
// *instruction == Instruction::RETURNDATACOPY || *instruction == Instruction::RETURNDATACOPY ||
// *instruction == Instruction::MSTORE || *instruction == Instruction::MSTORE ||
// *instruction == Instruction::MSTORE8; *instruction == Instruction::MSTORE8;
// bool isCandidateForRemoval = bool isCandidateForRemoval =
// SemanticInformation::otherState(*instruction) != SemanticInformation::Write && ( SemanticInformation::otherState(*instruction) != SemanticInformation::Write && (
// SemanticInformation::storage(*instruction) == SemanticInformation::Write || SemanticInformation::storage(*instruction) == SemanticInformation::Write ||
// (!m_ignoreMemory && SemanticInformation::memory(*instruction) == SemanticInformation::Write) (!m_ignoreMemory && SemanticInformation::memory(*instruction) == SemanticInformation::Write)
// ); );
// yulAssert(isCandidateForRemoval == (isStorageWrite || (!m_ignoreMemory && isMemoryWrite))); yulAssert(isCandidateForRemoval == (isStorageWrite || (!m_ignoreMemory && isMemoryWrite)));
// if (isCandidateForRemoval) if (isCandidateForRemoval)
// { {
// TODO what is this special case?
//State initialState = State::Undecided; //State initialState = State::Undecided;
// if (*instruction == Instruction::RETURNDATACOPY) // if (*instruction == Instruction::RETURNDATACOPY)
// { // {
@ -186,223 +188,254 @@ void UnusedStoreEliminator::run(OptimiserStepContext& /*_context*/, Block& /*_as
// initialState = State::Undecided; // initialState = State::Undecided;
// } // }
// } // }
// m_activeStores[YulString{}].insert({&_statement, initialState}); m_allStores.insert(&_statement);
// vector<Operation> operations = operationsFromFunctionCall(*funCall); vector<Operation> operations = operationsFromFunctionCall(*funCall);
// yulAssert(operations.size() == 1, ""); yulAssert(operations.size() == 1, "");
// m_storeOperations[&_statement] = std::move(operations.front()); if (operations.front().location == Location::Storage)
// } m_activeStores["s"_yulstring].insert(&_statement);
//} else
m_activeStores["m"_yulstring].insert(&_statement);
m_storeOperations[&_statement] = std::move(operations.front());
}
}
//void UnusedStoreEliminator::finalizeFunctionDefinition(FunctionDefinition const&) void UnusedStoreEliminator::finalizeFunctionDefinition(FunctionDefinition const&)
//{ {
// changeUndecidedTo(State::Used); markActiveAsUsed();
// scheduleUnusedForDeletion(); scheduleUnusedForDeletion();
//} }
//vector<UnusedStoreEliminator::Operation> UnusedStoreEliminator::operationsFromFunctionCall( vector<UnusedStoreEliminator::Operation> UnusedStoreEliminator::operationsFromFunctionCall(
// FunctionCall const& _functionCall FunctionCall const& _functionCall
//) const ) const
//{ {
// using evmasm::Instruction; using evmasm::Instruction;
// YulString functionName = _functionCall.functionName.name; YulString functionName = _functionCall.functionName.name;
// SideEffects sideEffects; SideEffects sideEffects;
// if (BuiltinFunction const* f = m_dialect.builtin(functionName)) if (BuiltinFunction const* f = m_dialect.builtin(functionName))
// sideEffects = f->sideEffects; sideEffects = f->sideEffects;
// else else
// sideEffects = m_functionSideEffects.at(functionName); sideEffects = m_functionSideEffects.at(functionName);
// optional<Instruction> instruction = toEVMInstruction(m_dialect, functionName); optional<Instruction> instruction = toEVMInstruction(m_dialect, functionName);
// if (!instruction) if (!instruction)
// { {
// vector<Operation> result; vector<Operation> result;
// // Unknown read is worse than unknown write. // Unknown read is worse than unknown write.
// if (sideEffects.memory != SideEffects::Effect::None) if (sideEffects.memory != SideEffects::Effect::None)
// result.emplace_back(Operation{Location::Memory, Effect::Read, {}, {}}); result.emplace_back(Operation{Location::Memory, Effect::Read, {}, {}});
// if (sideEffects.storage != SideEffects::Effect::None) if (sideEffects.storage != SideEffects::Effect::None)
// result.emplace_back(Operation{Location::Storage, Effect::Read, {}, {}}); result.emplace_back(Operation{Location::Storage, Effect::Read, {}, {}});
// return result; return result;
// } }
// using evmasm::SemanticInformation; using evmasm::SemanticInformation;
// return util::applyMap( return util::applyMap(
// SemanticInformation::readWriteOperations(*instruction), SemanticInformation::readWriteOperations(*instruction),
// [&](SemanticInformation::Operation const& _op) -> Operation [&](SemanticInformation::Operation const& _op) -> Operation
// { {
// yulAssert(!(_op.lengthParameter && _op.lengthConstant)); yulAssert(!(_op.lengthParameter && _op.lengthConstant));
// yulAssert(_op.effect != Effect::None); yulAssert(_op.effect != Effect::None);
// Operation ourOp{_op.location, _op.effect, {}, {}}; Operation ourOp{_op.location, _op.effect, {}, {}};
// if (_op.startParameter) if (_op.startParameter)
// ourOp.start = identifierNameIfSSA(_functionCall.arguments.at(*_op.startParameter)); ourOp.start = identifierNameIfSSA(_functionCall.arguments.at(*_op.startParameter));
// if (_op.lengthParameter) if (_op.lengthParameter)
// ourOp.length = identifierNameIfSSA(_functionCall.arguments.at(*_op.lengthParameter)); ourOp.length = identifierNameIfSSA(_functionCall.arguments.at(*_op.lengthParameter));
// if (_op.lengthConstant) if (_op.lengthConstant)
// switch (*_op.lengthConstant) switch (*_op.lengthConstant)
// { {
// case 1: ourOp.length = YulString(one); break; case 1: ourOp.length = YulString(one); break;
// case 32: ourOp.length = YulString(thirtyTwo); break; case 32: ourOp.length = YulString(thirtyTwo); break;
// default: yulAssert(false); default: yulAssert(false);
// } }
// return ourOp; return ourOp;
// } }
// ); );
//} }
//void UnusedStoreEliminator::applyOperation(UnusedStoreEliminator::Operation const& _operation) void UnusedStoreEliminator::applyOperation(UnusedStoreEliminator::Operation const& _operation)
//{ {
// for (auto& [statement, state]: m_activeStores[YulString{}]) // TODO only one will be relevant, depending on _operation.location
// if (state == State::Undecided) for (Statement const* statement: m_activeStores["s"_yulstring])
// { {
// Operation const& storeOperation = m_storeOperations.at(statement); Operation const& storeOperation = m_storeOperations.at(statement);
// if (_operation.effect == Effect::Read && !knownUnrelated(storeOperation, _operation)) if (_operation.effect == Effect::Read && !knownUnrelated(storeOperation, _operation))
// state = State::Used; // TODO remove from active!
// else if (_operation.effect == Effect::Write && knownCovered(storeOperation, _operation)) m_usedStores.insert(statement);
else if (_operation.effect == Effect::Write && knownCovered(storeOperation, _operation))
{
// TODO remove from active
// state = State::Unused; // state = State::Unused;
// } }
//} }
for (Statement const* statement: m_activeStores["m"_yulstring])
{
Operation const& storeOperation = m_storeOperations.at(statement);
if (_operation.effect == Effect::Read && !knownUnrelated(storeOperation, _operation))
// TODO remove from active!
m_usedStores.insert(statement);
else if (_operation.effect == Effect::Write && knownCovered(storeOperation, _operation))
{
// TODO remove from active
// state = State::Unused;
}
}
}
//bool UnusedStoreEliminator::knownUnrelated( bool UnusedStoreEliminator::knownUnrelated(
// UnusedStoreEliminator::Operation const& _op1, UnusedStoreEliminator::Operation const& _op1,
// UnusedStoreEliminator::Operation const& _op2 UnusedStoreEliminator::Operation const& _op2
//) const ) const
//{ {
// KnowledgeBase knowledge(m_dialect, [this](YulString _var) { return util::valueOrNullptr(m_ssaValues, _var); }); KnowledgeBase knowledge(m_dialect, [this](YulString _var) { return util::valueOrNullptr(m_ssaValues, _var); });
// if (_op1.location != _op2.location) if (_op1.location != _op2.location)
// return true; return true;
// if (_op1.location == Location::Storage) if (_op1.location == Location::Storage)
// { {
// if (_op1.start && _op2.start) if (_op1.start && _op2.start)
// { {
// yulAssert( yulAssert(
// _op1.length && _op1.length &&
// _op2.length && _op2.length &&
// knowledge.valueIfKnownConstant(*_op1.length) == 1 && knowledge.valueIfKnownConstant(*_op1.length) == 1 &&
// knowledge.valueIfKnownConstant(*_op2.length) == 1 knowledge.valueIfKnownConstant(*_op2.length) == 1
// ); );
// return knowledge.knownToBeDifferent(*_op1.start, *_op2.start); return knowledge.knownToBeDifferent(*_op1.start, *_op2.start);
// } }
// } }
// else else
// { {
// yulAssert(_op1.location == Location::Memory, ""); yulAssert(_op1.location == Location::Memory, "");
// if ( if (
// (_op1.length && knowledge.knownToBeZero(*_op1.length)) || (_op1.length && knowledge.knownToBeZero(*_op1.length)) ||
// (_op2.length && knowledge.knownToBeZero(*_op2.length)) (_op2.length && knowledge.knownToBeZero(*_op2.length))
// ) )
// return true; return true;
// if (_op1.start && _op1.length && _op2.start) if (_op1.start && _op1.length && _op2.start)
// { {
// optional<u256> length1 = knowledge.valueIfKnownConstant(*_op1.length); optional<u256> length1 = knowledge.valueIfKnownConstant(*_op1.length);
// optional<u256> start1 = knowledge.valueIfKnownConstant(*_op1.start); optional<u256> start1 = knowledge.valueIfKnownConstant(*_op1.start);
// optional<u256> start2 = knowledge.valueIfKnownConstant(*_op2.start); optional<u256> start2 = knowledge.valueIfKnownConstant(*_op2.start);
// if ( if (
// (length1 && start1 && start2) && (length1 && start1 && start2) &&
// *start1 + *length1 >= *start1 && // no overflow *start1 + *length1 >= *start1 && // no overflow
// *start1 + *length1 <= *start2 *start1 + *length1 <= *start2
// ) )
// return true; return true;
// } }
// if (_op2.start && _op2.length && _op1.start) if (_op2.start && _op2.length && _op1.start)
// { {
// optional<u256> length2 = knowledge.valueIfKnownConstant(*_op2.length); optional<u256> length2 = knowledge.valueIfKnownConstant(*_op2.length);
// optional<u256> start2 = knowledge.valueIfKnownConstant(*_op2.start); optional<u256> start2 = knowledge.valueIfKnownConstant(*_op2.start);
// optional<u256> start1 = knowledge.valueIfKnownConstant(*_op1.start); optional<u256> start1 = knowledge.valueIfKnownConstant(*_op1.start);
// if ( if (
// (length2 && start2 && start1) && (length2 && start2 && start1) &&
// *start2 + *length2 >= *start2 && // no overflow *start2 + *length2 >= *start2 && // no overflow
// *start2 + *length2 <= *start1 *start2 + *length2 <= *start1
// ) )
// return true; return true;
// } }
// if (_op1.start && _op1.length && _op2.start && _op2.length) if (_op1.start && _op1.length && _op2.start && _op2.length)
// { {
// optional<u256> length1 = knowledge.valueIfKnownConstant(*_op1.length); optional<u256> length1 = knowledge.valueIfKnownConstant(*_op1.length);
// optional<u256> length2 = knowledge.valueIfKnownConstant(*_op2.length); optional<u256> length2 = knowledge.valueIfKnownConstant(*_op2.length);
// if ( if (
// (length1 && *length1 <= 32) && (length1 && *length1 <= 32) &&
// (length2 && *length2 <= 32) && (length2 && *length2 <= 32) &&
// knowledge.knownToBeDifferentByAtLeast32(*_op1.start, *_op2.start) knowledge.knownToBeDifferentByAtLeast32(*_op1.start, *_op2.start)
// ) )
// return true; return true;
// } }
// } }
// return false; return false;
//} }
//bool UnusedStoreEliminator::knownCovered( bool UnusedStoreEliminator::knownCovered(
// UnusedStoreEliminator::Operation const& _covered, UnusedStoreEliminator::Operation const& _covered,
// UnusedStoreEliminator::Operation const& _covering UnusedStoreEliminator::Operation const& _covering
//) const ) const
//{ {
// if (_covered.location != _covering.location) if (_covered.location != _covering.location)
// return false; return false;
// if ( if (
// (_covered.start && _covered.start == _covering.start) && (_covered.start && _covered.start == _covering.start) &&
// (_covered.length && _covered.length == _covering.length) (_covered.length && _covered.length == _covering.length)
// ) )
// return true; return true;
// if (_covered.location == Location::Memory) if (_covered.location == Location::Memory)
// { {
// KnowledgeBase knowledge(m_dialect, [this](YulString _var) { return util::valueOrNullptr(m_ssaValues, _var); }); KnowledgeBase knowledge(m_dialect, [this](YulString _var) { return util::valueOrNullptr(m_ssaValues, _var); });
// if (_covered.length && knowledge.knownToBeZero(*_covered.length)) if (_covered.length && knowledge.knownToBeZero(*_covered.length))
// return true; return true;
// // Condition (i = cover_i_ng, e = cover_e_d): // Condition (i = cover_i_ng, e = cover_e_d):
// // i.start <= e.start && e.start + e.length <= i.start + i.length // i.start <= e.start && e.start + e.length <= i.start + i.length
// if (!_covered.start || !_covering.start || !_covered.length || !_covering.length) if (!_covered.start || !_covering.start || !_covered.length || !_covering.length)
// return false; return false;
// optional<u256> coveredLength = knowledge.valueIfKnownConstant(*_covered.length); optional<u256> coveredLength = knowledge.valueIfKnownConstant(*_covered.length);
// optional<u256> coveringLength = knowledge.valueIfKnownConstant(*_covering.length); optional<u256> coveringLength = knowledge.valueIfKnownConstant(*_covering.length);
// if (knowledge.knownToBeEqual(*_covered.start, *_covering.start)) if (knowledge.knownToBeEqual(*_covered.start, *_covering.start))
// if (coveredLength && coveringLength && *coveredLength <= *coveringLength) if (coveredLength && coveringLength && *coveredLength <= *coveringLength)
// return true; return true;
// optional<u256> coveredStart = knowledge.valueIfKnownConstant(*_covered.start); optional<u256> coveredStart = knowledge.valueIfKnownConstant(*_covered.start);
// optional<u256> coveringStart = knowledge.valueIfKnownConstant(*_covering.start); optional<u256> coveringStart = knowledge.valueIfKnownConstant(*_covering.start);
// if (coveredStart && coveringStart && coveredLength && coveringLength) if (coveredStart && coveringStart && coveredLength && coveringLength)
// if ( if (
// *coveringStart <= *coveredStart && *coveringStart <= *coveredStart &&
// *coveringStart + *coveringLength >= *coveringStart && // no overflow *coveringStart + *coveringLength >= *coveringStart && // no overflow
// *coveredStart + *coveredLength >= *coveredStart && // no overflow *coveredStart + *coveredLength >= *coveredStart && // no overflow
// *coveredStart + *coveredLength <= *coveringStart + *coveringLength *coveredStart + *coveredLength <= *coveringStart + *coveringLength
// ) )
// return true; return true;
// // TODO for this we probably need a non-overflow assumption as above. // TODO for this we probably need a non-overflow assumption as above.
// // Condition (i = cover_i_ng, e = cover_e_d): // Condition (i = cover_i_ng, e = cover_e_d):
// // i.start <= e.start && e.start + e.length <= i.start + i.length // i.start <= e.start && e.start + e.length <= i.start + i.length
// } }
// return false; return false;
//} }
//void UnusedStoreEliminator::changeUndecidedTo( void UnusedStoreEliminator::markActiveAsUsed(
// State _newState, optional<UnusedStoreEliminator::Location> _onlyLocation)
// optional<UnusedStoreEliminator::Location> _onlyLocation) {
//{ // TODO it might make sense to use YulString{"m"} and YulString{"s"} for memory and storage.
// for (auto& [statement, state]: m_activeStores[YulString{}]) // BUT: Could be both!
// if ( for (Statement const* statement: m_activeStores[YulString{}])
// state == State::Undecided && if (_onlyLocation == nullopt || *_onlyLocation == m_storeOperations.at(statement).location)
// (_onlyLocation == nullopt || *_onlyLocation == m_storeOperations.at(statement).location) m_usedStores.insert(statement);
// ) // TODO and remove from active
// state = _newState; }
//}
//optional<YulString> UnusedStoreEliminator::identifierNameIfSSA(Expression const& _expression) const void UnusedStoreEliminator::changeUndecidedTo(
//{ State _newState,
// if (Identifier const* identifier = get_if<Identifier>(&_expression)) optional<UnusedStoreEliminator::Location> _onlyLocation)
// if (m_ssaValues.count(identifier->name)) {
// return {identifier->name}; for (auto& [statement, state]: m_activeStores[YulString{}])
// return nullopt; if (
//} state == State::Undecided &&
(_onlyLocation == nullopt || *_onlyLocation == m_storeOperations.at(statement).location)
)
state = _newState;
}
//void UnusedStoreEliminator::scheduleUnusedForDeletion() optional<YulString> UnusedStoreEliminator::identifierNameIfSSA(Expression const& _expression) const
//{ {
// for (auto const& [statement, state]: m_activeStores[YulString{}]) if (Identifier const* identifier = get_if<Identifier>(&_expression))
// if (state == State::Unused) if (m_ssaValues.count(identifier->name))
// m_pendingRemovals.insert(statement); return {identifier->name};
//} return nullopt;
}
void UnusedStoreEliminator::scheduleUnusedForDeletion()
{
for (auto const& [statement, state]: m_activeStores[YulString{}])
if (state == State::Unused)
m_pendingRemovals.insert(statement);
}

View File

@ -70,57 +70,58 @@ public:
bool// _ignoreMemory bool// _ignoreMemory
): ):
UnusedStoreBase(_dialect)//, UnusedStoreBase(_dialect)//,
// m_ignoreMemory(_ignoreMemory), m_ignoreMemory(_ignoreMemory),
// m_functionSideEffects(_functionSideEffects), m_functionSideEffects(_functionSideEffects),
// m_controlFlowSideEffects(_controlFlowSideEffects), m_controlFlowSideEffects(_controlFlowSideEffects),
// m_ssaValues(_ssaValues) m_ssaValues(_ssaValues)
{} {}
// using UnusedStoreBase::operator(); using UnusedStoreBase::operator();
// void operator()(FunctionCall const& _functionCall) override; void operator()(FunctionCall const& _functionCall) override;
// void operator()(FunctionDefinition const&) override; void operator()(FunctionDefinition const&) override;
// void operator()(Leave const&) override; void operator()(Leave const&) override;
// using UnusedStoreBase::visit; using UnusedStoreBase::visit;
// void visit(Statement const& _statement) override; void visit(Statement const& _statement) override;
// using Location = evmasm::SemanticInformation::Location; using Location = evmasm::SemanticInformation::Location;
// using Effect = evmasm::SemanticInformation::Effect; using Effect = evmasm::SemanticInformation::Effect;
// struct Operation struct Operation
// { {
// Location location; Location location;
// Effect effect; Effect effect;
// /// Start of affected area. Unknown if not provided. /// Start of affected area. Unknown if not provided.
// std::optional<YulString> start; std::optional<YulString> start;
// /// Length of affected area, unknown if not provided. /// Length of affected area, unknown if not provided.
// /// Unused for storage. /// Unused for storage.
// std::optional<YulString> length; std::optional<YulString> length;
// }; };
private: private:
void shortcutNestedLoop(ActiveStores const&) override void shortcutNestedLoop(ActiveStores const&) override
{ {
// We might only need to do this for newly introduced stores in the loop. // We might only need to do this for newly introduced stores in the loop.
// changeUndecidedTo(State::Used); changeUndecidedTo(State::Used);
} }
// void finalizeFunctionDefinition(FunctionDefinition const&) override; void finalizeFunctionDefinition(FunctionDefinition const&) override;
// std::vector<Operation> operationsFromFunctionCall(FunctionCall const& _functionCall) const; std::vector<Operation> operationsFromFunctionCall(FunctionCall const& _functionCall) const;
// void applyOperation(Operation const& _operation); void applyOperation(Operation const& _operation);
// bool knownUnrelated(Operation const& _op1, Operation const& _op2) const; bool knownUnrelated(Operation const& _op1, Operation const& _op2) const;
// bool knownCovered(Operation const& _covered, Operation const& _covering) const; bool knownCovered(Operation const& _covered, Operation const& _covering) const;
// void changeUndecidedTo(State _newState, std::optional<Location> _onlyLocation = std::nullopt); void markActiveAsUsed(std::optional<Location> _onlyLocation = std::nullopt);
// void scheduleUnusedForDeletion(); void clearActive(std::optional<Location> _onlyLocation = std::nullopt);
void scheduleUnusedForDeletion();
// std::optional<YulString> identifierNameIfSSA(Expression const& _expression) const; std::optional<YulString> identifierNameIfSSA(Expression const& _expression) const;
// bool const m_ignoreMemory; bool const m_ignoreMemory;
// std::map<YulString, SideEffects> const& m_functionSideEffects; std::map<YulString, SideEffects> const& m_functionSideEffects;
// std::map<YulString, ControlFlowSideEffects> m_controlFlowSideEffects; std::map<YulString, ControlFlowSideEffects> m_controlFlowSideEffects;
// std::map<YulString, AssignedValue> const& m_ssaValues; std::map<YulString, AssignedValue> const& m_ssaValues;
// std::map<Statement const*, Operation> m_storeOperations; std::map<Statement const*, Operation> m_storeOperations;
}; };
} }

View File

@ -0,0 +1,21 @@
{
let a := calldataload(0)
if calldataload(1) {
// this can be removed
a := 2
revert(0, 0)
}
sstore(0, a)
}
// ----
// step: unusedAssignEliminator
//
// {
// let a := calldataload(0)
// if calldataload(1)
// {
// a := 2
// revert(0, 0)
// }
// sstore(0, a)
// }