2019-05-21 13:52:15 +00:00
|
|
|
/*
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Optimisation stage that replaces expressions of type ``sload(x)`` by the value
|
|
|
|
* currently stored in storage, if known.
|
|
|
|
*/
|
|
|
|
|
2019-05-27 22:14:01 +00:00
|
|
|
#include <libyul/optimiser/LoadResolver.h>
|
2019-05-21 13:52:15 +00:00
|
|
|
|
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
2019-05-27 22:14:01 +00:00
|
|
|
#include <libyul/optimiser/Semantics.h>
|
2019-09-02 12:22:53 +00:00
|
|
|
#include <libyul/optimiser/CallGraphGenerator.h>
|
|
|
|
#include <libyul/SideEffects.h>
|
2019-05-21 13:52:15 +00:00
|
|
|
#include <libyul/AsmData.h>
|
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
2019-05-21 13:52:15 +00:00
|
|
|
|
2019-09-23 14:32:50 +00:00
|
|
|
void LoadResolver::run(OptimiserStepContext& _context, Block& _ast)
|
2019-05-27 22:14:01 +00:00
|
|
|
{
|
2019-09-23 14:32:50 +00:00
|
|
|
bool containsMSize = MSizeFinder::containsMSize(_context.dialect, _ast);
|
2019-09-02 12:22:53 +00:00
|
|
|
LoadResolver{
|
2019-09-23 14:32:50 +00:00
|
|
|
_context.dialect,
|
|
|
|
SideEffectsPropagator::sideEffects(_context.dialect, CallGraphGenerator::callGraph(_ast)),
|
2019-09-02 12:22:53 +00:00
|
|
|
!containsMSize
|
|
|
|
}(_ast);
|
2019-05-27 22:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoadResolver::visit(Expression& _e)
|
2019-05-21 13:52:15 +00:00
|
|
|
{
|
2019-09-02 12:22:53 +00:00
|
|
|
DataFlowAnalyzer::visit(_e);
|
|
|
|
|
|
|
|
if (!dynamic_cast<EVMDialect const*>(&m_dialect))
|
|
|
|
return;
|
|
|
|
|
2019-11-19 15:42:49 +00:00
|
|
|
if (holds_alternative<FunctionCall>(_e))
|
2019-05-21 13:52:15 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
FunctionCall const& funCall = std::get<FunctionCall>(_e);
|
2019-05-21 13:52:15 +00:00
|
|
|
if (auto const* builtin = dynamic_cast<EVMDialect const&>(m_dialect).builtin(funCall.functionName.name))
|
2019-09-02 12:22:53 +00:00
|
|
|
if (builtin->instruction)
|
|
|
|
tryResolve(_e, *builtin->instruction, funCall.arguments);
|
|
|
|
}
|
2019-05-21 13:52:15 +00:00
|
|
|
}
|
2019-09-02 12:22:53 +00:00
|
|
|
|
|
|
|
void LoadResolver::tryResolve(
|
|
|
|
Expression& _e,
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::Instruction _instruction,
|
2019-09-02 12:22:53 +00:00
|
|
|
vector<Expression> const& _arguments
|
|
|
|
)
|
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
if (_arguments.empty() || !holds_alternative<Identifier>(_arguments.at(0)))
|
2019-09-02 12:22:53 +00:00
|
|
|
return;
|
|
|
|
|
2019-11-19 15:42:49 +00:00
|
|
|
YulString key = std::get<Identifier>(_arguments.at(0)).name;
|
2019-09-02 12:22:53 +00:00
|
|
|
if (
|
2019-12-11 16:31:36 +00:00
|
|
|
_instruction == evmasm::Instruction::SLOAD &&
|
2019-09-02 12:22:53 +00:00
|
|
|
m_storage.values.count(key)
|
|
|
|
)
|
|
|
|
_e = Identifier{locationOf(_e), m_storage.values[key]};
|
|
|
|
else if (
|
|
|
|
m_optimizeMLoad &&
|
2019-12-11 16:31:36 +00:00
|
|
|
_instruction == evmasm::Instruction::MLOAD &&
|
2019-09-02 12:22:53 +00:00
|
|
|
m_memory.values.count(key)
|
|
|
|
)
|
|
|
|
_e = Identifier{locationOf(_e), m_memory.values[key]};
|
|
|
|
}
|