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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2019-05-21 13:52:15 +00:00
|
|
|
/**
|
|
|
|
* 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>
|
2021-03-01 18:05:59 +00:00
|
|
|
#include <libyul/backends/evm/EVMMetrics.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>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2021-03-01 18:05:59 +00:00
|
|
|
#include <libyul/Utilities.h>
|
|
|
|
|
|
|
|
#include <libevmasm/GasMeter.h>
|
|
|
|
#include <libsolutil/Keccak256.h>
|
|
|
|
|
|
|
|
#include <limits>
|
2019-05-21 13:52:15 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
2021-03-01 18:05:59 +00:00
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::evmasm;
|
2019-12-11 16:31:36 +00:00
|
|
|
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)),
|
2021-04-14 15:04:26 +00:00
|
|
|
containsMSize,
|
2021-03-01 18:05:59 +00:00
|
|
|
_context.expectedExecutionsPerDeployment
|
2019-09-02 12:22:53 +00:00
|
|
|
}(_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);
|
|
|
|
|
2020-12-03 17:48:17 +00:00
|
|
|
if (FunctionCall const* funCall = std::get_if<FunctionCall>(&_e))
|
2021-03-01 18:05:59 +00:00
|
|
|
{
|
2020-12-03 17:48:17 +00:00
|
|
|
for (auto location: { StoreLoadLocation::Memory, StoreLoadLocation::Storage })
|
|
|
|
if (funCall->functionName.name == m_loadFunctionName[static_cast<unsigned>(location)])
|
|
|
|
{
|
|
|
|
tryResolve(_e, location, funCall->arguments);
|
|
|
|
break;
|
|
|
|
}
|
2021-03-01 18:05:59 +00:00
|
|
|
|
2021-04-14 15:04:26 +00:00
|
|
|
if (!m_containsMSize && funCall->functionName.name == m_dialect.hashFunction({}))
|
2021-03-01 18:05:59 +00:00
|
|
|
tryEvaluateKeccak(_e, funCall->arguments);
|
|
|
|
}
|
2019-05-21 13:52:15 +00:00
|
|
|
}
|
2019-09-02 12:22:53 +00:00
|
|
|
|
|
|
|
void LoadResolver::tryResolve(
|
|
|
|
Expression& _e,
|
2020-12-03 17:48:17 +00:00
|
|
|
StoreLoadLocation _location,
|
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;
|
2020-12-01 23:02:44 +00:00
|
|
|
if (_location == StoreLoadLocation::Storage)
|
|
|
|
{
|
2020-12-11 16:30:06 +00:00
|
|
|
if (auto value = util::valueOrNullptr(m_storage, key))
|
2021-01-06 09:39:19 +00:00
|
|
|
if (inScope(*value))
|
2021-04-27 14:53:04 +00:00
|
|
|
_e = Identifier{debugDataOf(_e), *value};
|
2020-12-01 23:02:44 +00:00
|
|
|
}
|
2021-04-14 15:04:26 +00:00
|
|
|
else if (!m_containsMSize && _location == StoreLoadLocation::Memory)
|
2020-12-11 16:30:06 +00:00
|
|
|
if (auto value = util::valueOrNullptr(m_memory, key))
|
2021-01-06 09:39:19 +00:00
|
|
|
if (inScope(*value))
|
2021-04-27 14:53:04 +00:00
|
|
|
_e = Identifier{debugDataOf(_e), *value};
|
2019-09-02 12:22:53 +00:00
|
|
|
}
|
2021-03-01 18:05:59 +00:00
|
|
|
|
|
|
|
void LoadResolver::tryEvaluateKeccak(
|
|
|
|
Expression& _e,
|
|
|
|
std::vector<Expression> const& _arguments
|
|
|
|
)
|
|
|
|
{
|
2021-09-06 10:02:55 +00:00
|
|
|
yulAssert(_arguments.size() == 2, "");
|
|
|
|
Identifier const* memoryKey = std::get_if<Identifier>(&_arguments.at(0));
|
|
|
|
Identifier const* length = std::get_if<Identifier>(&_arguments.at(1));
|
|
|
|
|
|
|
|
if (!memoryKey || !length)
|
|
|
|
return;
|
|
|
|
|
2021-03-01 18:05:59 +00:00
|
|
|
// The costs are only correct for hashes of 32 bytes or 1 word (when rounded up).
|
|
|
|
GasMeter gasMeter{
|
|
|
|
dynamic_cast<EVMDialect const&>(m_dialect),
|
|
|
|
!m_expectedExecutionsPerDeployment,
|
|
|
|
m_expectedExecutionsPerDeployment ? *m_expectedExecutionsPerDeployment : 1
|
|
|
|
};
|
|
|
|
|
|
|
|
bigint costOfKeccak = gasMeter.costs(_e);
|
|
|
|
bigint costOfLiteral = gasMeter.costs(
|
|
|
|
Literal{
|
|
|
|
{},
|
|
|
|
LiteralKind::Number,
|
|
|
|
// a dummy 256-bit number to represent the Keccak256 hash.
|
|
|
|
YulString{numeric_limits<u256>::max().str()},
|
|
|
|
{}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// We skip if there are no net gas savings.
|
|
|
|
// Note that for default `m_runs = 200`, the values are
|
|
|
|
// `costOfLiteral = 7200` and `costOfKeccak = 9000` for runtime context.
|
|
|
|
// For creation context: `costOfLiteral = 531` and `costOfKeccak = 90`.
|
|
|
|
if (costOfLiteral > costOfKeccak)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto memoryValue = util::valueOrNullptr(m_memory, memoryKey->name);
|
|
|
|
if (memoryValue && inScope(*memoryValue))
|
|
|
|
{
|
|
|
|
optional<u256> memoryContent = valueOfIdentifier(*memoryValue);
|
|
|
|
optional<u256> byteLength = valueOfIdentifier(length->name);
|
|
|
|
if (memoryContent && byteLength && *byteLength <= 32)
|
|
|
|
{
|
|
|
|
bytes contentAsBytes = toBigEndian(*memoryContent);
|
|
|
|
contentAsBytes.resize(static_cast<size_t>(*byteLength));
|
|
|
|
_e = Literal{
|
2021-04-27 14:53:04 +00:00
|
|
|
debugDataOf(_e),
|
2021-03-01 18:05:59 +00:00
|
|
|
LiteralKind::Number,
|
|
|
|
YulString{u256(keccak256(contentAsBytes)).str()},
|
|
|
|
m_dialect.defaultType
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|