aboutsummaryrefslogtreecommitdiffstats
path: root/libyul/optimiser/RedundantAssignEliminator.cpp
blob: 775b7673bd40160496de920bfb670ba0f0114acc (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
/*
    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 <libsolidity/inlineasm/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);
    for (auto const& retParam: _functionDefinition.returnVariables)
        changeUndecidedTo(retParam.name, State::Used);
}

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);

    std::set<Assignment const*> assignmentsToRemove;
    for (auto const& variables: rae.m_assignments)
        for (auto const& assignment: variables.second)
        {
            assertThrow(assignment.second != State::Undecided, OptimizerException, "");
            if (assignment.second == State::Unused && MovableChecker{*assignment.first->value}.movable())
                assignmentsToRemove.emplace(assignment.first);
        }

    AssignmentRemover remover{assignmentsToRemove};
    remover(_ast);
}

void RedundantAssignEliminator::join(RedundantAssignEliminator& _other)
{
    for (auto& var: _other.m_assignments)
        if (m_assignments.count(var.first))
        {
            map<Assignment const*, State>& assignmentsHere = m_assignments[var.first];
            for (auto& assignment: var.second)
                assignmentsHere[assignment.first].join(assignment.second);
        }
        else
            m_assignments[var.first] = std::move(var.second);
}

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

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);
}