mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Peephole optimizer for double push.
This commit is contained in:
parent
f008ddf836
commit
e5b0ad562e
@ -148,6 +148,14 @@ private:
|
||||
|
||||
using AssemblyItems = std::vector<AssemblyItem>;
|
||||
|
||||
inline size_t bytesRequired(AssemblyItems const& _items, size_t _addressLength)
|
||||
{
|
||||
size_t size = 0;
|
||||
for (AssemblyItem const& item: _items)
|
||||
size += item.bytesRequired(_addressLength);
|
||||
return size;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& _out, AssemblyItem const& _item);
|
||||
inline std::ostream& operator<<(std::ostream& _out, AssemblyItems const& _items)
|
||||
{
|
||||
|
@ -99,10 +99,7 @@ bigint ConstantOptimisationMethod::dataGas(bytes const& _data) const
|
||||
|
||||
size_t ConstantOptimisationMethod::bytesRequired(AssemblyItems const& _items)
|
||||
{
|
||||
size_t size = 0;
|
||||
for (AssemblyItem const& item: _items)
|
||||
size += item.bytesRequired(3); // assume 3 byte addresses
|
||||
return size;
|
||||
return eth::bytesRequired(_items, 3); // assume 3 byte addresses
|
||||
}
|
||||
|
||||
void ConstantOptimisationMethod::replaceConstants(
|
||||
|
@ -136,6 +136,21 @@ struct DoubleSwap: SimplePeepholeOptimizerMethod<DoubleSwap, 2>
|
||||
}
|
||||
};
|
||||
|
||||
struct DoublePush: SimplePeepholeOptimizerMethod<DoublePush, 2>
|
||||
{
|
||||
static bool applySimple(AssemblyItem const& _push1, AssemblyItem const& _push2, std::back_insert_iterator<AssemblyItems> _out)
|
||||
{
|
||||
if (_push1.type() == Push && _push2.type() == Push && _push1.data() == _push2.data())
|
||||
{
|
||||
*_out = _push1;
|
||||
*_out = {Instruction::DUP1, _push2.location()};
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
struct JumpToNext: SimplePeepholeOptimizerMethod<JumpToNext, 3>
|
||||
{
|
||||
static size_t applySimple(
|
||||
@ -235,13 +250,15 @@ bool PeepholeOptimiser::optimise()
|
||||
{
|
||||
OptimiserState state {m_items, 0, std::back_inserter(m_optimisedItems)};
|
||||
while (state.i < m_items.size())
|
||||
applyMethods(state, PushPop(), OpPop(), DoubleSwap(), JumpToNext(), UnreachableCode(), TagConjunctions(), Identity());
|
||||
if (m_optimisedItems.size() < m_items.size())
|
||||
applyMethods(state, PushPop(), OpPop(), DoublePush(), DoubleSwap(), JumpToNext(), UnreachableCode(), TagConjunctions(), Identity());
|
||||
if (m_optimisedItems.size() < m_items.size() || (
|
||||
m_optimisedItems.size() == m_items.size() &&
|
||||
eth::bytesRequired(m_optimisedItems, 3) < eth::bytesRequired(m_items, 3)
|
||||
))
|
||||
{
|
||||
m_items = std::move(m_optimisedItems);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
|
||||
}
|
||||
|
@ -1189,6 +1189,32 @@ BOOST_AUTO_TEST_CASE(clear_unreachable_code)
|
||||
);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(peephole_double_push)
|
||||
{
|
||||
AssemblyItems items{
|
||||
u256(0),
|
||||
u256(0),
|
||||
u256(5),
|
||||
u256(5),
|
||||
u256(4),
|
||||
u256(5)
|
||||
};
|
||||
AssemblyItems expectation{
|
||||
u256(0),
|
||||
Instruction::DUP1,
|
||||
u256(5),
|
||||
Instruction::DUP1,
|
||||
u256(4),
|
||||
u256(5)
|
||||
};
|
||||
PeepholeOptimiser peepOpt(items);
|
||||
BOOST_REQUIRE(peepOpt.optimise());
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(
|
||||
items.begin(), items.end(),
|
||||
expectation.begin(), expectation.end()
|
||||
);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(computing_constants)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
|
Loading…
Reference in New Issue
Block a user