2016-11-11 13:11:07 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2016-11-11 13:11:07 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2016-11-11 13:11:07 +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,
|
2016-11-11 13:11:07 +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/>.
|
2016-11-11 13:11:07 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2016-11-11 13:11:07 +00:00
|
|
|
/**
|
2016-11-24 11:17:29 +00:00
|
|
|
* @file PeepholeOptimiser.cpp
|
2016-11-11 13:11:07 +00:00
|
|
|
* Performs local optimising code changes to assembly.
|
|
|
|
*/
|
|
|
|
|
2019-03-28 12:07:52 +00:00
|
|
|
#include <libevmasm/PeepholeOptimiser.h>
|
2016-11-11 13:11:07 +00:00
|
|
|
|
|
|
|
#include <libevmasm/AssemblyItem.h>
|
|
|
|
#include <libevmasm/SemanticInformation.h>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::evmasm;
|
2016-11-11 13:11:07 +00:00
|
|
|
|
|
|
|
// TODO: Extend this to use the tools from ExpressionClasses.cpp
|
|
|
|
|
2017-08-29 00:01:47 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2016-11-22 14:43:51 +00:00
|
|
|
struct OptimiserState
|
2016-11-11 13:11:07 +00:00
|
|
|
{
|
2016-11-22 14:43:51 +00:00
|
|
|
AssemblyItems const& items;
|
|
|
|
size_t i;
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems> out;
|
2016-11-22 14:43:51 +00:00
|
|
|
};
|
|
|
|
|
2022-03-09 15:03:57 +00:00
|
|
|
template<typename FunctionType>
|
|
|
|
struct FunctionParameterCount;
|
|
|
|
template<typename R, typename... Args>
|
|
|
|
struct FunctionParameterCount<R(Args...)>
|
2016-11-23 13:50:20 +00:00
|
|
|
{
|
2022-03-09 15:03:57 +00:00
|
|
|
static constexpr auto value = sizeof...(Args);
|
2016-11-23 13:50:20 +00:00
|
|
|
};
|
2022-03-09 15:03:57 +00:00
|
|
|
|
2016-11-23 13:50:20 +00:00
|
|
|
template <class Method>
|
2022-03-09 15:03:57 +00:00
|
|
|
struct SimplePeepholeOptimizerMethod
|
2016-11-23 13:50:20 +00:00
|
|
|
{
|
2022-03-09 15:03:57 +00:00
|
|
|
template <size_t... Indices>
|
2022-11-23 10:51:34 +00:00
|
|
|
static bool applyRule(
|
|
|
|
AssemblyItems::const_iterator _in,
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems> _out,
|
|
|
|
std::index_sequence<Indices...>
|
2022-11-23 10:51:34 +00:00
|
|
|
)
|
2016-11-23 13:50:20 +00:00
|
|
|
{
|
2022-03-09 15:03:57 +00:00
|
|
|
return Method::applySimple(_in[Indices]..., _out);
|
2016-11-23 13:50:20 +00:00
|
|
|
}
|
2016-11-22 14:43:51 +00:00
|
|
|
static bool apply(OptimiserState& _state)
|
|
|
|
{
|
2022-03-09 15:03:57 +00:00
|
|
|
static constexpr size_t WindowSize = FunctionParameterCount<decltype(Method::applySimple)>::value - 1;
|
2016-11-22 14:43:51 +00:00
|
|
|
if (
|
|
|
|
_state.i + WindowSize <= _state.items.size() &&
|
2023-07-10 11:12:46 +00:00
|
|
|
applyRule(_state.items.begin() + static_cast<ptrdiff_t>(_state.i), _state.out, std::make_index_sequence<WindowSize>{})
|
2016-11-22 14:43:51 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
_state.i += WindowSize;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-09 15:03:57 +00:00
|
|
|
struct Identity: SimplePeepholeOptimizerMethod<Identity>
|
2016-11-22 14:43:51 +00:00
|
|
|
{
|
2022-11-23 10:51:34 +00:00
|
|
|
static bool applySimple(
|
|
|
|
AssemblyItem const& _item,
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems> _out
|
2022-11-23 10:51:34 +00:00
|
|
|
)
|
2016-11-11 13:11:07 +00:00
|
|
|
{
|
2016-11-23 13:50:20 +00:00
|
|
|
*_out = _item;
|
2016-11-11 13:11:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-09 15:03:57 +00:00
|
|
|
struct PushPop: SimplePeepholeOptimizerMethod<PushPop>
|
2016-11-11 13:11:07 +00:00
|
|
|
{
|
2022-11-23 10:51:34 +00:00
|
|
|
static bool applySimple(
|
|
|
|
AssemblyItem const& _push,
|
|
|
|
AssemblyItem const& _pop,
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems>
|
2022-11-23 10:51:34 +00:00
|
|
|
)
|
2016-11-11 13:11:07 +00:00
|
|
|
{
|
2016-11-23 13:50:20 +00:00
|
|
|
auto t = _push.type();
|
|
|
|
return _pop == Instruction::POP && (
|
|
|
|
SemanticInformation::isDupInstruction(_push) ||
|
2021-09-13 16:59:18 +00:00
|
|
|
t == Push || t == PushTag || t == PushSub ||
|
2016-11-11 13:11:07 +00:00
|
|
|
t == PushSubSize || t == PushProgramSize || t == PushData || t == PushLibraryAddress
|
2016-11-22 14:43:51 +00:00
|
|
|
);
|
2016-11-11 13:11:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-09 15:03:57 +00:00
|
|
|
struct OpPop: SimplePeepholeOptimizerMethod<OpPop>
|
2016-11-24 11:17:29 +00:00
|
|
|
{
|
2016-11-24 20:08:30 +00:00
|
|
|
static bool applySimple(
|
|
|
|
AssemblyItem const& _op,
|
|
|
|
AssemblyItem const& _pop,
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems> _out
|
2016-11-24 20:08:30 +00:00
|
|
|
)
|
2016-11-24 11:17:29 +00:00
|
|
|
{
|
2016-11-24 20:08:30 +00:00
|
|
|
if (_pop == Instruction::POP && _op.type() == Operation)
|
2016-11-24 11:17:29 +00:00
|
|
|
{
|
2016-11-24 20:08:30 +00:00
|
|
|
Instruction instr = _op.instruction();
|
2022-11-23 10:51:34 +00:00
|
|
|
if (instructionInfo(instr, langutil::EVMVersion()).ret == 1 && !instructionInfo(instr, langutil::EVMVersion()).sideEffects)
|
2016-11-24 11:17:29 +00:00
|
|
|
{
|
2022-11-23 10:51:34 +00:00
|
|
|
for (int j = 0; j < instructionInfo(instr, langutil::EVMVersion()).args; j++)
|
2016-12-30 12:52:08 +00:00
|
|
|
*_out = {Instruction::POP, _op.location()};
|
2016-11-24 17:21:57 +00:00
|
|
|
return true;
|
2016-11-24 11:17:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-14 12:37:40 +00:00
|
|
|
struct OpStop: SimplePeepholeOptimizerMethod<OpStop>
|
|
|
|
{
|
|
|
|
static bool applySimple(
|
|
|
|
AssemblyItem const& _op,
|
|
|
|
AssemblyItem const& _stop,
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems> _out
|
2022-01-14 12:37:40 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
if (_stop == Instruction::STOP)
|
|
|
|
{
|
|
|
|
if (_op.type() == Operation)
|
|
|
|
{
|
|
|
|
Instruction instr = _op.instruction();
|
2022-11-23 10:51:34 +00:00
|
|
|
if (!instructionInfo(instr, langutil::EVMVersion()).sideEffects)
|
2022-01-14 12:37:40 +00:00
|
|
|
{
|
|
|
|
*_out = {Instruction::STOP, _op.location()};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (_op.type() == Push)
|
|
|
|
{
|
|
|
|
*_out = {Instruction::STOP, _op.location()};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-15 16:25:22 +00:00
|
|
|
struct OpReturnRevert: SimplePeepholeOptimizerMethod<OpReturnRevert>
|
|
|
|
{
|
|
|
|
static bool applySimple(
|
|
|
|
AssemblyItem const& _op,
|
|
|
|
AssemblyItem const& _push,
|
|
|
|
AssemblyItem const& _pushOrDup,
|
|
|
|
AssemblyItem const& _returnRevert,
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems> _out
|
2022-03-15 16:25:22 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
(_returnRevert == Instruction::RETURN || _returnRevert == Instruction::REVERT) &&
|
|
|
|
_push.type() == Push &&
|
|
|
|
(_pushOrDup.type() == Push || _pushOrDup == dupInstruction(1))
|
|
|
|
)
|
|
|
|
if (
|
2022-11-23 10:51:34 +00:00
|
|
|
(_op.type() == Operation && !instructionInfo(_op.instruction(), langutil::EVMVersion()).sideEffects) ||
|
2022-03-15 16:25:22 +00:00
|
|
|
_op.type() == Push
|
|
|
|
)
|
|
|
|
{
|
|
|
|
*_out = _push;
|
|
|
|
*_out = _pushOrDup;
|
|
|
|
*_out = _returnRevert;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-09 15:03:57 +00:00
|
|
|
struct DoubleSwap: SimplePeepholeOptimizerMethod<DoubleSwap>
|
2016-11-11 13:11:07 +00:00
|
|
|
{
|
2022-11-23 10:51:34 +00:00
|
|
|
static size_t applySimple(
|
|
|
|
AssemblyItem const& _s1,
|
|
|
|
AssemblyItem const& _s2,
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems>
|
2022-11-23 10:51:34 +00:00
|
|
|
)
|
2016-11-11 13:11:07 +00:00
|
|
|
{
|
2016-11-23 13:50:20 +00:00
|
|
|
return _s1 == _s2 && SemanticInformation::isSwapInstruction(_s1);
|
2016-11-11 13:11:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-09 15:03:57 +00:00
|
|
|
struct DoublePush: SimplePeepholeOptimizerMethod<DoublePush>
|
2017-06-14 17:40:53 +00:00
|
|
|
{
|
2022-11-23 10:51:34 +00:00
|
|
|
static bool applySimple(
|
|
|
|
AssemblyItem const& _push1,
|
|
|
|
AssemblyItem const& _push2,
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems> _out
|
2022-11-23 10:51:34 +00:00
|
|
|
)
|
2017-06-14 17:40:53 +00:00
|
|
|
{
|
|
|
|
if (_push1.type() == Push && _push2.type() == Push && _push1.data() == _push2.data())
|
|
|
|
{
|
|
|
|
*_out = _push1;
|
|
|
|
*_out = {Instruction::DUP1, _push2.location()};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-09 15:03:57 +00:00
|
|
|
struct CommutativeSwap: SimplePeepholeOptimizerMethod<CommutativeSwap>
|
2018-03-29 11:10:39 +00:00
|
|
|
{
|
2022-11-23 10:51:34 +00:00
|
|
|
static bool applySimple(
|
|
|
|
AssemblyItem const& _swap,
|
|
|
|
AssemblyItem const& _op,
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems> _out
|
2022-11-23 10:51:34 +00:00
|
|
|
)
|
2018-03-29 11:10:39 +00:00
|
|
|
{
|
|
|
|
// Remove SWAP1 if following instruction is commutative
|
|
|
|
if (
|
2018-12-18 15:09:51 +00:00
|
|
|
_swap == Instruction::SWAP1 &&
|
2018-03-29 11:10:39 +00:00
|
|
|
SemanticInformation::isCommutativeOperation(_op)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
*_out = _op;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-09 15:03:57 +00:00
|
|
|
struct SwapComparison: SimplePeepholeOptimizerMethod<SwapComparison>
|
2018-04-04 13:28:15 +00:00
|
|
|
{
|
2022-11-23 10:51:34 +00:00
|
|
|
static bool applySimple(
|
|
|
|
AssemblyItem const& _swap,
|
|
|
|
AssemblyItem const& _op,
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems> _out
|
2022-11-23 10:51:34 +00:00
|
|
|
)
|
2018-04-04 13:28:15 +00:00
|
|
|
{
|
2023-07-10 11:12:46 +00:00
|
|
|
static std::map<Instruction, Instruction> const swappableOps{
|
2018-04-04 13:28:15 +00:00
|
|
|
{ Instruction::LT, Instruction::GT },
|
|
|
|
{ Instruction::GT, Instruction::LT },
|
|
|
|
{ Instruction::SLT, Instruction::SGT },
|
|
|
|
{ Instruction::SGT, Instruction::SLT }
|
|
|
|
};
|
|
|
|
|
|
|
|
if (
|
2018-12-18 15:09:51 +00:00
|
|
|
_swap == Instruction::SWAP1 &&
|
2018-04-04 13:28:15 +00:00
|
|
|
_op.type() == Operation &&
|
|
|
|
swappableOps.count(_op.instruction())
|
|
|
|
)
|
|
|
|
{
|
|
|
|
*_out = swappableOps.at(_op.instruction());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-13 09:16:30 +00:00
|
|
|
/// Remove swapN after dupN
|
2022-03-09 15:03:57 +00:00
|
|
|
struct DupSwap: SimplePeepholeOptimizerMethod<DupSwap>
|
2020-07-13 09:16:30 +00:00
|
|
|
{
|
|
|
|
static size_t applySimple(
|
|
|
|
AssemblyItem const& _dupN,
|
|
|
|
AssemblyItem const& _swapN,
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems> _out
|
2020-07-13 09:16:30 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
SemanticInformation::isDupInstruction(_dupN) &&
|
|
|
|
SemanticInformation::isSwapInstruction(_swapN) &&
|
|
|
|
getDupNumber(_dupN.instruction()) == getSwapNumber(_swapN.instruction())
|
|
|
|
)
|
|
|
|
{
|
|
|
|
*_out = _dupN;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-03-09 15:03:57 +00:00
|
|
|
struct IsZeroIsZeroJumpI: SimplePeepholeOptimizerMethod<IsZeroIsZeroJumpI>
|
2019-03-06 20:27:39 +00:00
|
|
|
{
|
|
|
|
static size_t applySimple(
|
|
|
|
AssemblyItem const& _iszero1,
|
|
|
|
AssemblyItem const& _iszero2,
|
|
|
|
AssemblyItem const& _pushTag,
|
|
|
|
AssemblyItem const& _jumpi,
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems> _out
|
2019-03-06 20:27:39 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
_iszero1 == Instruction::ISZERO &&
|
|
|
|
_iszero2 == Instruction::ISZERO &&
|
|
|
|
_pushTag.type() == PushTag &&
|
|
|
|
_jumpi == Instruction::JUMPI
|
|
|
|
)
|
|
|
|
{
|
|
|
|
*_out = _pushTag;
|
|
|
|
*_out = _jumpi;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-07 18:13:35 +00:00
|
|
|
struct EqIsZeroJumpI: SimplePeepholeOptimizerMethod<EqIsZeroJumpI>
|
|
|
|
{
|
|
|
|
static size_t applySimple(
|
|
|
|
AssemblyItem const& _eq,
|
|
|
|
AssemblyItem const& _iszero,
|
|
|
|
AssemblyItem const& _pushTag,
|
|
|
|
AssemblyItem const& _jumpi,
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems> _out
|
2022-03-07 18:13:35 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
_eq == Instruction::EQ &&
|
|
|
|
_iszero == Instruction::ISZERO &&
|
|
|
|
_pushTag.type() == PushTag &&
|
|
|
|
_jumpi == Instruction::JUMPI
|
|
|
|
)
|
|
|
|
{
|
|
|
|
*_out = AssemblyItem(Instruction::SUB, _eq.location());
|
|
|
|
*_out = _pushTag;
|
|
|
|
*_out = _jumpi;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// push_tag_1 jumpi push_tag_2 jump tag_1: -> iszero push_tag_2 jumpi tag_1:
|
|
|
|
struct DoubleJump: SimplePeepholeOptimizerMethod<DoubleJump>
|
|
|
|
{
|
|
|
|
static size_t applySimple(
|
|
|
|
AssemblyItem const& _pushTag1,
|
|
|
|
AssemblyItem const& _jumpi,
|
|
|
|
AssemblyItem const& _pushTag2,
|
|
|
|
AssemblyItem const& _jump,
|
|
|
|
AssemblyItem const& _tag1,
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems> _out
|
2022-03-07 18:13:35 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
_pushTag1.type() == PushTag &&
|
|
|
|
_jumpi == Instruction::JUMPI &&
|
|
|
|
_pushTag2.type() == PushTag &&
|
|
|
|
_jump == Instruction::JUMP &&
|
|
|
|
_tag1.type() == Tag &&
|
|
|
|
_pushTag1.data() == _tag1.data()
|
|
|
|
)
|
|
|
|
{
|
|
|
|
*_out = AssemblyItem(Instruction::ISZERO, _jumpi.location());
|
|
|
|
*_out = _pushTag2;
|
|
|
|
*_out = _jumpi;
|
|
|
|
*_out = _tag1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-09 15:03:57 +00:00
|
|
|
struct JumpToNext: SimplePeepholeOptimizerMethod<JumpToNext>
|
2016-11-11 13:11:07 +00:00
|
|
|
{
|
2016-11-23 13:50:20 +00:00
|
|
|
static size_t applySimple(
|
|
|
|
AssemblyItem const& _pushTag,
|
|
|
|
AssemblyItem const& _jump,
|
|
|
|
AssemblyItem const& _tag,
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems> _out
|
2016-11-23 13:50:20 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
_pushTag.type() == PushTag &&
|
|
|
|
(_jump == Instruction::JUMP || _jump == Instruction::JUMPI) &&
|
|
|
|
_tag.type() == Tag &&
|
|
|
|
_pushTag.data() == _tag.data()
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (_jump == Instruction::JUMPI)
|
|
|
|
*_out = AssemblyItem(Instruction::POP, _jump.location());
|
|
|
|
*_out = _tag;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-09 15:03:57 +00:00
|
|
|
struct TagConjunctions: SimplePeepholeOptimizerMethod<TagConjunctions>
|
2016-11-23 13:50:20 +00:00
|
|
|
{
|
|
|
|
static bool applySimple(
|
|
|
|
AssemblyItem const& _pushTag,
|
|
|
|
AssemblyItem const& _pushConstant,
|
|
|
|
AssemblyItem const& _and,
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems> _out
|
2016-11-23 13:50:20 +00:00
|
|
|
)
|
2016-11-11 13:11:07 +00:00
|
|
|
{
|
2020-07-08 18:13:11 +00:00
|
|
|
if (_and != Instruction::AND)
|
|
|
|
return false;
|
2016-11-11 13:11:07 +00:00
|
|
|
if (
|
2016-11-23 13:50:20 +00:00
|
|
|
_pushTag.type() == PushTag &&
|
|
|
|
_pushConstant.type() == Push &&
|
|
|
|
(_pushConstant.data() & u256(0xFFFFFFFF)) == u256(0xFFFFFFFF)
|
2016-11-11 13:11:07 +00:00
|
|
|
)
|
|
|
|
{
|
2016-11-23 13:50:20 +00:00
|
|
|
*_out = _pushTag;
|
2016-11-11 13:11:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-07-08 18:13:11 +00:00
|
|
|
else if (
|
|
|
|
// tag and constant are swapped
|
|
|
|
_pushConstant.type() == PushTag &&
|
|
|
|
_pushTag.type() == Push &&
|
|
|
|
(_pushTag.data() & u256(0xFFFFFFFF)) == u256(0xFFFFFFFF)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
*_out = _pushConstant;
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-11 13:11:07 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-09 15:03:57 +00:00
|
|
|
struct TruthyAnd: SimplePeepholeOptimizerMethod<TruthyAnd>
|
2018-06-14 17:29:42 +00:00
|
|
|
{
|
|
|
|
static bool applySimple(
|
|
|
|
AssemblyItem const& _push,
|
|
|
|
AssemblyItem const& _not,
|
|
|
|
AssemblyItem const& _and,
|
2023-07-10 11:12:46 +00:00
|
|
|
std::back_insert_iterator<AssemblyItems>
|
2018-06-14 17:29:42 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
_push.type() == Push && _push.data() == 0 &&
|
|
|
|
_not == Instruction::NOT &&
|
|
|
|
_and == Instruction::AND
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-22 14:43:51 +00:00
|
|
|
/// Removes everything after a JUMP (or similar) until the next JUMPDEST.
|
|
|
|
struct UnreachableCode
|
2016-11-11 13:11:07 +00:00
|
|
|
{
|
2016-11-22 14:43:51 +00:00
|
|
|
static bool apply(OptimiserState& _state)
|
|
|
|
{
|
2020-06-02 13:10:14 +00:00
|
|
|
auto it = _state.items.begin() + static_cast<ptrdiff_t>(_state.i);
|
2016-11-22 14:43:51 +00:00
|
|
|
auto end = _state.items.end();
|
|
|
|
if (it == end)
|
|
|
|
return false;
|
|
|
|
if (
|
|
|
|
it[0] != Instruction::JUMP &&
|
|
|
|
it[0] != Instruction::RETURN &&
|
|
|
|
it[0] != Instruction::STOP &&
|
2017-01-26 14:59:29 +00:00
|
|
|
it[0] != Instruction::INVALID &&
|
2017-02-06 20:21:27 +00:00
|
|
|
it[0] != Instruction::SELFDESTRUCT &&
|
|
|
|
it[0] != Instruction::REVERT
|
2016-11-22 14:43:51 +00:00
|
|
|
)
|
|
|
|
return false;
|
|
|
|
|
2020-06-02 13:10:14 +00:00
|
|
|
ptrdiff_t i = 1;
|
2016-11-22 14:43:51 +00:00
|
|
|
while (it + i != end && it[i].type() != Tag)
|
|
|
|
i++;
|
|
|
|
if (i > 1)
|
|
|
|
{
|
|
|
|
*_state.out = it[0];
|
2020-06-02 13:10:14 +00:00
|
|
|
_state.i += static_cast<size_t>(i);
|
2016-11-22 14:43:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-11 13:11:07 +00:00
|
|
|
void applyMethods(OptimiserState&)
|
|
|
|
{
|
|
|
|
assertThrow(false, OptimizerException, "Peephole optimizer failed to apply identity.");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Method, typename... OtherMethods>
|
|
|
|
void applyMethods(OptimiserState& _state, Method, OtherMethods... _other)
|
|
|
|
{
|
2016-11-22 14:43:51 +00:00
|
|
|
if (!Method::apply(_state))
|
2016-11-11 13:11:07 +00:00
|
|
|
applyMethods(_state, _other...);
|
|
|
|
}
|
|
|
|
|
2017-10-13 16:59:04 +00:00
|
|
|
size_t numberOfPops(AssemblyItems const& _items)
|
|
|
|
{
|
2020-06-02 13:10:14 +00:00
|
|
|
return static_cast<size_t>(std::count(_items.begin(), _items.end(), Instruction::POP));
|
2017-10-13 16:59:04 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 00:01:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 13:11:07 +00:00
|
|
|
bool PeepholeOptimiser::optimise()
|
|
|
|
{
|
2021-09-22 13:14:31 +00:00
|
|
|
// Avoid referencing immutables too early by using approx. counting in bytesRequired()
|
|
|
|
auto const approx = evmasm::Precision::Approximate;
|
2022-11-23 10:51:34 +00:00
|
|
|
OptimiserState state {m_items, 0, back_inserter(m_optimisedItems)};
|
2016-11-11 13:11:07 +00:00
|
|
|
while (state.i < m_items.size())
|
2019-03-06 20:27:39 +00:00
|
|
|
applyMethods(
|
|
|
|
state,
|
2022-03-15 16:25:22 +00:00
|
|
|
PushPop(), OpPop(), OpStop(), OpReturnRevert(), DoublePush(), DoubleSwap(), CommutativeSwap(), SwapComparison(),
|
2022-03-07 18:13:35 +00:00
|
|
|
DupSwap(), IsZeroIsZeroJumpI(), EqIsZeroJumpI(), DoubleJump(), JumpToNext(), UnreachableCode(),
|
2019-03-06 20:27:39 +00:00
|
|
|
TagConjunctions(), TruthyAnd(), Identity()
|
|
|
|
);
|
2017-06-14 17:40:53 +00:00
|
|
|
if (m_optimisedItems.size() < m_items.size() || (
|
2017-10-13 16:59:04 +00:00
|
|
|
m_optimisedItems.size() == m_items.size() && (
|
2021-09-22 13:14:31 +00:00
|
|
|
evmasm::bytesRequired(m_optimisedItems, 3, approx) < evmasm::bytesRequired(m_items, 3, approx) ||
|
2017-10-13 16:59:04 +00:00
|
|
|
numberOfPops(m_optimisedItems) > numberOfPops(m_items)
|
|
|
|
)
|
2017-06-14 17:40:53 +00:00
|
|
|
))
|
2016-11-11 13:11:07 +00:00
|
|
|
{
|
|
|
|
m_items = std::move(m_optimisedItems);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|