2018-10-16 19:39:22 +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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Optimiser suite that combines all steps and also provides the settings for the heuristics.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libyul/optimiser/Suite.h>
|
|
|
|
|
|
|
|
#include <libyul/optimiser/Disambiguator.h>
|
2018-12-13 14:26:01 +00:00
|
|
|
#include <libyul/optimiser/VarDeclInitializer.h>
|
2019-01-08 14:19:35 +00:00
|
|
|
#include <libyul/optimiser/BlockFlattener.h>
|
2019-08-13 16:38:11 +00:00
|
|
|
#include <libyul/optimiser/CallGraphGenerator.h>
|
2020-02-07 00:28:24 +00:00
|
|
|
#include <libyul/optimiser/CircularReferencesPruner.h>
|
2019-05-09 19:56:56 +00:00
|
|
|
#include <libyul/optimiser/ControlFlowSimplifier.h>
|
2019-09-11 09:42:59 +00:00
|
|
|
#include <libyul/optimiser/ConditionalSimplifier.h>
|
2019-10-07 08:06:00 +00:00
|
|
|
#include <libyul/optimiser/ConditionalUnsimplifier.h>
|
2019-03-28 13:18:17 +00:00
|
|
|
#include <libyul/optimiser/DeadCodeEliminator.h>
|
2018-10-16 19:39:22 +00:00
|
|
|
#include <libyul/optimiser/FunctionGrouper.h>
|
|
|
|
#include <libyul/optimiser/FunctionHoister.h>
|
2019-01-10 19:29:30 +00:00
|
|
|
#include <libyul/optimiser/EquivalentFunctionCombiner.h>
|
2018-10-16 19:39:22 +00:00
|
|
|
#include <libyul/optimiser/ExpressionSplitter.h>
|
|
|
|
#include <libyul/optimiser/ExpressionJoiner.h>
|
|
|
|
#include <libyul/optimiser/ExpressionInliner.h>
|
|
|
|
#include <libyul/optimiser/FullInliner.h>
|
2019-09-11 14:30:47 +00:00
|
|
|
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
|
2019-09-11 16:55:31 +00:00
|
|
|
#include <libyul/optimiser/ForLoopConditionOutOfBody.h>
|
2018-11-07 10:18:02 +00:00
|
|
|
#include <libyul/optimiser/ForLoopInitRewriter.h>
|
2019-09-11 09:42:59 +00:00
|
|
|
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
|
2018-10-16 19:39:22 +00:00
|
|
|
#include <libyul/optimiser/Rematerialiser.h>
|
|
|
|
#include <libyul/optimiser/UnusedPruner.h>
|
|
|
|
#include <libyul/optimiser/ExpressionSimplifier.h>
|
|
|
|
#include <libyul/optimiser/CommonSubexpressionEliminator.h>
|
2019-08-13 16:38:11 +00:00
|
|
|
#include <libyul/optimiser/Semantics.h>
|
2019-01-16 10:44:45 +00:00
|
|
|
#include <libyul/optimiser/SSAReverser.h>
|
2018-10-16 19:39:22 +00:00
|
|
|
#include <libyul/optimiser/SSATransform.h>
|
2019-02-04 16:30:29 +00:00
|
|
|
#include <libyul/optimiser/StackCompressor.h>
|
2018-12-03 16:19:37 +00:00
|
|
|
#include <libyul/optimiser/StructuralSimplifier.h>
|
2019-10-07 06:16:28 +00:00
|
|
|
#include <libyul/optimiser/SyntacticalEquality.h>
|
2018-10-16 19:39:22 +00:00
|
|
|
#include <libyul/optimiser/RedundantAssignEliminator.h>
|
2019-01-21 07:13:31 +00:00
|
|
|
#include <libyul/optimiser/VarNameCleaner.h>
|
2019-08-29 14:43:06 +00:00
|
|
|
#include <libyul/optimiser/LoadResolver.h>
|
2019-09-18 08:47:49 +00:00
|
|
|
#include <libyul/optimiser/LoopInvariantCodeMotion.h>
|
2019-03-04 17:50:55 +00:00
|
|
|
#include <libyul/optimiser/Metrics.h>
|
2019-05-28 10:57:15 +00:00
|
|
|
#include <libyul/backends/evm/ConstantOptimiser.h>
|
2019-01-21 07:13:31 +00:00
|
|
|
#include <libyul/AsmAnalysis.h>
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmAnalysisInfo.h>
|
|
|
|
#include <libyul/AsmData.h>
|
|
|
|
#include <libyul/AsmPrinter.h>
|
2019-07-09 15:23:14 +00:00
|
|
|
#include <libyul/Object.h>
|
2018-10-16 19:39:22 +00:00
|
|
|
|
2019-05-09 15:56:25 +00:00
|
|
|
#include <libyul/backends/wasm/WasmDialect.h>
|
2019-01-21 07:13:31 +00:00
|
|
|
#include <libyul/backends/evm/NoOutputAssembly.h>
|
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2018-10-16 19:39:22 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
2018-10-16 19:39:22 +00:00
|
|
|
|
|
|
|
void OptimiserSuite::run(
|
2019-05-16 08:56:56 +00:00
|
|
|
Dialect const& _dialect,
|
2019-05-29 21:14:06 +00:00
|
|
|
GasMeter const* _meter,
|
2019-07-09 15:23:14 +00:00
|
|
|
Object& _object,
|
2019-03-13 16:44:16 +00:00
|
|
|
bool _optimizeStackAllocation,
|
2018-10-29 14:12:02 +00:00
|
|
|
set<YulString> const& _externallyUsedIdentifiers
|
2018-10-16 19:39:22 +00:00
|
|
|
)
|
|
|
|
{
|
2018-10-29 14:12:02 +00:00
|
|
|
set<YulString> reservedIdentifiers = _externallyUsedIdentifiers;
|
2019-05-29 21:14:06 +00:00
|
|
|
reservedIdentifiers += _dialect.fixedFunctionNames();
|
2018-10-16 19:39:22 +00:00
|
|
|
|
2019-11-19 15:42:49 +00:00
|
|
|
*_object.code = std::get<Block>(Disambiguator(
|
2019-07-09 15:23:14 +00:00
|
|
|
_dialect,
|
|
|
|
*_object.analysisInfo,
|
|
|
|
reservedIdentifiers
|
|
|
|
)(*_object.code));
|
|
|
|
Block& ast = *_object.code;
|
2018-10-16 19:39:22 +00:00
|
|
|
|
2019-09-23 14:32:50 +00:00
|
|
|
OptimiserSuite suite(_dialect, reservedIdentifiers, Debug::None, ast);
|
|
|
|
|
|
|
|
suite.runSequence({
|
|
|
|
VarDeclInitializer::name,
|
|
|
|
FunctionHoister::name,
|
|
|
|
BlockFlattener::name,
|
|
|
|
ForLoopInitRewriter::name,
|
|
|
|
DeadCodeEliminator::name,
|
|
|
|
FunctionGrouper::name,
|
|
|
|
EquivalentFunctionCombiner::name,
|
|
|
|
UnusedPruner::name,
|
2020-02-07 00:28:24 +00:00
|
|
|
CircularReferencesPruner::name,
|
2019-09-23 14:32:50 +00:00
|
|
|
BlockFlattener::name,
|
|
|
|
ControlFlowSimplifier::name,
|
|
|
|
LiteralRematerialiser::name,
|
2019-10-07 08:06:00 +00:00
|
|
|
ConditionalUnsimplifier::name,
|
2019-09-23 14:32:50 +00:00
|
|
|
StructuralSimplifier::name,
|
|
|
|
ControlFlowSimplifier::name,
|
|
|
|
ForLoopConditionIntoBody::name,
|
|
|
|
BlockFlattener::name
|
|
|
|
}, ast);
|
2019-02-04 13:01:05 +00:00
|
|
|
|
|
|
|
// None of the above can make stack problems worse.
|
2018-10-16 19:39:22 +00:00
|
|
|
|
2019-03-04 17:50:55 +00:00
|
|
|
size_t codeSize = 0;
|
|
|
|
for (size_t rounds = 0; rounds < 12; ++rounds)
|
2018-10-16 19:39:22 +00:00
|
|
|
{
|
2019-03-04 17:50:55 +00:00
|
|
|
{
|
|
|
|
size_t newSize = CodeSize::codeSizeIncludingFunctions(ast);
|
|
|
|
if (newSize == codeSize)
|
|
|
|
break;
|
|
|
|
codeSize = newSize;
|
|
|
|
}
|
|
|
|
|
2019-02-04 13:01:05 +00:00
|
|
|
{
|
|
|
|
// Turn into SSA and simplify
|
2019-09-23 14:32:50 +00:00
|
|
|
suite.runSequence({
|
|
|
|
ExpressionSplitter::name,
|
|
|
|
SSATransform::name,
|
|
|
|
RedundantAssignEliminator::name,
|
|
|
|
RedundantAssignEliminator::name,
|
|
|
|
ExpressionSimplifier::name,
|
|
|
|
CommonSubexpressionEliminator::name,
|
2019-09-18 08:47:49 +00:00
|
|
|
LoadResolver::name,
|
|
|
|
LoopInvariantCodeMotion::name
|
2019-09-23 14:32:50 +00:00
|
|
|
}, ast);
|
2019-02-04 13:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-09-11 09:42:59 +00:00
|
|
|
// perform structural simplification
|
2019-09-23 14:32:50 +00:00
|
|
|
suite.runSequence({
|
2019-09-11 09:42:59 +00:00
|
|
|
CommonSubexpressionEliminator::name,
|
|
|
|
ConditionalSimplifier::name,
|
2019-09-23 14:32:50 +00:00
|
|
|
LiteralRematerialiser::name,
|
2019-10-07 08:06:00 +00:00
|
|
|
ConditionalUnsimplifier::name,
|
2019-09-11 09:42:59 +00:00
|
|
|
StructuralSimplifier::name,
|
2019-10-30 16:29:54 +00:00
|
|
|
LiteralRematerialiser::name,
|
2019-09-23 14:32:50 +00:00
|
|
|
ForLoopConditionOutOfBody::name,
|
|
|
|
ControlFlowSimplifier::name,
|
|
|
|
StructuralSimplifier::name,
|
|
|
|
ControlFlowSimplifier::name,
|
|
|
|
BlockFlattener::name,
|
|
|
|
DeadCodeEliminator::name,
|
|
|
|
ForLoopConditionIntoBody::name,
|
2020-02-07 00:28:24 +00:00
|
|
|
UnusedPruner::name,
|
|
|
|
CircularReferencesPruner::name
|
2019-09-23 14:32:50 +00:00
|
|
|
}, ast);
|
2019-02-04 13:01:05 +00:00
|
|
|
}
|
2019-08-13 16:38:11 +00:00
|
|
|
|
2019-02-04 13:01:05 +00:00
|
|
|
{
|
|
|
|
// simplify again
|
2019-09-23 14:32:50 +00:00
|
|
|
suite.runSequence({
|
|
|
|
LoadResolver::name,
|
|
|
|
CommonSubexpressionEliminator::name,
|
|
|
|
UnusedPruner::name,
|
2020-02-07 00:28:24 +00:00
|
|
|
CircularReferencesPruner::name,
|
2019-09-23 14:32:50 +00:00
|
|
|
}, ast);
|
2019-02-04 13:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// reverse SSA
|
2019-09-23 14:32:50 +00:00
|
|
|
suite.runSequence({
|
|
|
|
SSAReverser::name,
|
|
|
|
CommonSubexpressionEliminator::name,
|
|
|
|
UnusedPruner::name,
|
2020-02-07 00:28:24 +00:00
|
|
|
CircularReferencesPruner::name,
|
2019-09-23 14:32:50 +00:00
|
|
|
|
|
|
|
ExpressionJoiner::name,
|
|
|
|
ExpressionJoiner::name,
|
|
|
|
}, ast);
|
2019-02-04 13:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// should have good "compilability" property here.
|
|
|
|
|
|
|
|
{
|
|
|
|
// run functional expression inliner
|
2019-09-23 14:32:50 +00:00
|
|
|
suite.runSequence({
|
|
|
|
ExpressionInliner::name,
|
|
|
|
UnusedPruner::name,
|
2020-02-07 00:28:24 +00:00
|
|
|
CircularReferencesPruner::name,
|
2019-09-23 14:32:50 +00:00
|
|
|
}, ast);
|
2019-02-04 13:01:05 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 10:51:06 +00:00
|
|
|
{
|
|
|
|
// Prune a bit more in SSA
|
|
|
|
suite.runSequence({
|
|
|
|
ExpressionSplitter::name,
|
|
|
|
SSATransform::name,
|
|
|
|
RedundantAssignEliminator::name,
|
|
|
|
UnusedPruner::name,
|
2020-02-07 00:28:24 +00:00
|
|
|
CircularReferencesPruner::name,
|
2019-11-27 10:51:06 +00:00
|
|
|
RedundantAssignEliminator::name,
|
|
|
|
UnusedPruner::name,
|
2020-02-07 00:28:24 +00:00
|
|
|
CircularReferencesPruner::name,
|
2019-11-27 10:51:06 +00:00
|
|
|
}, ast);
|
|
|
|
}
|
|
|
|
|
2019-02-04 13:01:05 +00:00
|
|
|
{
|
|
|
|
// Turn into SSA again and simplify
|
2019-09-23 14:32:50 +00:00
|
|
|
suite.runSequence({
|
|
|
|
ExpressionSplitter::name,
|
|
|
|
SSATransform::name,
|
|
|
|
RedundantAssignEliminator::name,
|
|
|
|
RedundantAssignEliminator::name,
|
|
|
|
CommonSubexpressionEliminator::name,
|
|
|
|
LoadResolver::name,
|
|
|
|
}, ast);
|
2019-02-04 13:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// run full inliner
|
2019-09-23 14:32:50 +00:00
|
|
|
suite.runSequence({
|
|
|
|
FunctionGrouper::name,
|
|
|
|
EquivalentFunctionCombiner::name,
|
|
|
|
FullInliner::name,
|
|
|
|
BlockFlattener::name
|
|
|
|
}, ast);
|
2019-02-04 13:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// SSA plus simplify
|
2019-09-23 14:32:50 +00:00
|
|
|
suite.runSequence({
|
2019-09-11 09:42:59 +00:00
|
|
|
ConditionalSimplifier::name,
|
2019-10-07 08:06:00 +00:00
|
|
|
LiteralRematerialiser::name,
|
|
|
|
ConditionalUnsimplifier::name,
|
2019-09-11 09:42:59 +00:00
|
|
|
CommonSubexpressionEliminator::name,
|
2019-09-23 14:32:50 +00:00
|
|
|
SSATransform::name,
|
|
|
|
RedundantAssignEliminator::name,
|
|
|
|
RedundantAssignEliminator::name,
|
|
|
|
LoadResolver::name,
|
|
|
|
ExpressionSimplifier::name,
|
2019-10-30 16:29:54 +00:00
|
|
|
LiteralRematerialiser::name,
|
2019-09-23 14:32:50 +00:00
|
|
|
ForLoopConditionOutOfBody::name,
|
|
|
|
StructuralSimplifier::name,
|
|
|
|
BlockFlattener::name,
|
|
|
|
DeadCodeEliminator::name,
|
|
|
|
ControlFlowSimplifier::name,
|
|
|
|
CommonSubexpressionEliminator::name,
|
|
|
|
SSATransform::name,
|
|
|
|
RedundantAssignEliminator::name,
|
|
|
|
RedundantAssignEliminator::name,
|
|
|
|
ForLoopConditionIntoBody::name,
|
|
|
|
UnusedPruner::name,
|
2020-02-07 00:28:24 +00:00
|
|
|
CircularReferencesPruner::name,
|
2019-09-23 14:32:50 +00:00
|
|
|
CommonSubexpressionEliminator::name,
|
|
|
|
}, ast);
|
2019-02-04 13:01:05 +00:00
|
|
|
}
|
2018-10-16 19:39:22 +00:00
|
|
|
}
|
2019-02-04 13:01:05 +00:00
|
|
|
|
|
|
|
// Make source short and pretty.
|
|
|
|
|
2019-09-23 14:32:50 +00:00
|
|
|
suite.runSequence({
|
|
|
|
ExpressionJoiner::name,
|
|
|
|
Rematerialiser::name,
|
|
|
|
UnusedPruner::name,
|
2020-02-07 00:28:24 +00:00
|
|
|
CircularReferencesPruner::name,
|
2019-09-23 14:32:50 +00:00
|
|
|
ExpressionJoiner::name,
|
|
|
|
UnusedPruner::name,
|
2020-02-07 00:28:24 +00:00
|
|
|
CircularReferencesPruner::name,
|
2019-09-23 14:32:50 +00:00
|
|
|
ExpressionJoiner::name,
|
|
|
|
UnusedPruner::name,
|
2020-02-07 00:28:24 +00:00
|
|
|
CircularReferencesPruner::name,
|
2019-09-23 14:32:50 +00:00
|
|
|
|
|
|
|
SSAReverser::name,
|
|
|
|
CommonSubexpressionEliminator::name,
|
|
|
|
LiteralRematerialiser::name,
|
|
|
|
ForLoopConditionOutOfBody::name,
|
|
|
|
CommonSubexpressionEliminator::name,
|
|
|
|
UnusedPruner::name,
|
2020-02-07 00:28:24 +00:00
|
|
|
CircularReferencesPruner::name,
|
2019-09-23 14:32:50 +00:00
|
|
|
|
|
|
|
ExpressionJoiner::name,
|
|
|
|
Rematerialiser::name,
|
|
|
|
UnusedPruner::name,
|
2020-02-07 00:28:24 +00:00
|
|
|
CircularReferencesPruner::name,
|
2019-09-23 14:32:50 +00:00
|
|
|
}, ast);
|
2018-10-16 19:39:22 +00:00
|
|
|
|
2019-03-14 17:25:33 +00:00
|
|
|
// This is a tuning parameter, but actually just prevents infinite loops.
|
|
|
|
size_t stackCompressorMaxIterations = 16;
|
2019-09-23 14:32:50 +00:00
|
|
|
suite.runSequence({
|
|
|
|
FunctionGrouper::name
|
|
|
|
}, ast);
|
2019-03-14 17:25:33 +00:00
|
|
|
// We ignore the return value because we will get a much better error
|
|
|
|
// message once we perform code generation.
|
2019-07-09 15:23:14 +00:00
|
|
|
StackCompressor::run(
|
|
|
|
_dialect,
|
|
|
|
_object,
|
|
|
|
_optimizeStackAllocation,
|
|
|
|
stackCompressorMaxIterations
|
|
|
|
);
|
2019-09-23 14:32:50 +00:00
|
|
|
suite.runSequence({
|
|
|
|
BlockFlattener::name,
|
|
|
|
DeadCodeEliminator::name,
|
|
|
|
ControlFlowSimplifier::name,
|
|
|
|
LiteralRematerialiser::name,
|
|
|
|
ForLoopConditionOutOfBody::name,
|
|
|
|
CommonSubexpressionEliminator::name,
|
2019-01-21 07:13:31 +00:00
|
|
|
|
2019-09-23 14:32:50 +00:00
|
|
|
FunctionGrouper::name,
|
|
|
|
}, ast);
|
2019-05-15 08:33:35 +00:00
|
|
|
|
2019-05-28 10:57:15 +00:00
|
|
|
if (EVMDialect const* dialect = dynamic_cast<EVMDialect const*>(&_dialect))
|
2019-05-29 21:14:06 +00:00
|
|
|
{
|
|
|
|
yulAssert(_meter, "");
|
|
|
|
ConstantOptimiser{*dialect, *_meter}(ast);
|
|
|
|
}
|
2019-05-09 15:56:25 +00:00
|
|
|
else if (dynamic_cast<WasmDialect const*>(&_dialect))
|
|
|
|
{
|
|
|
|
// If the first statement is an empty block, remove it.
|
|
|
|
// We should only have function definitions after that.
|
2019-11-19 15:42:49 +00:00
|
|
|
if (ast.statements.size() > 1 && std::get<Block>(ast.statements.front()).statements.empty())
|
2019-05-09 15:56:25 +00:00
|
|
|
ast.statements.erase(ast.statements.begin());
|
|
|
|
}
|
2020-01-29 17:05:18 +00:00
|
|
|
VarNameCleaner::run(suite.m_context, ast);
|
2019-02-04 16:30:29 +00:00
|
|
|
|
2019-07-09 15:23:14 +00:00
|
|
|
*_object.analysisInfo = AsmAnalyzer::analyzeStrictAssertCorrect(_dialect, _object);
|
2018-10-16 19:39:22 +00:00
|
|
|
}
|
2019-09-23 14:32:50 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
template <class... Step>
|
|
|
|
map<string, unique_ptr<OptimiserStep>> optimiserStepCollection()
|
|
|
|
{
|
|
|
|
map<string, unique_ptr<OptimiserStep>> ret;
|
2019-12-11 16:31:36 +00:00
|
|
|
for (unique_ptr<OptimiserStep>& s: util::make_vector<unique_ptr<OptimiserStep>>(
|
2019-09-23 14:32:50 +00:00
|
|
|
(make_unique<OptimiserStepInstance<Step>>())...
|
|
|
|
))
|
|
|
|
{
|
|
|
|
yulAssert(!ret.count(s->name), "");
|
|
|
|
ret[s->name] = std::move(s);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
map<string, unique_ptr<OptimiserStep>> const& OptimiserSuite::allSteps()
|
|
|
|
{
|
|
|
|
static map<string, unique_ptr<OptimiserStep>> instance;
|
|
|
|
if (instance.empty())
|
|
|
|
instance = optimiserStepCollection<
|
|
|
|
BlockFlattener,
|
2020-02-07 00:28:24 +00:00
|
|
|
CircularReferencesPruner,
|
2019-09-23 14:32:50 +00:00
|
|
|
CommonSubexpressionEliminator,
|
2019-09-11 09:42:59 +00:00
|
|
|
ConditionalSimplifier,
|
2019-10-07 08:06:00 +00:00
|
|
|
ConditionalUnsimplifier,
|
2019-09-23 14:32:50 +00:00
|
|
|
ControlFlowSimplifier,
|
|
|
|
DeadCodeEliminator,
|
|
|
|
EquivalentFunctionCombiner,
|
|
|
|
ExpressionInliner,
|
|
|
|
ExpressionJoiner,
|
|
|
|
ExpressionSimplifier,
|
|
|
|
ExpressionSplitter,
|
|
|
|
ForLoopConditionIntoBody,
|
|
|
|
ForLoopConditionOutOfBody,
|
|
|
|
ForLoopInitRewriter,
|
|
|
|
FullInliner,
|
|
|
|
FunctionGrouper,
|
|
|
|
FunctionHoister,
|
|
|
|
LiteralRematerialiser,
|
|
|
|
LoadResolver,
|
2019-09-18 08:47:49 +00:00
|
|
|
LoopInvariantCodeMotion,
|
2019-09-23 14:32:50 +00:00
|
|
|
RedundantAssignEliminator,
|
|
|
|
Rematerialiser,
|
|
|
|
SSAReverser,
|
|
|
|
SSATransform,
|
|
|
|
StructuralSimplifier,
|
|
|
|
UnusedPruner,
|
2020-01-29 17:05:18 +00:00
|
|
|
VarDeclInitializer
|
2019-09-23 14:32:50 +00:00
|
|
|
>();
|
2020-01-29 17:05:18 +00:00
|
|
|
// Does not include VarNameCleaner because it destroys the property of unique names.
|
2019-09-23 14:32:50 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2020-01-24 21:03:44 +00:00
|
|
|
map<string, char> const& OptimiserSuite::stepNameToAbbreviationMap()
|
|
|
|
{
|
|
|
|
static map<string, char> lookupTable{
|
|
|
|
{BlockFlattener::name, 'f'},
|
2020-02-07 00:28:24 +00:00
|
|
|
{CircularReferencesPruner::name, 'l'},
|
2020-01-24 21:03:44 +00:00
|
|
|
{CommonSubexpressionEliminator::name, 'c'},
|
|
|
|
{ConditionalSimplifier::name, 'C'},
|
|
|
|
{ConditionalUnsimplifier::name, 'U'},
|
|
|
|
{ControlFlowSimplifier::name, 'n'},
|
|
|
|
{DeadCodeEliminator::name, 'D'},
|
|
|
|
{EquivalentFunctionCombiner::name, 'v'},
|
|
|
|
{ExpressionInliner::name, 'e'},
|
|
|
|
{ExpressionJoiner::name, 'j'},
|
|
|
|
{ExpressionSimplifier::name, 's'},
|
|
|
|
{ExpressionSplitter::name, 'x'},
|
|
|
|
{ForLoopConditionIntoBody::name, 'I'},
|
|
|
|
{ForLoopConditionOutOfBody::name, 'O'},
|
|
|
|
{ForLoopInitRewriter::name, 'o'},
|
|
|
|
{FullInliner::name, 'i'},
|
|
|
|
{FunctionGrouper::name, 'g'},
|
|
|
|
{FunctionHoister::name, 'h'},
|
|
|
|
{LiteralRematerialiser::name, 'T'},
|
|
|
|
{LoadResolver::name, 'L'},
|
|
|
|
{LoopInvariantCodeMotion::name, 'M'},
|
|
|
|
{RedundantAssignEliminator::name, 'r'},
|
|
|
|
{Rematerialiser::name, 'm'},
|
|
|
|
{SSAReverser::name, 'V'},
|
|
|
|
{SSATransform::name, 'a'},
|
|
|
|
{StructuralSimplifier::name, 't'},
|
|
|
|
{UnusedPruner::name, 'u'},
|
|
|
|
{VarDeclInitializer::name, 'd'},
|
|
|
|
};
|
|
|
|
yulAssert(lookupTable.size() == allSteps().size(), "");
|
|
|
|
|
|
|
|
return lookupTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
map<char, string> const& OptimiserSuite::stepAbbreviationToNameMap()
|
|
|
|
{
|
|
|
|
static map<char, string> lookupTable = util::invertMap(stepNameToAbbreviationMap());
|
|
|
|
|
|
|
|
return lookupTable;
|
|
|
|
}
|
|
|
|
|
2019-09-23 14:32:50 +00:00
|
|
|
void OptimiserSuite::runSequence(std::vector<string> const& _steps, Block& _ast)
|
|
|
|
{
|
2019-10-07 06:16:28 +00:00
|
|
|
unique_ptr<Block> copy;
|
|
|
|
if (m_debug == Debug::PrintChanges)
|
2019-11-19 15:42:49 +00:00
|
|
|
copy = make_unique<Block>(std::get<Block>(ASTCopier{}(_ast)));
|
2019-09-23 14:32:50 +00:00
|
|
|
for (string const& step: _steps)
|
|
|
|
{
|
|
|
|
if (m_debug == Debug::PrintStep)
|
|
|
|
cout << "Running " << step << endl;
|
|
|
|
allSteps().at(step)->run(m_context, _ast);
|
2019-10-07 06:16:28 +00:00
|
|
|
if (m_debug == Debug::PrintChanges)
|
|
|
|
{
|
|
|
|
// TODO should add switch to also compare variable names!
|
|
|
|
if (SyntacticallyEqual{}.statementEqual(_ast, *copy))
|
|
|
|
cout << "== Running " << step << " did not cause changes." << endl;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << "== Running " << step << " changed the AST." << endl;
|
|
|
|
cout << AsmPrinter{}(_ast) << endl;
|
2019-11-19 15:42:49 +00:00
|
|
|
copy = make_unique<Block>(std::get<Block>(ASTCopier{}(_ast)));
|
2019-10-07 06:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-23 14:32:50 +00:00
|
|
|
}
|
|
|
|
}
|