mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge remote-tracking branch 'origin/develop' into breaking
This commit is contained in:
@@ -30,8 +30,8 @@ add_library(yul
|
||||
AsmParser.h
|
||||
AsmPrinter.cpp
|
||||
AsmPrinter.h
|
||||
AssemblyStack.h
|
||||
AssemblyStack.cpp
|
||||
YulStack.h
|
||||
YulStack.cpp
|
||||
CompilabilityChecker.cpp
|
||||
CompilabilityChecker.h
|
||||
ControlFlowSideEffects.h
|
||||
|
||||
+3
-3
@@ -63,13 +63,13 @@ struct StackTooDeepError: virtual YulException
|
||||
#endif
|
||||
|
||||
#define yulAssert_1(CONDITION) \
|
||||
yulAssert_2(CONDITION, "")
|
||||
yulAssert_2((CONDITION), "")
|
||||
|
||||
#define yulAssert_2(CONDITION, DESCRIPTION) \
|
||||
assertThrowWithDefaultDescription( \
|
||||
CONDITION, \
|
||||
(CONDITION), \
|
||||
::solidity::yul::YulAssertion, \
|
||||
DESCRIPTION, \
|
||||
(DESCRIPTION), \
|
||||
"Yul assertion failed" \
|
||||
)
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
*/
|
||||
|
||||
|
||||
#include <libyul/AssemblyStack.h>
|
||||
#include <libyul/YulStack.h>
|
||||
|
||||
#include <libyul/AsmAnalysis.h>
|
||||
#include <libyul/AsmAnalysisInfo.h>
|
||||
@@ -48,16 +48,16 @@ using namespace solidity::langutil;
|
||||
|
||||
namespace
|
||||
{
|
||||
Dialect const& languageToDialect(AssemblyStack::Language _language, EVMVersion _version)
|
||||
Dialect const& languageToDialect(YulStack::Language _language, EVMVersion _version)
|
||||
{
|
||||
switch (_language)
|
||||
{
|
||||
case AssemblyStack::Language::Assembly:
|
||||
case AssemblyStack::Language::StrictAssembly:
|
||||
case YulStack::Language::Assembly:
|
||||
case YulStack::Language::StrictAssembly:
|
||||
return EVMDialect::strictAssemblyForEVMObjects(_version);
|
||||
case AssemblyStack::Language::Yul:
|
||||
case YulStack::Language::Yul:
|
||||
return EVMDialectTyped::instance(_version);
|
||||
case AssemblyStack::Language::Ewasm:
|
||||
case YulStack::Language::Ewasm:
|
||||
return WasmDialect::instance();
|
||||
}
|
||||
yulAssert(false, "");
|
||||
@@ -88,14 +88,14 @@ evmasm::Assembly::OptimiserSettings translateOptimiserSettings(
|
||||
}
|
||||
|
||||
|
||||
CharStream const& AssemblyStack::charStream(string const& _sourceName) const
|
||||
CharStream const& YulStack::charStream(string const& _sourceName) const
|
||||
{
|
||||
yulAssert(m_charStream, "");
|
||||
yulAssert(m_charStream->name() == _sourceName, "");
|
||||
return *m_charStream;
|
||||
}
|
||||
|
||||
bool AssemblyStack::parseAndAnalyze(std::string const& _sourceName, std::string const& _source)
|
||||
bool YulStack::parseAndAnalyze(std::string const& _sourceName, std::string const& _source)
|
||||
{
|
||||
m_errors.clear();
|
||||
m_analysisSuccessful = false;
|
||||
@@ -110,7 +110,7 @@ bool AssemblyStack::parseAndAnalyze(std::string const& _sourceName, std::string
|
||||
return analyzeParsed();
|
||||
}
|
||||
|
||||
void AssemblyStack::optimize()
|
||||
void YulStack::optimize()
|
||||
{
|
||||
if (!m_optimiserSettings.runYulOptimiser)
|
||||
return;
|
||||
@@ -123,7 +123,7 @@ void AssemblyStack::optimize()
|
||||
yulAssert(analyzeParsed(), "Invalid source code after optimization.");
|
||||
}
|
||||
|
||||
void AssemblyStack::translate(AssemblyStack::Language _targetLanguage)
|
||||
void YulStack::translate(YulStack::Language _targetLanguage)
|
||||
{
|
||||
if (m_language == _targetLanguage)
|
||||
return;
|
||||
@@ -141,14 +141,14 @@ void AssemblyStack::translate(AssemblyStack::Language _targetLanguage)
|
||||
m_language = _targetLanguage;
|
||||
}
|
||||
|
||||
bool AssemblyStack::analyzeParsed()
|
||||
bool YulStack::analyzeParsed()
|
||||
{
|
||||
yulAssert(m_parserResult, "");
|
||||
m_analysisSuccessful = analyzeParsed(*m_parserResult);
|
||||
return m_analysisSuccessful;
|
||||
}
|
||||
|
||||
bool AssemblyStack::analyzeParsed(Object& _object)
|
||||
bool YulStack::analyzeParsed(Object& _object)
|
||||
{
|
||||
yulAssert(_object.code, "");
|
||||
_object.analysisInfo = make_shared<AsmAnalysisInfo>();
|
||||
@@ -168,7 +168,7 @@ bool AssemblyStack::analyzeParsed(Object& _object)
|
||||
return success;
|
||||
}
|
||||
|
||||
void AssemblyStack::compileEVM(AbstractAssembly& _assembly, bool _optimize) const
|
||||
void YulStack::compileEVM(AbstractAssembly& _assembly, bool _optimize) const
|
||||
{
|
||||
EVMDialect const* dialect = nullptr;
|
||||
switch (m_language)
|
||||
@@ -188,7 +188,7 @@ void AssemblyStack::compileEVM(AbstractAssembly& _assembly, bool _optimize) cons
|
||||
EVMObjectCompiler::compile(*m_parserResult, _assembly, *dialect, _optimize);
|
||||
}
|
||||
|
||||
void AssemblyStack::optimize(Object& _object, bool _isCreation)
|
||||
void YulStack::optimize(Object& _object, bool _isCreation)
|
||||
{
|
||||
yulAssert(_object.code, "");
|
||||
yulAssert(_object.analysisInfo, "");
|
||||
@@ -214,7 +214,7 @@ void AssemblyStack::optimize(Object& _object, bool _isCreation)
|
||||
);
|
||||
}
|
||||
|
||||
MachineAssemblyObject AssemblyStack::assemble(Machine _machine) const
|
||||
MachineAssemblyObject YulStack::assemble(Machine _machine) const
|
||||
{
|
||||
yulAssert(m_analysisSuccessful, "");
|
||||
yulAssert(m_parserResult, "");
|
||||
@@ -243,7 +243,7 @@ MachineAssemblyObject AssemblyStack::assemble(Machine _machine) const
|
||||
}
|
||||
|
||||
std::pair<MachineAssemblyObject, MachineAssemblyObject>
|
||||
AssemblyStack::assembleWithDeployed(optional<string_view> _deployName) const
|
||||
YulStack::assembleWithDeployed(optional<string_view> _deployName) const
|
||||
{
|
||||
auto [creationAssembly, deployedAssembly] = assembleEVMWithDeployed(_deployName);
|
||||
yulAssert(creationAssembly, "");
|
||||
@@ -277,7 +277,7 @@ AssemblyStack::assembleWithDeployed(optional<string_view> _deployName) const
|
||||
}
|
||||
|
||||
std::pair<std::shared_ptr<evmasm::Assembly>, std::shared_ptr<evmasm::Assembly>>
|
||||
AssemblyStack::assembleEVMWithDeployed(optional<string_view> _deployName) const
|
||||
YulStack::assembleEVMWithDeployed(optional<string_view> _deployName) const
|
||||
{
|
||||
yulAssert(m_analysisSuccessful, "");
|
||||
yulAssert(m_parserResult, "");
|
||||
@@ -317,7 +317,7 @@ AssemblyStack::assembleEVMWithDeployed(optional<string_view> _deployName) const
|
||||
return {make_shared<evmasm::Assembly>(assembly), {}};
|
||||
}
|
||||
|
||||
string AssemblyStack::print(
|
||||
string YulStack::print(
|
||||
CharStreamProvider const* _soliditySourceProvider
|
||||
) const
|
||||
{
|
||||
@@ -326,7 +326,7 @@ string AssemblyStack::print(
|
||||
return m_parserResult->toString(&languageToDialect(m_language, m_evmVersion), m_debugInfoSelection, _soliditySourceProvider) + "\n";
|
||||
}
|
||||
|
||||
shared_ptr<Object> AssemblyStack::parserResult() const
|
||||
shared_ptr<Object> YulStack::parserResult() const
|
||||
{
|
||||
yulAssert(m_analysisSuccessful, "Analysis was not successful.");
|
||||
yulAssert(m_parserResult, "");
|
||||
@@ -63,14 +63,14 @@ struct MachineAssemblyObject
|
||||
* Full assembly stack that can support EVM-assembly and Yul as input and EVM, EVM1.5 and
|
||||
* Ewasm as output.
|
||||
*/
|
||||
class AssemblyStack: public langutil::CharStreamProvider
|
||||
class YulStack: public langutil::CharStreamProvider
|
||||
{
|
||||
public:
|
||||
enum class Language { Yul, Assembly, StrictAssembly, Ewasm };
|
||||
enum class Machine { EVM, Ewasm };
|
||||
|
||||
AssemblyStack():
|
||||
AssemblyStack(
|
||||
YulStack():
|
||||
YulStack(
|
||||
langutil::EVMVersion{},
|
||||
Language::Assembly,
|
||||
solidity::frontend::OptimiserSettings::none(),
|
||||
@@ -78,7 +78,7 @@ public:
|
||||
)
|
||||
{}
|
||||
|
||||
AssemblyStack(
|
||||
YulStack(
|
||||
langutil::EVMVersion _evmVersion,
|
||||
Language _language,
|
||||
solidity::frontend::OptimiserSettings _optimiserSettings,
|
||||
@@ -30,18 +30,22 @@ using namespace solidity::util;
|
||||
|
||||
void NameCollector::operator()(VariableDeclaration const& _varDecl)
|
||||
{
|
||||
for (auto const& var: _varDecl.variables)
|
||||
m_names.emplace(var.name);
|
||||
if (m_collectWhat != OnlyFunctions)
|
||||
for (auto const& var: _varDecl.variables)
|
||||
m_names.emplace(var.name);
|
||||
}
|
||||
|
||||
void NameCollector::operator ()(FunctionDefinition const& _funDef)
|
||||
void NameCollector::operator()(FunctionDefinition const& _funDef)
|
||||
{
|
||||
if (m_collectWhat == VariablesAndFunctions)
|
||||
if (m_collectWhat != OnlyVariables)
|
||||
m_names.emplace(_funDef.name);
|
||||
for (auto const& arg: _funDef.parameters)
|
||||
m_names.emplace(arg.name);
|
||||
for (auto const& ret: _funDef.returnVariables)
|
||||
m_names.emplace(ret.name);
|
||||
if (m_collectWhat != OnlyFunctions)
|
||||
{
|
||||
for (auto const& arg: _funDef.parameters)
|
||||
m_names.emplace(arg.name);
|
||||
for (auto const& ret: _funDef.returnVariables)
|
||||
m_names.emplace(ret.name);
|
||||
}
|
||||
ASTWalker::operator ()(_funDef);
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ namespace solidity::yul
|
||||
class NameCollector: public ASTWalker
|
||||
{
|
||||
public:
|
||||
enum CollectWhat { VariablesAndFunctions, OnlyVariables };
|
||||
enum CollectWhat { VariablesAndFunctions, OnlyVariables, OnlyFunctions };
|
||||
|
||||
explicit NameCollector(
|
||||
Block const& _block,
|
||||
|
||||
@@ -37,16 +37,6 @@ void Rematerialiser::run(Dialect const& _dialect, Block& _ast, set<YulString> _v
|
||||
Rematerialiser{_dialect, _ast, std::move(_varsToAlwaysRematerialize), _onlySelectedVariables}(_ast);
|
||||
}
|
||||
|
||||
void Rematerialiser::run(
|
||||
Dialect const& _dialect,
|
||||
FunctionDefinition& _function,
|
||||
set<YulString> _varsToAlwaysRematerialize,
|
||||
bool _onlySelectedVariables
|
||||
)
|
||||
{
|
||||
Rematerialiser{_dialect, _function, std::move(_varsToAlwaysRematerialize), _onlySelectedVariables}(_function);
|
||||
}
|
||||
|
||||
Rematerialiser::Rematerialiser(
|
||||
Dialect const& _dialect,
|
||||
Block& _ast,
|
||||
@@ -60,19 +50,6 @@ Rematerialiser::Rematerialiser(
|
||||
{
|
||||
}
|
||||
|
||||
Rematerialiser::Rematerialiser(
|
||||
Dialect const& _dialect,
|
||||
FunctionDefinition& _function,
|
||||
set<YulString> _varsToAlwaysRematerialize,
|
||||
bool _onlySelectedVariables
|
||||
):
|
||||
DataFlowAnalyzer(_dialect),
|
||||
m_referenceCounts(ReferencesCounter::countReferences(_function)),
|
||||
m_varsToAlwaysRematerialize(std::move(_varsToAlwaysRematerialize)),
|
||||
m_onlySelectedVariables(_onlySelectedVariables)
|
||||
{
|
||||
}
|
||||
|
||||
void Rematerialiser::visit(Expression& _e)
|
||||
{
|
||||
if (holds_alternative<Identifier>(_e))
|
||||
|
||||
@@ -53,12 +53,6 @@ public:
|
||||
std::set<YulString> _varsToAlwaysRematerialize = {},
|
||||
bool _onlySelectedVariables = false
|
||||
);
|
||||
static void run(
|
||||
Dialect const& _dialect,
|
||||
FunctionDefinition& _function,
|
||||
std::set<YulString> _varsToAlwaysRematerialize = {},
|
||||
bool _onlySelectedVariables = false
|
||||
);
|
||||
|
||||
protected:
|
||||
Rematerialiser(
|
||||
@@ -67,12 +61,6 @@ protected:
|
||||
std::set<YulString> _varsToAlwaysRematerialize = {},
|
||||
bool _onlySelectedVariables = false
|
||||
);
|
||||
Rematerialiser(
|
||||
Dialect const& _dialect,
|
||||
FunctionDefinition& _function,
|
||||
std::set<YulString> _varsToAlwaysRematerialize = {},
|
||||
bool _onlySelectedVariables = false
|
||||
);
|
||||
|
||||
using DataFlowAnalyzer::operator();
|
||||
|
||||
|
||||
@@ -50,31 +50,40 @@ namespace
|
||||
/**
|
||||
* Class that discovers all variables that can be fully eliminated by rematerialization,
|
||||
* and the corresponding approximate costs.
|
||||
*
|
||||
* Prerequisite: Disambiguator, Function Grouper
|
||||
*/
|
||||
class RematCandidateSelector: public DataFlowAnalyzer
|
||||
{
|
||||
public:
|
||||
explicit RematCandidateSelector(Dialect const& _dialect): DataFlowAnalyzer(_dialect) {}
|
||||
|
||||
/// @returns a map from rematerialisation costs to a vector of variables to rematerialise
|
||||
/// @returns a map from function name to rematerialisation costs to a vector of variables to rematerialise
|
||||
/// and variables that occur in their expression.
|
||||
/// While the map is sorted by cost, the contained vectors are sorted by the order of occurrence.
|
||||
map<size_t, vector<tuple<YulString, set<YulString>>>> candidates()
|
||||
map<YulString, map<size_t, vector<YulString>>> candidates()
|
||||
{
|
||||
map<size_t, vector<tuple<YulString, set<YulString>>>> cand;
|
||||
for (auto const& candidate: m_candidates)
|
||||
map<YulString, map<size_t, vector<YulString>>> cand;
|
||||
for (auto const& [functionName, candidate]: m_candidates)
|
||||
{
|
||||
if (size_t const* cost = util::valueOrNullptr(m_expressionCodeCost, candidate))
|
||||
{
|
||||
size_t numRef = m_numReferences[candidate];
|
||||
set<YulString> const* ref = references(candidate);
|
||||
cand[*cost * numRef].emplace_back(candidate, ref ? move(*ref) : set<YulString>{});
|
||||
cand[functionName][*cost * numRef].emplace_back(candidate);
|
||||
}
|
||||
}
|
||||
return cand;
|
||||
}
|
||||
|
||||
using DataFlowAnalyzer::operator();
|
||||
void operator()(FunctionDefinition& _function) override
|
||||
{
|
||||
yulAssert(m_currentFunctionName.empty());
|
||||
m_currentFunctionName = _function.name;
|
||||
DataFlowAnalyzer::operator()(_function);
|
||||
m_currentFunctionName = {};
|
||||
}
|
||||
|
||||
void operator()(VariableDeclaration& _varDecl) override
|
||||
{
|
||||
DataFlowAnalyzer::operator()(_varDecl);
|
||||
@@ -84,7 +93,7 @@ public:
|
||||
if (AssignedValue const* value = variableValue(varName))
|
||||
{
|
||||
yulAssert(!m_expressionCodeCost.count(varName), "");
|
||||
m_candidates.emplace_back(varName);
|
||||
m_candidates.emplace_back(m_currentFunctionName, varName);
|
||||
m_expressionCodeCost[varName] = CodeCost::codeCost(m_dialect, *value->value);
|
||||
}
|
||||
}
|
||||
@@ -122,8 +131,10 @@ public:
|
||||
m_expressionCodeCost.erase(_variable);
|
||||
}
|
||||
|
||||
/// All candidate variables in order of occurrence.
|
||||
vector<YulString> m_candidates;
|
||||
YulString m_currentFunctionName = {};
|
||||
|
||||
/// All candidate variables by function name, in order of occurrence.
|
||||
vector<pair<YulString, YulString>> m_candidates;
|
||||
/// Candidate variables and the code cost of their value.
|
||||
map<YulString, size_t> m_expressionCodeCost;
|
||||
/// Number of references to each candidate variable.
|
||||
@@ -132,86 +143,95 @@ public:
|
||||
|
||||
/// Selects at most @a _numVariables among @a _candidates.
|
||||
set<YulString> chooseVarsToEliminate(
|
||||
map<size_t, vector<tuple<YulString, set<YulString>>>> const& _candidates,
|
||||
map<size_t, vector<YulString>> const& _candidates,
|
||||
size_t _numVariables
|
||||
)
|
||||
{
|
||||
set<YulString> varsToEliminate;
|
||||
for (auto&& [cost, candidates]: _candidates)
|
||||
for (auto&& [candidate, references]: candidates)
|
||||
for (auto&& candidate: candidates)
|
||||
{
|
||||
if (varsToEliminate.size() >= _numVariables)
|
||||
return varsToEliminate;
|
||||
// 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.
|
||||
for (YulString const& referencedVar: references)
|
||||
if (varsToEliminate.count(referencedVar))
|
||||
return varsToEliminate;
|
||||
varsToEliminate.insert(candidate);
|
||||
}
|
||||
return varsToEliminate;
|
||||
}
|
||||
|
||||
template <typename ASTNode>
|
||||
void eliminateVariables(
|
||||
Dialect const& _dialect,
|
||||
ASTNode& _node,
|
||||
size_t _numVariables,
|
||||
Block& _ast,
|
||||
map<YulString, int> const& _numVariables,
|
||||
bool _allowMSizeOptimization
|
||||
)
|
||||
{
|
||||
RematCandidateSelector selector{_dialect};
|
||||
selector(_node);
|
||||
Rematerialiser::run(_dialect, _node, chooseVarsToEliminate(selector.candidates(), _numVariables));
|
||||
UnusedPruner::runUntilStabilised(_dialect, _node, _allowMSizeOptimization);
|
||||
selector(_ast);
|
||||
map<YulString, map<size_t, vector<YulString>>> candidates = selector.candidates();
|
||||
|
||||
set<YulString> varsToEliminate;
|
||||
for (auto const& [functionName, numVariables]: _numVariables)
|
||||
{
|
||||
yulAssert(numVariables > 0);
|
||||
varsToEliminate += chooseVarsToEliminate(candidates[functionName], static_cast<size_t>(numVariables));
|
||||
}
|
||||
|
||||
Rematerialiser::run(_dialect, _ast, move(varsToEliminate));
|
||||
// Do not remove functions.
|
||||
set<YulString> allFunctions = NameCollector{_ast, NameCollector::OnlyFunctions}.names();
|
||||
UnusedPruner::runUntilStabilised(_dialect, _ast, _allowMSizeOptimization, nullptr, allFunctions);
|
||||
}
|
||||
|
||||
void eliminateVariables(
|
||||
void eliminateVariablesOptimizedCodegen(
|
||||
Dialect const& _dialect,
|
||||
Block& _block,
|
||||
vector<StackLayoutGenerator::StackTooDeep> const& _unreachables,
|
||||
Block& _ast,
|
||||
map<YulString, vector<StackLayoutGenerator::StackTooDeep>> const& _unreachables,
|
||||
bool _allowMSizeOptimization
|
||||
)
|
||||
{
|
||||
if (std::all_of(_unreachables.begin(), _unreachables.end(), [](auto const& _item) { return _item.second.empty(); }))
|
||||
return;
|
||||
|
||||
RematCandidateSelector selector{_dialect};
|
||||
selector(_block);
|
||||
std::map<YulString, size_t> candidates;
|
||||
for (auto [cost, candidatesWithCost]: selector.candidates())
|
||||
for (auto candidate: candidatesWithCost)
|
||||
candidates[get<0>(candidate)] = cost;
|
||||
selector(_ast);
|
||||
|
||||
map<YulString, size_t> candidates;
|
||||
for (auto const& [functionName, candidatesInFunction]: selector.candidates())
|
||||
for (auto [cost, candidatesWithCost]: candidatesInFunction)
|
||||
for (auto candidate: candidatesWithCost)
|
||||
candidates[candidate] = cost;
|
||||
|
||||
set<YulString> varsToEliminate;
|
||||
|
||||
// TODO: this currently ignores the fact that variables may reference other variables we want to eliminate.
|
||||
for (auto const& unreachable: _unreachables)
|
||||
{
|
||||
map<size_t, vector<YulString>> suitableCandidates;
|
||||
size_t neededSlots = unreachable.deficit;
|
||||
for (auto varName: unreachable.variableChoices)
|
||||
for (auto const& [functionName, unreachables]: _unreachables)
|
||||
for (auto const& unreachable: unreachables)
|
||||
{
|
||||
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
|
||||
map<size_t, vector<YulString>> suitableCandidates;
|
||||
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)
|
||||
break;
|
||||
if (!neededSlots)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Rematerialiser::run(_dialect, _block, std::move(varsToEliminate), true);
|
||||
UnusedPruner::runUntilStabilised(_dialect, _block, _allowMSizeOptimization);
|
||||
Rematerialiser::run(_dialect, _ast, std::move(varsToEliminate), true);
|
||||
// Do not remove functions.
|
||||
set<YulString> allFunctions = NameCollector{_ast, NameCollector::OnlyFunctions}.names();
|
||||
UnusedPruner::runUntilStabilised(_dialect, _ast, _allowMSizeOptimization, nullptr, allFunctions);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -239,21 +259,12 @@ bool StackCompressor::run(
|
||||
{
|
||||
yul::AsmAnalysisInfo analysisInfo = yul::AsmAnalyzer::analyzeStrictAssertCorrect(_dialect, _object);
|
||||
unique_ptr<CFG> cfg = ControlFlowGraphBuilder::build(analysisInfo, _dialect, *_object.code);
|
||||
Block& mainBlock = std::get<Block>(_object.code->statements.at(0));
|
||||
if (
|
||||
auto stackTooDeepErrors = StackLayoutGenerator::reportStackTooDeep(*cfg, YulString{});
|
||||
!stackTooDeepErrors.empty()
|
||||
)
|
||||
eliminateVariables(_dialect, mainBlock, stackTooDeepErrors, allowMSizeOptimzation);
|
||||
for (size_t i = 1; i < _object.code->statements.size(); ++i)
|
||||
{
|
||||
auto& fun = std::get<FunctionDefinition>(_object.code->statements[i]);
|
||||
if (
|
||||
auto stackTooDeepErrors = StackLayoutGenerator::reportStackTooDeep(*cfg, fun.name);
|
||||
!stackTooDeepErrors.empty()
|
||||
)
|
||||
eliminateVariables(_dialect, fun.body, stackTooDeepErrors, allowMSizeOptimzation);
|
||||
}
|
||||
eliminateVariablesOptimizedCodegen(
|
||||
_dialect,
|
||||
*_object.code,
|
||||
StackLayoutGenerator::reportStackTooDeep(*cfg),
|
||||
allowMSizeOptimzation
|
||||
);
|
||||
}
|
||||
else
|
||||
for (size_t iterations = 0; iterations < _maxIterations; iterations++)
|
||||
@@ -261,32 +272,12 @@ bool StackCompressor::run(
|
||||
map<YulString, int> stackSurplus = CompilabilityChecker(_dialect, _object, _optimizeStackAllocation).stackDeficit;
|
||||
if (stackSurplus.empty())
|
||||
return true;
|
||||
|
||||
if (stackSurplus.count(YulString{}))
|
||||
{
|
||||
yulAssert(stackSurplus.at({}) > 0, "Invalid surplus value.");
|
||||
eliminateVariables(
|
||||
_dialect,
|
||||
std::get<Block>(_object.code->statements.at(0)),
|
||||
static_cast<size_t>(stackSurplus.at({})),
|
||||
allowMSizeOptimzation
|
||||
);
|
||||
}
|
||||
|
||||
for (size_t i = 1; i < _object.code->statements.size(); ++i)
|
||||
{
|
||||
auto& fun = std::get<FunctionDefinition>(_object.code->statements[i]);
|
||||
if (!stackSurplus.count(fun.name))
|
||||
continue;
|
||||
|
||||
yulAssert(stackSurplus.at(fun.name) > 0, "Invalid surplus value.");
|
||||
eliminateVariables(
|
||||
_dialect,
|
||||
fun,
|
||||
static_cast<size_t>(stackSurplus.at(fun.name)),
|
||||
allowMSizeOptimzation
|
||||
);
|
||||
}
|
||||
eliminateVariables(
|
||||
_dialect,
|
||||
*_object.code,
|
||||
stackSurplus,
|
||||
allowMSizeOptimzation
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -93,26 +93,31 @@ void StructuralSimplifier::operator()(Block& _block)
|
||||
|
||||
void StructuralSimplifier::simplify(std::vector<yul::Statement>& _statements)
|
||||
{
|
||||
util::GenericVisitor visitor{
|
||||
util::VisitorFallback<OptionalStatements>{},
|
||||
[&](If& _ifStmt) -> OptionalStatements {
|
||||
if (expressionAlwaysTrue(*_ifStmt.condition))
|
||||
return {std::move(_ifStmt.body.statements)};
|
||||
else if (expressionAlwaysFalse(*_ifStmt.condition))
|
||||
return {vector<Statement>{}};
|
||||
return {};
|
||||
},
|
||||
[&](Switch& _switchStmt) -> OptionalStatements {
|
||||
if (std::optional<u256> const constExprVal = hasLiteralValue(*_switchStmt.expression))
|
||||
return replaceConstArgSwitch(_switchStmt, constExprVal.value());
|
||||
return {};
|
||||
},
|
||||
[&](ForLoop& _forLoop) -> OptionalStatements {
|
||||
if (expressionAlwaysFalse(*_forLoop.condition))
|
||||
return {std::move(_forLoop.pre.statements)};
|
||||
return {};
|
||||
}
|
||||
// Explicit local variables ifLambda, switchLambda, forLoopLambda are created to avoid MSVC C++17 Debug test crash
|
||||
// (Run-Time Check Failure #2 - Stack around the variable '....' was corrupted).
|
||||
// As soon as the issue is fixed, this workaround can be removed.
|
||||
auto ifLambda = [&](If& _ifStmt) -> OptionalStatements
|
||||
{
|
||||
if (expressionAlwaysTrue(*_ifStmt.condition))
|
||||
return {std::move(_ifStmt.body.statements)};
|
||||
else if (expressionAlwaysFalse(*_ifStmt.condition))
|
||||
return {vector<Statement>{}};
|
||||
return {};
|
||||
};
|
||||
auto switchLambda = [&](Switch& _switchStmt) -> OptionalStatements
|
||||
{
|
||||
if (std::optional<u256> const constExprVal = hasLiteralValue(*_switchStmt.expression))
|
||||
return replaceConstArgSwitch(_switchStmt, constExprVal.value());
|
||||
return {};
|
||||
};
|
||||
auto forLoopLambda = [&](ForLoop& _forLoop) -> OptionalStatements
|
||||
{
|
||||
if (expressionAlwaysFalse(*_forLoop.condition))
|
||||
return {std::move(_forLoop.pre.statements)};
|
||||
return {};
|
||||
};
|
||||
|
||||
util::GenericVisitor visitor{util::VisitorFallback<OptionalStatements>{}, ifLambda, switchLambda, forLoopLambda};
|
||||
|
||||
util::iterateReplacing(
|
||||
_statements,
|
||||
|
||||
Reference in New Issue
Block a user