2017-01-06 15:16:11 +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
|
2017-01-06 15:16:11 +00:00
|
|
|
/**
|
|
|
|
* @file ExpressionClasses.cpp
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Container for equivalence classes of expressions for use in common subexpression elimination.
|
|
|
|
*/
|
|
|
|
|
2018-09-14 13:49:55 +00:00
|
|
|
#include <libevmasm/SimplificationRules.h>
|
|
|
|
|
2017-01-06 15:16:11 +00:00
|
|
|
#include <libevmasm/ExpressionClasses.h>
|
|
|
|
#include <libevmasm/Assembly.h>
|
2018-01-17 16:56:33 +00:00
|
|
|
#include <libevmasm/RuleList.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Assertions.h>
|
2018-09-14 13:49:55 +00:00
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
#include <functional>
|
2018-01-17 16:56:33 +00:00
|
|
|
|
2017-01-06 15:16:11 +00:00
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::evmasm;
|
|
|
|
using namespace solidity::langutil;
|
2017-01-06 15:16:11 +00:00
|
|
|
|
2018-02-06 11:20:00 +00:00
|
|
|
SimplificationRule<Pattern> const* Rules::findFirstMatch(
|
2017-01-06 15:16:11 +00:00
|
|
|
Expression const& _expr,
|
|
|
|
ExpressionClasses const& _classes
|
|
|
|
)
|
|
|
|
{
|
|
|
|
resetMatchGroups();
|
|
|
|
|
|
|
|
assertThrow(_expr.item, OptimizerException, "");
|
2018-11-07 11:04:46 +00:00
|
|
|
for (auto const& rule: m_rules[uint8_t(_expr.item->instruction())])
|
2017-01-06 15:16:11 +00:00
|
|
|
{
|
2018-02-06 11:20:00 +00:00
|
|
|
if (rule.pattern.matches(_expr, _classes))
|
2019-02-25 11:31:35 +00:00
|
|
|
if (!rule.feasible || rule.feasible())
|
|
|
|
return &rule;
|
|
|
|
|
2017-01-10 15:35:18 +00:00
|
|
|
resetMatchGroups();
|
2017-01-06 15:16:11 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-09-14 13:49:55 +00:00
|
|
|
bool Rules::isInitialized() const
|
|
|
|
{
|
2018-11-07 11:04:46 +00:00
|
|
|
return !m_rules[uint8_t(Instruction::ADD)].empty();
|
2018-09-14 13:49:55 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 11:20:00 +00:00
|
|
|
void Rules::addRules(std::vector<SimplificationRule<Pattern>> const& _rules)
|
2017-01-06 15:16:11 +00:00
|
|
|
{
|
|
|
|
for (auto const& r: _rules)
|
|
|
|
addRule(r);
|
|
|
|
}
|
|
|
|
|
2018-02-06 11:20:00 +00:00
|
|
|
void Rules::addRule(SimplificationRule<Pattern> const& _rule)
|
2017-01-06 15:16:11 +00:00
|
|
|
{
|
2018-11-07 11:04:46 +00:00
|
|
|
m_rules[uint8_t(_rule.pattern.instruction())].push_back(_rule);
|
2017-01-06 15:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Rules::Rules()
|
|
|
|
{
|
2018-07-10 07:18:59 +00:00
|
|
|
// Multiple occurrences of one of these inside one rule must match the same equivalence class.
|
2017-01-06 15:16:11 +00:00
|
|
|
// Constants.
|
|
|
|
Pattern A(Push);
|
|
|
|
Pattern B(Push);
|
|
|
|
Pattern C(Push);
|
|
|
|
// Anything.
|
2019-09-09 11:37:25 +00:00
|
|
|
Pattern W;
|
2017-01-06 15:16:11 +00:00
|
|
|
Pattern X;
|
|
|
|
Pattern Y;
|
2019-09-09 11:37:25 +00:00
|
|
|
Pattern Z;
|
2017-01-06 15:16:11 +00:00
|
|
|
A.setMatchGroup(1, m_matchGroups);
|
|
|
|
B.setMatchGroup(2, m_matchGroups);
|
|
|
|
C.setMatchGroup(3, m_matchGroups);
|
2019-09-09 11:37:25 +00:00
|
|
|
W.setMatchGroup(4, m_matchGroups);
|
|
|
|
X.setMatchGroup(5, m_matchGroups);
|
|
|
|
Y.setMatchGroup(6, m_matchGroups);
|
|
|
|
Z.setMatchGroup(7, m_matchGroups);
|
2017-05-10 09:25:53 +00:00
|
|
|
|
2020-05-15 23:59:57 +00:00
|
|
|
addRules(simplificationRuleList(nullopt, A, B, C, W, X, Y, Z));
|
2018-09-14 13:49:55 +00:00
|
|
|
assertThrow(isInitialized(), OptimizerException, "Rule list not properly initialized.");
|
2017-01-06 15:16:11 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 20:29:56 +00:00
|
|
|
Pattern::Pattern(Instruction _instruction, std::initializer_list<Pattern> _arguments):
|
2017-01-06 15:16:11 +00:00
|
|
|
m_type(Operation),
|
|
|
|
m_instruction(_instruction),
|
|
|
|
m_arguments(_arguments)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Pattern::setMatchGroup(unsigned _group, map<unsigned, Expression const*>& _matchGroups)
|
|
|
|
{
|
|
|
|
m_matchGroup = _group;
|
|
|
|
m_matchGroups = &_matchGroups;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Pattern::matches(Expression const& _expr, ExpressionClasses const& _classes) const
|
|
|
|
{
|
|
|
|
if (!matchesBaseItem(_expr.item))
|
|
|
|
return false;
|
|
|
|
if (m_matchGroup)
|
|
|
|
{
|
|
|
|
if (!m_matchGroups->count(m_matchGroup))
|
|
|
|
(*m_matchGroups)[m_matchGroup] = &_expr;
|
|
|
|
else if ((*m_matchGroups)[m_matchGroup]->id != _expr.id)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
assertThrow(m_arguments.size() == 0 || _expr.arguments.size() == m_arguments.size(), OptimizerException, "");
|
|
|
|
for (size_t i = 0; i < m_arguments.size(); ++i)
|
|
|
|
if (!m_arguments[i].matches(_classes.representative(_expr.arguments[i]), _classes))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
AssemblyItem Pattern::toAssemblyItem(SourceLocation const& _location) const
|
|
|
|
{
|
|
|
|
if (m_type == Operation)
|
|
|
|
return AssemblyItem(m_instruction, _location);
|
|
|
|
else
|
|
|
|
return AssemblyItem(m_type, data(), _location);
|
|
|
|
}
|
|
|
|
|
|
|
|
string Pattern::toString() const
|
|
|
|
{
|
|
|
|
stringstream s;
|
|
|
|
switch (m_type)
|
|
|
|
{
|
|
|
|
case Operation:
|
|
|
|
s << instructionInfo(m_instruction).name;
|
|
|
|
break;
|
|
|
|
case Push:
|
2017-01-10 15:35:10 +00:00
|
|
|
if (m_data)
|
|
|
|
s << "PUSH " << hex << data();
|
|
|
|
else
|
|
|
|
s << "PUSH ";
|
2017-01-06 15:16:11 +00:00
|
|
|
break;
|
|
|
|
case UndefinedItem:
|
|
|
|
s << "ANY";
|
|
|
|
break;
|
|
|
|
default:
|
2017-01-10 15:35:10 +00:00
|
|
|
if (m_data)
|
|
|
|
s << "t=" << dec << m_type << " d=" << hex << data();
|
|
|
|
else
|
|
|
|
s << "t=" << dec << m_type << " d: nullptr";
|
2017-01-06 15:16:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!m_requireDataMatch)
|
|
|
|
s << " ~";
|
|
|
|
if (m_matchGroup)
|
|
|
|
s << "[" << dec << m_matchGroup << "]";
|
|
|
|
s << "(";
|
|
|
|
for (Pattern const& p: m_arguments)
|
|
|
|
s << p.toString() << ", ";
|
|
|
|
s << ")";
|
|
|
|
return s.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Pattern::matchesBaseItem(AssemblyItem const* _item) const
|
|
|
|
{
|
|
|
|
if (m_type == UndefinedItem)
|
|
|
|
return true;
|
|
|
|
if (!_item)
|
|
|
|
return false;
|
|
|
|
if (m_type != _item->type())
|
|
|
|
return false;
|
|
|
|
else if (m_type == Operation)
|
|
|
|
return m_instruction == _item->instruction();
|
|
|
|
else if (m_requireDataMatch)
|
|
|
|
return data() == _item->data();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Pattern::Expression const& Pattern::matchGroupValue() const
|
|
|
|
{
|
|
|
|
assertThrow(m_matchGroup > 0, OptimizerException, "");
|
|
|
|
assertThrow(!!m_matchGroups, OptimizerException, "");
|
|
|
|
assertThrow((*m_matchGroups)[m_matchGroup], OptimizerException, "");
|
|
|
|
return *(*m_matchGroups)[m_matchGroup];
|
|
|
|
}
|
|
|
|
|
|
|
|
u256 const& Pattern::data() const
|
|
|
|
{
|
|
|
|
assertThrow(m_data, OptimizerException, "");
|
|
|
|
return *m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExpressionTemplate::ExpressionTemplate(Pattern const& _pattern, SourceLocation const& _location)
|
|
|
|
{
|
|
|
|
if (_pattern.matchGroup())
|
|
|
|
{
|
|
|
|
hasId = true;
|
|
|
|
id = _pattern.id();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hasId = false;
|
|
|
|
item = _pattern.toAssemblyItem(_location);
|
|
|
|
}
|
|
|
|
for (auto const& arg: _pattern.arguments())
|
2018-12-10 18:02:39 +00:00
|
|
|
arguments.emplace_back(arg, _location);
|
2017-01-06 15:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string ExpressionTemplate::toString() const
|
|
|
|
{
|
|
|
|
stringstream s;
|
|
|
|
if (hasId)
|
|
|
|
s << id;
|
|
|
|
else
|
|
|
|
s << item;
|
|
|
|
s << "(";
|
|
|
|
for (auto const& arg: arguments)
|
|
|
|
s << arg.toString();
|
|
|
|
s << ")";
|
|
|
|
return s.str();
|
|
|
|
}
|