2018-10-09 13:43:55 +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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2018-10-09 13:43:55 +00:00
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <test/libyul/YulOptimizerTest.h>
|
2018-10-09 13:43:55 +00:00
|
|
|
|
2019-07-21 13:46:03 +00:00
|
|
|
#include <test/libsolidity/util/SoltestErrors.h>
|
2019-12-19 16:58:20 +00:00
|
|
|
#include <test/libyul/Common.h>
|
2020-01-14 16:48:17 +00:00
|
|
|
#include <test/Common.h>
|
2018-10-09 13:43:55 +00:00
|
|
|
|
2018-10-15 13:17:20 +00:00
|
|
|
#include <libyul/optimiser/BlockFlattener.h>
|
2018-12-13 14:26:01 +00:00
|
|
|
#include <libyul/optimiser/VarDeclInitializer.h>
|
2019-01-21 07:13:31 +00:00
|
|
|
#include <libyul/optimiser/VarNameCleaner.h>
|
2019-05-09 19:56:56 +00:00
|
|
|
#include <libyul/optimiser/ControlFlowSimplifier.h>
|
2019-03-28 13:18:17 +00:00
|
|
|
#include <libyul/optimiser/DeadCodeEliminator.h>
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/Disambiguator.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-11-01 13:07:25 +00:00
|
|
|
#include <libyul/optimiser/ConditionalUnsimplifier.h>
|
2019-09-11 09:42:59 +00:00
|
|
|
#include <libyul/optimiser/ConditionalSimplifier.h>
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/CommonSubexpressionEliminator.h>
|
|
|
|
#include <libyul/optimiser/NameCollector.h>
|
2019-01-10 19:29:30 +00:00
|
|
|
#include <libyul/optimiser/EquivalentFunctionCombiner.h>
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/ExpressionSplitter.h>
|
|
|
|
#include <libyul/optimiser/FunctionGrouper.h>
|
|
|
|
#include <libyul/optimiser/FunctionHoister.h>
|
|
|
|
#include <libyul/optimiser/ExpressionInliner.h>
|
|
|
|
#include <libyul/optimiser/FullInliner.h>
|
2019-05-18 05:36:29 +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-05-27 22:14:01 +00:00
|
|
|
#include <libyul/optimiser/LoadResolver.h>
|
2019-09-18 08:47:49 +00:00
|
|
|
#include <libyul/optimiser/LoopInvariantCodeMotion.h>
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/MainFunction.h>
|
2020-07-02 10:48:20 +00:00
|
|
|
#include <libyul/optimiser/StackLimitEvader.h>
|
2019-06-19 09:37:22 +00:00
|
|
|
#include <libyul/optimiser/NameDisplacer.h>
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/Rematerialiser.h>
|
|
|
|
#include <libyul/optimiser/ExpressionSimplifier.h>
|
2020-07-10 16:56:32 +00:00
|
|
|
#include <libyul/optimiser/UnusedFunctionParameterPruner.h>
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/UnusedPruner.h>
|
|
|
|
#include <libyul/optimiser/ExpressionJoiner.h>
|
2019-09-23 14:32:50 +00:00
|
|
|
#include <libyul/optimiser/OptimiserStep.h>
|
2020-05-11 17:56:29 +00:00
|
|
|
#include <libyul/optimiser/ReasoningBasedSimplifier.h>
|
2019-01-16 10:44:45 +00:00
|
|
|
#include <libyul/optimiser/SSAReverser.h>
|
2018-10-16 21:47:02 +00:00
|
|
|
#include <libyul/optimiser/SSATransform.h>
|
2019-08-13 16:38:11 +00:00
|
|
|
#include <libyul/optimiser/Semantics.h>
|
2018-10-23 13:55:48 +00:00
|
|
|
#include <libyul/optimiser/RedundantAssignEliminator.h>
|
2018-12-03 16:19:37 +00:00
|
|
|
#include <libyul/optimiser/StructuralSimplifier.h>
|
2019-02-04 16:30:29 +00:00
|
|
|
#include <libyul/optimiser/StackCompressor.h>
|
2020-07-02 10:48:20 +00:00
|
|
|
#include <libyul/optimiser/StackToMemoryMover.h>
|
2018-10-16 19:39:22 +00:00
|
|
|
#include <libyul/optimiser/Suite.h>
|
2019-05-28 10:57:15 +00:00
|
|
|
#include <libyul/backends/evm/ConstantOptimiser.h>
|
2018-12-06 23:56:16 +00:00
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
2019-05-28 10:57:15 +00:00
|
|
|
#include <libyul/backends/evm/EVMMetrics.h>
|
2019-04-29 15:55:57 +00:00
|
|
|
#include <libyul/backends/wasm/WordSizeTransform.h>
|
2019-12-18 20:54:30 +00:00
|
|
|
#include <libyul/backends/wasm/WasmDialect.h>
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmPrinter.h>
|
|
|
|
#include <libyul/AsmParser.h>
|
|
|
|
#include <libyul/AsmAnalysis.h>
|
2019-02-13 11:21:53 +00:00
|
|
|
#include <libyul/AssemblyStack.h>
|
2020-07-02 10:48:20 +00:00
|
|
|
#include <libyul/CompilabilityChecker.h>
|
2020-12-01 13:22:15 +00:00
|
|
|
#include <liblangutil/SourceReferenceFormatter.h>
|
2018-10-09 13:43:55 +00:00
|
|
|
|
2018-11-23 10:31:45 +00:00
|
|
|
#include <liblangutil/ErrorReporter.h>
|
|
|
|
#include <liblangutil/Scanner.h>
|
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/AnsiColorized.h>
|
2019-02-11 15:00:24 +00:00
|
|
|
|
2020-04-24 12:19:36 +00:00
|
|
|
#include <libsolidity/interface/OptimiserSettings.h>
|
|
|
|
|
2018-10-09 13:43:55 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
|
|
|
#include <fstream>
|
2019-11-19 15:42:49 +00:00
|
|
|
#include <variant>
|
2018-10-09 13:43:55 +00:00
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::yul;
|
|
|
|
using namespace solidity::yul::test;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::frontend::test;
|
2018-10-09 13:43:55 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2020-03-06 00:22:51 +00:00
|
|
|
YulOptimizerTest::YulOptimizerTest(string const& _filename):
|
|
|
|
EVMVersionRestrictedTestCase(_filename)
|
2018-10-09 13:43:55 +00:00
|
|
|
{
|
|
|
|
boost::filesystem::path path(_filename);
|
|
|
|
|
|
|
|
if (path.empty() || std::next(path.begin()) == path.end() || std::next(std::next(path.begin())) == path.end())
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Filename path has to contain a directory: \"" + _filename + "\"."));
|
|
|
|
m_optimizerStep = std::prev(std::prev(path.end()))->string();
|
|
|
|
|
2020-09-17 11:43:18 +00:00
|
|
|
if (m_optimizerStep == "reasoningBasedSimplifier" && (
|
|
|
|
solidity::test::CommonOptions::get().disableSMT ||
|
|
|
|
ReasoningBasedSimplifier::invalidInCurrentEnvironment()
|
|
|
|
))
|
2020-09-10 14:16:11 +00:00
|
|
|
m_shouldRun = false;
|
|
|
|
|
2020-03-06 00:22:51 +00:00
|
|
|
m_source = m_reader.source();
|
2018-10-09 13:43:55 +00:00
|
|
|
|
2020-03-06 00:22:51 +00:00
|
|
|
auto dialectName = m_reader.stringSetting("dialect", "evm");
|
2020-03-08 23:40:54 +00:00
|
|
|
m_dialect = &dialect(dialectName, solidity::test::CommonOptions::get().evmVersion());
|
2019-12-18 20:54:30 +00:00
|
|
|
|
2020-03-06 00:22:51 +00:00
|
|
|
m_expectation = m_reader.simpleExpectations();
|
2018-10-09 13:43:55 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 13:33:22 +00:00
|
|
|
TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted)
|
2018-10-09 13:43:55 +00:00
|
|
|
{
|
2020-08-05 11:33:45 +00:00
|
|
|
std::tie(m_object, m_analysisInfo) = parse(_stream, _linePrefix, _formatted, m_source);
|
|
|
|
if (!m_object)
|
2019-05-07 13:33:22 +00:00
|
|
|
return TestResult::FatalError;
|
2018-10-09 13:43:55 +00:00
|
|
|
|
2019-05-16 08:56:56 +00:00
|
|
|
soltestAssert(m_dialect, "Dialect not set.");
|
2019-09-23 14:32:50 +00:00
|
|
|
|
|
|
|
updateContext();
|
|
|
|
|
2018-10-09 13:43:55 +00:00
|
|
|
if (m_optimizerStep == "disambiguator")
|
|
|
|
disambiguate();
|
2019-06-19 09:37:22 +00:00
|
|
|
else if (m_optimizerStep == "nameDisplacer")
|
|
|
|
{
|
|
|
|
disambiguate();
|
|
|
|
NameDisplacer{
|
2019-09-23 14:32:50 +00:00
|
|
|
*m_nameDispenser,
|
2019-06-19 09:37:22 +00:00
|
|
|
{"illegal1"_yulstring, "illegal2"_yulstring, "illegal3"_yulstring, "illegal4"_yulstring, "illegal5"_yulstring}
|
2020-08-04 23:32:18 +00:00
|
|
|
}(*m_object->code);
|
2019-06-19 09:37:22 +00:00
|
|
|
}
|
2018-10-15 13:17:20 +00:00
|
|
|
else if (m_optimizerStep == "blockFlattener")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
BlockFlattener::run(*m_context, *m_object->code);
|
2018-10-15 13:17:20 +00:00
|
|
|
}
|
2019-05-15 08:33:35 +00:00
|
|
|
else if (m_optimizerStep == "constantOptimiser")
|
|
|
|
{
|
|
|
|
GasMeter meter(dynamic_cast<EVMDialect const&>(*m_dialect), false, 200);
|
2020-08-04 23:32:18 +00:00
|
|
|
ConstantOptimiser{dynamic_cast<EVMDialect const&>(*m_dialect), meter}(*m_object->code);
|
2019-05-15 08:33:35 +00:00
|
|
|
}
|
2018-12-13 14:26:01 +00:00
|
|
|
else if (m_optimizerStep == "varDeclInitializer")
|
2020-08-04 23:32:18 +00:00
|
|
|
VarDeclInitializer::run(*m_context, *m_object->code);
|
2019-01-21 07:13:31 +00:00
|
|
|
else if (m_optimizerStep == "varNameCleaner")
|
2020-08-05 21:12:51 +00:00
|
|
|
{
|
|
|
|
disambiguate();
|
|
|
|
FunctionGrouper::run(*m_context, *m_object->code);
|
2020-08-04 23:32:18 +00:00
|
|
|
VarNameCleaner::run(*m_context, *m_object->code);
|
2020-08-05 21:12:51 +00:00
|
|
|
}
|
2019-05-18 05:36:29 +00:00
|
|
|
else if (m_optimizerStep == "forLoopConditionIntoBody")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
ForLoopConditionIntoBody::run(*m_context, *m_object->code);
|
2019-05-18 05:36:29 +00:00
|
|
|
}
|
2018-11-07 10:18:02 +00:00
|
|
|
else if (m_optimizerStep == "forLoopInitRewriter")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
ForLoopInitRewriter::run(*m_context, *m_object->code);
|
2018-11-07 10:18:02 +00:00
|
|
|
}
|
2018-10-11 12:48:57 +00:00
|
|
|
else if (m_optimizerStep == "commonSubexpressionEliminator")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
CommonSubexpressionEliminator::run(*m_context, *m_object->code);
|
2018-10-11 12:48:57 +00:00
|
|
|
}
|
2019-11-01 13:07:25 +00:00
|
|
|
else if (m_optimizerStep == "conditionalUnsimplifier")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
ConditionalUnsimplifier::run(*m_context, *m_object->code);
|
2019-11-01 13:07:25 +00:00
|
|
|
}
|
2019-09-11 09:42:59 +00:00
|
|
|
else if (m_optimizerStep == "conditionalSimplifier")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
ConditionalSimplifier::run(*m_context, *m_object->code);
|
2019-09-11 09:42:59 +00:00
|
|
|
}
|
2018-10-11 13:13:42 +00:00
|
|
|
else if (m_optimizerStep == "expressionSplitter")
|
2020-08-04 23:32:18 +00:00
|
|
|
ExpressionSplitter::run(*m_context, *m_object->code);
|
2018-09-25 14:29:46 +00:00
|
|
|
else if (m_optimizerStep == "expressionJoiner")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
ExpressionJoiner::run(*m_context, *m_object->code);
|
2018-09-25 14:29:46 +00:00
|
|
|
}
|
|
|
|
else if (m_optimizerStep == "splitJoin")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
ExpressionSplitter::run(*m_context, *m_object->code);
|
|
|
|
ExpressionJoiner::run(*m_context, *m_object->code);
|
|
|
|
ExpressionJoiner::run(*m_context, *m_object->code);
|
2018-09-25 14:29:46 +00:00
|
|
|
}
|
2018-10-11 13:42:31 +00:00
|
|
|
else if (m_optimizerStep == "functionGrouper")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
FunctionGrouper::run(*m_context, *m_object->code);
|
2018-10-11 13:42:31 +00:00
|
|
|
}
|
2018-10-11 13:47:49 +00:00
|
|
|
else if (m_optimizerStep == "functionHoister")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
FunctionHoister::run(*m_context, *m_object->code);
|
2018-10-11 13:47:49 +00:00
|
|
|
}
|
2018-10-11 14:58:21 +00:00
|
|
|
else if (m_optimizerStep == "expressionInliner")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
ExpressionInliner::run(*m_context, *m_object->code);
|
2018-10-11 14:58:21 +00:00
|
|
|
}
|
2018-10-11 15:14:59 +00:00
|
|
|
else if (m_optimizerStep == "fullInliner")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
FunctionHoister::run(*m_context, *m_object->code);
|
|
|
|
FunctionGrouper::run(*m_context, *m_object->code);
|
|
|
|
ExpressionSplitter::run(*m_context, *m_object->code);
|
|
|
|
FullInliner::run(*m_context, *m_object->code);
|
|
|
|
ExpressionJoiner::run(*m_context, *m_object->code);
|
2018-10-11 15:14:59 +00:00
|
|
|
}
|
2018-10-11 15:45:12 +00:00
|
|
|
else if (m_optimizerStep == "mainFunction")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
FunctionGrouper::run(*m_context, *m_object->code);
|
|
|
|
MainFunction::run(*m_context, *m_object->code);
|
2018-10-11 15:45:12 +00:00
|
|
|
}
|
2018-10-11 15:59:45 +00:00
|
|
|
else if (m_optimizerStep == "rematerialiser")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
Rematerialiser::run(*m_context, *m_object->code);
|
2018-10-11 15:59:45 +00:00
|
|
|
}
|
2018-10-11 16:35:13 +00:00
|
|
|
else if (m_optimizerStep == "expressionSimplifier")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-03 20:42:39 +00:00
|
|
|
ExpressionSplitter::run(*m_context, *m_object->code);
|
|
|
|
CommonSubexpressionEliminator::run(*m_context, *m_object->code);
|
2020-08-04 23:32:18 +00:00
|
|
|
ExpressionSimplifier::run(*m_context, *m_object->code);
|
|
|
|
ExpressionSimplifier::run(*m_context, *m_object->code);
|
|
|
|
ExpressionSimplifier::run(*m_context, *m_object->code);
|
2020-08-03 20:42:39 +00:00
|
|
|
UnusedPruner::run(*m_context, *m_object->code);
|
|
|
|
ExpressionJoiner::run(*m_context, *m_object->code);
|
|
|
|
ExpressionJoiner::run(*m_context, *m_object->code);
|
2018-10-11 16:35:13 +00:00
|
|
|
}
|
2018-10-11 19:16:37 +00:00
|
|
|
else if (m_optimizerStep == "fullSimplify")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
ExpressionSplitter::run(*m_context, *m_object->code);
|
|
|
|
ForLoopInitRewriter::run(*m_context, *m_object->code);
|
|
|
|
CommonSubexpressionEliminator::run(*m_context, *m_object->code);
|
|
|
|
ExpressionSimplifier::run(*m_context, *m_object->code);
|
|
|
|
UnusedPruner::run(*m_context, *m_object->code);
|
|
|
|
CircularReferencesPruner::run(*m_context, *m_object->code);
|
|
|
|
DeadCodeEliminator::run(*m_context, *m_object->code);
|
|
|
|
ExpressionJoiner::run(*m_context, *m_object->code);
|
|
|
|
ExpressionJoiner::run(*m_context, *m_object->code);
|
2018-10-11 19:16:37 +00:00
|
|
|
}
|
2020-07-10 16:56:32 +00:00
|
|
|
else if (m_optimizerStep == "unusedFunctionParameterPruner")
|
|
|
|
{
|
|
|
|
disambiguate();
|
|
|
|
FunctionHoister::run(*m_context, *m_object->code);
|
|
|
|
LiteralRematerialiser::run(*m_context, *m_object->code);
|
|
|
|
UnusedFunctionParameterPruner::run(*m_context, *m_object->code);
|
|
|
|
}
|
2018-10-11 16:41:01 +00:00
|
|
|
else if (m_optimizerStep == "unusedPruner")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
UnusedPruner::run(*m_context, *m_object->code);
|
2018-10-11 16:41:01 +00:00
|
|
|
}
|
2020-02-07 00:28:24 +00:00
|
|
|
else if (m_optimizerStep == "circularReferencesPruner")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
FunctionHoister::run(*m_context, *m_object->code);
|
|
|
|
CircularReferencesPruner::run(*m_context, *m_object->code);
|
2020-02-07 00:28:24 +00:00
|
|
|
}
|
2019-03-28 13:18:17 +00:00
|
|
|
else if (m_optimizerStep == "deadCodeEliminator")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
ForLoopInitRewriter::run(*m_context, *m_object->code);
|
|
|
|
DeadCodeEliminator::run(*m_context, *m_object->code);
|
2019-03-28 13:18:17 +00:00
|
|
|
}
|
2018-10-16 21:47:02 +00:00
|
|
|
else if (m_optimizerStep == "ssaTransform")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
ForLoopInitRewriter::run(*m_context, *m_object->code);
|
|
|
|
SSATransform::run(*m_context, *m_object->code);
|
2018-10-16 21:47:02 +00:00
|
|
|
}
|
2018-10-23 13:55:48 +00:00
|
|
|
else if (m_optimizerStep == "redundantAssignEliminator")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
RedundantAssignEliminator::run(*m_context, *m_object->code);
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
else if (m_optimizerStep == "ssaPlusCleanup")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
SSATransform::run(*m_context, *m_object->code);
|
|
|
|
RedundantAssignEliminator::run(*m_context, *m_object->code);
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
2019-05-27 22:14:01 +00:00
|
|
|
else if (m_optimizerStep == "loadResolver")
|
2019-05-21 13:52:15 +00:00
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
ForLoopInitRewriter::run(*m_context, *m_object->code);
|
|
|
|
ExpressionSplitter::run(*m_context, *m_object->code);
|
|
|
|
CommonSubexpressionEliminator::run(*m_context, *m_object->code);
|
|
|
|
ExpressionSimplifier::run(*m_context, *m_object->code);
|
2019-05-21 13:52:15 +00:00
|
|
|
|
2020-08-04 23:32:18 +00:00
|
|
|
LoadResolver::run(*m_context, *m_object->code);
|
2019-05-21 13:52:15 +00:00
|
|
|
|
2020-08-04 23:32:18 +00:00
|
|
|
UnusedPruner::run(*m_context, *m_object->code);
|
|
|
|
ExpressionJoiner::run(*m_context, *m_object->code);
|
|
|
|
ExpressionJoiner::run(*m_context, *m_object->code);
|
2019-05-21 13:52:15 +00:00
|
|
|
}
|
2019-09-18 08:47:49 +00:00
|
|
|
else if (m_optimizerStep == "loopInvariantCodeMotion")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
ForLoopInitRewriter::run(*m_context, *m_object->code);
|
|
|
|
LoopInvariantCodeMotion::run(*m_context, *m_object->code);
|
2019-05-21 13:52:15 +00:00
|
|
|
}
|
2019-05-09 20:01:18 +00:00
|
|
|
else if (m_optimizerStep == "controlFlowSimplifier")
|
2018-12-03 16:19:37 +00:00
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
ControlFlowSimplifier::run(*m_context, *m_object->code);
|
2019-05-09 20:01:18 +00:00
|
|
|
}
|
|
|
|
else if (m_optimizerStep == "structuralSimplifier")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
ForLoopInitRewriter::run(*m_context, *m_object->code);
|
|
|
|
LiteralRematerialiser::run(*m_context, *m_object->code);
|
|
|
|
StructuralSimplifier::run(*m_context, *m_object->code);
|
2018-12-03 16:19:37 +00:00
|
|
|
}
|
2020-05-11 17:56:29 +00:00
|
|
|
else if (m_optimizerStep == "reasoningBasedSimplifier")
|
|
|
|
{
|
|
|
|
disambiguate();
|
|
|
|
ReasoningBasedSimplifier::run(*m_context, *m_object->code);
|
|
|
|
}
|
2019-01-10 19:29:30 +00:00
|
|
|
else if (m_optimizerStep == "equivalentFunctionCombiner")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
EquivalentFunctionCombiner::run(*m_context, *m_object->code);
|
2019-01-10 19:29:30 +00:00
|
|
|
}
|
2019-01-16 10:44:45 +00:00
|
|
|
else if (m_optimizerStep == "ssaReverser")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
SSAReverser::run(*m_context, *m_object->code);
|
2019-01-16 10:44:45 +00:00
|
|
|
}
|
|
|
|
else if (m_optimizerStep == "ssaAndBack")
|
|
|
|
{
|
|
|
|
disambiguate();
|
|
|
|
// apply SSA
|
2020-08-04 23:32:18 +00:00
|
|
|
SSATransform::run(*m_context, *m_object->code);
|
|
|
|
RedundantAssignEliminator::run(*m_context, *m_object->code);
|
2019-01-16 10:44:45 +00:00
|
|
|
// reverse SSA
|
2020-08-04 23:32:18 +00:00
|
|
|
SSAReverser::run(*m_context, *m_object->code);
|
|
|
|
CommonSubexpressionEliminator::run(*m_context, *m_object->code);
|
|
|
|
UnusedPruner::run(*m_context, *m_object->code);
|
2019-01-16 10:44:45 +00:00
|
|
|
}
|
2019-02-04 16:30:29 +00:00
|
|
|
else if (m_optimizerStep == "stackCompressor")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
FunctionGrouper::run(*m_context, *m_object->code);
|
2019-03-14 17:25:33 +00:00
|
|
|
size_t maxIterations = 16;
|
2019-07-09 15:23:14 +00:00
|
|
|
Object obj;
|
2020-08-04 23:32:18 +00:00
|
|
|
obj.code = m_object->code;
|
2019-07-09 15:23:14 +00:00
|
|
|
StackCompressor::run(*m_dialect, obj, true, maxIterations);
|
2020-08-04 23:32:18 +00:00
|
|
|
m_object->code = obj.code;
|
|
|
|
BlockFlattener::run(*m_context, *m_object->code);
|
2019-02-04 16:30:29 +00:00
|
|
|
}
|
2019-04-29 15:55:57 +00:00
|
|
|
else if (m_optimizerStep == "wordSizeTransform")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2020-08-04 23:32:18 +00:00
|
|
|
ExpressionSplitter::run(*m_context, *m_object->code);
|
|
|
|
WordSizeTransform::run(*m_dialect, *m_dialect, *m_object->code, *m_nameDispenser);
|
2019-04-29 15:55:57 +00:00
|
|
|
}
|
2018-10-16 19:39:22 +00:00
|
|
|
else if (m_optimizerStep == "fullSuite")
|
2019-05-15 08:33:35 +00:00
|
|
|
{
|
|
|
|
GasMeter meter(dynamic_cast<EVMDialect const&>(*m_dialect), false, 200);
|
2019-07-09 15:23:14 +00:00
|
|
|
yul::Object obj;
|
2020-08-04 23:32:18 +00:00
|
|
|
obj.code = m_object->code;
|
2019-07-09 15:23:14 +00:00
|
|
|
obj.analysisInfo = m_analysisInfo;
|
2020-04-24 12:19:36 +00:00
|
|
|
OptimiserSuite::run(*m_dialect, &meter, obj, true, solidity::frontend::OptimiserSettings::DefaultYulOptimiserSteps);
|
2019-05-15 08:33:35 +00:00
|
|
|
}
|
2020-07-02 10:48:20 +00:00
|
|
|
else if (m_optimizerStep == "stackLimitEvader")
|
|
|
|
{
|
|
|
|
yul::Object obj;
|
|
|
|
obj.code = m_object->code;
|
|
|
|
obj.analysisInfo = m_analysisInfo;
|
|
|
|
disambiguate();
|
|
|
|
StackLimitEvader::run(*m_context, obj, CompilabilityChecker{
|
|
|
|
*m_dialect,
|
|
|
|
obj,
|
|
|
|
true
|
|
|
|
}.unreachableVariables);
|
|
|
|
}
|
|
|
|
else if (m_optimizerStep == "fakeStackLimitEvader")
|
|
|
|
{
|
|
|
|
yul::Object obj;
|
|
|
|
obj.code = m_object->code;
|
|
|
|
obj.analysisInfo = m_analysisInfo;
|
|
|
|
disambiguate();
|
|
|
|
// Mark all variables with a name starting with "$" for escalation to memory.
|
|
|
|
struct FakeUnreachableGenerator: ASTWalker
|
|
|
|
{
|
|
|
|
map<YulString, set<YulString>> fakeUnreachables;
|
|
|
|
using ASTWalker::operator();
|
|
|
|
void operator()(FunctionDefinition const& _function) override
|
|
|
|
{
|
|
|
|
YulString originalFunctionName = m_currentFunction;
|
|
|
|
m_currentFunction = _function.name;
|
|
|
|
ASTWalker::operator()(_function);
|
|
|
|
m_currentFunction = originalFunctionName;
|
|
|
|
}
|
|
|
|
void visitVariableName(YulString _var)
|
|
|
|
{
|
|
|
|
if (!_var.empty() && _var.str().front() == '$')
|
|
|
|
fakeUnreachables[m_currentFunction].insert(_var);
|
|
|
|
}
|
|
|
|
void operator()(VariableDeclaration const& _varDecl) override
|
|
|
|
{
|
|
|
|
for (auto const& var: _varDecl.variables)
|
|
|
|
visitVariableName(var.name);
|
|
|
|
ASTWalker::operator()(_varDecl);
|
|
|
|
}
|
|
|
|
void operator()(Identifier const& _identifier) override
|
|
|
|
{
|
|
|
|
visitVariableName(_identifier.name);
|
|
|
|
ASTWalker::operator()(_identifier);
|
|
|
|
}
|
|
|
|
YulString m_currentFunction = YulString{};
|
|
|
|
};
|
|
|
|
FakeUnreachableGenerator fakeUnreachableGenerator;
|
|
|
|
fakeUnreachableGenerator(*obj.code);
|
|
|
|
StackLimitEvader::run(*m_context, obj, fakeUnreachableGenerator.fakeUnreachables);
|
|
|
|
}
|
2018-10-09 13:43:55 +00:00
|
|
|
else
|
|
|
|
{
|
2019-02-11 15:00:24 +00:00
|
|
|
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Invalid optimizer step: " << m_optimizerStep << endl;
|
2019-05-07 13:33:22 +00:00
|
|
|
return TestResult::FatalError;
|
2018-10-09 13:43:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 11:33:45 +00:00
|
|
|
auto const printed = (m_object->subObjects.empty() ? AsmPrinter{ *m_dialect }(*m_object->code) : m_object->toString(m_dialect));
|
|
|
|
|
|
|
|
// Re-parse new code for compilability
|
|
|
|
// TODO: support for wordSizeTransform which needs different input and output dialects
|
|
|
|
if (m_optimizerStep != "wordSizeTransform" && !std::get<0>(parse(_stream, _linePrefix, _formatted, printed)))
|
|
|
|
{
|
|
|
|
util::AnsiColorized(_stream, _formatted, {util::formatting::BOLD, util::formatting::CYAN})
|
|
|
|
<< _linePrefix << "Result after the optimiser:" << endl;
|
|
|
|
printIndented(_stream, printed, _linePrefix + " ");
|
|
|
|
return TestResult::FatalError;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_obtainedResult = "step: " + m_optimizerStep + "\n\n" + printed + "\n";
|
2018-10-09 13:43:55 +00:00
|
|
|
|
2020-05-25 13:31:24 +00:00
|
|
|
return checkResult(_stream, _linePrefix, _formatted);
|
2018-10-09 13:43:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 11:33:45 +00:00
|
|
|
std::pair<std::shared_ptr<Object>, std::shared_ptr<AsmAnalysisInfo>> YulOptimizerTest::parse(
|
|
|
|
ostream& _stream,
|
|
|
|
string const& _linePrefix,
|
|
|
|
bool const _formatted,
|
|
|
|
string const& _source
|
|
|
|
)
|
2018-10-09 13:43:55 +00:00
|
|
|
{
|
2019-12-19 16:58:20 +00:00
|
|
|
ErrorList errors;
|
|
|
|
soltestAssert(m_dialect, "");
|
2020-08-05 11:33:45 +00:00
|
|
|
shared_ptr<Object> object;
|
|
|
|
shared_ptr<AsmAnalysisInfo> analysisInfo;
|
|
|
|
std::tie(object, analysisInfo) = yul::test::parse(_source, *m_dialect, errors);
|
|
|
|
if (!object || !analysisInfo || !Error::containsOnlyWarnings(errors))
|
2018-10-09 13:43:55 +00:00
|
|
|
{
|
2019-02-13 11:21:53 +00:00
|
|
|
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
|
2019-12-19 16:58:20 +00:00
|
|
|
printErrors(_stream, errors);
|
2020-08-05 11:33:45 +00:00
|
|
|
return {};
|
2018-10-09 13:43:55 +00:00
|
|
|
}
|
2020-08-05 11:33:45 +00:00
|
|
|
return {std::move(object), std::move(analysisInfo)};
|
2018-10-09 13:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void YulOptimizerTest::disambiguate()
|
|
|
|
{
|
2020-08-04 23:32:18 +00:00
|
|
|
*m_object->code = std::get<Block>(Disambiguator(*m_dialect, *m_analysisInfo)(*m_object->code));
|
2018-10-09 13:43:55 +00:00
|
|
|
m_analysisInfo.reset();
|
2019-09-23 14:32:50 +00:00
|
|
|
updateContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
void YulOptimizerTest::updateContext()
|
|
|
|
{
|
2020-08-04 23:32:18 +00:00
|
|
|
m_nameDispenser = make_unique<NameDispenser>(*m_dialect, *m_object->code, m_reservedIdentifiers);
|
2019-11-27 16:24:21 +00:00
|
|
|
m_context = make_unique<OptimiserStepContext>(OptimiserStepContext{
|
|
|
|
*m_dialect,
|
|
|
|
*m_nameDispenser,
|
|
|
|
m_reservedIdentifiers
|
|
|
|
});
|
2018-10-09 13:43:55 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:34:08 +00:00
|
|
|
void YulOptimizerTest::printErrors(ostream& _stream, ErrorList const& _errors)
|
2018-10-09 13:43:55 +00:00
|
|
|
{
|
2020-12-01 13:22:15 +00:00
|
|
|
SourceReferenceFormatter formatter(_stream, true, false);
|
2018-10-09 13:43:55 +00:00
|
|
|
|
|
|
|
for (auto const& error: _errors)
|
2019-04-05 15:49:39 +00:00
|
|
|
formatter.printErrorInformation(*error);
|
2018-10-09 13:43:55 +00:00
|
|
|
}
|