/* This file is part of solidity. solidity is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. solidity is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with solidity. If not, see . */ /** * Optimisation stage that replaces expressions of type ``sload(x)`` by the value * currently stored in storage, if known. */ #include #include #include #include #include #include using namespace std; using namespace dev; using namespace yul; void LoadResolver::run(OptimiserStepContext& _context, Block& _ast) { bool containsMSize = MSizeFinder::containsMSize(_context.dialect, _ast); LoadResolver{ _context.dialect, SideEffectsPropagator::sideEffects(_context.dialect, CallGraphGenerator::callGraph(_ast)), !containsMSize }(_ast); } void LoadResolver::visit(Expression& _e) { DataFlowAnalyzer::visit(_e); if (!dynamic_cast(&m_dialect)) return; if (_e.type() == typeid(FunctionCall)) { FunctionCall const& funCall = boost::get(_e); if (auto const* builtin = dynamic_cast(m_dialect).builtin(funCall.functionName.name)) if (builtin->instruction) tryResolve(_e, *builtin->instruction, funCall.arguments); } else if (_e.type() == typeid(FunctionalInstruction)) { FunctionalInstruction const& instruction = boost::get(_e); tryResolve(_e, instruction.instruction, instruction.arguments); } } void LoadResolver::tryResolve( Expression& _e, dev::eth::Instruction _instruction, vector const& _arguments ) { if (_arguments.empty() || _arguments.at(0).type() != typeid(Identifier)) return; YulString key = boost::get(_arguments.at(0)).name; if ( _instruction == dev::eth::Instruction::SLOAD && m_storage.values.count(key) ) _e = Identifier{locationOf(_e), m_storage.values[key]}; else if ( m_optimizeMLoad && _instruction == dev::eth::Instruction::MLOAD && m_memory.values.count(key) ) _e = Identifier{locationOf(_e), m_memory.values[key]}; }