solidity/libyul/optimiser/FullInliner.h

182 lines
5.5 KiB
C
Raw Normal View History

2017-12-07 14:43:23 +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/>.
*/
// SPDX-License-Identifier: GPL-3.0
2017-12-07 14:43:23 +00:00
/**
* Optimiser component that performs function inlining for arbitrary functions.
*/
#pragma once
#include <libyul/ASTForward.h>
2017-12-07 14:43:23 +00:00
2018-10-15 09:52:35 +00:00
#include <libyul/optimiser/ASTCopier.h>
#include <libyul/optimiser/ASTWalker.h>
#include <libyul/optimiser/NameDispenser.h>
2019-09-23 14:32:50 +00:00
#include <libyul/optimiser/OptimiserStep.h>
2018-10-15 09:52:35 +00:00
#include <libyul/Exceptions.h>
2017-12-07 14:43:23 +00:00
#include <liblangutil/SourceLocation.h>
2017-12-07 14:43:23 +00:00
#include <optional>
2017-12-07 14:43:23 +00:00
#include <set>
2020-04-01 03:04:29 +00:00
#include <utility>
2017-12-07 14:43:23 +00:00
2019-12-11 16:31:36 +00:00
namespace solidity::yul
2017-12-07 14:43:23 +00:00
{
class NameCollector;
/**
2018-10-02 08:46:59 +00:00
* Optimiser component that modifies an AST in place, inlining functions.
* Expressions are expected to be split, i.e. the component will only inline
* function calls that are at the root of the expression and that only contains
* variables as arguments. More specifically, it will inline
* - let x1, ..., xn := f(a1, ..., am)
* - x1, ..., xn := f(a1, ..., am)
* f(a1, ..., am)
2017-12-07 14:43:23 +00:00
*
2018-10-02 08:46:59 +00:00
* The transform changes code of the form
2017-12-07 14:43:23 +00:00
*
* function f(a, b) -> c { ... }
2018-10-02 08:46:59 +00:00
* let z := f(x, y)
2017-12-07 14:43:23 +00:00
*
2018-10-02 08:46:59 +00:00
* into
2017-12-07 14:43:23 +00:00
*
* function f(a, b) -> c { ... }
*
2018-10-02 08:46:59 +00:00
* let f_a := x
* let f_b := y
* let f_c
* code of f, with replacements: a -> f_a, b -> f_b, c -> f_c
* let z := f_c
2017-12-07 14:43:23 +00:00
*
* Prerequisites: Disambiguator
* More efficient if run after: Function Hoister, Expression Splitter
2017-12-07 14:43:23 +00:00
*/
class FullInliner: public ASTModifier
{
public:
2019-09-23 14:32:50 +00:00
static constexpr char const* name{"FullInliner"};
static void run(OptimiserStepContext& _context, Block& _ast);
2017-12-07 14:43:23 +00:00
2018-01-22 23:06:55 +00:00
/// Inlining heuristic.
/// @param _callSite the name of the function in which the function call is located.
bool shallInline(FunctionCall const& _funCall, YulString _callSite);
2018-01-22 23:06:55 +00:00
FunctionDefinition* function(YulString _name)
{
auto it = m_functions.find(_name);
if (it != m_functions.end())
return it->second;
return nullptr;
}
2017-12-07 14:43:23 +00:00
/// Adds the size of _funCall to the size of _callSite. This is just
/// a rough estimate that is done during inlining. The proper size
/// should be determined after inlining is completed.
void tentativelyUpdateCodeSize(YulString _function, YulString _callSite);
2017-12-07 14:43:23 +00:00
private:
2020-09-09 13:07:38 +00:00
enum Pass { InlineTiny, InlineRest };
FullInliner(Block& _ast, NameDispenser& _dispenser, Dialect const& _dialect);
2020-09-09 13:07:38 +00:00
void run(Pass _pass);
2019-09-23 14:32:50 +00:00
/// @returns a map containing the maximum depths of a call chain starting at each
/// function. For recursive functions, the value is one larger than for all others.
std::map<YulString, size_t> callDepths() const;
2019-01-09 15:51:09 +00:00
void updateCodeSize(FunctionDefinition const& _fun);
void handleBlock(YulString _currentFunctionName, Block& _block);
2019-05-22 16:13:04 +00:00
bool recursive(FunctionDefinition const& _fun) const;
2018-10-02 08:46:59 +00:00
2020-09-09 13:07:38 +00:00
Pass m_pass;
2017-12-07 14:43:23 +00:00
/// The AST to be modified. The root block itself will not be modified, because
/// we store pointers to functions.
Block& m_ast;
std::map<YulString, FunctionDefinition*> m_functions;
2019-10-28 14:25:02 +00:00
/// Functions not to be inlined (because they contain the ``leave`` statement).
std::set<YulString> m_noInlineFunctions;
2022-03-02 14:52:22 +00:00
/// True, if the code contains a ``memoryguard`` and we can expect to be able to move variables to memory later.
bool m_hasMemoryGuard = false;
/// Set of recursive functions.
std::set<YulString> m_recursiveFunctions;
2018-01-22 23:06:55 +00:00
/// Names of functions to always inline.
std::set<YulString> m_singleUse;
2018-01-22 23:06:55 +00:00
/// Variables that are constants (used for inlining heuristic)
std::set<YulString> m_constants;
std::map<YulString, size_t> m_functionSizes;
2018-10-16 19:38:47 +00:00
NameDispenser& m_nameDispenser;
Dialect const& m_dialect;
2017-12-07 14:43:23 +00:00
};
/**
* Class that walks the AST of a block that does not contain function definitions and perform
* the actual code modifications.
*/
class InlineModifier: public ASTModifier
{
public:
InlineModifier(FullInliner& _driver, NameDispenser& _nameDispenser, YulString _functionName, Dialect const& _dialect):
2017-12-07 14:43:23 +00:00
m_currentFunction(std::move(_functionName)),
m_driver(_driver),
m_nameDispenser(_nameDispenser),
m_dialect(_dialect)
2017-12-07 14:43:23 +00:00
{ }
void operator()(Block& _block) override;
2017-12-07 14:43:23 +00:00
private:
std::optional<std::vector<Statement>> tryInlineStatement(Statement& _statement);
2018-10-16 15:21:14 +00:00
std::vector<Statement> performInline(Statement& _statement, FunctionCall& _funCall);
2017-12-07 14:43:23 +00:00
YulString m_currentFunction;
2017-12-07 14:43:23 +00:00
FullInliner& m_driver;
NameDispenser& m_nameDispenser;
Dialect const& m_dialect;
2017-12-07 14:43:23 +00:00
};
/**
* Creates a copy of a block that is supposed to be the body of a function.
* Applies replacements to referenced variables and creates new names for
* variable declarations.
*/
class BodyCopier: public ASTCopier
{
public:
BodyCopier(
NameDispenser& _nameDispenser,
2020-04-01 03:04:29 +00:00
std::map<YulString, YulString> _variableReplacements
2017-12-07 14:43:23 +00:00
):
m_nameDispenser(_nameDispenser),
2020-04-01 03:04:29 +00:00
m_variableReplacements(std::move(_variableReplacements))
2017-12-07 14:43:23 +00:00
{}
using ASTCopier::operator ();
Statement operator()(VariableDeclaration const& _varDecl) override;
Statement operator()(FunctionDefinition const& _funDef) override;
2017-12-07 14:43:23 +00:00
YulString translateIdentifier(YulString _name) override;
2017-12-07 14:43:23 +00:00
NameDispenser& m_nameDispenser;
std::map<YulString, YulString> m_variableReplacements;
2017-12-07 14:43:23 +00:00
};
}