aboutsummaryrefslogtreecommitdiffstats
path: root/libyul/optimiser/FullInliner.cpp
blob: cd0ed52a002b8261dcd43f1d33afa2083bef8eb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
    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.
 */

#include <libyul/optimiser/FullInliner.h>

#include <libyul/optimiser/ASTCopier.h>
#include <libyul/optimiser/ASTWalker.h>
#include <libyul/optimiser/NameCollector.h>
#include <libyul/optimiser/Utilities.h>
#include <libyul/Exceptions.h>

#include <libsolidity/inlineasm/AsmData.h>

#include <libdevcore/CommonData.h>
#include <libdevcore/Visitor.h>

#include <boost/range/adaptor/reversed.hpp>

using namespace std;
using namespace dev;
using namespace dev::yul;
using namespace dev::solidity;

FullInliner::FullInliner(Block& _ast):
    m_ast(_ast)
{
    assertThrow(m_ast.statements.size() >= 1, OptimizerException, "");
    assertThrow(m_ast.statements.front().type() == typeid(Block), OptimizerException, "");
    m_nameDispenser.m_usedNames = NameCollector(m_ast).names();

    for (size_t i = 1; i < m_ast.statements.size(); ++i)
    {
        assertThrow(m_ast.statements.at(i).type() == typeid(FunctionDefinition), OptimizerException, "");
        FunctionDefinition& fun = boost::get<FunctionDefinition>(m_ast.statements.at(i));
        m_functions[fun.name] = &fun;
    }
}

void FullInliner::run()
{
    assertThrow(m_ast.statements[0].type() == typeid(Block), OptimizerException, "");

    handleBlock("", boost::get<Block>(m_ast.statements[0]));
    for (auto const& fun: m_functions)
        handleBlock(fun.second->name, fun.second->body);
}

void FullInliner::handleBlock(string const& _currentFunctionName, Block& _block)
{
    InlineModifier{*this, m_nameDispenser, _currentFunctionName}(_block);
}

void InlineModifier::operator()(Block& _block)
{
    function<boost::optional<vector<Statement>>(Statement&)> f = [&](Statement& _statement) -> boost::optional<vector<Statement>> {
        visit(_statement);
        return tryInlineStatement(_statement);
    };
    iterateReplacing(_block.statements, f);
}

boost::optional<vector<Statement>> InlineModifier::tryInlineStatement(Statement& _statement)
{
    // Only inline for expression statements, assignments and variable declarations.
    Expression* e = boost::apply_visitor(GenericFallbackReturnsVisitor<Expression*, ExpressionStatement, Assignment, VariableDeclaration>(
        [](ExpressionStatement& _s) { return &_s.expression; },
        [](Assignment& _s) { return _s.value.get(); },
        [](VariableDeclaration& _s) { return _s.value.get(); }
    ), _statement);
    if (e)
    {
        // Only inline direct function calls.
        FunctionCall* funCall = boost::apply_visitor(GenericFallbackReturnsVisitor<FunctionCall*, FunctionCall&>(
            [](FunctionCall& _e) { return &_e; }
        ), *e);
        if (funCall)
        {
            // TODO: Insert good heuristic here. Perhaps implement that inside the driver.
            bool doInline = funCall->functionName.name != m_currentFunction;

            if (doInline)
                return performInline(_statement, *funCall);
        }
    }
    return {};
}

vector<Statement> InlineModifier::performInline(Statement& _statement, FunctionCall& _funCall)
{
    vector<Statement> newStatements;
    map<string, string> variableReplacements;

    FunctionDefinition& function = m_driver.function(_funCall.functionName.name);

    // helper function to create a new variable that is supposed to model
    // an existing variable.
    auto newVariable = [&](TypedName const& _existingVariable, Expression* _value) {
        string newName = m_nameDispenser.newName(function.name + "_" + _existingVariable.name);
        variableReplacements[_existingVariable.name] = newName;
        VariableDeclaration varDecl{_funCall.location, {{_funCall.location, newName, _existingVariable.type}}, {}};
        if (_value)
            varDecl.value = make_shared<Expression>(std::move(*_value));
        newStatements.emplace_back(std::move(varDecl));
    };

    for (size_t i = 0; i < _funCall.arguments.size(); ++i)
        newVariable(function.parameters[i], &_funCall.arguments[i]);
    for (auto const& var: function.returnVariables)
        newVariable(var, nullptr);

    Statement newBody = BodyCopier(m_nameDispenser, function.name + "_", variableReplacements)(function.body);
    newStatements += std::move(boost::get<Block>(newBody).statements);

    boost::apply_visitor(GenericFallbackVisitor<Assignment, VariableDeclaration>{
        [&](Assignment& _assignment)
        {
            for (size_t i = 0; i < _assignment.variableNames.size(); ++i)
                newStatements.emplace_back(Assignment{
                    _assignment.location,
                    {_assignment.variableNames[i]},
                    make_shared<Expression>(Identifier{
                        _assignment.location,
                        variableReplacements.at(function.returnVariables[i].name)
                    })
                });
        },
        [&](VariableDeclaration& _varDecl)
        {
            for (size_t i = 0; i < _varDecl.variables.size(); ++i)
                newStatements.emplace_back(VariableDeclaration{
                    _varDecl.location,
                    {std::move(_varDecl.variables[i])},
                    make_shared<Expression>(Identifier{
                        _varDecl.location,
                        variableReplacements.at(function.returnVariables[i].name)
                    })
                });
        }
        // nothing to be done for expression statement
    }, _statement);
    return newStatements;
}

string InlineModifier::newName(string const& _prefix)
{
    return m_nameDispenser.newName(_prefix);
}

Statement BodyCopier::operator()(VariableDeclaration const& _varDecl)
{
    for (auto const& var: _varDecl.variables)
        m_variableReplacements[var.name] = m_nameDispenser.newName(m_varNamePrefix + var.name);
    return ASTCopier::operator()(_varDecl);
}

Statement BodyCopier::operator()(FunctionDefinition const& _funDef)
{
    assertThrow(false, OptimizerException, "Function hoisting has to be done before function inlining.");
    return _funDef;
}

string BodyCopier::translateIdentifier(string const& _name)
{
    if (m_variableReplacements.count(_name))
        return m_variableReplacements.at(_name);
    else
        return _name;
}