2015-04-24 15:35:16 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-04-24 15:35:16 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2015-04-24 15:35:16 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2015-04-24 15:35:16 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2015-04-24 15:35:16 +00:00
|
|
|
/**
|
|
|
|
* @file ExpressionClasses.cpp
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Container for equivalence classes of expressions for use in common subexpression elimination.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libevmasm/ExpressionClasses.h>
|
2022-03-18 06:07:03 +00:00
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
#include <libevmasm/Assembly.h>
|
|
|
|
#include <libevmasm/CommonSubexpressionEliminator.h>
|
2017-01-06 15:16:11 +00:00
|
|
|
#include <libevmasm/SimplificationRules.h>
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2022-03-18 06:07:03 +00:00
|
|
|
#include <boost/container_hash/hash.hpp>
|
|
|
|
|
2020-04-01 03:04:29 +00:00
|
|
|
#include <functional>
|
2021-09-16 14:33:28 +00:00
|
|
|
#include <limits>
|
2022-03-18 06:07:03 +00:00
|
|
|
#include <tuple>
|
2020-04-01 03:04:29 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::evmasm;
|
|
|
|
using namespace solidity::langutil;
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2022-03-18 06:07:03 +00:00
|
|
|
bool ExpressionClasses::Expression::operator==(ExpressionClasses::Expression const& _other) const
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2015-04-30 09:40:43 +00:00
|
|
|
assertThrow(!!item && !!_other.item, OptimizerException, "");
|
2015-04-24 15:35:16 +00:00
|
|
|
auto type = item->type();
|
|
|
|
auto otherType = _other.item->type();
|
2017-01-06 10:33:08 +00:00
|
|
|
if (type != otherType)
|
2022-03-18 06:07:03 +00:00
|
|
|
return false;
|
2017-01-06 10:33:08 +00:00
|
|
|
else if (type == Operation)
|
|
|
|
{
|
|
|
|
auto instr = item->instruction();
|
|
|
|
auto otherInstr = _other.item->instruction();
|
2022-03-18 06:07:03 +00:00
|
|
|
return std::tie(instr, arguments, sequenceNumber) ==
|
2017-01-06 10:33:08 +00:00
|
|
|
std::tie(otherInstr, _other.arguments, _other.sequenceNumber);
|
|
|
|
}
|
|
|
|
else
|
2022-03-18 06:07:03 +00:00
|
|
|
return std::tie(item->data(), arguments, sequenceNumber) ==
|
2017-01-06 10:33:08 +00:00
|
|
|
std::tie(_other.item->data(), _other.arguments, _other.sequenceNumber);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
|
2023-07-10 11:12:46 +00:00
|
|
|
size_t ExpressionClasses::Expression::ExpressionHash::operator()(Expression const& _expression) const
|
2022-03-18 06:07:03 +00:00
|
|
|
{
|
|
|
|
assertThrow(!!_expression.item, OptimizerException, "");
|
2023-07-10 11:12:46 +00:00
|
|
|
size_t seed = 0;
|
2022-03-18 06:07:03 +00:00
|
|
|
auto type = _expression.item->type();
|
|
|
|
boost::hash_combine(seed, type);
|
|
|
|
|
|
|
|
if (type == Operation)
|
|
|
|
boost::hash_combine(seed, _expression.item->instruction());
|
|
|
|
else
|
|
|
|
boost::hash_combine(seed, _expression.item->data());
|
|
|
|
|
|
|
|
boost::hash_range(seed, _expression.arguments.begin(), _expression.arguments.end());
|
|
|
|
boost::hash_combine(seed, _expression.sequenceNumber);
|
|
|
|
|
|
|
|
return seed;
|
|
|
|
}
|
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
ExpressionClasses::Id ExpressionClasses::find(
|
|
|
|
AssemblyItem const& _item,
|
|
|
|
Ids const& _arguments,
|
|
|
|
bool _copyItem,
|
|
|
|
unsigned _sequenceNumber
|
|
|
|
)
|
|
|
|
{
|
|
|
|
Expression exp;
|
|
|
|
exp.id = Id(-1);
|
|
|
|
exp.item = &_item;
|
|
|
|
exp.arguments = _arguments;
|
|
|
|
exp.sequenceNumber = _sequenceNumber;
|
|
|
|
|
2015-05-22 08:48:54 +00:00
|
|
|
if (SemanticInformation::isCommutativeOperation(_item))
|
|
|
|
sort(exp.arguments.begin(), exp.arguments.end());
|
|
|
|
|
2015-04-30 13:31:16 +00:00
|
|
|
if (SemanticInformation::isDeterministic(_item))
|
|
|
|
{
|
|
|
|
auto it = m_expressions.find(exp);
|
|
|
|
if (it != m_expressions.end())
|
|
|
|
return it->id;
|
|
|
|
}
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
if (_copyItem)
|
|
|
|
exp.item = storeItem(_item);
|
|
|
|
|
|
|
|
ExpressionClasses::Id id = tryToSimplify(exp);
|
|
|
|
if (id < m_representatives.size())
|
|
|
|
exp.id = id;
|
|
|
|
else
|
|
|
|
{
|
2019-12-12 23:39:29 +00:00
|
|
|
exp.id = static_cast<Id>(m_representatives.size());
|
2015-04-24 15:35:16 +00:00
|
|
|
m_representatives.push_back(exp);
|
|
|
|
}
|
|
|
|
m_expressions.insert(exp);
|
|
|
|
return exp.id;
|
|
|
|
}
|
|
|
|
|
2015-05-22 08:48:54 +00:00
|
|
|
void ExpressionClasses::forceEqual(
|
|
|
|
ExpressionClasses::Id _id,
|
|
|
|
AssemblyItem const& _item,
|
|
|
|
ExpressionClasses::Ids const& _arguments,
|
|
|
|
bool _copyItem
|
|
|
|
)
|
|
|
|
{
|
|
|
|
Expression exp;
|
|
|
|
exp.id = _id;
|
|
|
|
exp.item = &_item;
|
|
|
|
exp.arguments = _arguments;
|
|
|
|
|
|
|
|
if (SemanticInformation::isCommutativeOperation(_item))
|
|
|
|
sort(exp.arguments.begin(), exp.arguments.end());
|
|
|
|
|
|
|
|
if (_copyItem)
|
|
|
|
exp.item = storeItem(_item);
|
|
|
|
|
|
|
|
m_expressions.insert(exp);
|
|
|
|
}
|
|
|
|
|
2015-05-12 19:27:04 +00:00
|
|
|
ExpressionClasses::Id ExpressionClasses::newClass(SourceLocation const& _location)
|
|
|
|
{
|
|
|
|
Expression exp;
|
2019-12-12 23:39:29 +00:00
|
|
|
exp.id = static_cast<Id>(m_representatives.size());
|
2015-05-12 19:27:04 +00:00
|
|
|
exp.item = storeItem(AssemblyItem(UndefinedItem, (u256(1) << 255) + exp.id, _location));
|
|
|
|
m_representatives.push_back(exp);
|
|
|
|
m_expressions.insert(exp);
|
|
|
|
return exp.id;
|
|
|
|
}
|
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
bool ExpressionClasses::knownToBeDifferent(ExpressionClasses::Id _a, ExpressionClasses::Id _b)
|
|
|
|
{
|
|
|
|
// Try to simplify "_a - _b" and return true iff the value is a non-zero constant.
|
|
|
|
return knownNonZero(find(Instruction::SUB, {_a, _b}));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExpressionClasses::knownToBeDifferentBy32(ExpressionClasses::Id _a, ExpressionClasses::Id _b)
|
|
|
|
{
|
|
|
|
// Try to simplify "_a - _b" and return true iff the value is at least 32 away from zero.
|
|
|
|
u256 const* v = knownConstant(find(Instruction::SUB, {_a, _b}));
|
|
|
|
// forbidden interval is ["-31", 31]
|
|
|
|
return v && *v + 31 > u256(62);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExpressionClasses::knownZero(Id _c)
|
|
|
|
{
|
|
|
|
return Pattern(u256(0)).matches(representative(_c), *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExpressionClasses::knownNonZero(Id _c)
|
|
|
|
{
|
|
|
|
return Pattern(u256(0)).matches(representative(find(Instruction::ISZERO, {_c})), *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
u256 const* ExpressionClasses::knownConstant(Id _c)
|
|
|
|
{
|
2023-07-10 11:12:46 +00:00
|
|
|
std::map<unsigned, Expression const*> matchGroups;
|
2015-04-24 15:35:16 +00:00
|
|
|
Pattern constant(Push);
|
|
|
|
constant.setMatchGroup(1, matchGroups);
|
|
|
|
if (!constant.matches(representative(_c), *this))
|
|
|
|
return nullptr;
|
|
|
|
return &constant.d();
|
|
|
|
}
|
|
|
|
|
|
|
|
AssemblyItem const* ExpressionClasses::storeItem(AssemblyItem const& _item)
|
|
|
|
{
|
2023-07-10 11:12:46 +00:00
|
|
|
m_spareAssemblyItems.push_back(std::make_shared<AssemblyItem>(_item));
|
2015-04-24 15:35:16 +00:00
|
|
|
return m_spareAssemblyItems.back().get();
|
|
|
|
}
|
|
|
|
|
2023-07-10 11:12:46 +00:00
|
|
|
std::string ExpressionClasses::fullDAGToString(ExpressionClasses::Id _id) const
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
|
|
|
Expression const& expr = representative(_id);
|
2023-07-10 11:12:46 +00:00
|
|
|
std::stringstream str;
|
|
|
|
str << std::dec << expr.id << ":";
|
2015-04-30 09:40:43 +00:00
|
|
|
if (expr.item)
|
|
|
|
{
|
|
|
|
str << *expr.item << "(";
|
|
|
|
for (Id arg: expr.arguments)
|
|
|
|
str << fullDAGToString(arg) << ",";
|
|
|
|
str << ")";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
str << " UNIQUE";
|
2015-04-24 15:35:16 +00:00
|
|
|
return str.str();
|
|
|
|
}
|
|
|
|
|
2018-01-18 12:58:51 +00:00
|
|
|
ExpressionClasses::Id ExpressionClasses::tryToSimplify(Expression const& _expr)
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
|
|
|
static Rules rules;
|
2018-09-14 13:49:55 +00:00
|
|
|
assertThrow(rules.isInitialized(), OptimizerException, "Rule list not properly initialized.");
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2015-04-30 13:31:16 +00:00
|
|
|
if (
|
|
|
|
!_expr.item ||
|
|
|
|
_expr.item->type() != Operation ||
|
|
|
|
!SemanticInformation::isDeterministic(*_expr.item)
|
|
|
|
)
|
2023-07-10 11:12:46 +00:00
|
|
|
return std::numeric_limits<unsigned>::max();
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2017-01-06 15:16:11 +00:00
|
|
|
if (auto match = rules.findFirstMatch(_expr, *this))
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2017-01-06 15:16:11 +00:00
|
|
|
// Debug info
|
2019-03-12 18:46:52 +00:00
|
|
|
if (false)
|
|
|
|
{
|
2023-07-10 11:12:46 +00:00
|
|
|
std::cout << "Simplifying " << *_expr.item << "(";
|
2019-03-12 18:46:52 +00:00
|
|
|
for (Id arg: _expr.arguments)
|
2023-07-10 11:12:46 +00:00
|
|
|
std::cout << fullDAGToString(arg) << ", ";
|
|
|
|
std::cout << ")" << std::endl;
|
|
|
|
std::cout << "with rule " << match->pattern.toString() << std::endl;
|
|
|
|
std::cout << "to " << match->action().toString() << std::endl;
|
2019-03-12 18:46:52 +00:00
|
|
|
}
|
2019-02-25 11:31:35 +00:00
|
|
|
|
2018-02-06 11:20:00 +00:00
|
|
|
return rebuildExpression(ExpressionTemplate(match->action(), _expr.item->location()));
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
|
2023-07-10 11:12:46 +00:00
|
|
|
return std::numeric_limits<unsigned>::max();
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ExpressionClasses::Id ExpressionClasses::rebuildExpression(ExpressionTemplate const& _template)
|
|
|
|
{
|
|
|
|
if (_template.hasId)
|
|
|
|
return _template.id;
|
|
|
|
|
|
|
|
Ids arguments;
|
|
|
|
for (ExpressionTemplate const& t: _template.arguments)
|
|
|
|
arguments.push_back(rebuildExpression(t));
|
|
|
|
return find(_template.item, arguments);
|
|
|
|
}
|