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 * Optimiser component that combines syntactically equivalent functions. 20 */ 21 #pragma once 22 23 #include <libyul/optimiser/ASTWalker.h> 24 #include <libyul/optimiser/BlockHasher.h> 25 #include <libyul/ASTForward.h> 26 27 namespace solidity::yul 28 { 29 30 /** 31 * Optimiser component that detects syntactically equivalent functions. 32 * 33 * Prerequisite: Disambiguator, ForLoopInitRewriter 34 */ 35 class EquivalentFunctionDetector: public ASTWalker 36 { 37 public: run(Block & _block)38 static std::map<YulString, FunctionDefinition const*> run(Block& _block) 39 { 40 EquivalentFunctionDetector detector{BlockHasher::run(_block)}; 41 detector(_block); 42 return std::move(detector.m_duplicates); 43 } 44 45 using ASTWalker::operator(); 46 void operator()(FunctionDefinition const& _fun) override; 47 48 private: EquivalentFunctionDetector(std::map<Block const *,uint64_t> _blockHashes)49 EquivalentFunctionDetector(std::map<Block const*, uint64_t> _blockHashes): m_blockHashes(std::move(_blockHashes)) {} 50 51 std::map<Block const*, uint64_t> m_blockHashes; 52 std::map<uint64_t, std::vector<FunctionDefinition const*>> m_candidates; 53 std::map<YulString, FunctionDefinition const*> m_duplicates; 54 }; 55 56 57 } 58