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
|
|
|
*/
|
|
|
|
/**
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "PeepholeOptimiser.h"
|
|
|
|
|
|
|
|
#include <libevmasm/AssemblyItem.h>
|
|
|
|
#include <libevmasm/SemanticInformation.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev::eth;
|
|
|
|
using namespace dev;
|
|
|
|
|
|
|
|
// TODO: Extend this to use the tools from ExpressionClasses.cpp
|
|
|
|
|
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;
|
|
|
|
std::back_insert_iterator<AssemblyItems> out;
|
|
|
|
};
|
|
|
|
|
2016-11-23 13:50:20 +00:00
|
|
|
template <class Method, size_t Arguments>
|
|
|
|
struct ApplyRule
|
|
|
|
{
|
|
|
|
};
|
|
|
|
template <class Method>
|
|
|
|
struct ApplyRule<Method, 3>
|
|
|
|
{
|
|
|
|
static bool applyRule(AssemblyItems::const_iterator _in, std::back_insert_iterator<AssemblyItems> _out)
|
|
|
|
{
|
|
|
|
return Method::applySimple(_in[0], _in[1], _in[2], _out);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <class Method>
|
|
|
|
struct ApplyRule<Method, 2>
|
|
|
|
{
|
|
|
|
static bool applyRule(AssemblyItems::const_iterator _in, std::back_insert_iterator<AssemblyItems> _out)
|
|
|
|
{
|
|
|
|
return Method::applySimple(_in[0], _in[1], _out);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <class Method>
|
|
|
|
struct ApplyRule<Method, 1>
|
|
|
|
{
|
|
|
|
static bool applyRule(AssemblyItems::const_iterator _in, std::back_insert_iterator<AssemblyItems> _out)
|
|
|
|
{
|
|
|
|
return Method::applySimple(_in[0], _out);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-22 14:43:51 +00:00
|
|
|
template <class Method, size_t WindowSize>
|
|
|
|
struct SimplePeepholeOptimizerMethod
|
|
|
|
{
|
|
|
|
static bool apply(OptimiserState& _state)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
_state.i + WindowSize <= _state.items.size() &&
|
2016-11-23 13:50:20 +00:00
|
|
|
ApplyRule<Method, WindowSize>::applyRule(_state.items.begin() + _state.i, _state.out)
|
2016-11-22 14:43:51 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
_state.i += WindowSize;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Identity: SimplePeepholeOptimizerMethod<Identity, 1>
|
|
|
|
{
|
2016-11-23 13:50:20 +00:00
|
|
|
static bool applySimple(AssemblyItem const& _item, std::back_insert_iterator<AssemblyItems> _out)
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-22 14:43:51 +00:00
|
|
|
struct PushPop: SimplePeepholeOptimizerMethod<PushPop, 2>
|
2016-11-11 13:11:07 +00:00
|
|
|
{
|
2016-11-24 20:08:30 +00:00
|
|
|
static bool applySimple(AssemblyItem const& _push, AssemblyItem const& _pop, std::back_insert_iterator<AssemblyItems>)
|
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) ||
|
2016-11-11 13:11:07 +00:00
|
|
|
t == Push || t == PushString || t == PushTag || t == PushSub ||
|
|
|
|
t == PushSubSize || t == PushProgramSize || t == PushData || t == PushLibraryAddress
|
2016-11-22 14:43:51 +00:00
|
|
|
);
|
2016-11-11 13:11:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-24 20:08:30 +00:00
|
|
|
struct OpPop: SimplePeepholeOptimizerMethod<OpPop, 2>
|
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,
|
|
|
|
std::back_insert_iterator<AssemblyItems> _out
|
|
|
|
)
|
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();
|
|
|
|
if (instructionInfo(instr).ret == 1 && !instructionInfo(instr).sideEffects)
|
2016-11-24 11:17:29 +00:00
|
|
|
{
|
2016-11-24 20:08:30 +00:00
|
|
|
for (int j = 0; j < instructionInfo(instr).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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-22 14:43:51 +00:00
|
|
|
struct DoubleSwap: SimplePeepholeOptimizerMethod<DoubleSwap, 2>
|
2016-11-11 13:11:07 +00:00
|
|
|
{
|
2016-11-23 13:50:20 +00:00
|
|
|
static size_t applySimple(AssemblyItem const& _s1, AssemblyItem const& _s2, std::back_insert_iterator<AssemblyItems>)
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-22 14:43:51 +00:00
|
|
|
struct JumpToNext: SimplePeepholeOptimizerMethod<JumpToNext, 3>
|
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,
|
|
|
|
std::back_insert_iterator<AssemblyItems> _out
|
|
|
|
)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TagConjunctions: SimplePeepholeOptimizerMethod<TagConjunctions, 3>
|
|
|
|
{
|
|
|
|
static bool applySimple(
|
|
|
|
AssemblyItem const& _pushTag,
|
|
|
|
AssemblyItem const& _pushConstant,
|
|
|
|
AssemblyItem const& _and,
|
|
|
|
std::back_insert_iterator<AssemblyItems> _out
|
|
|
|
)
|
2016-11-11 13:11:07 +00:00
|
|
|
{
|
|
|
|
if (
|
2016-11-23 13:50:20 +00:00
|
|
|
_pushTag.type() == PushTag &&
|
|
|
|
_and == Instruction::AND &&
|
|
|
|
_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;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
auto it = _state.items.begin() + _state.i;
|
|
|
|
auto end = _state.items.end();
|
|
|
|
if (it == end)
|
|
|
|
return false;
|
|
|
|
if (
|
|
|
|
it[0] != Instruction::JUMP &&
|
|
|
|
it[0] != Instruction::RETURN &&
|
|
|
|
it[0] != Instruction::STOP &&
|
|
|
|
it[0] != Instruction::SUICIDE
|
|
|
|
)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size_t i = 1;
|
|
|
|
while (it + i != end && it[i].type() != Tag)
|
|
|
|
i++;
|
|
|
|
if (i > 1)
|
|
|
|
{
|
|
|
|
*_state.out = it[0];
|
|
|
|
_state.i += i;
|
|
|
|
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...);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PeepholeOptimiser::optimise()
|
|
|
|
{
|
|
|
|
OptimiserState state {m_items, 0, std::back_inserter(m_optimisedItems)};
|
|
|
|
while (state.i < m_items.size())
|
2016-11-24 20:08:30 +00:00
|
|
|
applyMethods(state, PushPop(), OpPop(), DoubleSwap(), JumpToNext(), UnreachableCode(), TagConjunctions(), Identity());
|
2016-11-11 13:11:07 +00:00
|
|
|
if (m_optimisedItems.size() < m_items.size())
|
|
|
|
{
|
|
|
|
m_items = std::move(m_optimisedItems);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|