|
| 1 | +/* |
| 2 | + This file is part of solidity. |
| 3 | +
|
| 4 | + solidity is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation, either version 3 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | + solidity is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU General Public License |
| 15 | + along with solidity. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +*/ |
| 17 | +// SPDX-License-Identifier: GPL-3.0 |
| 18 | + |
| 19 | +#include <libevmasm/TrivialBlockRemover.h> |
| 20 | + |
| 21 | +#include <libevmasm/AssemblyItem.h> |
| 22 | +#include <libevmasm/BlockDeduplicator.h> |
| 23 | +#include <libevmasm/SemanticInformation.h> |
| 24 | + |
| 25 | +#include <range/v3/view/drop.hpp> |
| 26 | +#include <range/v3/view/enumerate.hpp> |
| 27 | + |
| 28 | +bool solidity::evmasm::TrivialBlockRemover::optimise(std::set<size_t> const& _tagsReferencedFromOutside) |
| 29 | +{ |
| 30 | + std::map<u256, u256> replacedTags; |
| 31 | + for (auto&& [index, item]: m_items | ranges::views::enumerate | ranges::views::drop(1)) |
| 32 | + { |
| 33 | + if (item.type() != Tag) |
| 34 | + continue; |
| 35 | + if (index >= m_items.size() - 2) |
| 36 | + continue; |
| 37 | + auto const & next = m_items[index + 1]; |
| 38 | + if (next.type() != PushTag) |
| 39 | + continue; |
| 40 | + if (next.data() == item.data()) |
| 41 | + continue; |
| 42 | + auto const & nextNext = m_items[index + 2]; |
| 43 | + if (nextNext.type() != Operation) |
| 44 | + continue; |
| 45 | + if (nextNext.instruction() != Instruction::JUMP) |
| 46 | + continue; |
| 47 | + auto const & previous = m_items[index - 1]; |
| 48 | + if (previous.type() != Operation) |
| 49 | + continue; |
| 50 | + if (previous.instruction() != Instruction::JUMP && !SemanticInformation::terminatesControlFlow(previous)) |
| 51 | + continue; |
| 52 | + auto const [subId, tag] = item.splitForeignPushTag(); |
| 53 | + solAssert(subId.empty(), "Sub-assembly tag used as label."); |
| 54 | + if (_tagsReferencedFromOutside.contains(tag)) |
| 55 | + continue; |
| 56 | + replacedTags.insert({item.data(), next.data()}); |
| 57 | + } |
| 58 | + if (!replacedTags.empty()) |
| 59 | + { |
| 60 | + return BlockDeduplicator::applyTagReplacement(m_items, replacedTags); |
| 61 | + } |
| 62 | + return false; |
| 63 | +} |
0 commit comments