2015-04-24 15:35:16 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-04-24 15:35:16 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2015-04-24 15:35:16 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2015-04-24 15:35:16 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2015-04-24 15:35:16 +00:00
|
|
|
/**
|
|
|
|
* @file ControlFlowGraph.h
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Control flow analysis for the optimizer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <vector>
|
2015-05-04 08:15:41 +00:00
|
|
|
#include <memory>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Common.h>
|
|
|
|
#include <libsolutil/Assertions.h>
|
2015-05-04 08:15:41 +00:00
|
|
|
#include <libevmasm/ExpressionClasses.h>
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::evmasm
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
|
|
|
|
2015-05-04 08:15:41 +00:00
|
|
|
class KnownState;
|
2015-05-05 15:45:58 +00:00
|
|
|
using KnownStatePointer = std::shared_ptr<KnownState>;
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Identifier for a block, coincides with the tag number of an AssemblyItem but adds a special
|
2018-07-10 07:18:59 +00:00
|
|
|
* ID for the initial block.
|
2015-04-24 15:35:16 +00:00
|
|
|
*/
|
|
|
|
class BlockId
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BlockId() { *this = invalid(); }
|
|
|
|
explicit BlockId(unsigned _id): m_id(_id) {}
|
|
|
|
explicit BlockId(u256 const& _id);
|
2020-06-02 13:10:14 +00:00
|
|
|
static BlockId initial() { return BlockId(std::numeric_limits<unsigned>::max() - 1); }
|
|
|
|
static BlockId invalid() { return BlockId(std::numeric_limits<unsigned>::max()); }
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
bool operator==(BlockId const& _other) const { return m_id == _other.m_id; }
|
|
|
|
bool operator!=(BlockId const& _other) const { return m_id != _other.m_id; }
|
|
|
|
bool operator<(BlockId const& _other) const { return m_id < _other.m_id; }
|
|
|
|
explicit operator bool() const { return *this != invalid(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned m_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Control flow block inside which instruction counter is always incremented by one
|
|
|
|
* (except for possibly the last instruction).
|
|
|
|
*/
|
|
|
|
struct BasicBlock
|
|
|
|
{
|
|
|
|
/// Start index into assembly item list.
|
|
|
|
unsigned begin = 0;
|
|
|
|
/// End index (excluded) inte assembly item list.
|
|
|
|
unsigned end = 0;
|
|
|
|
/// Tags pushed inside this block, with multiplicity.
|
|
|
|
std::vector<BlockId> pushedTags;
|
2015-05-04 08:15:41 +00:00
|
|
|
/// ID of the block that always follows this one (either non-branching part of JUMPI or flow
|
|
|
|
/// into new block), or BlockId::invalid() otherwise
|
2015-04-24 15:35:16 +00:00
|
|
|
BlockId next = BlockId::invalid();
|
2015-05-04 08:15:41 +00:00
|
|
|
/// ID of the block that has to precede this one (because control flows into it).
|
2015-04-24 15:35:16 +00:00
|
|
|
BlockId prev = BlockId::invalid();
|
|
|
|
|
|
|
|
enum class EndType { JUMP, JUMPI, STOP, HANDOVER };
|
|
|
|
EndType endType = EndType::HANDOVER;
|
2015-05-04 08:15:41 +00:00
|
|
|
|
|
|
|
/// Knowledge about the state when this block is entered. Intersection of all possible ways
|
|
|
|
/// to enter this block.
|
2015-05-05 15:45:58 +00:00
|
|
|
KnownStatePointer startState;
|
2015-05-04 08:15:41 +00:00
|
|
|
/// Knowledge about the state at the end of this block.
|
2015-05-05 15:45:58 +00:00
|
|
|
KnownStatePointer endState;
|
2015-04-24 15:35:16 +00:00
|
|
|
};
|
|
|
|
|
2015-05-05 15:45:58 +00:00
|
|
|
using BasicBlocks = std::vector<BasicBlock>;
|
|
|
|
|
2016-11-10 17:16:21 +00:00
|
|
|
/**
|
|
|
|
* Control flow graph optimizer.
|
|
|
|
* ASSUMES THAT WE ONLY JUMP TO TAGS THAT WERE PREVIOUSLY PUSHED. THIS IS NOT TRUE ANYMORE
|
|
|
|
* NOW THAT FUNCTION TAGS CAN BE STORED IN STORAGE.
|
|
|
|
*/
|
2015-04-24 15:35:16 +00:00
|
|
|
class ControlFlowGraph
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// Initializes the control flow graph.
|
|
|
|
/// @a _items has to persist across the usage of this class.
|
2016-07-29 09:47:26 +00:00
|
|
|
/// @a _joinKnowledge if true, reduces state knowledge to common base at the join of two paths
|
|
|
|
explicit ControlFlowGraph(AssemblyItems const& _items, bool _joinKnowledge = true):
|
|
|
|
m_items(_items),
|
|
|
|
m_joinKnowledge(_joinKnowledge)
|
|
|
|
{}
|
2015-05-05 15:45:58 +00:00
|
|
|
/// @returns vector of basic blocks in the order they should be used in the final code.
|
|
|
|
/// Should be called only once.
|
|
|
|
BasicBlocks optimisedBlocks();
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void findLargestTag();
|
|
|
|
void splitBlocks();
|
|
|
|
void resolveNextLinks();
|
|
|
|
void removeUnusedBlocks();
|
2015-05-04 08:15:41 +00:00
|
|
|
void gatherKnowledge();
|
2015-04-24 15:35:16 +00:00
|
|
|
void setPrevLinks();
|
2015-05-05 15:45:58 +00:00
|
|
|
BasicBlocks rebuildCode();
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
BlockId generateNewId();
|
|
|
|
|
|
|
|
unsigned m_lastUsedId = 0;
|
|
|
|
AssemblyItems const& m_items;
|
2016-08-01 21:13:56 +00:00
|
|
|
bool m_joinKnowledge = true;
|
2015-04-24 15:35:16 +00:00
|
|
|
std::map<BlockId, BasicBlock> m_blocks;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|