2019-05-15 08:33:35 +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-15 08:33:35 +00:00
|
|
|
/**
|
|
|
|
* Optimisation stage that replaces constants by expressions that compute them.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <libyul/optimiser/ASTWalker.h>
|
|
|
|
#include <libyul/YulString.h>
|
|
|
|
#include <libyul/Dialect.h>
|
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/ASTForward.h>
|
2019-05-15 08:33:35 +00:00
|
|
|
|
|
|
|
#include <liblangutil/SourceLocation.h>
|
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Common.h>
|
2019-05-15 08:33:35 +00:00
|
|
|
|
|
|
|
#include <tuple>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::yul
|
2019-05-15 08:33:35 +00:00
|
|
|
{
|
|
|
|
struct Dialect;
|
|
|
|
class GasMeter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Optimisation stage that replaces constants by expressions that compute them.
|
|
|
|
*
|
|
|
|
* Prerequisite: None
|
|
|
|
*/
|
|
|
|
class ConstantOptimiser: public ASTModifier
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ConstantOptimiser(EVMDialect const& _dialect, GasMeter const& _meter):
|
|
|
|
m_dialect(_dialect),
|
|
|
|
m_meter(_meter)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void visit(Expression& _e) override;
|
|
|
|
|
|
|
|
struct Representation
|
|
|
|
{
|
|
|
|
std::unique_ptr<Expression> expression;
|
2021-04-19 11:08:19 +00:00
|
|
|
bigint cost;
|
2019-05-15 08:33:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
EVMDialect const& m_dialect;
|
|
|
|
GasMeter const& m_meter;
|
2019-12-11 16:31:36 +00:00
|
|
|
std::map<u256, Representation> m_cache;
|
2019-05-15 08:33:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class RepresentationFinder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Representation = ConstantOptimiser::Representation;
|
|
|
|
RepresentationFinder(
|
|
|
|
EVMDialect const& _dialect,
|
|
|
|
GasMeter const& _meter,
|
|
|
|
langutil::SourceLocation _location,
|
2019-12-11 16:31:36 +00:00
|
|
|
std::map<u256, Representation>& _cache
|
2019-05-15 08:33:35 +00:00
|
|
|
):
|
|
|
|
m_dialect(_dialect),
|
|
|
|
m_meter(_meter),
|
|
|
|
m_location(std::move(_location)),
|
|
|
|
m_cache(_cache)
|
|
|
|
{}
|
|
|
|
|
|
|
|
/// @returns a cheaper representation for the number than its representation
|
|
|
|
/// as a literal or nullptr otherwise.
|
2019-12-11 16:31:36 +00:00
|
|
|
Expression const* tryFindRepresentation(u256 const& _value);
|
2019-05-15 08:33:35 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Recursively try to find the cheapest representation of the given number,
|
|
|
|
/// literal if necessary.
|
2019-12-11 16:31:36 +00:00
|
|
|
Representation const& findRepresentation(u256 const& _value);
|
2019-05-15 08:33:35 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
Representation represent(u256 const& _value) const;
|
2019-05-15 08:33:35 +00:00
|
|
|
Representation represent(YulString _instruction, Representation const& _arg) const;
|
|
|
|
Representation represent(YulString _instruction, Representation const& _arg1, Representation const& _arg2) const;
|
|
|
|
|
|
|
|
Representation min(Representation _a, Representation _b);
|
|
|
|
|
|
|
|
EVMDialect const& m_dialect;
|
|
|
|
GasMeter const& m_meter;
|
|
|
|
langutil::SourceLocation m_location;
|
|
|
|
/// Counter for the complexity of optimization, will stop when it reaches zero.
|
|
|
|
size_t m_maxSteps = 10000;
|
2019-12-11 16:31:36 +00:00
|
|
|
std::map<u256, Representation>& m_cache;
|
2019-05-15 08:33:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|