solidity/libevmasm/ExpressionClasses.cpp

245 lines
6.7 KiB
C++
Raw Normal View History

/*
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
/**
* @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>
#include <libevmasm/Assembly.h>
#include <libevmasm/CommonSubexpressionEliminator.h>
#include <libevmasm/SimplificationRules.h>
#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>
#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;
bool ExpressionClasses::Expression::operator==(ExpressionClasses::Expression const& _other) const
{
assertThrow(!!item && !!_other.item, OptimizerException, "");
auto type = item->type();
auto otherType = _other.item->type();
2017-01-06 10:33:08 +00:00
if (type != otherType)
return false;
2017-01-06 10:33:08 +00:00
else if (type == Operation)
{
auto instr = item->instruction();
auto otherInstr = _other.item->instruction();
return std::tie(instr, arguments, sequenceNumber) ==
2017-01-06 10:33:08 +00:00
std::tie(otherInstr, _other.arguments, _other.sequenceNumber);
}
else
return std::tie(item->data(), arguments, sequenceNumber) ==
2017-01-06 10:33:08 +00:00
std::tie(_other.item->data(), _other.arguments, _other.sequenceNumber);
}
2023-07-10 11:12:46 +00:00
size_t ExpressionClasses::Expression::ExpressionHash::operator()(Expression const& _expression) const
{
assertThrow(!!_expression.item, OptimizerException, "");
2023-07-10 11:12:46 +00:00
size_t seed = 0;
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;
}
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());
if (SemanticInformation::isDeterministic(_item))
{
auto it = m_expressions.find(exp);
if (it != m_expressions.end())
return it->id;
}
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());
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);
}
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());
exp.item = storeItem(AssemblyItem(UndefinedItem, (u256(1) << 255) + exp.id, _location));
m_representatives.push_back(exp);
m_expressions.insert(exp);
return exp.id;
}
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;
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));
return m_spareAssemblyItems.back().get();
}
2023-07-10 11:12:46 +00:00
std::string ExpressionClasses::fullDAGToString(ExpressionClasses::Id _id) const
{
Expression const& expr = representative(_id);
2023-07-10 11:12:46 +00:00
std::stringstream str;
str << std::dec << expr.id << ":";
if (expr.item)
{
str << *expr.item << "(";
for (Id arg: expr.arguments)
str << fullDAGToString(arg) << ",";
str << ")";
}
else
str << " UNIQUE";
return str.str();
}
ExpressionClasses::Id ExpressionClasses::tryToSimplify(Expression const& _expr)
{
static Rules rules;
assertThrow(rules.isInitialized(), OptimizerException, "Rule list not properly initialized.");
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();
if (auto match = rules.findFirstMatch(_expr, *this))
{
// 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
}
return rebuildExpression(ExpressionTemplate(match->action(), _expr.item->location()));
}
2023-07-10 11:12:46 +00:00
return std::numeric_limits<unsigned>::max();
}
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);
}