2018-02-02 14:23:44 +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/>.
|
|
|
|
*/
|
|
|
|
/**
|
2018-06-06 04:43:58 +00:00
|
|
|
* Base class to perform data flow analysis during AST walks.
|
2018-02-02 14:23:44 +00:00
|
|
|
* Tracks assignments and is used as base class for both Rematerialiser and
|
|
|
|
* Common Subexpression Eliminator.
|
|
|
|
*/
|
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/DataFlowAnalyzer.h>
|
2018-02-02 14:23:44 +00:00
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/NameCollector.h>
|
|
|
|
#include <libyul/optimiser/Semantics.h>
|
|
|
|
#include <libyul/Exceptions.h>
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmData.h>
|
2018-02-02 14:23:44 +00:00
|
|
|
|
|
|
|
#include <libdevcore/CommonData.h>
|
|
|
|
|
|
|
|
#include <boost/range/adaptor/reversed.hpp>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
2018-11-21 11:42:34 +00:00
|
|
|
using namespace yul;
|
2018-02-02 14:23:44 +00:00
|
|
|
|
|
|
|
void DataFlowAnalyzer::operator()(Assignment& _assignment)
|
|
|
|
{
|
2018-10-29 14:12:02 +00:00
|
|
|
set<YulString> names;
|
2018-02-02 14:23:44 +00:00
|
|
|
for (auto const& var: _assignment.variableNames)
|
2018-10-29 14:12:02 +00:00
|
|
|
names.emplace(var.name);
|
2018-05-09 09:43:14 +00:00
|
|
|
assertThrow(_assignment.value, OptimizerException, "");
|
2018-02-02 14:23:44 +00:00
|
|
|
visit(*_assignment.value);
|
|
|
|
handleAssignment(names, _assignment.value.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlowAnalyzer::operator()(VariableDeclaration& _varDecl)
|
|
|
|
{
|
2018-10-29 14:12:02 +00:00
|
|
|
set<YulString> names;
|
2018-02-02 14:23:44 +00:00
|
|
|
for (auto const& var: _varDecl.variables)
|
2018-10-29 14:12:02 +00:00
|
|
|
names.emplace(var.name);
|
2018-02-05 17:02:32 +00:00
|
|
|
m_variableScopes.back().variables += names;
|
2018-02-02 14:23:44 +00:00
|
|
|
if (_varDecl.value)
|
|
|
|
visit(*_varDecl.value);
|
|
|
|
handleAssignment(names, _varDecl.value.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlowAnalyzer::operator()(If& _if)
|
|
|
|
{
|
|
|
|
ASTModifier::operator()(_if);
|
|
|
|
|
|
|
|
Assignments assignments;
|
|
|
|
assignments(_if.body);
|
|
|
|
clearValues(assignments.names());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlowAnalyzer::operator()(Switch& _switch)
|
|
|
|
{
|
|
|
|
visit(*_switch.expression);
|
2018-10-29 14:12:02 +00:00
|
|
|
set<YulString> assignedVariables;
|
2018-02-02 14:23:44 +00:00
|
|
|
for (auto& _case: _switch.cases)
|
|
|
|
{
|
|
|
|
(*this)(_case.body);
|
|
|
|
Assignments assignments;
|
|
|
|
assignments(_case.body);
|
|
|
|
assignedVariables += assignments.names();
|
|
|
|
// This is a little too destructive, we could retain the old values.
|
|
|
|
clearValues(assignments.names());
|
|
|
|
}
|
|
|
|
clearValues(assignedVariables);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlowAnalyzer::operator()(FunctionDefinition& _fun)
|
|
|
|
{
|
2018-10-28 11:57:13 +00:00
|
|
|
// Save all information. We might rather reinstantiate this class,
|
|
|
|
// but this could be difficult if it is subclassed.
|
|
|
|
map<YulString, Expression const*> value;
|
|
|
|
map<YulString, set<YulString>> references;
|
|
|
|
map<YulString, set<YulString>> referencedBy;
|
|
|
|
m_value.swap(value);
|
|
|
|
m_references.swap(references);
|
|
|
|
m_referencedBy.swap(referencedBy);
|
2018-10-18 12:45:11 +00:00
|
|
|
pushScope(true);
|
2018-10-28 11:57:13 +00:00
|
|
|
|
2018-02-02 14:23:44 +00:00
|
|
|
for (auto const& parameter: _fun.parameters)
|
2018-10-29 14:12:02 +00:00
|
|
|
m_variableScopes.back().variables.emplace(parameter.name);
|
2018-02-02 14:23:44 +00:00
|
|
|
for (auto const& var: _fun.returnVariables)
|
2018-12-13 11:33:03 +00:00
|
|
|
{
|
2018-10-29 14:12:02 +00:00
|
|
|
m_variableScopes.back().variables.emplace(var.name);
|
2018-12-13 11:33:03 +00:00
|
|
|
handleAssignment({var.name}, nullptr);
|
|
|
|
}
|
2018-02-02 14:23:44 +00:00
|
|
|
ASTModifier::operator()(_fun);
|
2018-10-28 11:57:13 +00:00
|
|
|
|
2018-10-18 12:45:11 +00:00
|
|
|
popScope();
|
2018-10-28 11:57:13 +00:00
|
|
|
m_value.swap(value);
|
|
|
|
m_references.swap(references);
|
|
|
|
m_referencedBy.swap(referencedBy);
|
2018-02-02 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlowAnalyzer::operator()(ForLoop& _for)
|
|
|
|
{
|
|
|
|
// Special scope handling of the pre block.
|
2018-10-18 12:45:11 +00:00
|
|
|
pushScope(false);
|
2018-02-02 14:23:44 +00:00
|
|
|
for (auto& statement: _for.pre.statements)
|
|
|
|
visit(statement);
|
|
|
|
|
|
|
|
Assignments assignments;
|
|
|
|
assignments(_for.body);
|
|
|
|
assignments(_for.post);
|
|
|
|
clearValues(assignments.names());
|
|
|
|
|
|
|
|
visit(*_for.condition);
|
|
|
|
(*this)(_for.body);
|
|
|
|
(*this)(_for.post);
|
|
|
|
|
|
|
|
clearValues(assignments.names());
|
2018-10-18 12:45:11 +00:00
|
|
|
popScope();
|
2018-02-02 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlowAnalyzer::operator()(Block& _block)
|
|
|
|
{
|
|
|
|
size_t numScopes = m_variableScopes.size();
|
2018-10-18 12:45:11 +00:00
|
|
|
pushScope(false);
|
2018-02-02 14:23:44 +00:00
|
|
|
ASTModifier::operator()(_block);
|
2018-10-18 12:45:11 +00:00
|
|
|
popScope();
|
2018-05-09 09:43:14 +00:00
|
|
|
assertThrow(numScopes == m_variableScopes.size(), OptimizerException, "");
|
2018-02-02 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 14:12:02 +00:00
|
|
|
void DataFlowAnalyzer::handleAssignment(set<YulString> const& _variables, Expression* _value)
|
2018-02-02 14:23:44 +00:00
|
|
|
{
|
2018-12-11 23:49:36 +00:00
|
|
|
static Expression const zero{Literal{{}, LiteralKind::Number, YulString{"0"}, {}}};
|
2018-02-02 14:23:44 +00:00
|
|
|
clearValues(_variables);
|
|
|
|
|
2018-12-20 17:55:32 +00:00
|
|
|
MovableChecker movableChecker{m_dialect};
|
2018-02-02 14:23:44 +00:00
|
|
|
if (_value)
|
|
|
|
movableChecker.visit(*_value);
|
2018-12-11 23:49:36 +00:00
|
|
|
else
|
|
|
|
for (auto const& var: _variables)
|
|
|
|
m_value[var] = &zero;
|
|
|
|
|
|
|
|
if (_value && _variables.size() == 1)
|
2018-02-02 14:23:44 +00:00
|
|
|
{
|
2018-10-29 14:12:02 +00:00
|
|
|
YulString name = *_variables.begin();
|
2018-02-02 14:23:44 +00:00
|
|
|
// Expression has to be movable and cannot contain a reference
|
|
|
|
// to the variable that will be assigned to.
|
2018-12-11 23:49:36 +00:00
|
|
|
if (movableChecker.movable() && !movableChecker.referencedVariables().count(name))
|
2018-02-02 14:23:44 +00:00
|
|
|
m_value[name] = _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const& referencedVariables = movableChecker.referencedVariables();
|
|
|
|
for (auto const& name: _variables)
|
|
|
|
{
|
|
|
|
m_references[name] = referencedVariables;
|
|
|
|
for (auto const& ref: referencedVariables)
|
2018-10-29 14:12:02 +00:00
|
|
|
m_referencedBy[ref].emplace(name);
|
2018-02-02 14:23:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 12:45:11 +00:00
|
|
|
void DataFlowAnalyzer::pushScope(bool _functionScope)
|
|
|
|
{
|
|
|
|
m_variableScopes.emplace_back(_functionScope);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlowAnalyzer::popScope()
|
|
|
|
{
|
|
|
|
clearValues(std::move(m_variableScopes.back().variables));
|
|
|
|
m_variableScopes.pop_back();
|
|
|
|
}
|
|
|
|
|
2018-10-29 14:12:02 +00:00
|
|
|
void DataFlowAnalyzer::clearValues(set<YulString> _variables)
|
2018-02-02 14:23:44 +00:00
|
|
|
{
|
|
|
|
// All variables that reference variables to be cleared also have to be
|
|
|
|
// cleared, but not recursively, since only the value of the original
|
|
|
|
// variables changes. Example:
|
|
|
|
// let a := 1
|
|
|
|
// let b := a
|
|
|
|
// let c := b
|
|
|
|
// let a := 2
|
|
|
|
// add(b, c)
|
|
|
|
// In the last line, we can replace c by b, but not b by a.
|
|
|
|
//
|
|
|
|
// This cannot be easily tested since the substitutions will be done
|
|
|
|
// one by one on the fly, and the last line will just be add(1, 1)
|
|
|
|
|
|
|
|
// Clear variables that reference variables to be cleared.
|
2018-10-18 12:45:11 +00:00
|
|
|
for (auto const& name: _variables)
|
2018-02-02 14:23:44 +00:00
|
|
|
for (auto const& ref: m_referencedBy[name])
|
2018-10-29 14:12:02 +00:00
|
|
|
_variables.emplace(ref);
|
2018-02-02 14:23:44 +00:00
|
|
|
|
|
|
|
// Clear the value and update the reference relation.
|
2018-10-18 12:45:11 +00:00
|
|
|
for (auto const& name: _variables)
|
2018-02-02 14:23:44 +00:00
|
|
|
m_value.erase(name);
|
2018-10-18 12:45:11 +00:00
|
|
|
for (auto const& name: _variables)
|
2018-02-02 14:23:44 +00:00
|
|
|
{
|
|
|
|
for (auto const& ref: m_references[name])
|
|
|
|
m_referencedBy[ref].erase(name);
|
|
|
|
m_references[name].clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 14:12:02 +00:00
|
|
|
bool DataFlowAnalyzer::inScope(YulString _variableName) const
|
2018-02-02 14:23:44 +00:00
|
|
|
{
|
|
|
|
for (auto const& scope: m_variableScopes | boost::adaptors::reversed)
|
|
|
|
{
|
2018-02-05 17:02:32 +00:00
|
|
|
if (scope.variables.count(_variableName))
|
2018-02-02 14:23:44 +00:00
|
|
|
return true;
|
2018-02-05 17:02:32 +00:00
|
|
|
if (scope.isFunction)
|
2018-02-02 14:23:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|