2015-04-24 15:35:16 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-04-24 15:35:16 +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,
|
2015-04-24 15:35:16 +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/>.
|
2015-04-24 15:35:16 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @file CommonSubexpressionEliminator.cpp
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Optimizer step for common subexpression elimination and stack reorganisation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <boost/range/adaptor/reversed.hpp>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Keccak256.h>
|
2015-04-24 15:35:16 +00:00
|
|
|
#include <libevmasm/CommonSubexpressionEliminator.h>
|
|
|
|
#include <libevmasm/AssemblyItem.h>
|
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::evmasm;
|
|
|
|
using namespace solidity::langutil;
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
vector<AssemblyItem> CommonSubexpressionEliminator::getOptimizedItems()
|
|
|
|
{
|
|
|
|
optimizeBreakingItem();
|
|
|
|
|
2015-06-10 07:58:59 +00:00
|
|
|
KnownState nextInitialState = m_state;
|
|
|
|
if (m_breakingItem)
|
|
|
|
nextInitialState.feedItem(*m_breakingItem);
|
|
|
|
KnownState nextState = nextInitialState;
|
|
|
|
|
|
|
|
ScopeGuard reset([&]()
|
|
|
|
{
|
|
|
|
m_breakingItem = nullptr;
|
|
|
|
m_storeOperations.clear();
|
|
|
|
m_initialState = move(nextInitialState);
|
|
|
|
m_state = move(nextState);
|
|
|
|
});
|
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
map<int, Id> initialStackContents;
|
|
|
|
map<int, Id> targetStackContents;
|
2015-04-29 16:16:05 +00:00
|
|
|
int minHeight = m_state.stackHeight() + 1;
|
|
|
|
if (!m_state.stackElements().empty())
|
|
|
|
minHeight = min(minHeight, m_state.stackElements().begin()->first);
|
2015-04-30 12:41:55 +00:00
|
|
|
for (int height = minHeight; height <= m_initialState.stackHeight(); ++height)
|
|
|
|
initialStackContents[height] = m_initialState.stackElement(height, SourceLocation());
|
2015-04-29 16:16:05 +00:00
|
|
|
for (int height = minHeight; height <= m_state.stackHeight(); ++height)
|
|
|
|
targetStackContents[height] = m_state.stackElement(height, SourceLocation());
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2015-04-29 16:16:05 +00:00
|
|
|
AssemblyItems items = CSECodeGenerator(m_state.expressionClasses(), m_storeOperations).generateCode(
|
2015-06-06 13:31:22 +00:00
|
|
|
m_initialState.sequenceNumber(),
|
2015-04-30 12:41:55 +00:00
|
|
|
m_initialState.stackHeight(),
|
2015-04-24 15:35:16 +00:00
|
|
|
initialStackContents,
|
|
|
|
targetStackContents
|
|
|
|
);
|
|
|
|
if (m_breakingItem)
|
|
|
|
items.push_back(*m_breakingItem);
|
2015-05-06 17:15:14 +00:00
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommonSubexpressionEliminator::feedItem(AssemblyItem const& _item, bool _copyItem)
|
|
|
|
{
|
2015-04-29 16:16:05 +00:00
|
|
|
StoreOperation op = m_state.feedItem(_item, _copyItem);
|
|
|
|
if (op.isValid())
|
|
|
|
m_storeOperations.push_back(op);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CommonSubexpressionEliminator::optimizeBreakingItem()
|
|
|
|
{
|
2015-06-12 09:06:05 +00:00
|
|
|
if (!m_breakingItem)
|
2015-04-24 15:35:16 +00:00
|
|
|
return;
|
|
|
|
|
2015-06-12 09:06:05 +00:00
|
|
|
ExpressionClasses& classes = m_state.expressionClasses();
|
2015-09-08 15:11:02 +00:00
|
|
|
SourceLocation const& itemLocation = m_breakingItem->location();
|
2015-06-12 09:06:05 +00:00
|
|
|
if (*m_breakingItem == AssemblyItem(Instruction::JUMPI))
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2015-06-12 09:06:05 +00:00
|
|
|
AssemblyItem::JumpType jumpType = m_breakingItem->getJumpType();
|
|
|
|
|
2015-09-08 15:11:02 +00:00
|
|
|
Id condition = m_state.stackElement(m_state.stackHeight() - 1, itemLocation);
|
2015-06-12 09:06:05 +00:00
|
|
|
if (classes.knownNonZero(condition))
|
|
|
|
{
|
2015-09-08 15:11:02 +00:00
|
|
|
feedItem(AssemblyItem(Instruction::SWAP1, itemLocation), true);
|
|
|
|
feedItem(AssemblyItem(Instruction::POP, itemLocation), true);
|
2015-06-12 09:06:05 +00:00
|
|
|
|
2015-09-08 15:11:02 +00:00
|
|
|
AssemblyItem item(Instruction::JUMP, itemLocation);
|
2015-06-12 09:06:05 +00:00
|
|
|
item.setJumpType(jumpType);
|
|
|
|
m_breakingItem = classes.storeItem(item);
|
|
|
|
}
|
|
|
|
else if (classes.knownZero(condition))
|
|
|
|
{
|
2015-09-08 15:11:02 +00:00
|
|
|
AssemblyItem it(Instruction::POP, itemLocation);
|
2015-06-12 09:06:05 +00:00
|
|
|
feedItem(it, true);
|
|
|
|
feedItem(it, true);
|
|
|
|
m_breakingItem = nullptr;
|
|
|
|
}
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
2015-06-12 09:06:05 +00:00
|
|
|
else if (*m_breakingItem == AssemblyItem(Instruction::RETURN))
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2015-09-08 15:11:02 +00:00
|
|
|
Id size = m_state.stackElement(m_state.stackHeight() - 1, itemLocation);
|
2015-06-12 09:06:05 +00:00
|
|
|
if (classes.knownZero(size))
|
|
|
|
{
|
2015-09-08 15:11:02 +00:00
|
|
|
feedItem(AssemblyItem(Instruction::POP, itemLocation), true);
|
|
|
|
feedItem(AssemblyItem(Instruction::POP, itemLocation), true);
|
|
|
|
AssemblyItem item(Instruction::STOP, itemLocation);
|
2015-06-12 09:06:05 +00:00
|
|
|
m_breakingItem = classes.storeItem(item);
|
|
|
|
}
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CSECodeGenerator::CSECodeGenerator(
|
|
|
|
ExpressionClasses& _expressionClasses,
|
|
|
|
vector<CSECodeGenerator::StoreOperation> const& _storeOperations
|
|
|
|
):
|
|
|
|
m_expressionClasses(_expressionClasses)
|
|
|
|
{
|
|
|
|
for (auto const& store: _storeOperations)
|
|
|
|
m_storeOperations[make_pair(store.target, store.slot)].push_back(store);
|
|
|
|
}
|
|
|
|
|
|
|
|
AssemblyItems CSECodeGenerator::generateCode(
|
2015-06-06 13:31:22 +00:00
|
|
|
unsigned _initialSequenceNumber,
|
2015-04-30 12:41:55 +00:00
|
|
|
int _initialStackHeight,
|
2015-04-24 15:35:16 +00:00
|
|
|
map<int, Id> const& _initialStack,
|
|
|
|
map<int, Id> const& _targetStackContents
|
|
|
|
)
|
|
|
|
{
|
2015-04-30 12:41:55 +00:00
|
|
|
m_stackHeight = _initialStackHeight;
|
2015-04-24 15:35:16 +00:00
|
|
|
m_stack = _initialStack;
|
2015-05-06 17:15:14 +00:00
|
|
|
m_targetStack = _targetStackContents;
|
2015-04-24 15:35:16 +00:00
|
|
|
for (auto const& item: m_stack)
|
2015-05-07 16:31:21 +00:00
|
|
|
m_classPositions[item.second].insert(item.first);
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
// generate the dependency graph starting from final storage and memory writes and target stack contents
|
|
|
|
for (auto const& p: m_storeOperations)
|
|
|
|
addDependencies(p.second.back().expression);
|
2015-05-06 17:15:14 +00:00
|
|
|
for (auto const& targetItem: m_targetStack)
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
|
|
|
m_finalClasses.insert(targetItem.second);
|
|
|
|
addDependencies(targetItem.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
// store all needed sequenced expressions
|
|
|
|
set<pair<unsigned, Id>> sequencedExpressions;
|
|
|
|
for (auto const& p: m_neededBy)
|
|
|
|
for (auto id: {p.first, p.second})
|
|
|
|
if (unsigned seqNr = m_expressionClasses.representative(id).sequenceNumber)
|
2015-06-06 13:31:22 +00:00
|
|
|
{
|
2019-11-22 17:02:12 +00:00
|
|
|
// Invalid sequenced operation.
|
|
|
|
// @todo quick fix for now. Proper fix needs to choose representative with higher
|
|
|
|
// sequence number during dependency analysis.
|
|
|
|
assertThrow(seqNr >= _initialSequenceNumber, StackTooDeepException, "");
|
2015-04-24 15:35:16 +00:00
|
|
|
sequencedExpressions.insert(make_pair(seqNr, id));
|
2015-06-06 13:31:22 +00:00
|
|
|
}
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
// Perform all operations on storage and memory in order, if they are needed.
|
|
|
|
for (auto const& seqAndId: sequencedExpressions)
|
|
|
|
if (!m_classPositions.count(seqAndId.second))
|
|
|
|
generateClassElement(seqAndId.second, true);
|
|
|
|
|
|
|
|
// generate the target stack elements
|
2015-05-06 17:15:14 +00:00
|
|
|
for (auto const& targetItem: m_targetStack)
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2015-05-06 17:15:14 +00:00
|
|
|
if (m_stack.count(targetItem.first) && m_stack.at(targetItem.first) == targetItem.second)
|
|
|
|
continue; // already there
|
2015-05-07 16:31:21 +00:00
|
|
|
generateClassElement(targetItem.second);
|
|
|
|
assertThrow(!m_classPositions[targetItem.second].empty(), OptimizerException, "");
|
|
|
|
if (m_classPositions[targetItem.second].count(targetItem.first))
|
2015-04-24 15:35:16 +00:00
|
|
|
continue;
|
2015-09-08 15:11:02 +00:00
|
|
|
SourceLocation sourceLocation;
|
2015-05-12 19:27:04 +00:00
|
|
|
if (m_expressionClasses.representative(targetItem.second).item)
|
2015-09-08 15:11:02 +00:00
|
|
|
sourceLocation = m_expressionClasses.representative(targetItem.second).item->location();
|
2015-05-07 16:31:21 +00:00
|
|
|
int position = classElementPosition(targetItem.second);
|
2015-04-24 15:35:16 +00:00
|
|
|
if (position < targetItem.first)
|
|
|
|
// it is already at its target, we need another copy
|
2015-09-08 15:11:02 +00:00
|
|
|
appendDup(position, sourceLocation);
|
2015-04-24 15:35:16 +00:00
|
|
|
else
|
2015-09-08 15:11:02 +00:00
|
|
|
appendOrRemoveSwap(position, sourceLocation);
|
|
|
|
appendOrRemoveSwap(targetItem.first, sourceLocation);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove surplus elements
|
|
|
|
while (removeStackTopIfPossible())
|
|
|
|
{
|
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
|
|
|
|
// check validity
|
|
|
|
int finalHeight = 0;
|
2015-05-06 17:15:14 +00:00
|
|
|
if (!m_targetStack.empty())
|
2015-04-24 15:35:16 +00:00
|
|
|
// have target stack, so its height should be the final height
|
2015-05-06 17:15:14 +00:00
|
|
|
finalHeight = (--m_targetStack.end())->first;
|
2015-04-24 15:35:16 +00:00
|
|
|
else if (!_initialStack.empty())
|
|
|
|
// no target stack, only erase the initial stack
|
|
|
|
finalHeight = _initialStack.begin()->first - 1;
|
|
|
|
else
|
|
|
|
// neither initial no target stack, no change in height
|
2015-05-06 17:15:14 +00:00
|
|
|
finalHeight = _initialStackHeight;
|
2015-04-24 15:35:16 +00:00
|
|
|
assertThrow(finalHeight == m_stackHeight, OptimizerException, "Incorrect final stack height.");
|
2015-05-06 17:15:14 +00:00
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
return m_generatedItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSECodeGenerator::addDependencies(Id _c)
|
|
|
|
{
|
2015-05-06 17:15:14 +00:00
|
|
|
if (m_classPositions.count(_c))
|
|
|
|
return; // it is already on the stack
|
2015-04-24 15:35:16 +00:00
|
|
|
if (m_neededBy.count(_c))
|
|
|
|
return; // we already computed the dependencies for _c
|
|
|
|
ExpressionClasses::Expression expr = m_expressionClasses.representative(_c);
|
2017-08-22 12:55:28 +00:00
|
|
|
assertThrow(expr.item, OptimizerException, "");
|
2019-11-22 17:02:12 +00:00
|
|
|
// If this exception happens, we need to find a different way to generate the
|
|
|
|
// compound expression.
|
|
|
|
assertThrow(expr.item->type() != UndefinedItem, ItemNotAvailableException, "Undefined item requested but not available.");
|
2015-04-24 15:35:16 +00:00
|
|
|
for (Id argument: expr.arguments)
|
|
|
|
{
|
|
|
|
addDependencies(argument);
|
|
|
|
m_neededBy.insert(make_pair(argument, _c));
|
|
|
|
}
|
2015-05-13 17:13:03 +00:00
|
|
|
if (expr.item && expr.item->type() == Operation && (
|
2015-04-24 15:35:16 +00:00
|
|
|
expr.item->instruction() == Instruction::SLOAD ||
|
|
|
|
expr.item->instruction() == Instruction::MLOAD ||
|
2017-05-10 07:48:00 +00:00
|
|
|
expr.item->instruction() == Instruction::KECCAK256
|
2015-04-24 15:35:16 +00:00
|
|
|
))
|
|
|
|
{
|
|
|
|
// this loads an unknown value from storage or memory and thus, in addition to its
|
|
|
|
// arguments, depends on all store operations to addresses where we do not know that
|
|
|
|
// they are different that occur before this load
|
|
|
|
StoreOperation::Target target = expr.item->instruction() == Instruction::SLOAD ?
|
|
|
|
StoreOperation::Storage : StoreOperation::Memory;
|
|
|
|
Id slotToLoadFrom = expr.arguments.at(0);
|
|
|
|
for (auto const& p: m_storeOperations)
|
|
|
|
{
|
|
|
|
if (p.first.first != target)
|
|
|
|
continue;
|
|
|
|
Id slot = p.first.second;
|
|
|
|
StoreOperations const& storeOps = p.second;
|
|
|
|
if (storeOps.front().sequenceNumber > expr.sequenceNumber)
|
|
|
|
continue;
|
|
|
|
bool knownToBeIndependent = false;
|
|
|
|
switch (expr.item->instruction())
|
|
|
|
{
|
|
|
|
case Instruction::SLOAD:
|
|
|
|
knownToBeIndependent = m_expressionClasses.knownToBeDifferent(slot, slotToLoadFrom);
|
|
|
|
break;
|
|
|
|
case Instruction::MLOAD:
|
|
|
|
knownToBeIndependent = m_expressionClasses.knownToBeDifferentBy32(slot, slotToLoadFrom);
|
|
|
|
break;
|
2017-05-10 07:48:00 +00:00
|
|
|
case Instruction::KECCAK256:
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
|
|
|
Id length = expr.arguments.at(1);
|
2015-09-08 15:11:02 +00:00
|
|
|
AssemblyItem offsetInstr(Instruction::SUB, expr.item->location());
|
2015-04-24 15:35:16 +00:00
|
|
|
Id offsetToStart = m_expressionClasses.find(offsetInstr, {slot, slotToLoadFrom});
|
|
|
|
u256 const* o = m_expressionClasses.knownConstant(offsetToStart);
|
|
|
|
u256 const* l = m_expressionClasses.knownConstant(length);
|
|
|
|
if (l && *l == 0)
|
|
|
|
knownToBeIndependent = true;
|
|
|
|
else if (o)
|
|
|
|
{
|
|
|
|
// We could get problems here if both *o and *l are larger than 2**254
|
|
|
|
// but it is probably ok for the optimizer to produce wrong code for such cases
|
|
|
|
// which cannot be executed anyway because of the non-payable price.
|
|
|
|
if (u2s(*o) <= -32)
|
|
|
|
knownToBeIndependent = true;
|
|
|
|
else if (l && u2s(*o) >= 0 && *o >= *l)
|
|
|
|
knownToBeIndependent = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (knownToBeIndependent)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// note that store and load never have the same sequence number
|
|
|
|
Id latestStore = storeOps.front().expression;
|
|
|
|
for (auto it = ++storeOps.begin(); it != storeOps.end(); ++it)
|
|
|
|
if (it->sequenceNumber < expr.sequenceNumber)
|
|
|
|
latestStore = it->expression;
|
|
|
|
addDependencies(latestStore);
|
|
|
|
m_neededBy.insert(make_pair(latestStore, _c));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 16:31:21 +00:00
|
|
|
void CSECodeGenerator::generateClassElement(Id _c, bool _allowSequenced)
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2015-05-07 16:31:21 +00:00
|
|
|
for (auto it: m_classPositions)
|
|
|
|
for (auto p: it.second)
|
|
|
|
if (p > m_stackHeight)
|
2017-01-10 16:38:42 +00:00
|
|
|
{
|
2015-05-07 16:31:21 +00:00
|
|
|
assertThrow(false, OptimizerException, "");
|
2017-01-10 16:38:42 +00:00
|
|
|
}
|
2015-04-24 15:35:16 +00:00
|
|
|
// do some cleanup
|
|
|
|
removeStackTopIfPossible();
|
|
|
|
|
|
|
|
if (m_classPositions.count(_c))
|
|
|
|
{
|
|
|
|
assertThrow(
|
2015-05-07 16:31:21 +00:00
|
|
|
!m_classPositions[_c].empty(),
|
2015-04-24 15:35:16 +00:00
|
|
|
OptimizerException,
|
|
|
|
"Element already removed but still needed."
|
|
|
|
);
|
2015-05-07 16:31:21 +00:00
|
|
|
return;
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
ExpressionClasses::Expression const& expr = m_expressionClasses.representative(_c);
|
|
|
|
assertThrow(
|
|
|
|
_allowSequenced || expr.sequenceNumber == 0,
|
|
|
|
OptimizerException,
|
|
|
|
"Sequence constrained operation requested out of sequence."
|
|
|
|
);
|
2015-05-12 19:27:04 +00:00
|
|
|
assertThrow(expr.item, OptimizerException, "Non-generated expression without item.");
|
2015-08-18 17:41:47 +00:00
|
|
|
assertThrow(
|
|
|
|
expr.item->type() != UndefinedItem,
|
|
|
|
OptimizerException,
|
|
|
|
"Undefined item requested but not available."
|
|
|
|
);
|
2015-04-24 15:35:16 +00:00
|
|
|
vector<Id> const& arguments = expr.arguments;
|
|
|
|
for (Id arg: boost::adaptors::reverse(arguments))
|
|
|
|
generateClassElement(arg);
|
|
|
|
|
2015-09-08 15:11:02 +00:00
|
|
|
SourceLocation const& itemLocation = expr.item->location();
|
2015-04-24 15:35:16 +00:00
|
|
|
// The arguments are somewhere on the stack now, so it remains to move them at the correct place.
|
|
|
|
// This is quite difficult as sometimes, the values also have to removed in this process
|
|
|
|
// (if canBeRemoved() returns true) and the two arguments can be equal. For now, this is
|
|
|
|
// implemented for every single case for combinations of up to two arguments manually.
|
|
|
|
if (arguments.size() == 1)
|
|
|
|
{
|
|
|
|
if (canBeRemoved(arguments[0], _c))
|
2015-09-08 15:11:02 +00:00
|
|
|
appendOrRemoveSwap(classElementPosition(arguments[0]), itemLocation);
|
2015-04-24 15:35:16 +00:00
|
|
|
else
|
2015-09-08 15:11:02 +00:00
|
|
|
appendDup(classElementPosition(arguments[0]), itemLocation);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
else if (arguments.size() == 2)
|
|
|
|
{
|
|
|
|
if (canBeRemoved(arguments[1], _c))
|
|
|
|
{
|
2015-09-08 15:11:02 +00:00
|
|
|
appendOrRemoveSwap(classElementPosition(arguments[1]), itemLocation);
|
2015-04-24 15:35:16 +00:00
|
|
|
if (arguments[0] == arguments[1])
|
2015-09-08 15:11:02 +00:00
|
|
|
appendDup(m_stackHeight, itemLocation);
|
2015-04-24 15:35:16 +00:00
|
|
|
else if (canBeRemoved(arguments[0], _c))
|
|
|
|
{
|
2015-09-08 15:11:02 +00:00
|
|
|
appendOrRemoveSwap(m_stackHeight - 1, itemLocation);
|
|
|
|
appendOrRemoveSwap(classElementPosition(arguments[0]), itemLocation);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
else
|
2015-09-08 15:11:02 +00:00
|
|
|
appendDup(classElementPosition(arguments[0]), itemLocation);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (arguments[0] == arguments[1])
|
|
|
|
{
|
2015-09-08 15:11:02 +00:00
|
|
|
appendDup(classElementPosition(arguments[0]), itemLocation);
|
|
|
|
appendDup(m_stackHeight, itemLocation);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
else if (canBeRemoved(arguments[0], _c))
|
|
|
|
{
|
2015-09-08 15:11:02 +00:00
|
|
|
appendOrRemoveSwap(classElementPosition(arguments[0]), itemLocation);
|
|
|
|
appendDup(classElementPosition(arguments[1]), itemLocation);
|
|
|
|
appendOrRemoveSwap(m_stackHeight - 1, itemLocation);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-08 15:11:02 +00:00
|
|
|
appendDup(classElementPosition(arguments[1]), itemLocation);
|
|
|
|
appendDup(classElementPosition(arguments[0]), itemLocation);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
assertThrow(
|
|
|
|
arguments.size() <= 2,
|
|
|
|
OptimizerException,
|
|
|
|
"Opcodes with more than two arguments not implemented yet."
|
|
|
|
);
|
|
|
|
for (size_t i = 0; i < arguments.size(); ++i)
|
2020-06-02 13:10:14 +00:00
|
|
|
assertThrow(
|
|
|
|
m_stack[m_stackHeight - static_cast<int>(i)] == arguments[i],
|
|
|
|
OptimizerException,
|
|
|
|
"Expected arguments not present."
|
|
|
|
);
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
while (SemanticInformation::isCommutativeOperation(*expr.item) &&
|
|
|
|
!m_generatedItems.empty() &&
|
|
|
|
m_generatedItems.back() == AssemblyItem(Instruction::SWAP1))
|
|
|
|
// this will not append a swap but remove the one that is already there
|
2015-09-08 15:11:02 +00:00
|
|
|
appendOrRemoveSwap(m_stackHeight - 1, itemLocation);
|
2015-04-24 15:35:16 +00:00
|
|
|
for (size_t i = 0; i < arguments.size(); ++i)
|
2015-05-07 16:31:21 +00:00
|
|
|
{
|
2020-06-02 13:10:14 +00:00
|
|
|
m_classPositions[m_stack[m_stackHeight - static_cast<int>(i)]].erase(m_stackHeight - static_cast<int>(i));
|
|
|
|
m_stack.erase(m_stackHeight - static_cast<int>(i));
|
2015-05-07 16:31:21 +00:00
|
|
|
}
|
2015-04-24 15:35:16 +00:00
|
|
|
appendItem(*expr.item);
|
|
|
|
if (expr.item->type() != Operation || instructionInfo(expr.item->instruction()).ret == 1)
|
|
|
|
{
|
|
|
|
m_stack[m_stackHeight] = _c;
|
2015-05-07 16:31:21 +00:00
|
|
|
m_classPositions[_c].insert(m_stackHeight);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assertThrow(
|
|
|
|
instructionInfo(expr.item->instruction()).ret == 0,
|
|
|
|
OptimizerException,
|
|
|
|
"Invalid number of return values."
|
|
|
|
);
|
2015-05-07 16:31:21 +00:00
|
|
|
m_classPositions[_c]; // ensure it is created to mark the expression as generated
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSECodeGenerator::classElementPosition(Id _id) const
|
|
|
|
{
|
|
|
|
assertThrow(
|
2015-05-07 16:31:21 +00:00
|
|
|
m_classPositions.count(_id) && !m_classPositions.at(_id).empty(),
|
2015-04-24 15:35:16 +00:00
|
|
|
OptimizerException,
|
|
|
|
"Element requested but is not present."
|
|
|
|
);
|
2015-05-07 16:31:21 +00:00
|
|
|
return *max_element(m_classPositions.at(_id).begin(), m_classPositions.at(_id).end());
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 17:15:14 +00:00
|
|
|
bool CSECodeGenerator::canBeRemoved(Id _element, Id _result, int _fromPosition)
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2015-05-06 17:15:14 +00:00
|
|
|
// Default for _fromPosition is the canonical position of the element.
|
|
|
|
if (_fromPosition == c_invalidPosition)
|
|
|
|
_fromPosition = classElementPosition(_element);
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2015-05-07 16:31:21 +00:00
|
|
|
bool haveCopy = m_classPositions.at(_element).size() > 1;
|
2015-05-06 17:15:14 +00:00
|
|
|
if (m_finalClasses.count(_element))
|
|
|
|
// It is part of the target stack. It can be removed if it is a copy that is not in the target position.
|
2015-05-07 16:31:21 +00:00
|
|
|
return haveCopy && (!m_targetStack.count(_fromPosition) || m_targetStack[_fromPosition] != _element);
|
|
|
|
else if (!haveCopy)
|
|
|
|
{
|
|
|
|
// Can be removed unless it is needed by a class that has not been computed yet.
|
|
|
|
// Note that m_classPositions also includes classes that were deleted in the meantime.
|
|
|
|
auto range = m_neededBy.equal_range(_element);
|
|
|
|
for (auto it = range.first; it != range.second; ++it)
|
|
|
|
if (it->second != _result && !m_classPositions.count(it->second))
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-24 15:35:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSECodeGenerator::removeStackTopIfPossible()
|
|
|
|
{
|
|
|
|
if (m_stack.empty())
|
|
|
|
return false;
|
|
|
|
assertThrow(m_stack.count(m_stackHeight) > 0, OptimizerException, "");
|
|
|
|
Id top = m_stack[m_stackHeight];
|
2015-05-06 17:15:14 +00:00
|
|
|
if (!canBeRemoved(top, Id(-1), m_stackHeight))
|
2015-04-24 15:35:16 +00:00
|
|
|
return false;
|
2015-05-07 16:31:21 +00:00
|
|
|
m_classPositions[m_stack[m_stackHeight]].erase(m_stackHeight);
|
2015-04-24 15:35:16 +00:00
|
|
|
m_stack.erase(m_stackHeight);
|
2015-05-07 16:31:21 +00:00
|
|
|
appendItem(AssemblyItem(Instruction::POP));
|
2015-04-24 15:35:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSECodeGenerator::appendDup(int _fromPosition, SourceLocation const& _location)
|
|
|
|
{
|
|
|
|
assertThrow(_fromPosition != c_invalidPosition, OptimizerException, "");
|
|
|
|
int instructionNum = 1 + m_stackHeight - _fromPosition;
|
2015-06-05 23:04:55 +00:00
|
|
|
assertThrow(instructionNum <= 16, StackTooDeepException, "Stack too deep, try removing local variables.");
|
2015-04-24 15:35:16 +00:00
|
|
|
assertThrow(1 <= instructionNum, OptimizerException, "Invalid stack access.");
|
2020-06-02 13:10:14 +00:00
|
|
|
appendItem(AssemblyItem(dupInstruction(static_cast<unsigned>(instructionNum)), _location));
|
2015-04-24 15:35:16 +00:00
|
|
|
m_stack[m_stackHeight] = m_stack[_fromPosition];
|
2015-05-07 16:31:21 +00:00
|
|
|
m_classPositions[m_stack[m_stackHeight]].insert(m_stackHeight);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSECodeGenerator::appendOrRemoveSwap(int _fromPosition, SourceLocation const& _location)
|
|
|
|
{
|
|
|
|
assertThrow(_fromPosition != c_invalidPosition, OptimizerException, "");
|
|
|
|
if (_fromPosition == m_stackHeight)
|
|
|
|
return;
|
|
|
|
int instructionNum = m_stackHeight - _fromPosition;
|
2015-06-05 23:04:55 +00:00
|
|
|
assertThrow(instructionNum <= 16, StackTooDeepException, "Stack too deep, try removing local variables.");
|
2015-04-24 15:35:16 +00:00
|
|
|
assertThrow(1 <= instructionNum, OptimizerException, "Invalid stack access.");
|
2020-06-02 13:10:14 +00:00
|
|
|
appendItem(AssemblyItem(swapInstruction(static_cast<unsigned>(instructionNum)), _location));
|
2015-05-07 16:31:21 +00:00
|
|
|
|
|
|
|
if (m_stack[m_stackHeight] != m_stack[_fromPosition])
|
|
|
|
{
|
|
|
|
m_classPositions[m_stack[m_stackHeight]].erase(m_stackHeight);
|
|
|
|
m_classPositions[m_stack[m_stackHeight]].insert(_fromPosition);
|
|
|
|
m_classPositions[m_stack[_fromPosition]].erase(_fromPosition);
|
|
|
|
m_classPositions[m_stack[_fromPosition]].insert(m_stackHeight);
|
|
|
|
swap(m_stack[m_stackHeight], m_stack[_fromPosition]);
|
|
|
|
}
|
2015-04-24 15:35:16 +00:00
|
|
|
if (m_generatedItems.size() >= 2 &&
|
|
|
|
SemanticInformation::isSwapInstruction(m_generatedItems.back()) &&
|
|
|
|
*(m_generatedItems.end() - 2) == m_generatedItems.back())
|
|
|
|
{
|
|
|
|
m_generatedItems.pop_back();
|
|
|
|
m_generatedItems.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSECodeGenerator::appendItem(AssemblyItem const& _item)
|
|
|
|
{
|
|
|
|
m_generatedItems.push_back(_item);
|
2020-06-02 13:10:14 +00:00
|
|
|
m_stackHeight += static_cast<int>(_item.deposit());
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|