From 04c8a7f0c6b74170d39d4210e9e7ea130f3fae3a Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 9 Mar 2022 09:18:32 +0100 Subject: [PATCH] Less destructive switch processing. --- libyul/optimiser/DataFlowAnalyzer.cpp | 9 ++++-- .../fullSuite/loadrestest.yul | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 test/libyul/yulOptimizerTests/fullSuite/loadrestest.yul diff --git a/libyul/optimiser/DataFlowAnalyzer.cpp b/libyul/optimiser/DataFlowAnalyzer.cpp index af25cde59..19ba2822d 100644 --- a/libyul/optimiser/DataFlowAnalyzer.cpp +++ b/libyul/optimiser/DataFlowAnalyzer.cpp @@ -136,16 +136,18 @@ void DataFlowAnalyzer::operator()(Switch& _switch) unordered_map storage = m_storage; unordered_map memory = m_memory; (*this)(_case.body); + //TODO throw away knowledge if it is terminating joinKnowledge(storage, memory); set variables = assignedVariableNames(_case.body); assignedVariables += variables; // This is a little too destructive, we could retain the old values. clearValues(variables); - clearKnowledgeIfInvalidated(_case.body); + //clearKnowledgeIfInvalidated(_case.body); } - for (auto& _case: _switch.cases) - clearKnowledgeIfInvalidated(_case.body); + // TODO Handle special case where we have a default switch body? + //for (auto& _case: _switch.cases) +// clearKnowledgeIfInvalidated(_case.body); clearValues(assignedVariables); } @@ -341,6 +343,7 @@ void DataFlowAnalyzer::clearKnowledgeIfInvalidated(Block const& _block) void DataFlowAnalyzer::clearKnowledgeIfInvalidated(Expression const& _expr) { + // TODO with the extended side-effects, this could also be more precise now. SideEffectsCollector sideEffects(m_dialect, _expr, &m_functionSideEffects); if (sideEffects.invalidatesStorage()) m_storage.clear(); diff --git a/test/libyul/yulOptimizerTests/fullSuite/loadrestest.yul b/test/libyul/yulOptimizerTests/fullSuite/loadrestest.yul new file mode 100644 index 000000000..8cd05ece3 --- /dev/null +++ b/test/libyul/yulOptimizerTests/fullSuite/loadrestest.yul @@ -0,0 +1,28 @@ +{ + let _1 := memoryguard(0x80) + mstore(64, _1) + let _2 := 0 + switch shr(224, calldataload(_2)) + case 0x06661abd { + mstore(_1, sload(_2)) + return(_1, 32) + } + case 0xc2985578 { + return(mload(64), _2) + } +} +// ---- +// step: fullSuite +// +// { +// { +// let _1 := memoryguard(0x80) +// mstore(64, _1) +// switch shr(224, calldataload(0)) +// case 0x06661abd { +// mstore(_1, sload(0)) +// return(_1, 32) +// } +// case 0xc2985578 { return(mload(64), 0) } +// } +// }