Optimize multiplicity map

This commit is contained in:
Vectorized 2023-04-25 02:11:19 +00:00 committed by Daniel Kirchner
parent 7c323a1faa
commit 050927b026
2 changed files with 46 additions and 2 deletions

View File

@ -377,6 +377,50 @@ private:
} }
}; };
/// 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. /// Transforms @a _currentStack to @a _targetStack, invoking the provided shuffling operations.
/// Modifies @a _currentStack itself after each invocation of the shuffling operations. /// Modifies @a _currentStack itself after each invocation of the shuffling operations.
@ -395,7 +439,7 @@ void createStackLayout(Stack& _currentStack, Stack const& _targetStack, Swap _sw
Swap swapCallback; Swap swapCallback;
PushOrDup pushOrDupCallback; PushOrDup pushOrDupCallback;
Pop popCallback; Pop popCallback;
std::map<StackSlot, int> multiplicity; Multiplicity multiplicity;
ShuffleOperations( ShuffleOperations(
Stack& _currentStack, Stack& _currentStack,
Stack const& _targetStack, Stack const& _targetStack,

View File

@ -172,7 +172,7 @@ Stack createIdealLayout(Stack const& _operationOutput, Stack const& _post, Calla
vector<variant<PreviousSlot, StackSlot>>& layout; vector<variant<PreviousSlot, StackSlot>>& layout;
Stack const& post; Stack const& post;
std::set<StackSlot> outputs; std::set<StackSlot> outputs;
std::map<StackSlot, int> multiplicity; Multiplicity multiplicity;
Callable generateSlotOnTheFly; Callable generateSlotOnTheFly;
ShuffleOperations( ShuffleOperations(
vector<variant<PreviousSlot, StackSlot>>& _layout, vector<variant<PreviousSlot, StackSlot>>& _layout,