2018-02-02 14:23:44 +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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2018-02-02 14:23:44 +00:00
|
|
|
/**
|
|
|
|
* Base class to perform data flow analysis during AST walks.
|
|
|
|
* Tracks assignments and is used as base class for both Rematerialiser and
|
|
|
|
* Common Subexpression Eliminator.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/ASTWalker.h>
|
2019-05-21 13:52:15 +00:00
|
|
|
#include <libyul/optimiser/KnowledgeBase.h>
|
2018-10-29 14:12:02 +00:00
|
|
|
#include <libyul/YulString.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h> // Needed for m_zero below.
|
2019-08-13 16:38:11 +00:00
|
|
|
#include <libyul/SideEffects.h>
|
2018-10-29 14:12:02 +00:00
|
|
|
|
2021-03-01 18:05:59 +00:00
|
|
|
#include <libsolutil/Common.h>
|
|
|
|
|
2018-02-02 14:23:44 +00:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::yul
|
2018-02-02 14:23:44 +00:00
|
|
|
{
|
2018-12-20 17:55:32 +00:00
|
|
|
struct Dialect;
|
2019-08-13 16:38:11 +00:00
|
|
|
struct SideEffects;
|
2018-02-02 14:23:44 +00:00
|
|
|
|
2019-11-28 13:22:17 +00:00
|
|
|
/// Value assigned to a variable.
|
|
|
|
struct AssignedValue
|
|
|
|
{
|
|
|
|
Expression const* value{nullptr};
|
|
|
|
/// Loop nesting depth of the definition of the variable.
|
|
|
|
size_t loopDepth{0};
|
|
|
|
};
|
|
|
|
|
2018-02-02 14:23:44 +00:00
|
|
|
/**
|
|
|
|
* Base class to perform data flow analysis during AST walks.
|
|
|
|
* Tracks assignments and is used as base class for both Rematerialiser and
|
|
|
|
* Common Subexpression Eliminator.
|
|
|
|
*
|
2018-12-11 23:49:36 +00:00
|
|
|
* A special zero constant expression is used for the default value of variables.
|
|
|
|
*
|
2019-05-21 13:52:15 +00:00
|
|
|
* The class also tracks contents in storage and memory. Both keys and values
|
|
|
|
* are names of variables. Whenever such a variable is re-assigned, the knowledge
|
|
|
|
* is cleared.
|
|
|
|
*
|
|
|
|
* For elementary statements, we check if it is an SSTORE(x, y) / MSTORE(x, y)
|
|
|
|
* If yes, visit the statement. Then record that fact and clear all storage slots t
|
|
|
|
* where we cannot prove x != t or y == m_storage[t] using the current values of the variables x and t.
|
|
|
|
* Otherwise, determine if the statement invalidates storage/memory. If yes, clear all knowledge
|
|
|
|
* about storage/memory before visiting the statement. Then visit the statement.
|
|
|
|
*
|
|
|
|
* For forward-joining control flow, storage/memory information from the branches is combined.
|
|
|
|
* If the keys or values are different or non-existent in one branch, the key is deleted.
|
|
|
|
* This works also for memory (where addresses overlap) because one branch is always an
|
|
|
|
* older version of the other and thus overlapping contents would have been deleted already
|
|
|
|
* at the point of assignment.
|
|
|
|
*
|
2019-10-28 14:25:02 +00:00
|
|
|
* The DataFlowAnalyzer currently does not deal with the ``leave`` statement. This is because
|
|
|
|
* it only matters at the end of a function body, which is a point in the code a derived class
|
|
|
|
* can not easily deal with.
|
|
|
|
*
|
2019-04-05 18:38:23 +00:00
|
|
|
* Prerequisite: Disambiguator, ForLoopInitRewriter.
|
2018-02-02 14:23:44 +00:00
|
|
|
*/
|
|
|
|
class DataFlowAnalyzer: public ASTModifier
|
|
|
|
{
|
|
|
|
public:
|
2019-08-13 16:38:11 +00:00
|
|
|
/// @param _functionSideEffects
|
|
|
|
/// Side-effects of user-defined functions. Worst-case side-effects are assumed
|
|
|
|
/// if this is not provided or the function is not found.
|
|
|
|
/// The parameter is mostly used to determine movability of expressions.
|
|
|
|
explicit DataFlowAnalyzer(
|
|
|
|
Dialect const& _dialect,
|
|
|
|
std::map<YulString, SideEffects> _functionSideEffects = {}
|
2020-12-03 17:48:17 +00:00
|
|
|
);
|
2018-12-20 17:55:32 +00:00
|
|
|
|
2018-02-02 14:23:44 +00:00
|
|
|
using ASTModifier::operator();
|
2019-05-21 13:52:15 +00:00
|
|
|
void operator()(ExpressionStatement& _statement) override;
|
2018-11-16 01:09:04 +00:00
|
|
|
void operator()(Assignment& _assignment) override;
|
|
|
|
void operator()(VariableDeclaration& _varDecl) override;
|
|
|
|
void operator()(If& _if) override;
|
|
|
|
void operator()(Switch& _switch) override;
|
|
|
|
void operator()(FunctionDefinition&) override;
|
|
|
|
void operator()(ForLoop&) override;
|
|
|
|
void operator()(Block& _block) override;
|
2018-02-02 14:23:44 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/// Registers the assignment.
|
2020-11-26 17:07:12 +00:00
|
|
|
void handleAssignment(std::set<YulString> const& _names, Expression* _value, bool _isDeclaration);
|
2018-02-02 14:23:44 +00:00
|
|
|
|
2018-10-18 12:45:11 +00:00
|
|
|
/// Creates a new inner scope.
|
|
|
|
void pushScope(bool _functionScope);
|
|
|
|
|
|
|
|
/// Removes the innermost scope and clears all variables in it.
|
|
|
|
void popScope();
|
|
|
|
|
2018-07-10 07:18:59 +00:00
|
|
|
/// Clears information about the values assigned to the given variables,
|
2018-02-02 14:23:44 +00:00
|
|
|
/// for example at points where control flow is merged.
|
2018-10-29 14:12:02 +00:00
|
|
|
void clearValues(std::set<YulString> _names);
|
2018-02-02 14:23:44 +00:00
|
|
|
|
2019-11-27 09:44:40 +00:00
|
|
|
void assignValue(YulString _variable, Expression const* _value);
|
|
|
|
|
2019-05-27 22:14:01 +00:00
|
|
|
/// Clears knowledge about storage or memory if they may be modified inside the block.
|
|
|
|
void clearKnowledgeIfInvalidated(Block const& _block);
|
|
|
|
|
|
|
|
/// Clears knowledge about storage or memory if they may be modified inside the expression.
|
|
|
|
void clearKnowledgeIfInvalidated(Expression const& _expression);
|
2019-05-21 13:52:15 +00:00
|
|
|
|
2019-05-27 22:14:01 +00:00
|
|
|
/// Joins knowledge about storage and memory with an older point in the control-flow.
|
|
|
|
/// This only works if the current state is a direct successor of the older point,
|
|
|
|
/// i.e. `_otherStorage` and `_otherMemory` cannot have additional changes.
|
|
|
|
void joinKnowledge(
|
2020-12-11 16:30:06 +00:00
|
|
|
std::unordered_map<YulString, YulString> const& _olderStorage,
|
|
|
|
std::unordered_map<YulString, YulString> const& _olderMemory
|
2019-05-27 22:14:01 +00:00
|
|
|
);
|
2019-05-21 13:52:15 +00:00
|
|
|
|
2019-05-27 22:14:01 +00:00
|
|
|
static void joinKnowledgeHelper(
|
2020-12-11 16:30:06 +00:00
|
|
|
std::unordered_map<YulString, YulString>& _thisData,
|
|
|
|
std::unordered_map<YulString, YulString> const& _olderData
|
2019-05-27 22:14:01 +00:00
|
|
|
);
|
2019-05-21 13:52:15 +00:00
|
|
|
|
2018-02-02 14:23:44 +00:00
|
|
|
/// Returns true iff the variable is in scope.
|
2018-10-29 14:12:02 +00:00
|
|
|
bool inScope(YulString _variableName) const;
|
2018-02-02 14:23:44 +00:00
|
|
|
|
2021-03-01 18:05:59 +00:00
|
|
|
/// Returns the literal value of the identifier, if it exists.
|
|
|
|
std::optional<u256> valueOfIdentifier(YulString const& _name);
|
|
|
|
|
2020-12-03 17:48:17 +00:00
|
|
|
enum class StoreLoadLocation {
|
|
|
|
Memory = 0,
|
|
|
|
Storage = 1,
|
|
|
|
Last = Storage
|
|
|
|
};
|
|
|
|
|
2020-06-29 17:32:32 +00:00
|
|
|
/// Checks if the statement is sstore(a, b) / mstore(a, b)
|
|
|
|
/// where a and b are variables and returns these variables in that case.
|
2019-10-28 10:39:30 +00:00
|
|
|
std::optional<std::pair<YulString, YulString>> isSimpleStore(
|
2020-12-03 17:48:17 +00:00
|
|
|
StoreLoadLocation _location,
|
2019-05-27 22:14:01 +00:00
|
|
|
ExpressionStatement const& _statement
|
|
|
|
) const;
|
2019-05-21 13:52:15 +00:00
|
|
|
|
2020-06-29 17:32:32 +00:00
|
|
|
/// Checks if the expression is sload(a) / mload(a)
|
|
|
|
/// where a is a variable and returns the variable in that case.
|
|
|
|
std::optional<YulString> isSimpleLoad(
|
2020-12-03 17:48:17 +00:00
|
|
|
StoreLoadLocation _location,
|
2020-06-29 17:32:32 +00:00
|
|
|
Expression const& _expression
|
|
|
|
) const;
|
|
|
|
|
2019-05-21 13:52:15 +00:00
|
|
|
Dialect const& m_dialect;
|
2019-08-13 16:38:11 +00:00
|
|
|
/// Side-effects of user-defined functions. Worst-case side-effects are assumed
|
|
|
|
/// if this is not provided or the function is not found.
|
|
|
|
std::map<YulString, SideEffects> m_functionSideEffects;
|
2019-05-21 13:52:15 +00:00
|
|
|
|
2018-02-02 14:23:44 +00:00
|
|
|
/// Current values of variables, always movable.
|
2019-11-28 13:22:17 +00:00
|
|
|
std::map<YulString, AssignedValue> m_value;
|
2020-12-01 23:02:44 +00:00
|
|
|
/// m_references[a].contains(b) <=> the current expression assigned to a references b
|
|
|
|
std::unordered_map<YulString, std::set<YulString>> m_references;
|
2018-02-05 17:02:32 +00:00
|
|
|
|
2020-12-11 16:30:06 +00:00
|
|
|
std::unordered_map<YulString, YulString> m_storage;
|
|
|
|
std::unordered_map<YulString, YulString> m_memory;
|
2019-05-21 13:52:15 +00:00
|
|
|
|
|
|
|
KnowledgeBase m_knowledgeBase;
|
|
|
|
|
2020-12-03 17:48:17 +00:00
|
|
|
YulString m_storeFunctionName[static_cast<unsigned>(StoreLoadLocation::Last) + 1];
|
|
|
|
YulString m_loadFunctionName[static_cast<unsigned>(StoreLoadLocation::Last) + 1];
|
|
|
|
|
2019-11-27 09:44:40 +00:00
|
|
|
/// Current nesting depth of loops.
|
|
|
|
size_t m_loopDepth{0};
|
|
|
|
|
2018-02-05 17:02:32 +00:00
|
|
|
struct Scope
|
|
|
|
{
|
|
|
|
explicit Scope(bool _isFunction): isFunction(_isFunction) {}
|
2018-10-29 14:12:02 +00:00
|
|
|
std::set<YulString> variables;
|
2018-02-05 17:02:32 +00:00
|
|
|
bool isFunction;
|
|
|
|
};
|
2019-04-24 12:03:09 +00:00
|
|
|
/// Special expression whose address will be used in m_value.
|
|
|
|
/// YulString does not need to be reset because DataFlowAnalyzer is short-lived.
|
|
|
|
Expression const m_zero{Literal{{}, LiteralKind::Number, YulString{"0"}, {}}};
|
2018-02-05 17:02:32 +00:00
|
|
|
/// List of scopes.
|
|
|
|
std::vector<Scope> m_variableScopes;
|
2018-02-02 14:23:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|