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
|
/*
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 turns subsequent assignments to variable declarations
* and assignments.
*/
#pragma once
#include <libyul/ASTDataForward.h>
#include <libyul/optimiser/ASTWalker.h>
#include <vector>
namespace yul
{
class NameDispenser;
/**
* Optimizer stage that tries to replace repeated assignments to
* existing variables by declarations of new variables as much as
* possible.
* The reassignments are still there, but all references to the
* reassigned variables are replaced by the newly declared variables.
*
* Example:
* {
* let a := 1
* mstore(a, 2)
* a := 3
* }
* is transformed to
* {
* let a_1 := 1
* let a := a_1
* mstore(a_1, 2)
* let a_3 := 3
* a := a_3
* }
*
* Exact semantics:
*
* For any variable a that is assigned to somewhere in the code (assignment with
* declaration does not count) perform the following transforms:
* - replace "let a := v" by "let a_1 := v let a := a_1"
* - replace "a := v" by "let a_1 := v a := a_1"
* Furthermore, always note the current variable/value assigned to a and replace each
* reference to a by this variable.
* The current value mapping is cleared for a variable a at the end of each block
* in which it was assigned and just after the for loop init block if it is assigned
* inside the for loop.
*
* After this stage, redundantAssignmentRemover is recommended to remove the unnecessary
* intermediate assignments.
*
* This stage provides best results if CSE is run right before it, because
* then it does not generate excessive amounts of variables.
*
* TODO Which transforms are required to keep this idempotent?
*/
class SSATransform: public ASTModifier
{
public:
void operator()(Identifier&) override;
void operator()(ForLoop&) override;
void operator()(Block& _block) override;
static void run(Block& _ast, NameDispenser& _nameDispenser);
private:
explicit SSATransform(NameDispenser& _nameDispenser, std::set<YulString> const& _variablesToReplace):
m_nameDispenser(_nameDispenser), m_variablesToReplace(_variablesToReplace)
{ }
NameDispenser& m_nameDispenser;
std::set<YulString> const& m_variablesToReplace;
std::map<YulString, YulString> m_currentVariableValues;
};
}
|