solidity/libyul/backends/evm/ConstantOptimiser.cpp

225 lines
6.5 KiB
C++
Raw Permalink Normal View History

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/>.
*/
// SPDX-License-Identifier: GPL-3.0
2019-05-15 08:33:35 +00:00
/**
* Optimisation stage that replaces constants by expressions that compute them.
*/
2019-05-28 10:57:15 +00:00
#include <libyul/backends/evm/ConstantOptimiser.h>
2019-05-15 08:33:35 +00:00
#include <libyul/optimiser/ASTCopier.h>
2019-05-28 10:57:15 +00:00
#include <libyul/backends/evm/EVMMetrics.h>
#include <libyul/AST.h>
2019-05-15 08:33:35 +00:00
#include <libyul/Utilities.h>
#include <libsolutil/CommonData.h>
2019-05-15 08:33:35 +00:00
#include <variant>
2019-12-11 16:31:36 +00:00
using namespace solidity;
using namespace solidity::yul;
using namespace solidity::util;
2019-05-15 08:33:35 +00:00
using Representation = ConstantOptimiser::Representation;
namespace
{
struct MiniEVMInterpreter
{
explicit MiniEVMInterpreter(EVMDialect const& _dialect): m_dialect(_dialect) {}
u256 eval(Expression const& _expr)
{
return std::visit(*this, _expr);
}
u256 eval(evmasm::Instruction _instr, std::vector<Expression> const& _arguments)
{
std::vector<u256> args;
for (auto const& arg: _arguments)
args.emplace_back(eval(arg));
switch (_instr)
{
2019-12-11 16:31:36 +00:00
case evmasm::Instruction::ADD:
return args.at(0) + args.at(1);
2019-12-11 16:31:36 +00:00
case evmasm::Instruction::SUB:
return args.at(0) - args.at(1);
2019-12-11 16:31:36 +00:00
case evmasm::Instruction::MUL:
return args.at(0) * args.at(1);
2019-12-11 16:31:36 +00:00
case evmasm::Instruction::EXP:
return exp256(args.at(0), args.at(1));
2019-12-11 16:31:36 +00:00
case evmasm::Instruction::SHL:
return args.at(0) > 255 ? 0 : (args.at(1) << unsigned(args.at(0)));
2019-12-11 16:31:36 +00:00
case evmasm::Instruction::NOT:
return ~args.at(0);
default:
yulAssert(false, "Invalid operation generated in constant optimizer.");
}
return 0;
}
u256 operator()(FunctionCall const& _funCall)
{
BuiltinFunctionForEVM const* fun = m_dialect.builtin(_funCall.functionName.name);
yulAssert(fun, "Expected builtin function.");
yulAssert(fun->instruction, "Expected EVM instruction.");
return eval(*fun->instruction, _funCall.arguments);
}
u256 operator()(Literal const& _literal)
{
return valueOfLiteral(_literal);
}
u256 operator()(Identifier const&) { yulAssert(false, ""); }
EVMDialect const& m_dialect;
};
}
2019-05-15 08:33:35 +00:00
void ConstantOptimiser::visit(Expression& _e)
{
if (std::holds_alternative<Literal>(_e))
2019-05-15 08:33:35 +00:00
{
Literal const& literal = std::get<Literal>(_e);
2019-05-15 08:33:35 +00:00
if (literal.kind != LiteralKind::Number)
return;
if (
Expression const* repr =
2021-04-27 14:53:04 +00:00
RepresentationFinder(m_dialect, m_meter, debugDataOf(_e), m_cache)
2019-05-15 08:33:35 +00:00
.tryFindRepresentation(valueOfLiteral(literal))
)
_e = ASTCopier{}.translate(*repr);
}
else
ASTModifier::visit(_e);
}
2019-12-11 16:31:36 +00:00
Expression const* RepresentationFinder::tryFindRepresentation(u256 const& _value)
2019-05-15 08:33:35 +00:00
{
if (_value < 0x10000)
return nullptr;
Representation const& repr = findRepresentation(_value);
if (std::holds_alternative<Literal>(*repr.expression))
2019-05-15 08:33:35 +00:00
return nullptr;
else
return repr.expression.get();
}
2019-12-11 16:31:36 +00:00
Representation const& RepresentationFinder::findRepresentation(u256 const& _value)
2019-05-15 08:33:35 +00:00
{
if (m_cache.count(_value))
return m_cache.at(_value);
Representation routine = represent(_value);
2021-09-22 09:19:53 +00:00
if (numberEncodingSize(~_value) < numberEncodingSize(_value))
2019-05-15 08:33:35 +00:00
// Negated is shorter to represent
2022-08-23 17:28:45 +00:00
routine = min(std::move(routine), represent("not"_yulstring, findRepresentation(~_value)));
2019-05-15 08:33:35 +00:00
// Decompose value into a * 2**k + b where abs(b) << 2**k
for (unsigned bits = 255; bits > 8 && m_maxSteps > 0; --bits)
{
unsigned gapDetector = unsigned((_value >> (bits - 8)) & 0x1ff);
if (gapDetector != 0xff && gapDetector != 0x100)
continue;
u256 powerOfTwo = u256(1) << bits;
u256 upperPart = _value >> bits;
bigint lowerPart = _value & (powerOfTwo - 1);
if ((powerOfTwo - lowerPart) < lowerPart)
{
lowerPart = lowerPart - powerOfTwo; // make it negative
upperPart++;
}
if (upperPart == 0)
continue;
if (abs(lowerPart) >= (powerOfTwo >> 8))
continue;
Representation newRoutine;
if (m_dialect.evmVersion().hasBitwiseShifting())
newRoutine = represent("shl"_yulstring, represent(bits), findRepresentation(upperPart));
else
{
newRoutine = represent("exp"_yulstring, represent(2), represent(bits));
if (upperPart != 1)
newRoutine = represent("mul"_yulstring, findRepresentation(upperPart), newRoutine);
}
if (newRoutine.cost >= routine.cost)
continue;
if (lowerPart > 0)
newRoutine = represent("add"_yulstring, newRoutine, findRepresentation(u256(abs(lowerPart))));
else if (lowerPart < 0)
newRoutine = represent("sub"_yulstring, newRoutine, findRepresentation(u256(abs(lowerPart))));
if (m_maxSteps > 0)
m_maxSteps--;
2022-08-23 17:28:45 +00:00
routine = min(std::move(routine), std::move(newRoutine));
2019-05-15 08:33:35 +00:00
}
yulAssert(MiniEVMInterpreter{m_dialect}.eval(*routine.expression) == _value, "Invalid expression generated.");
2022-08-23 17:28:45 +00:00
return m_cache[_value] = std::move(routine);
2019-05-15 08:33:35 +00:00
}
2019-12-11 16:31:36 +00:00
Representation RepresentationFinder::represent(u256 const& _value) const
2019-05-15 08:33:35 +00:00
{
Representation repr;
repr.expression = std::make_unique<Expression>(Literal{m_debugData, LiteralKind::Number, YulString{formatNumber(_value)}, {}});
2019-05-15 08:33:35 +00:00
repr.cost = m_meter.costs(*repr.expression);
return repr;
}
Representation RepresentationFinder::represent(
YulString _instruction,
Representation const& _argument
) const
{
Representation repr;
repr.expression = std::make_unique<Expression>(FunctionCall{
2021-04-27 14:53:04 +00:00
m_debugData,
Identifier{m_debugData, _instruction},
2019-05-15 08:33:35 +00:00
{ASTCopier{}.translate(*_argument.expression)}
});
repr.cost = _argument.cost + m_meter.instructionCosts(*m_dialect.builtin(_instruction)->instruction);
return repr;
}
Representation RepresentationFinder::represent(
YulString _instruction,
Representation const& _arg1,
Representation const& _arg2
) const
{
Representation repr;
repr.expression = std::make_unique<Expression>(FunctionCall{
2021-04-27 14:53:04 +00:00
m_debugData,
Identifier{m_debugData, _instruction},
2019-05-15 08:33:35 +00:00
{ASTCopier{}.translate(*_arg1.expression), ASTCopier{}.translate(*_arg2.expression)}
});
repr.cost = m_meter.instructionCosts(*m_dialect.builtin(_instruction)->instruction) + _arg1.cost + _arg2.cost;
return repr;
}
Representation RepresentationFinder::min(Representation _a, Representation _b)
{
if (_a.cost <= _b.cost)
return _a;
else
return _b;
}