2019-02-04 16:30:29 +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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Optimisation stage that aggressively rematerializes certain variables ina a function to free
|
|
|
|
* space on the stack until it is compilable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libyul/optimiser/StackCompressor.h>
|
|
|
|
|
|
|
|
#include <libyul/optimiser/SSAValueTracker.h>
|
|
|
|
#include <libyul/optimiser/NameCollector.h>
|
|
|
|
#include <libyul/optimiser/Rematerialiser.h>
|
|
|
|
#include <libyul/optimiser/UnusedPruner.h>
|
|
|
|
#include <libyul/optimiser/Metrics.h>
|
|
|
|
#include <libyul/optimiser/Semantics.h>
|
|
|
|
|
|
|
|
#include <libyul/CompilabilityChecker.h>
|
|
|
|
|
|
|
|
#include <libyul/AsmData.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace yul;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2019-03-13 16:44:45 +00:00
|
|
|
/**
|
|
|
|
* Class that discovers all variables that can be fully eliminated by rematerialization,
|
|
|
|
* and the corresponding approximate costs.
|
|
|
|
*/
|
|
|
|
class RematCandidateSelector: public DataFlowAnalyzer
|
2019-02-04 16:30:29 +00:00
|
|
|
{
|
2019-03-13 16:44:45 +00:00
|
|
|
public:
|
|
|
|
explicit RematCandidateSelector(Dialect const& _dialect): DataFlowAnalyzer(_dialect) {}
|
|
|
|
|
2019-05-22 15:55:36 +00:00
|
|
|
/// @returns a set of tuples of rematerialisation costs, variable to rematerialise
|
|
|
|
/// and variables that occur in its expression.
|
2019-03-13 16:44:45 +00:00
|
|
|
/// Note that this set is sorted by cost.
|
2019-05-22 15:55:36 +00:00
|
|
|
set<tuple<size_t, YulString, set<YulString>>> candidates()
|
2019-03-13 16:44:45 +00:00
|
|
|
{
|
2019-05-22 15:55:36 +00:00
|
|
|
set<tuple<size_t, YulString, set<YulString>>> cand;
|
2019-03-13 16:44:45 +00:00
|
|
|
for (auto const& codeCost: m_expressionCodeCost)
|
|
|
|
{
|
|
|
|
size_t numRef = m_numReferences[codeCost.first];
|
2019-05-27 18:27:06 +00:00
|
|
|
cand.emplace(make_tuple(codeCost.second * numRef, codeCost.first, m_references.forward[codeCost.first]));
|
2019-03-13 16:44:45 +00:00
|
|
|
}
|
|
|
|
return cand;
|
|
|
|
}
|
|
|
|
|
|
|
|
using DataFlowAnalyzer::operator();
|
|
|
|
void operator()(VariableDeclaration& _varDecl) override
|
|
|
|
{
|
|
|
|
DataFlowAnalyzer::operator()(_varDecl);
|
|
|
|
if (_varDecl.variables.size() == 1)
|
|
|
|
{
|
|
|
|
YulString varName = _varDecl.variables.front().name;
|
|
|
|
if (m_value.count(varName))
|
2019-05-20 12:30:32 +00:00
|
|
|
m_expressionCodeCost[varName] = CodeCost::codeCost(m_dialect, *m_value[varName]);
|
2019-03-13 16:44:45 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-04 16:30:29 +00:00
|
|
|
|
2019-03-13 16:44:45 +00:00
|
|
|
void operator()(Assignment& _assignment) override
|
|
|
|
{
|
|
|
|
for (auto const& var: _assignment.variableNames)
|
|
|
|
rematImpossible(var.name);
|
|
|
|
DataFlowAnalyzer::operator()(_assignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use visit(Expression) because operator()(Identifier) would also
|
|
|
|
// get called on left-hand-sides of assignments.
|
|
|
|
void visit(Expression& _e) override
|
|
|
|
{
|
|
|
|
if (_e.type() == typeid(Identifier))
|
|
|
|
{
|
|
|
|
YulString name = boost::get<Identifier>(_e).name;
|
|
|
|
if (m_expressionCodeCost.count(name))
|
|
|
|
{
|
|
|
|
if (!m_value.count(name))
|
|
|
|
rematImpossible(name);
|
|
|
|
else
|
|
|
|
++m_numReferences[name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DataFlowAnalyzer::visit(_e);
|
|
|
|
}
|
2019-02-04 16:30:29 +00:00
|
|
|
|
2019-03-13 16:44:45 +00:00
|
|
|
/// Remove the variable from the candidate set.
|
|
|
|
void rematImpossible(YulString _variable)
|
2019-02-04 16:30:29 +00:00
|
|
|
{
|
2019-03-13 16:44:45 +00:00
|
|
|
m_numReferences.erase(_variable);
|
|
|
|
m_expressionCodeCost.erase(_variable);
|
2019-02-04 16:30:29 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 16:44:45 +00:00
|
|
|
/// Candidate variables and the code cost of their value.
|
|
|
|
map<YulString, size_t> m_expressionCodeCost;
|
|
|
|
/// Number of references to each candidate variable.
|
|
|
|
map<YulString, size_t> m_numReferences;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ASTNode>
|
2019-05-27 11:42:50 +00:00
|
|
|
void eliminateVariables(
|
|
|
|
Dialect const& _dialect,
|
|
|
|
ASTNode& _node,
|
|
|
|
size_t _numVariables,
|
|
|
|
bool _allowMSizeOptimization
|
|
|
|
)
|
2019-03-13 16:44:45 +00:00
|
|
|
{
|
2019-05-16 08:56:56 +00:00
|
|
|
RematCandidateSelector selector{_dialect};
|
2019-03-13 16:44:45 +00:00
|
|
|
selector(_node);
|
|
|
|
|
2019-02-04 16:30:29 +00:00
|
|
|
// Select at most _numVariables
|
|
|
|
set<YulString> varsToEliminate;
|
2019-03-13 16:44:45 +00:00
|
|
|
for (auto const& costs: selector.candidates())
|
2019-02-04 16:30:29 +00:00
|
|
|
{
|
|
|
|
if (varsToEliminate.size() >= _numVariables)
|
|
|
|
break;
|
2019-05-22 15:55:36 +00:00
|
|
|
// If a variable we would like to eliminate references another one
|
|
|
|
// we already selected for elimination, then stop selecting
|
|
|
|
// candidates. If we would add that variable, then the cost calculation
|
|
|
|
// for the previous variable would be off. Furthermore, we
|
|
|
|
// do not skip the variable because it would be better to properly re-compute
|
|
|
|
// the costs of all other variables instead.
|
|
|
|
bool referencesVarToEliminate = false;
|
|
|
|
for (YulString const& referencedVar: get<2>(costs))
|
|
|
|
if (varsToEliminate.count(referencedVar))
|
|
|
|
{
|
|
|
|
referencesVarToEliminate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (referencesVarToEliminate)
|
|
|
|
break;
|
|
|
|
varsToEliminate.insert(get<1>(costs));
|
2019-02-04 16:30:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 08:56:56 +00:00
|
|
|
Rematerialiser::run(_dialect, _node, std::move(varsToEliminate));
|
2019-05-27 11:42:50 +00:00
|
|
|
UnusedPruner::runUntilStabilised(_dialect, _node, _allowMSizeOptimization);
|
2019-02-04 16:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-14 17:25:33 +00:00
|
|
|
bool StackCompressor::run(
|
2019-05-16 08:56:56 +00:00
|
|
|
Dialect const& _dialect,
|
2019-07-09 15:23:14 +00:00
|
|
|
Object& _object,
|
2019-03-14 17:25:33 +00:00
|
|
|
bool _optimizeStackAllocation,
|
|
|
|
size_t _maxIterations
|
|
|
|
)
|
2019-02-04 16:30:29 +00:00
|
|
|
{
|
|
|
|
yulAssert(
|
2019-07-09 15:23:14 +00:00
|
|
|
_object.code &&
|
|
|
|
_object.code->statements.size() > 0 && _object.code->statements.at(0).type() == typeid(Block),
|
2019-02-04 16:30:29 +00:00
|
|
|
"Need to run the function grouper before the stack compressor."
|
|
|
|
);
|
2019-07-09 15:23:14 +00:00
|
|
|
bool allowMSizeOptimzation = !SideEffectsCollector(_dialect, *_object.code).containsMSize();
|
2019-03-14 17:25:33 +00:00
|
|
|
for (size_t iterations = 0; iterations < _maxIterations; iterations++)
|
2019-02-04 16:30:29 +00:00
|
|
|
{
|
2019-07-09 15:23:14 +00:00
|
|
|
map<YulString, int> stackSurplus = CompilabilityChecker::run(_dialect, _object, _optimizeStackAllocation);
|
2019-02-04 16:30:29 +00:00
|
|
|
if (stackSurplus.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (stackSurplus.count(YulString{}))
|
|
|
|
{
|
|
|
|
yulAssert(stackSurplus.at({}) > 0, "Invalid surplus value.");
|
2019-05-27 11:42:50 +00:00
|
|
|
eliminateVariables(
|
|
|
|
_dialect,
|
2019-07-09 15:23:14 +00:00
|
|
|
boost::get<Block>(_object.code->statements.at(0)),
|
2019-05-27 11:42:50 +00:00
|
|
|
stackSurplus.at({}),
|
|
|
|
allowMSizeOptimzation
|
|
|
|
);
|
2019-02-04 16:30:29 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 15:23:14 +00:00
|
|
|
for (size_t i = 1; i < _object.code->statements.size(); ++i)
|
2019-02-04 16:30:29 +00:00
|
|
|
{
|
2019-07-09 15:23:14 +00:00
|
|
|
FunctionDefinition& fun = boost::get<FunctionDefinition>(_object.code->statements[i]);
|
2019-02-04 16:30:29 +00:00
|
|
|
if (!stackSurplus.count(fun.name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
yulAssert(stackSurplus.at(fun.name) > 0, "Invalid surplus value.");
|
2019-05-27 11:42:50 +00:00
|
|
|
eliminateVariables(
|
|
|
|
_dialect,
|
|
|
|
fun,
|
|
|
|
stackSurplus.at(fun.name),
|
|
|
|
allowMSizeOptimzation
|
|
|
|
);
|
2019-02-04 16:30:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|