diff --git a/Changelog.md b/Changelog.md
index 627b16fe2..7c710c3c3 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -7,6 +7,8 @@ Compiler Features:
* SMTChecker: Support shifts.
* SMTChecker: Support structs.
* Yul Optimizer: Prune unused parameters in functions.
+ * Yul Optimizer: Try to simplify function names.
+
Bugfixes:
* Type Checker: Disallow ``virtual`` for modifiers in libraries.
diff --git a/libsolidity/interface/OptimiserSettings.h b/libsolidity/interface/OptimiserSettings.h
index 4b1cd7c4e..1a2095198 100644
--- a/libsolidity/interface/OptimiserSettings.h
+++ b/libsolidity/interface/OptimiserSettings.h
@@ -32,7 +32,7 @@ namespace solidity::frontend
struct OptimiserSettings
{
static char constexpr DefaultYulOptimiserSteps[] =
- "dhfoDgvulfnTUtnIf" // None of these can make stack problems worse
+ "NdhfoDgvulfnTUtnIf" // None of these can make stack problems worse
"["
"xarrscLM" // Turn into SSA and simplify
"cCTUtTOntnfDIul" // Perform structural simplification
@@ -47,7 +47,7 @@ struct OptimiserSettings
"gvif" // Run full inliner
"CTUcarrLsTOtfDncarrIulc" // SSA plus simplify
"]"
- "jmuljuljul VcTOcul jmul"; // Make source short and pretty
+ "jmuljuljul VcTOcul jmulN"; // Make source short and pretty
/// No optimisations at all - not recommended.
static OptimiserSettings none()
diff --git a/libyul/CMakeLists.txt b/libyul/CMakeLists.txt
index f3318bd84..bb385c755 100644
--- a/libyul/CMakeLists.txt
+++ b/libyul/CMakeLists.txt
@@ -127,6 +127,8 @@ add_library(yul
optimiser/NameDispenser.h
optimiser/NameDisplacer.cpp
optimiser/NameDisplacer.h
+ optimiser/NameSimplifier.cpp
+ optimiser/NameSimplifier.h
optimiser/OptimiserStep.h
optimiser/OptimizerUtilities.cpp
optimiser/OptimizerUtilities.h
diff --git a/libyul/optimiser/NameSimplifier.cpp b/libyul/optimiser/NameSimplifier.cpp
new file mode 100644
index 000000000..2e1bb822a
--- /dev/null
+++ b/libyul/optimiser/NameSimplifier.cpp
@@ -0,0 +1,122 @@
+/*
+ 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 .
+*/
+
+#include
+#include
+#include
+#include
+#include
+
+#include
+
+#include
+
+using namespace solidity::yul;
+using namespace std;
+
+NameSimplifier::NameSimplifier(
+ OptimiserStepContext& _context,
+ Block const& _ast
+):
+ m_context(_context),
+ m_usedNames(_context.reservedIdentifiers)
+{
+ for (YulString name: m_usedNames)
+ m_translations[name] = name;
+
+ set allNames = NameCollector(_ast).names();
+ m_usedNames += allNames;
+ for (YulString name: allNames)
+ findSimplification(name);
+}
+
+void NameSimplifier::operator()(FunctionDefinition& _funDef)
+{
+ translate(_funDef.name);
+ renameVariables(_funDef.parameters);
+ renameVariables(_funDef.returnVariables);
+ ASTModifier::operator()(_funDef);
+}
+
+void NameSimplifier::operator()(VariableDeclaration& _varDecl)
+{
+ renameVariables(_varDecl.variables);
+ ASTModifier::operator()(_varDecl);
+}
+
+void NameSimplifier::renameVariables(vector& _variables)
+{
+ for (TypedName& typedName: _variables)
+ translate(typedName.name);
+}
+
+void NameSimplifier::operator()(Identifier& _identifier)
+{
+ translate(_identifier.name);
+}
+
+void NameSimplifier::operator()(FunctionCall& _funCall)
+{
+ // The visitor on its own does not visit the function name.
+ if (!m_context.dialect.builtin(_funCall.functionName.name))
+ (*this)(_funCall.functionName);
+ ASTModifier::operator()(_funCall);
+}
+
+void NameSimplifier::findSimplification(YulString _name)
+{
+ if (m_translations.count(_name))
+ return;
+
+ string name = _name.str();
+
+ static auto replacements = vector>{
+ {regex("_\\$\\d+"), ""}, // removes AST IDs
+ {regex("(abi_..code.*)_to_.*"), "$1"}, // removes _to... for abi functions
+ {regex("(stringliteral_[0-9a-f][0-9a-f][0-9a-f][0-9a-f])[0-9a-f]*"), "$1"}, // shorten string literal
+ {regex("tuple_t_"), ""},
+ {regex("_memory_ptr"), ""},
+ {regex("_calldata_ptr"), "_calldata"},
+ {regex("_fromStack"), ""},
+ {regex("_storage_storage"), "_storage"},
+ {regex("_memory_memory"), "_memory"},
+ {regex("t_contract\\$_([^_]*)_"), "$1_"},
+ {regex("index_access_t_array"), "index_access"},
+ {regex("[0-9]*_$"), ""}
+ };
+ for (auto const& [pattern, substitute]: replacements)
+ {
+ string candidate = regex_replace(name, pattern, substitute);
+ if (
+ !isRestrictedIdentifier(m_context.dialect, YulString(candidate)) &&
+ !m_usedNames.count(YulString(candidate))
+ )
+ name = candidate;
+ }
+ if (name != _name.str())
+ {
+ m_usedNames.insert(YulString(name));
+ m_translations[_name] = YulString(name);
+ }
+}
+
+void NameSimplifier::translate(YulString& _name)
+{
+ auto it = m_translations.find(_name);
+ if (it != m_translations.end())
+ _name = it->second;
+}
diff --git a/libyul/optimiser/NameSimplifier.h b/libyul/optimiser/NameSimplifier.h
new file mode 100644
index 000000000..924e734f9
--- /dev/null
+++ b/libyul/optimiser/NameSimplifier.h
@@ -0,0 +1,76 @@
+/*
+ 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 .
+*/
+// SPDX-License-Identifier: GPL-3.0
+
+#pragma once
+
+#include
+#include
+#include
+#include
+
+#include