From 18efbb52c0e22a6bac4427053dbda66d38319cbf Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 18 Dec 2018 13:09:31 +0100 Subject: [PATCH 1/3] Use the move. --- libevmasm/AssemblyItem.h | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/libevmasm/AssemblyItem.h b/libevmasm/AssemblyItem.h index a78751719..e17179fa8 100644 --- a/libevmasm/AssemblyItem.h +++ b/libevmasm/AssemblyItem.h @@ -57,22 +57,26 @@ class AssemblyItem public: enum class JumpType { Ordinary, IntoFunction, OutOfFunction }; - AssemblyItem(u256 _push, langutil::SourceLocation const& _location = langutil::SourceLocation()): - AssemblyItem(Push, _push, _location) { } - AssemblyItem(solidity::Instruction _i, langutil::SourceLocation const& _location = langutil::SourceLocation()): + AssemblyItem(u256 _push, langutil::SourceLocation _location = langutil::SourceLocation()): + AssemblyItem(Push, std::move(_push), std::move(_location)) { } + AssemblyItem(solidity::Instruction _i, langutil::SourceLocation _location = langutil::SourceLocation()): m_type(Operation), m_instruction(_i), - m_location(_location) + m_location(std::move(_location)) {} - AssemblyItem(AssemblyItemType _type, u256 _data = 0, langutil::SourceLocation const& _location = langutil::SourceLocation()): + AssemblyItem(AssemblyItemType _type, u256 _data = 0, langutil::SourceLocation _location = langutil::SourceLocation()): m_type(_type), - m_location(_location) + m_location(std::move(_location)) { if (m_type == Operation) m_instruction = Instruction(uint8_t(_data)); else - m_data = std::make_shared(_data); + m_data = std::make_shared(std::move(_data)); } + AssemblyItem(AssemblyItem const&) = default; + AssemblyItem(AssemblyItem&&) = default; + AssemblyItem& operator=(AssemblyItem const&) = default; + AssemblyItem& operator=(AssemblyItem&&) = default; AssemblyItem tag() const { assertThrow(m_type == PushTag || m_type == Tag, Exception, ""); return AssemblyItem(Tag, data()); } AssemblyItem pushTag() const { assertThrow(m_type == PushTag || m_type == Tag, Exception, ""); return AssemblyItem(PushTag, data()); } From 01249984f2b5cbabc9fe3d4fd98917c606f77931 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 18 Dec 2018 13:14:12 +0100 Subject: [PATCH 2/3] Provide comparison shortcut. --- libevmasm/AssemblyItem.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libevmasm/AssemblyItem.h b/libevmasm/AssemblyItem.h index e17179fa8..d21be1991 100644 --- a/libevmasm/AssemblyItem.h +++ b/libevmasm/AssemblyItem.h @@ -118,6 +118,13 @@ public: return data() < _other.data(); } + /// Shortcut that avoids constructing an AssemblyItem just to perform the comparison. + bool operator==(Instruction _instr) const + { + return type() == Operation && instruction() == _instr; + } + bool operator!=(Instruction _instr) const { return !operator==(_instr); } + /// @returns an upper bound for the number of bytes required by this item, assuming that /// the value of a jump tag takes @a _addressLength bytes. unsigned bytesRequired(unsigned _addressLength) const; From 27e4e25a99d32be439ad1db9516b1f9f7d80cd99 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 18 Dec 2018 16:09:51 +0100 Subject: [PATCH 3/3] Optimize some instruction comparisons. --- libevmasm/PeepholeOptimiser.cpp | 6 ++---- libevmasm/SemanticInformation.cpp | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/libevmasm/PeepholeOptimiser.cpp b/libevmasm/PeepholeOptimiser.cpp index f3ae8cbf1..e211026b2 100644 --- a/libevmasm/PeepholeOptimiser.cpp +++ b/libevmasm/PeepholeOptimiser.cpp @@ -160,8 +160,7 @@ struct CommutativeSwap: SimplePeepholeOptimizerMethod { // Remove SWAP1 if following instruction is commutative if ( - _swap.type() == Operation && - _swap.instruction() == Instruction::SWAP1 && + _swap == Instruction::SWAP1 && SemanticInformation::isCommutativeOperation(_op) ) { @@ -185,8 +184,7 @@ struct SwapComparison: SimplePeepholeOptimizerMethod }; if ( - _swap.type() == Operation && - _swap.instruction() == Instruction::SWAP1 && + _swap == Instruction::SWAP1 && _op.type() == Operation && swappableOps.count(_op.instruction()) ) diff --git a/libevmasm/SemanticInformation.cpp b/libevmasm/SemanticInformation.cpp index 78f3c9c7b..2a24a27ea 100644 --- a/libevmasm/SemanticInformation.cpp +++ b/libevmasm/SemanticInformation.cpp @@ -108,7 +108,7 @@ bool SemanticInformation::isSwapInstruction(AssemblyItem const& _item) bool SemanticInformation::isJumpInstruction(AssemblyItem const& _item) { - return _item == AssemblyItem(Instruction::JUMP) || _item == AssemblyItem(Instruction::JUMPI); + return _item == Instruction::JUMP || _item == Instruction::JUMPI; } bool SemanticInformation::altersControlFlow(AssemblyItem const& _item)