Skip to content

Commit

Permalink
Optimize multiplicity map
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized authored and ekpyron committed May 8, 2023
1 parent 7c323a1 commit 050927b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
46 changes: 45 additions & 1 deletion libyul/backends/evm/StackHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,50 @@ class Shuffler
}
};

/// A simple optimized map for mapping StackSlots to ints.
class Multiplicity
{
public:
int& operator[](StackSlot const& _slot)
{
if (auto* p = std::get_if<FunctionCallReturnLabelSlot>(&_slot))
return m_functionCallReturnLabelSlotMultiplicity[*p];
if (std::holds_alternative<FunctionReturnLabelSlot>(_slot))
return m_functionReturnLabelSlotMultiplicity;
if (auto* p = std::get_if<VariableSlot>(&_slot))
return m_variableSlotMultiplicity[*p];
if (auto* p = std::get_if<LiteralSlot>(&_slot))
return m_literalSlotMultiplicity[*p];
if (auto* p = std::get_if<TemporarySlot>(&_slot))
return m_temporarySlotMultiplicity[*p];
yulAssert(std::holds_alternative<JunkSlot>(_slot));
return m_junkSlotMultiplicity;
}

int at(StackSlot const& _slot) const
{
if (auto* p = std::get_if<FunctionCallReturnLabelSlot>(&_slot))
return m_functionCallReturnLabelSlotMultiplicity.at(*p);
if (std::holds_alternative<FunctionReturnLabelSlot>(_slot))
return m_functionReturnLabelSlotMultiplicity;
if (auto* p = std::get_if<VariableSlot>(&_slot))
return m_variableSlotMultiplicity.at(*p);
if (auto* p = std::get_if<LiteralSlot>(&_slot))
return m_literalSlotMultiplicity.at(*p);
if (auto* p = std::get_if<TemporarySlot>(&_slot))
return m_temporarySlotMultiplicity.at(*p);
yulAssert(std::holds_alternative<JunkSlot>(_slot));
return m_junkSlotMultiplicity;
}

private:
std::map<FunctionCallReturnLabelSlot, int> m_functionCallReturnLabelSlotMultiplicity;
int m_functionReturnLabelSlotMultiplicity = 0;
std::map<VariableSlot, int> m_variableSlotMultiplicity;
std::map<LiteralSlot, int> m_literalSlotMultiplicity;
std::map<TemporarySlot, int> m_temporarySlotMultiplicity;
int m_junkSlotMultiplicity = 0;
};

/// Transforms @a _currentStack to @a _targetStack, invoking the provided shuffling operations.
/// Modifies @a _currentStack itself after each invocation of the shuffling operations.
Expand All @@ -395,7 +439,7 @@ void createStackLayout(Stack& _currentStack, Stack const& _targetStack, Swap _sw
Swap swapCallback;
PushOrDup pushOrDupCallback;
Pop popCallback;
std::map<StackSlot, int> multiplicity;
Multiplicity multiplicity;
ShuffleOperations(
Stack& _currentStack,
Stack const& _targetStack,
Expand Down
2 changes: 1 addition & 1 deletion libyul/backends/evm/StackLayoutGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ Stack createIdealLayout(Stack const& _operationOutput, Stack const& _post, Calla
vector<variant<PreviousSlot, StackSlot>>& layout;
Stack const& post;
std::set<StackSlot> outputs;
std::map<StackSlot, int> multiplicity;
Multiplicity multiplicity;
Callable generateSlotOnTheFly;
ShuffleOperations(
vector<variant<PreviousSlot, StackSlot>>& _layout,
Expand Down

0 comments on commit 050927b

Please sign in to comment.