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/NameCollector.h>
|
|
|
|
#include <libyul/optimiser/Rematerialiser.h>
|
|
|
|
#include <libyul/optimiser/UnusedPruner.h>
|
|
|
|
#include <libyul/optimiser/Metrics.h>
|
|
|
|
#include <libyul/optimiser/Semantics.h>
|
|
|
|
|
2021-08-12 15:17:21 +00:00
|
|
|
#include <libyul/backends/evm/ControlFlowGraphBuilder.h>
|
|
|
|
#include <libyul/backends/evm/StackHelpers.h>
|
|
|
|
#include <libyul/backends/evm/StackLayoutGenerator.h>
|
|
|
|
|
|
|
|
#include <libyul/AsmAnalysis.h>
|
|
|
|
#include <libyul/AsmAnalysisInfo.h>
|
|
|
|
|
2019-02-04 16:30:29 +00:00
|
|
|
#include <libyul/CompilabilityChecker.h>
|
|
|
|
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2019-02-04 16:30:29 +00:00
|
|
|
|
2021-09-08 11:50:36 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
2019-02-04 16:30:29 +00:00
|
|
|
|
|
|
|
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.
|
2022-03-15 17:10:52 +00:00
|
|
|
*
|
|
|
|
* Prerequisite: Disambiguator, Function Grouper
|
2019-03-13 16:44:45 +00:00
|
|
|
*/
|
|
|
|
class RematCandidateSelector: public DataFlowAnalyzer
|
2019-02-04 16:30:29 +00:00
|
|
|
{
|
2019-03-13 16:44:45 +00:00
|
|
|
public:
|
2022-07-11 13:51:25 +00:00
|
|
|
explicit RematCandidateSelector(Dialect const& _dialect): DataFlowAnalyzer(_dialect, MemoryAndStorage::Ignore) {}
|
2019-03-13 16:44:45 +00:00
|
|
|
|
2022-03-15 17:10:52 +00:00
|
|
|
/// @returns a map from function name to rematerialisation costs to a vector of variables to rematerialise
|
2021-09-08 11:50:36 +00:00
|
|
|
/// and variables that occur in their expression.
|
|
|
|
/// While the map is sorted by cost, the contained vectors are sorted by the order of occurrence.
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, std::map<size_t, std::vector<YulString>>> candidates()
|
2019-03-13 16:44:45 +00:00
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, std::map<size_t, std::vector<YulString>>> cand;
|
2022-03-15 17:10:52 +00:00
|
|
|
for (auto const& [functionName, candidate]: m_candidates)
|
2019-03-13 16:44:45 +00:00
|
|
|
{
|
2021-09-08 11:50:36 +00:00
|
|
|
if (size_t const* cost = util::valueOrNullptr(m_expressionCodeCost, candidate))
|
|
|
|
{
|
|
|
|
size_t numRef = m_numReferences[candidate];
|
2022-03-16 15:43:21 +00:00
|
|
|
cand[functionName][*cost * numRef].emplace_back(candidate);
|
2021-09-08 11:50:36 +00:00
|
|
|
}
|
2019-03-13 16:44:45 +00:00
|
|
|
}
|
|
|
|
return cand;
|
|
|
|
}
|
|
|
|
|
|
|
|
using DataFlowAnalyzer::operator();
|
2022-03-15 17:10:52 +00:00
|
|
|
void operator()(FunctionDefinition& _function) override
|
|
|
|
{
|
|
|
|
yulAssert(m_currentFunctionName.empty());
|
|
|
|
m_currentFunctionName = _function.name;
|
|
|
|
DataFlowAnalyzer::operator()(_function);
|
|
|
|
m_currentFunctionName = {};
|
|
|
|
}
|
|
|
|
|
2019-03-13 16:44:45 +00:00
|
|
|
void operator()(VariableDeclaration& _varDecl) override
|
|
|
|
{
|
|
|
|
DataFlowAnalyzer::operator()(_varDecl);
|
|
|
|
if (_varDecl.variables.size() == 1)
|
|
|
|
{
|
|
|
|
YulString varName = _varDecl.variables.front().name;
|
2022-03-10 17:25:00 +00:00
|
|
|
if (AssignedValue const* value = variableValue(varName))
|
2021-09-08 11:50:36 +00:00
|
|
|
{
|
|
|
|
yulAssert(!m_expressionCodeCost.count(varName), "");
|
2022-03-15 17:10:52 +00:00
|
|
|
m_candidates.emplace_back(m_currentFunctionName, varName);
|
2022-03-10 17:25:00 +00:00
|
|
|
m_expressionCodeCost[varName] = CodeCost::codeCost(m_dialect, *value->value);
|
2021-09-08 11:50:36 +00:00
|
|
|
}
|
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
|
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
if (std::holds_alternative<Identifier>(_e))
|
2019-03-13 16:44:45 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
YulString name = std::get<Identifier>(_e).name;
|
2019-03-13 16:44:45 +00:00
|
|
|
if (m_expressionCodeCost.count(name))
|
|
|
|
{
|
2022-03-10 17:25:00 +00:00
|
|
|
if (!variableValue(name))
|
2019-03-13 16:44:45 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-15 17:10:52 +00:00
|
|
|
YulString m_currentFunctionName = {};
|
|
|
|
|
|
|
|
/// All candidate variables by function name, in order of occurrence.
|
2023-08-23 02:03:44 +00:00
|
|
|
std::vector<std::pair<YulString, YulString>> m_candidates;
|
2019-03-13 16:44:45 +00:00
|
|
|
/// Candidate variables and the code cost of their value.
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, size_t> m_expressionCodeCost;
|
2019-03-13 16:44:45 +00:00
|
|
|
/// Number of references to each candidate variable.
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, size_t> m_numReferences;
|
2019-03-13 16:44:45 +00:00
|
|
|
};
|
|
|
|
|
2021-09-08 11:50:36 +00:00
|
|
|
/// Selects at most @a _numVariables among @a _candidates.
|
2023-08-23 02:03:44 +00:00
|
|
|
std::set<YulString> chooseVarsToEliminate(
|
|
|
|
std::map<size_t, std::vector<YulString>> const& _candidates,
|
2021-09-08 11:50:36 +00:00
|
|
|
size_t _numVariables
|
|
|
|
)
|
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
std::set<YulString> varsToEliminate;
|
2021-09-08 11:50:36 +00:00
|
|
|
for (auto&& [cost, candidates]: _candidates)
|
2022-03-16 15:43:21 +00:00
|
|
|
for (auto&& candidate: candidates)
|
2021-09-08 11:50:36 +00:00
|
|
|
{
|
|
|
|
if (varsToEliminate.size() >= _numVariables)
|
|
|
|
return varsToEliminate;
|
|
|
|
varsToEliminate.insert(candidate);
|
|
|
|
}
|
|
|
|
return varsToEliminate;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:42:50 +00:00
|
|
|
void eliminateVariables(
|
|
|
|
Dialect const& _dialect,
|
2022-03-15 17:10:52 +00:00
|
|
|
Block& _ast,
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, int> const& _numVariables,
|
2019-05-27 11:42:50 +00:00
|
|
|
bool _allowMSizeOptimization
|
|
|
|
)
|
2019-03-13 16:44:45 +00:00
|
|
|
{
|
2019-05-16 08:56:56 +00:00
|
|
|
RematCandidateSelector selector{_dialect};
|
2022-03-15 17:10:52 +00:00
|
|
|
selector(_ast);
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, std::map<size_t, std::vector<YulString>>> candidates = selector.candidates();
|
2022-03-15 17:10:52 +00:00
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::set<YulString> varsToEliminate;
|
2022-03-15 17:10:52 +00:00
|
|
|
for (auto const& [functionName, numVariables]: _numVariables)
|
|
|
|
{
|
|
|
|
yulAssert(numVariables > 0);
|
|
|
|
varsToEliminate += chooseVarsToEliminate(candidates[functionName], static_cast<size_t>(numVariables));
|
|
|
|
}
|
|
|
|
|
2022-08-23 17:28:45 +00:00
|
|
|
Rematerialiser::run(_dialect, _ast, std::move(varsToEliminate));
|
2022-03-15 17:10:52 +00:00
|
|
|
// Do not remove functions.
|
2023-08-23 02:03:44 +00:00
|
|
|
std::set<YulString> allFunctions = NameCollector{_ast, NameCollector::OnlyFunctions}.names();
|
2022-03-15 17:10:52 +00:00
|
|
|
UnusedPruner::runUntilStabilised(_dialect, _ast, _allowMSizeOptimization, nullptr, allFunctions);
|
2019-02-04 16:30:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 17:10:52 +00:00
|
|
|
void eliminateVariablesOptimizedCodegen(
|
2021-08-12 15:17:21 +00:00
|
|
|
Dialect const& _dialect,
|
2022-03-15 17:10:52 +00:00
|
|
|
Block& _ast,
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, std::vector<StackLayoutGenerator::StackTooDeep>> const& _unreachables,
|
2021-08-12 15:17:21 +00:00
|
|
|
bool _allowMSizeOptimization
|
|
|
|
)
|
|
|
|
{
|
2022-03-15 17:10:52 +00:00
|
|
|
if (std::all_of(_unreachables.begin(), _unreachables.end(), [](auto const& _item) { return _item.second.empty(); }))
|
|
|
|
return;
|
|
|
|
|
2021-08-12 15:17:21 +00:00
|
|
|
RematCandidateSelector selector{_dialect};
|
2022-03-15 17:10:52 +00:00
|
|
|
selector(_ast);
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, size_t> candidates;
|
2022-03-15 17:10:52 +00:00
|
|
|
for (auto const& [functionName, candidatesInFunction]: selector.candidates())
|
|
|
|
for (auto [cost, candidatesWithCost]: candidatesInFunction)
|
|
|
|
for (auto candidate: candidatesWithCost)
|
2022-03-16 15:43:21 +00:00
|
|
|
candidates[candidate] = cost;
|
2021-08-12 15:17:21 +00:00
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::set<YulString> varsToEliminate;
|
2021-08-12 15:17:21 +00:00
|
|
|
|
|
|
|
// TODO: this currently ignores the fact that variables may reference other variables we want to eliminate.
|
2022-03-15 17:10:52 +00:00
|
|
|
for (auto const& [functionName, unreachables]: _unreachables)
|
|
|
|
for (auto const& unreachable: unreachables)
|
2021-08-12 15:17:21 +00:00
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<size_t, std::vector<YulString>> suitableCandidates;
|
2022-03-15 17:10:52 +00:00
|
|
|
size_t neededSlots = unreachable.deficit;
|
|
|
|
for (auto varName: unreachable.variableChoices)
|
|
|
|
{
|
|
|
|
if (varsToEliminate.count(varName))
|
|
|
|
--neededSlots;
|
|
|
|
else if (size_t* cost = util::valueOrNullptr(candidates, varName))
|
|
|
|
if (!util::contains(suitableCandidates[*cost], varName))
|
|
|
|
suitableCandidates[*cost].emplace_back(varName);
|
|
|
|
}
|
|
|
|
for (auto candidatesByCost: suitableCandidates)
|
|
|
|
{
|
|
|
|
for (auto candidate: candidatesByCost.second)
|
|
|
|
if (neededSlots--)
|
|
|
|
varsToEliminate.emplace(candidate);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
if (!neededSlots)
|
2021-08-12 15:17:21 +00:00
|
|
|
break;
|
2022-03-15 17:10:52 +00:00
|
|
|
}
|
2021-08-12 15:17:21 +00:00
|
|
|
}
|
2022-03-15 17:10:52 +00:00
|
|
|
Rematerialiser::run(_dialect, _ast, std::move(varsToEliminate), true);
|
|
|
|
// Do not remove functions.
|
2023-08-23 02:03:44 +00:00
|
|
|
std::set<YulString> allFunctions = NameCollector{_ast, NameCollector::OnlyFunctions}.names();
|
2022-03-15 17:10:52 +00:00
|
|
|
UnusedPruner::runUntilStabilised(_dialect, _ast, _allowMSizeOptimization, nullptr, allFunctions);
|
2021-08-12 15:17:21 +00:00
|
|
|
}
|
|
|
|
|
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 &&
|
2023-08-23 02:03:44 +00:00
|
|
|
_object.code->statements.size() > 0 && std::holds_alternative<Block>(_object.code->statements.at(0)),
|
2019-02-04 16:30:29 +00:00
|
|
|
"Need to run the function grouper before the stack compressor."
|
|
|
|
);
|
2021-08-12 15:17:21 +00:00
|
|
|
bool usesOptimizedCodeGenerator = false;
|
|
|
|
if (auto evmDialect = dynamic_cast<EVMDialect const*>(&_dialect))
|
|
|
|
usesOptimizedCodeGenerator =
|
|
|
|
_optimizeStackAllocation &&
|
|
|
|
evmDialect->evmVersion().canOverchargeGasForCall() &&
|
|
|
|
evmDialect->providesObjectAccess();
|
2019-08-13 11:34:33 +00:00
|
|
|
bool allowMSizeOptimzation = !MSizeFinder::containsMSize(_dialect, *_object.code);
|
2021-08-12 15:17:21 +00:00
|
|
|
if (usesOptimizedCodeGenerator)
|
2019-02-04 16:30:29 +00:00
|
|
|
{
|
2021-08-12 15:17:21 +00:00
|
|
|
yul::AsmAnalysisInfo analysisInfo = yul::AsmAnalyzer::analyzeStrictAssertCorrect(_dialect, _object);
|
2023-08-23 02:03:44 +00:00
|
|
|
std::unique_ptr<CFG> cfg = ControlFlowGraphBuilder::build(analysisInfo, _dialect, *_object.code);
|
2022-03-15 17:10:52 +00:00
|
|
|
eliminateVariablesOptimizedCodegen(
|
|
|
|
_dialect,
|
|
|
|
*_object.code,
|
|
|
|
StackLayoutGenerator::reportStackTooDeep(*cfg),
|
|
|
|
allowMSizeOptimzation
|
|
|
|
);
|
2019-02-04 16:30:29 +00:00
|
|
|
}
|
2021-08-12 15:17:21 +00:00
|
|
|
else
|
|
|
|
for (size_t iterations = 0; iterations < _maxIterations; iterations++)
|
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, int> stackSurplus = CompilabilityChecker(_dialect, _object, _optimizeStackAllocation).stackDeficit;
|
2021-08-12 15:17:21 +00:00
|
|
|
if (stackSurplus.empty())
|
|
|
|
return true;
|
2022-03-15 17:10:52 +00:00
|
|
|
eliminateVariables(
|
|
|
|
_dialect,
|
|
|
|
*_object.code,
|
|
|
|
stackSurplus,
|
|
|
|
allowMSizeOptimzation
|
|
|
|
);
|
2021-08-12 15:17:21 +00:00
|
|
|
}
|
2019-02-04 16:30:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|