diff --git a/libevmasm/Assembly.cpp b/libevmasm/Assembly.cpp index 7f7e1b9c3..2024b1e91 100644 --- a/libevmasm/Assembly.cpp +++ b/libevmasm/Assembly.cpp @@ -322,7 +322,8 @@ Assembly& Assembly::optimise(bool _enable, bool _isCreation, size_t _runs) count++; { - ControlFlowGraph cfg(m_items); + // Control flow graph that resets knowledge at path joins. + ControlFlowGraph cfg(m_items, false); AssemblyItems optimisedItems; for (BasicBlock const& block: cfg.optimisedBlocks()) { diff --git a/libevmasm/ControlFlowGraph.cpp b/libevmasm/ControlFlowGraph.cpp index fc2144c74..61b24e4b6 100644 --- a/libevmasm/ControlFlowGraph.cpp +++ b/libevmasm/ControlFlowGraph.cpp @@ -250,7 +250,10 @@ void ControlFlowGraph::gatherKnowledge() KnownStatePointer state = item.state; if (block.startState) { - state->reduceToCommonKnowledge(*block.startState, !item.blocksSeen.count(item.blockId)); + if (m_joinKnowledge) + state->reduceToCommonKnowledge(*block.startState, !item.blocksSeen.count(item.blockId)); + else + state->reset(); if (*state == *block.startState) continue; } diff --git a/libevmasm/ControlFlowGraph.h b/libevmasm/ControlFlowGraph.h index 4480ba491..a6a9df8e0 100644 --- a/libevmasm/ControlFlowGraph.h +++ b/libevmasm/ControlFlowGraph.h @@ -94,7 +94,11 @@ class ControlFlowGraph public: /// Initializes the control flow graph. /// @a _items has to persist across the usage of this class. - ControlFlowGraph(AssemblyItems const& _items): m_items(_items) {} + /// @a _joinKnowledge if true, reduces state knowledge to common base at the join of two paths + explicit ControlFlowGraph(AssemblyItems const& _items, bool _joinKnowledge = true): + m_items(_items), + m_joinKnowledge(_joinKnowledge) + {} /// @returns vector of basic blocks in the order they should be used in the final code. /// Should be called only once. BasicBlocks optimisedBlocks(); @@ -112,6 +116,7 @@ private: unsigned m_lastUsedId = 0; AssemblyItems const& m_items; + bool m_joinKnowledge; std::map m_blocks; };