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.cpp
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Control flow analysis for the optimizer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libevmasm/ControlFlowGraph.h>
|
|
|
|
#include <map>
|
2015-05-04 08:15:41 +00:00
|
|
|
#include <memory>
|
2015-05-12 19:27:04 +00:00
|
|
|
#include <algorithm>
|
2015-04-24 15:35:16 +00:00
|
|
|
#include <libevmasm/Exceptions.h>
|
|
|
|
#include <libevmasm/AssemblyItem.h>
|
|
|
|
#include <libevmasm/SemanticInformation.h>
|
2015-05-04 08:15:41 +00:00
|
|
|
#include <libevmasm/KnownState.h>
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::evmasm;
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2015-11-16 10:48:38 +00:00
|
|
|
BlockId::BlockId(u256 const& _id):
|
|
|
|
m_id(unsigned(_id))
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
|
|
|
assertThrow( _id < initial().m_id, OptimizerException, "Tag number too large.");
|
|
|
|
}
|
|
|
|
|
2015-05-05 15:45:58 +00:00
|
|
|
BasicBlocks ControlFlowGraph::optimisedBlocks()
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
|
|
|
if (m_items.empty())
|
2015-05-05 15:45:58 +00:00
|
|
|
return BasicBlocks();
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
findLargestTag();
|
|
|
|
splitBlocks();
|
|
|
|
resolveNextLinks();
|
|
|
|
removeUnusedBlocks();
|
|
|
|
setPrevLinks();
|
2015-05-04 08:15:41 +00:00
|
|
|
gatherKnowledge();
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
return rebuildCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowGraph::findLargestTag()
|
|
|
|
{
|
|
|
|
m_lastUsedId = 0;
|
|
|
|
for (auto const& item: m_items)
|
|
|
|
if (item.type() == Tag || item.type() == PushTag)
|
|
|
|
{
|
|
|
|
// Assert that it can be converted.
|
|
|
|
BlockId(item.data());
|
|
|
|
m_lastUsedId = max(unsigned(item.data()), m_lastUsedId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowGraph::splitBlocks()
|
|
|
|
{
|
|
|
|
m_blocks.clear();
|
|
|
|
BlockId id = BlockId::initial();
|
|
|
|
m_blocks[id].begin = 0;
|
|
|
|
for (size_t index = 0; index < m_items.size(); ++index)
|
|
|
|
{
|
|
|
|
AssemblyItem const& item = m_items.at(index);
|
|
|
|
if (item.type() == Tag)
|
|
|
|
{
|
|
|
|
if (id)
|
2019-12-12 23:39:29 +00:00
|
|
|
m_blocks[id].end = static_cast<unsigned>(index);
|
2015-04-24 15:35:16 +00:00
|
|
|
id = BlockId::invalid();
|
|
|
|
}
|
|
|
|
if (!id)
|
|
|
|
{
|
|
|
|
id = item.type() == Tag ? BlockId(item.data()) : generateNewId();
|
2019-12-12 23:39:29 +00:00
|
|
|
m_blocks[id].begin = static_cast<unsigned>(index);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
if (item.type() == PushTag)
|
2018-12-10 18:02:39 +00:00
|
|
|
m_blocks[id].pushedTags.emplace_back(item.data());
|
2015-04-24 15:35:16 +00:00
|
|
|
if (SemanticInformation::altersControlFlow(item))
|
|
|
|
{
|
2019-12-12 23:39:29 +00:00
|
|
|
m_blocks[id].end = static_cast<unsigned>(index + 1);
|
2015-04-24 15:35:16 +00:00
|
|
|
if (item == Instruction::JUMP)
|
|
|
|
m_blocks[id].endType = BasicBlock::EndType::JUMP;
|
|
|
|
else if (item == Instruction::JUMPI)
|
|
|
|
m_blocks[id].endType = BasicBlock::EndType::JUMPI;
|
|
|
|
else
|
|
|
|
m_blocks[id].endType = BasicBlock::EndType::STOP;
|
|
|
|
id = BlockId::invalid();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (id)
|
|
|
|
{
|
2019-12-12 23:39:29 +00:00
|
|
|
m_blocks[id].end = static_cast<unsigned>(m_items.size());
|
2015-04-24 15:35:16 +00:00
|
|
|
if (m_blocks[id].endType == BasicBlock::EndType::HANDOVER)
|
|
|
|
m_blocks[id].endType = BasicBlock::EndType::STOP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowGraph::resolveNextLinks()
|
|
|
|
{
|
|
|
|
map<unsigned, BlockId> blockByBeginPos;
|
|
|
|
for (auto const& idAndBlock: m_blocks)
|
|
|
|
if (idAndBlock.second.begin != idAndBlock.second.end)
|
|
|
|
blockByBeginPos[idAndBlock.second.begin] = idAndBlock.first;
|
|
|
|
|
|
|
|
for (auto& idAndBlock: m_blocks)
|
|
|
|
{
|
|
|
|
BasicBlock& block = idAndBlock.second;
|
|
|
|
switch (block.endType)
|
|
|
|
{
|
|
|
|
case BasicBlock::EndType::JUMPI:
|
|
|
|
case BasicBlock::EndType::HANDOVER:
|
|
|
|
assertThrow(
|
|
|
|
blockByBeginPos.count(block.end),
|
|
|
|
OptimizerException,
|
|
|
|
"Successor block not found."
|
|
|
|
);
|
|
|
|
block.next = blockByBeginPos.at(block.end);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowGraph::removeUnusedBlocks()
|
|
|
|
{
|
|
|
|
vector<BlockId> blocksToProcess{BlockId::initial()};
|
|
|
|
set<BlockId> neededBlocks{BlockId::initial()};
|
|
|
|
while (!blocksToProcess.empty())
|
|
|
|
{
|
|
|
|
BasicBlock const& block = m_blocks.at(blocksToProcess.back());
|
|
|
|
blocksToProcess.pop_back();
|
|
|
|
for (BlockId tag: block.pushedTags)
|
2015-05-06 17:15:14 +00:00
|
|
|
if (!neededBlocks.count(tag) && m_blocks.count(tag))
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
|
|
|
neededBlocks.insert(tag);
|
|
|
|
blocksToProcess.push_back(tag);
|
|
|
|
}
|
|
|
|
if (block.next && !neededBlocks.count(block.next))
|
|
|
|
{
|
|
|
|
neededBlocks.insert(block.next);
|
|
|
|
blocksToProcess.push_back(block.next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto it = m_blocks.begin(); it != m_blocks.end();)
|
|
|
|
if (neededBlocks.count(it->first))
|
|
|
|
++it;
|
|
|
|
else
|
|
|
|
m_blocks.erase(it++);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowGraph::setPrevLinks()
|
|
|
|
{
|
|
|
|
for (auto& idAndBlock: m_blocks)
|
|
|
|
{
|
|
|
|
BasicBlock& block = idAndBlock.second;
|
|
|
|
switch (block.endType)
|
|
|
|
{
|
|
|
|
case BasicBlock::EndType::JUMPI:
|
|
|
|
case BasicBlock::EndType::HANDOVER:
|
|
|
|
assertThrow(
|
|
|
|
!m_blocks.at(block.next).prev,
|
|
|
|
OptimizerException,
|
|
|
|
"Successor already has predecessor."
|
|
|
|
);
|
|
|
|
m_blocks[block.next].prev = idAndBlock.first;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If block ends with jump to not yet linked block, link them removing the jump
|
|
|
|
for (auto& idAndBlock: m_blocks)
|
|
|
|
{
|
|
|
|
BlockId blockId = idAndBlock.first;
|
|
|
|
BasicBlock& block = idAndBlock.second;
|
|
|
|
if (block.endType != BasicBlock::EndType::JUMP || block.end - block.begin < 2)
|
|
|
|
continue;
|
|
|
|
AssemblyItem const& push = m_items.at(block.end - 2);
|
|
|
|
if (push.type() != PushTag)
|
|
|
|
continue;
|
|
|
|
BlockId nextId(push.data());
|
2015-05-06 17:15:14 +00:00
|
|
|
if (m_blocks.count(nextId) && m_blocks.at(nextId).prev)
|
2015-04-24 15:35:16 +00:00
|
|
|
continue;
|
|
|
|
bool hasLoop = false;
|
2015-05-06 17:15:14 +00:00
|
|
|
for (BlockId id = nextId; id && m_blocks.count(id) && !hasLoop; id = m_blocks.at(id).next)
|
2015-04-24 15:35:16 +00:00
|
|
|
hasLoop = (id == blockId);
|
2015-05-06 17:15:14 +00:00
|
|
|
if (hasLoop || !m_blocks.count(nextId))
|
2015-04-24 15:35:16 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
m_blocks[nextId].prev = blockId;
|
|
|
|
block.next = nextId;
|
|
|
|
block.end -= 2;
|
|
|
|
assertThrow(
|
|
|
|
!block.pushedTags.empty() && block.pushedTags.back() == nextId,
|
|
|
|
OptimizerException,
|
|
|
|
"Last pushed tag not at end of pushed list."
|
|
|
|
);
|
|
|
|
block.pushedTags.pop_back();
|
|
|
|
block.endType = BasicBlock::EndType::HANDOVER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 08:15:41 +00:00
|
|
|
void ControlFlowGraph::gatherKnowledge()
|
|
|
|
{
|
|
|
|
// @todo actually we know that memory is filled with zeros at the beginning,
|
|
|
|
// we could make use of that.
|
2015-05-05 15:45:58 +00:00
|
|
|
KnownStatePointer emptyState = make_shared<KnownState>();
|
2015-05-04 08:15:41 +00:00
|
|
|
bool unknownJumpEncountered = false;
|
|
|
|
|
2016-01-15 15:26:12 +00:00
|
|
|
struct WorkQueueItem {
|
|
|
|
BlockId blockId;
|
|
|
|
KnownStatePointer state;
|
|
|
|
set<BlockId> blocksSeen;
|
|
|
|
};
|
|
|
|
|
|
|
|
vector<WorkQueueItem> workQueue{WorkQueueItem{BlockId::initial(), emptyState->copy(), set<BlockId>()}};
|
|
|
|
auto addWorkQueueItem = [&](WorkQueueItem const& _currentItem, BlockId _to, KnownStatePointer const& _state)
|
|
|
|
{
|
|
|
|
WorkQueueItem item;
|
|
|
|
item.blockId = _to;
|
|
|
|
item.state = _state->copy();
|
|
|
|
item.blocksSeen = _currentItem.blocksSeen;
|
|
|
|
item.blocksSeen.insert(_currentItem.blockId);
|
2022-08-23 17:28:45 +00:00
|
|
|
workQueue.push_back(std::move(item));
|
2016-01-15 15:26:12 +00:00
|
|
|
};
|
|
|
|
|
2015-05-04 08:15:41 +00:00
|
|
|
while (!workQueue.empty())
|
|
|
|
{
|
2022-08-23 17:28:45 +00:00
|
|
|
WorkQueueItem item = std::move(workQueue.back());
|
2016-01-15 15:26:12 +00:00
|
|
|
workQueue.pop_back();
|
2015-05-04 08:15:41 +00:00
|
|
|
//@todo we might have to do something like incrementing the sequence number for each JUMPDEST
|
2016-01-15 15:26:12 +00:00
|
|
|
assertThrow(!!item.blockId, OptimizerException, "");
|
|
|
|
if (!m_blocks.count(item.blockId))
|
2015-05-06 17:15:14 +00:00
|
|
|
continue; // too bad, we do not know the tag, probably an invalid jump
|
2016-01-15 15:26:12 +00:00
|
|
|
BasicBlock& block = m_blocks.at(item.blockId);
|
|
|
|
KnownStatePointer state = item.state;
|
2015-05-04 08:15:41 +00:00
|
|
|
if (block.startState)
|
|
|
|
{
|
2016-08-01 21:13:56 +00:00
|
|
|
// We call reduceToCommonKnowledge even in the non-join setting to get the correct
|
|
|
|
// sequence number
|
|
|
|
if (!m_joinKnowledge)
|
2016-07-29 09:47:26 +00:00
|
|
|
state->reset();
|
2016-08-01 21:13:56 +00:00
|
|
|
state->reduceToCommonKnowledge(*block.startState, !item.blocksSeen.count(item.blockId));
|
2015-05-04 08:15:41 +00:00
|
|
|
if (*state == *block.startState)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
block.startState = state->copy();
|
|
|
|
|
|
|
|
// Feed all items except for the final jump yet because it will erase the target tag.
|
|
|
|
unsigned pc = block.begin;
|
|
|
|
while (pc < block.end && !SemanticInformation::altersControlFlow(m_items.at(pc)))
|
|
|
|
state->feedItem(m_items.at(pc++));
|
|
|
|
|
|
|
|
if (
|
|
|
|
block.endType == BasicBlock::EndType::JUMP ||
|
|
|
|
block.endType == BasicBlock::EndType::JUMPI
|
|
|
|
)
|
|
|
|
{
|
|
|
|
assertThrow(block.begin <= pc && pc == block.end - 1, OptimizerException, "");
|
|
|
|
//@todo in the case of JUMPI, add knowledge about the condition to the state
|
|
|
|
// (for both values of the condition)
|
2015-05-12 19:27:04 +00:00
|
|
|
set<u256> tags = state->tagsInExpression(
|
2018-11-14 16:11:55 +00:00
|
|
|
state->stackElement(state->stackHeight(), langutil::SourceLocation{})
|
2015-05-04 08:15:41 +00:00
|
|
|
);
|
|
|
|
state->feedItem(m_items.at(pc++));
|
2015-05-12 19:27:04 +00:00
|
|
|
|
2015-05-15 10:23:13 +00:00
|
|
|
if (tags.empty())
|
2015-05-04 08:15:41 +00:00
|
|
|
{
|
2015-05-12 19:27:04 +00:00
|
|
|
if (!unknownJumpEncountered)
|
|
|
|
{
|
|
|
|
// We do not know the target of this jump, so we have to reset the states of all
|
|
|
|
// JUMPDESTs.
|
|
|
|
unknownJumpEncountered = true;
|
|
|
|
for (auto const& it: m_blocks)
|
|
|
|
if (it.second.begin < it.second.end && m_items[it.second.begin].type() == Tag)
|
2016-01-18 09:57:03 +00:00
|
|
|
workQueue.push_back(WorkQueueItem{it.first, emptyState->copy(), set<BlockId>()});
|
2015-05-12 19:27:04 +00:00
|
|
|
}
|
2015-05-04 08:15:41 +00:00
|
|
|
}
|
2015-05-12 19:27:04 +00:00
|
|
|
else
|
|
|
|
for (auto tag: tags)
|
2016-01-15 15:26:12 +00:00
|
|
|
addWorkQueueItem(item, BlockId(tag), state);
|
2015-05-04 08:15:41 +00:00
|
|
|
}
|
|
|
|
else if (block.begin <= pc && pc < block.end)
|
|
|
|
state->feedItem(m_items.at(pc++));
|
|
|
|
assertThrow(block.end <= block.begin || pc == block.end, OptimizerException, "");
|
|
|
|
|
|
|
|
block.endState = state;
|
|
|
|
|
|
|
|
if (
|
|
|
|
block.endType == BasicBlock::EndType::HANDOVER ||
|
|
|
|
block.endType == BasicBlock::EndType::JUMPI
|
|
|
|
)
|
2016-01-15 15:26:12 +00:00
|
|
|
addWorkQueueItem(item, block.next, state);
|
2015-05-04 08:15:41 +00:00
|
|
|
}
|
2015-05-06 17:15:14 +00:00
|
|
|
|
|
|
|
// Remove all blocks we never visited here. This might happen because a tag is pushed but
|
|
|
|
// never used for a JUMP.
|
|
|
|
// Note that this invalidates some contents of pushedTags
|
|
|
|
for (auto it = m_blocks.begin(); it != m_blocks.end();)
|
|
|
|
if (!it->second.startState)
|
2015-05-11 17:44:45 +00:00
|
|
|
it = m_blocks.erase(it);
|
2015-05-06 17:15:14 +00:00
|
|
|
else
|
|
|
|
it++;
|
2015-05-04 08:15:41 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 15:45:58 +00:00
|
|
|
BasicBlocks ControlFlowGraph::rebuildCode()
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
|
|
|
map<BlockId, unsigned> pushes;
|
|
|
|
for (auto& idAndBlock: m_blocks)
|
|
|
|
for (BlockId ref: idAndBlock.second.pushedTags)
|
2015-05-06 17:15:14 +00:00
|
|
|
if (m_blocks.count(ref))
|
|
|
|
pushes[ref]++;
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
set<BlockId> blocksToAdd;
|
|
|
|
for (auto it: m_blocks)
|
|
|
|
blocksToAdd.insert(it.first);
|
|
|
|
set<BlockId> blocksAdded;
|
2015-05-05 15:45:58 +00:00
|
|
|
BasicBlocks blocks;
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
for (
|
|
|
|
BlockId blockId = BlockId::initial();
|
|
|
|
blockId;
|
|
|
|
blockId = blocksToAdd.empty() ? BlockId::invalid() : *blocksToAdd.begin()
|
|
|
|
)
|
|
|
|
{
|
|
|
|
bool previousHandedOver = (blockId == BlockId::initial());
|
|
|
|
while (m_blocks.at(blockId).prev)
|
|
|
|
blockId = m_blocks.at(blockId).prev;
|
|
|
|
for (; blockId; blockId = m_blocks.at(blockId).next)
|
|
|
|
{
|
2015-05-04 08:15:41 +00:00
|
|
|
BasicBlock& block = m_blocks.at(blockId);
|
2015-04-24 15:35:16 +00:00
|
|
|
blocksToAdd.erase(blockId);
|
|
|
|
blocksAdded.insert(blockId);
|
|
|
|
|
2015-05-05 15:45:58 +00:00
|
|
|
if (block.begin == block.end)
|
2015-04-24 15:35:16 +00:00
|
|
|
continue;
|
|
|
|
// If block starts with unused tag, skip it.
|
2015-05-05 15:45:58 +00:00
|
|
|
if (previousHandedOver && !pushes[blockId] && m_items[block.begin].type() == Tag)
|
2015-05-04 08:15:41 +00:00
|
|
|
++block.begin;
|
2015-05-05 15:45:58 +00:00
|
|
|
if (block.begin < block.end)
|
2015-05-12 19:27:04 +00:00
|
|
|
{
|
2015-05-05 15:45:58 +00:00
|
|
|
blocks.push_back(block);
|
2015-05-12 19:27:04 +00:00
|
|
|
blocks.back().startState->clearTagUnions();
|
|
|
|
blocks.back().endState->clearTagUnions();
|
|
|
|
}
|
2015-04-24 15:35:16 +00:00
|
|
|
previousHandedOver = (block.endType == BasicBlock::EndType::HANDOVER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 15:45:58 +00:00
|
|
|
return blocks;
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockId ControlFlowGraph::generateNewId()
|
|
|
|
{
|
|
|
|
BlockId id = BlockId(++m_lastUsedId);
|
|
|
|
assertThrow(id < BlockId::initial(), OptimizerException, "Out of block IDs.");
|
|
|
|
return id;
|
|
|
|
}
|