aboutsummaryrefslogtreecommitdiffstats
path: root/libyul/optimiser/RedundantAssignEliminator.cpp
blob: f415a36cb1ee11487a6ca50b8425a5aa7bc4849c (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
    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 removes assignments to variables that are not used
 * until they go out of scope or are re-assigned.
 */

#include <libyul/optimiser/RedundantAssignEliminator.h>

#include <libyul/optimiser/Semantics.h>
#include <libyul/AsmData.h>

#include <libdevcore/CommonData.h>

#include <boost/range/algorithm_ext/erase.hpp>

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

void RedundantAssignEliminator::operator()(Identifier const& _identifier)
{
    changeUndecidedTo(_identifier.name, State::Used);
}

void RedundantAssignEliminator::operator()(VariableDeclaration const& _variableDeclaration)
{
    ASTWalker::operator()(_variableDeclaration);

    for (auto const& var: _variableDeclaration.variables)
        m_declaredVariables.emplace(var.name);
}

void RedundantAssignEliminator::operator()(Assignment const& _assignment)
{
    visit(*_assignment.value);
    for (auto const& var: _assignment.variableNames)
        changeUndecidedTo(var.name, State::Unused);

    if (_assignment.variableNames.size() == 1)
        // Default-construct it in "Undecided" state if it does not yet exist.
        m_assignments[_assignment.variableNames.front().name][&_assignment];
}

void RedundantAssignEliminator::operator()(If const& _if)
{
    visit(*_if.condition);

    RedundantAssignEliminator branch{*this};
    branch(_if.body);

    join(branch);
}

void RedundantAssignEliminator::operator()(Switch const& _switch)
{
    visit(*_switch.expression);

    bool hasDefault = false;
    vector<RedundantAssignEliminator> branches;
    for (auto const& c: _switch.cases)
    {
        if (!c.value)
            hasDefault = true;
        branches.emplace_back(*this);
        branches.back()(c.body);
    }

    if (hasDefault)
    {
        *this = std::move(branches.back());
        branches.pop_back();
    }
    for (auto& branch: branches)
        join(branch);
}

void RedundantAssignEliminator::operator()(FunctionDefinition const& _functionDefinition)
{
    (*this)(_functionDefinition.body);

    for (auto const& param: _functionDefinition.parameters)
    {
        changeUndecidedTo(param.name, State::Unused);
        finalize(param.name);
    }
    for (auto const& retParam: _functionDefinition.returnVariables)
    {
        changeUndecidedTo(retParam.name, State::Used);
        finalize(retParam.name);
    }
}

void RedundantAssignEliminator::operator()(ForLoop const& _forLoop)
{
    // This will set all variables that are declared in this
    // block to "unused" when it is destroyed.
    BlockScope scope(*this);

    // We need to visit the statements directly because of the
    // scoping rules.
    walkVector(_forLoop.pre.statements);

    // We just run the loop twice to account for the
    // back edge.
    // There need not be more runs because we only have three different states.

    visit(*_forLoop.condition);

    RedundantAssignEliminator zeroRuns{*this};

    (*this)(_forLoop.body);
    (*this)(_forLoop.post);

    visit(*_forLoop.condition);

    RedundantAssignEliminator oneRun{*this};

    (*this)(_forLoop.body);
    (*this)(_forLoop.post);

    visit(*_forLoop.condition);

    // Order does not matter because "max" is commutative and associative.
    join(oneRun);
    join(zeroRuns);
}

void RedundantAssignEliminator::operator()(Block const& _block)
{
    // This will set all variables that are declared in this
    // block to "unused" when it is destroyed.
    BlockScope scope(*this);

    ASTWalker::operator()(_block);
}

void RedundantAssignEliminator::run(Block& _ast)
{
    RedundantAssignEliminator rae;
    rae(_ast);

    AssignmentRemover remover{rae.m_assignmentsToRemove};
    remover(_ast);
}

template <class K, class V, class F>
void joinMap(std::map<K, V>& _a, std::map<K, V>&& _b, F _conflictSolver)
{
    // TODO Perhaps it is better to just create a sorted list
    // and then use insert(begin, end)

    auto ita = _a.begin();
    auto aend = _a.end();
    auto itb = _b.begin();
    auto bend = _b.end();

    for (; itb != bend; ++ita)
    {
        if (ita == aend)
            ita = _a.insert(ita, std::move(*itb++));
        else if (ita->first < itb->first)
            continue;
        else if (itb->first < ita->first)
            ita = _a.insert(ita, std::move(*itb++));
        else
        {
            _conflictSolver(ita->second, std::move(itb->second));
            ++itb;
        }
    }
}

void RedundantAssignEliminator::join(RedundantAssignEliminator& _other)
{
    m_assignmentsToRemove.insert(begin(_other.m_assignmentsToRemove), end(_other.m_assignmentsToRemove));

    joinMap(m_assignments, std::move(_other.m_assignments), [](
        map<Assignment const*, State>& _assignmentHere,
        map<Assignment const*, State>&& _assignmentThere
    )
    {
        return joinMap(_assignmentHere, std::move(_assignmentThere), State::join);
    });
}

void RedundantAssignEliminator::changeUndecidedTo(YulString _variable, RedundantAssignEliminator::State _newState)
{
    for (auto& assignment: m_assignments[_variable])
        if (assignment.second == State{State::Undecided})
            assignment.second = _newState;
}

void RedundantAssignEliminator::finalize(YulString _variable)
{
    for (auto& assignment: m_assignments[_variable])
    {
        assertThrow(assignment.second != State::Undecided, OptimizerException, "");
        if (assignment.second == State{State::Unused} && MovableChecker{*assignment.first->value}.movable())
            // TODO the only point where we actually need this
            // to be a set is for the for loop
            m_assignmentsToRemove.insert(assignment.first);
    }
    m_assignments.erase(_variable);
}

void AssignmentRemover::operator()(Block& _block)
{
    boost::range::remove_erase_if(_block.statements, [=](Statement const& _statement) -> bool {
        return _statement.type() == typeid(Assignment) && m_toRemove.count(&boost::get<Assignment>(_statement));
    });

    ASTModifier::operator()(_block);
}