solidity/libyul/optimiser/FullInliner.h

157 lines
4.4 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/>.
*/
/**
* Optimiser component that performs function inlining for arbitrary functions.
*/
#pragma once
2018-10-15 09:52:35 +00:00
#include <libyul/ASTDataForward.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>
#include <libyul/Exceptions.h>
2017-12-07 14:43:23 +00:00
2018-05-09 09:43:14 +00:00
#include <libevmasm/SourceLocation.h>
2017-12-07 14:43:23 +00:00
#include <boost/variant.hpp>
#include <boost/optional.hpp>
#include <set>
namespace dev
{
namespace 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
*
2018-10-02 08:46:59 +00:00
* Prerequisites: Disambiguator, Function Hoister
* More efficient if run after: Expression Splitter
2017-12-07 14:43:23 +00:00
*/
class FullInliner: public ASTModifier
{
public:
2018-10-16 19:38:47 +00:00
explicit FullInliner(Block& _ast, NameDispenser& _dispenser);
2017-12-07 14:43:23 +00:00
void run();
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, std::string const& _callSite);
2017-12-07 14:43:23 +00:00
FunctionDefinition& function(std::string _name) { return *m_functions.at(_name); }
private:
2018-01-22 23:06:55 +00:00
void updateCodeSize(FunctionDefinition& fun);
2018-10-02 08:46:59 +00:00
void handleBlock(std::string const& _currentFunctionName, Block& _block);
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<std::string, FunctionDefinition*> m_functions;
2018-01-22 23:06:55 +00:00
/// Names of functions to always inline.
std::set<std::string> m_alwaysInline;
/// Variables that are constants (used for inlining heuristic)
std::set<std::string> m_constants;
std::map<std::string, size_t> m_functionSizes;
2018-10-16 19:38:47 +00:00
NameDispenser& m_nameDispenser;
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, std::string _functionName):
m_currentFunction(std::move(_functionName)),
m_driver(_driver),
m_nameDispenser(_nameDispenser)
{ }
2018-10-02 08:46:59 +00:00
virtual void operator()(Block& _block) override;
2017-12-07 14:43:23 +00:00
private:
2018-10-02 08:46:59 +00:00
boost::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
std::string m_currentFunction;
FullInliner& m_driver;
NameDispenser& m_nameDispenser;
};
/**
* 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,
std::string const& _varNamePrefix,
std::map<std::string, std::string> const& _variableReplacements
):
m_nameDispenser(_nameDispenser),
m_varNamePrefix(_varNamePrefix),
m_variableReplacements(_variableReplacements)
{}
using ASTCopier::operator ();
virtual Statement operator()(VariableDeclaration const& _varDecl) override;
virtual Statement operator()(FunctionDefinition const& _funDef) override;
virtual std::string translateIdentifier(std::string const& _name) override;
NameDispenser& m_nameDispenser;
std::string const& m_varNamePrefix;
std::map<std::string, std::string> m_variableReplacements;
};
}
}