2018-10-23 13:55:48 +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 removes assignments to variables that are not used
|
|
|
|
* until they go out of scope or are re-assigned.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-11-21 11:43:41 +00:00
|
|
|
#include <libyul/AsmDataForward.h>
|
2018-10-23 13:55:48 +00:00
|
|
|
#include <libyul/optimiser/ASTWalker.h>
|
2019-09-23 14:32:50 +00:00
|
|
|
#include <libyul/optimiser/OptimiserStep.h>
|
2018-10-23 13:55:48 +00:00
|
|
|
|
|
|
|
#include <map>
|
2019-03-18 16:43:28 +00:00
|
|
|
#include <vector>
|
2018-10-23 13:55:48 +00:00
|
|
|
|
|
|
|
namespace yul
|
|
|
|
{
|
2018-12-20 17:55:32 +00:00
|
|
|
struct Dialect;
|
2018-10-23 13:55:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Optimiser component that removes assignments to variables that are not used
|
|
|
|
* until they go out of scope or are re-assigned. This component
|
|
|
|
* respects the control-flow and takes it into account for removal.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* let a
|
|
|
|
* a := 1
|
|
|
|
* a := 2
|
|
|
|
* b := 2
|
|
|
|
* if calldataload(0)
|
|
|
|
* {
|
|
|
|
* b := mload(a)
|
|
|
|
* }
|
|
|
|
* a := b
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* In the example, "a := 1" can be removed because the value from this assignment
|
|
|
|
* is not used in any control-flow branch (it is replaced right away).
|
|
|
|
* The assignment "a := 2" is also overwritten by "a := b" at the end,
|
|
|
|
* but there is a control-flow path (through the condition body) which uses
|
|
|
|
* the value from "a := 2" and thus, this assignment cannot be removed.
|
|
|
|
*
|
|
|
|
* Detailed rules:
|
|
|
|
*
|
|
|
|
* The AST is traversed twice: in an information gathering step and in the
|
|
|
|
* actual removal step. During information gathering, we maintain a
|
|
|
|
* mapping from assignment statements to the three states
|
|
|
|
* "unused", "undecided" and "used".
|
|
|
|
* When an assignment is visited, it is added to the mapping in the "undecided" state
|
|
|
|
* (see remark about for loops below) and every other assignment to the same variable
|
|
|
|
* that is still in the "undecided" state is changed to "unused".
|
|
|
|
* When a variable is referenced, the state of any assignment to that variable still
|
|
|
|
* in the "undecided" state is changed to "used".
|
|
|
|
* At points where control flow splits, a copy
|
|
|
|
* of the mapping is handed over to each branch. At points where control flow
|
|
|
|
* joins, the two mappings coming from the two branches are combined in the following way:
|
|
|
|
* Statements that are only in one mapping or have the same state are used unchanged.
|
|
|
|
* Conflicting values are resolved in the following way:
|
|
|
|
* "unused", "undecided" -> "undecided"
|
|
|
|
* "unused", "used" -> "used"
|
|
|
|
* "undecided, "used" -> "used".
|
|
|
|
*
|
|
|
|
* For for-loops, the condition, body and post-part are visited twice, taking
|
|
|
|
* the joining control-flow at the condition into account.
|
|
|
|
* In other words, we create three control flow paths: Zero runs of the loop,
|
|
|
|
* one run and two runs and then combine them at the end.
|
|
|
|
* Running at most twice is enough because there are only three different states.
|
|
|
|
*
|
2019-04-23 07:49:16 +00:00
|
|
|
* Since this algorithm has exponential runtime in the nesting depth of for loops,
|
|
|
|
* a shortcut is taken at a certain nesting level: We only use the zero- and
|
|
|
|
* once-run of the for loop and change any assignment that was newly introduced
|
|
|
|
* in the for loop from to "used".
|
|
|
|
*
|
2018-10-23 13:55:48 +00:00
|
|
|
* For switch statements that have a "default"-case, there is no control-flow
|
|
|
|
* part that skips the switch.
|
|
|
|
*
|
2019-10-28 14:25:02 +00:00
|
|
|
* At ``leave`` statements, all return variables are set to "used".
|
|
|
|
*
|
2018-10-23 13:55:48 +00:00
|
|
|
* When a variable goes out of scope, all statements still in the "undecided"
|
|
|
|
* state are changed to "unused", unless the variable is the return
|
|
|
|
* parameter of a function - there, the state changes to "used".
|
|
|
|
*
|
|
|
|
* In the second traversal, all assignments that are in the "unused" state are removed.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This step is usually run right after the SSA transform to complete
|
|
|
|
* the generation of the pseudo-SSA.
|
|
|
|
*
|
2019-04-05 18:38:23 +00:00
|
|
|
* Prerequisite: Disambiguator, ForLoopInitRewriter.
|
2018-10-23 13:55:48 +00:00
|
|
|
*/
|
|
|
|
class RedundantAssignEliminator: public ASTWalker
|
|
|
|
{
|
|
|
|
public:
|
2019-09-23 14:32:50 +00:00
|
|
|
static constexpr char const* name{"RedundantAssignEliminator"};
|
|
|
|
static void run(OptimiserStepContext&, Block& _ast);
|
|
|
|
|
2018-12-20 17:55:32 +00:00
|
|
|
explicit RedundantAssignEliminator(Dialect const& _dialect): m_dialect(&_dialect) {}
|
2019-03-18 16:38:25 +00:00
|
|
|
RedundantAssignEliminator() = delete;
|
|
|
|
RedundantAssignEliminator(RedundantAssignEliminator const&) = delete;
|
|
|
|
RedundantAssignEliminator& operator=(RedundantAssignEliminator const&) = delete;
|
2018-10-23 13:55:48 +00:00
|
|
|
RedundantAssignEliminator(RedundantAssignEliminator&&) = default;
|
|
|
|
RedundantAssignEliminator& operator=(RedundantAssignEliminator&&) = default;
|
|
|
|
|
|
|
|
void operator()(Identifier const& _identifier) override;
|
|
|
|
void operator()(VariableDeclaration const& _variableDeclaration) override;
|
|
|
|
void operator()(Assignment const& _assignment) override;
|
|
|
|
void operator()(If const& _if) override;
|
|
|
|
void operator()(Switch const& _switch) override;
|
|
|
|
void operator()(FunctionDefinition const&) override;
|
|
|
|
void operator()(ForLoop const&) override;
|
2019-03-04 14:38:05 +00:00
|
|
|
void operator()(Break const&) override;
|
|
|
|
void operator()(Continue const&) override;
|
2019-10-28 14:25:02 +00:00
|
|
|
void operator()(Leave const&) override;
|
2018-10-23 13:55:48 +00:00
|
|
|
void operator()(Block const& _block) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
class State
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum Value { Unused, Undecided, Used };
|
|
|
|
State(Value _value = Undecided): m_value(_value) {}
|
2018-10-29 14:12:02 +00:00
|
|
|
inline bool operator==(State _other) const { return m_value == _other.m_value; }
|
|
|
|
inline bool operator!=(State _other) const { return !operator==(_other); }
|
2018-10-29 14:12:31 +00:00
|
|
|
static inline void join(State& _a, State const& _b)
|
2018-10-23 13:55:48 +00:00
|
|
|
{
|
|
|
|
// Using "max" works here because of the order of the values in the enum.
|
2018-10-29 14:12:31 +00:00
|
|
|
_a.m_value = Value(std::max(int(_a.m_value), int(_b.m_value)));
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
Value m_value = Undecided;
|
|
|
|
};
|
|
|
|
|
2019-03-18 16:38:25 +00:00
|
|
|
// TODO check that this does not cause nondeterminism!
|
|
|
|
// This could also be a pseudo-map from state to assignment.
|
|
|
|
using TrackedAssignments = std::map<YulString, std::map<Assignment const*, State>>;
|
|
|
|
|
|
|
|
/// Joins the assignment mapping of @a _source into @a _target according to the rules laid out
|
2018-10-23 13:55:48 +00:00
|
|
|
/// above.
|
2019-03-18 16:38:25 +00:00
|
|
|
/// Will destroy @a _source.
|
|
|
|
static void merge(TrackedAssignments& _target, TrackedAssignments&& _source);
|
2019-03-18 16:43:28 +00:00
|
|
|
static void merge(TrackedAssignments& _target, std::vector<TrackedAssignments>&& _source);
|
2018-10-29 14:12:02 +00:00
|
|
|
void changeUndecidedTo(YulString _variable, State _newState);
|
2019-03-26 16:35:26 +00:00
|
|
|
/// Called when a variable goes out of scope. Sets the state of all still undecided
|
|
|
|
/// assignments to the final state. In this case, this also applies to pending
|
|
|
|
/// break and continue TrackedAssignments.
|
|
|
|
void finalize(YulString _variable, State _finalState);
|
|
|
|
/// Helper function for the above.
|
|
|
|
void finalize(TrackedAssignments& _assignments, YulString _variable, State _finalState);
|
2018-10-23 13:55:48 +00:00
|
|
|
|
2018-12-20 17:55:32 +00:00
|
|
|
Dialect const* m_dialect;
|
2018-10-29 14:12:02 +00:00
|
|
|
std::set<YulString> m_declaredVariables;
|
2019-10-28 14:25:02 +00:00
|
|
|
std::set<YulString> m_returnVariables;
|
2019-03-18 16:38:25 +00:00
|
|
|
std::set<Assignment const*> m_pendingRemovals;
|
|
|
|
TrackedAssignments m_assignments;
|
2019-03-18 16:43:28 +00:00
|
|
|
|
|
|
|
/// Working data for traversing for-loops.
|
|
|
|
struct ForLoopInfo
|
|
|
|
{
|
|
|
|
/// Tracked assignment states for each break statement.
|
|
|
|
std::vector<TrackedAssignments> pendingBreakStmts;
|
|
|
|
/// Tracked assignment states for each continue statement.
|
|
|
|
std::vector<TrackedAssignments> pendingContinueStmts;
|
|
|
|
};
|
|
|
|
ForLoopInfo m_forLoopInfo;
|
2019-04-23 07:49:16 +00:00
|
|
|
size_t m_forLoopNestingDepth = 0;
|
2018-10-23 13:55:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class AssignmentRemover: public ASTModifier
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit AssignmentRemover(std::set<Assignment const*> const& _toRemove):
|
|
|
|
m_toRemove(_toRemove)
|
|
|
|
{}
|
|
|
|
void operator()(Block& _block) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::set<Assignment const*> const& m_toRemove;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|