Skip resolving Keccak if AST contains msize.

Also a refactoring changing `m_optimizeMLoad` to `m_containsMSize`.
This commit is contained in:
hrkrshnn 2021-04-14 17:04:26 +02:00
parent 3d29ae73e7
commit b599235b8d
3 changed files with 26 additions and 6 deletions

View File

@ -48,7 +48,7 @@ void LoadResolver::run(OptimiserStepContext& _context, Block& _ast)
LoadResolver{ LoadResolver{
_context.dialect, _context.dialect,
SideEffectsPropagator::sideEffects(_context.dialect, CallGraphGenerator::callGraph(_ast)), SideEffectsPropagator::sideEffects(_context.dialect, CallGraphGenerator::callGraph(_ast)),
!containsMSize, containsMSize,
_context.expectedExecutionsPerDeployment _context.expectedExecutionsPerDeployment
}(_ast); }(_ast);
} }
@ -66,7 +66,7 @@ void LoadResolver::visit(Expression& _e)
break; break;
} }
if (funCall->functionName.name == m_dialect.hashFunction({})) if (!m_containsMSize && funCall->functionName.name == m_dialect.hashFunction({}))
tryEvaluateKeccak(_e, funCall->arguments); tryEvaluateKeccak(_e, funCall->arguments);
} }
} }
@ -87,7 +87,7 @@ void LoadResolver::tryResolve(
if (inScope(*value)) if (inScope(*value))
_e = Identifier{locationOf(_e), *value}; _e = Identifier{locationOf(_e), *value};
} }
else if (m_optimizeMLoad && _location == StoreLoadLocation::Memory) else if (!m_containsMSize && _location == StoreLoadLocation::Memory)
if (auto value = util::valueOrNullptr(m_memory, key)) if (auto value = util::valueOrNullptr(m_memory, key))
if (inScope(*value)) if (inScope(*value))
_e = Identifier{locationOf(_e), *value}; _e = Identifier{locationOf(_e), *value};

View File

@ -50,11 +50,11 @@ private:
LoadResolver( LoadResolver(
Dialect const& _dialect, Dialect const& _dialect,
std::map<YulString, SideEffects> _functionSideEffects, std::map<YulString, SideEffects> _functionSideEffects,
bool _optimizeMLoad, bool _containsMSize,
std::optional<size_t> _expectedExecutionsPerDeployment std::optional<size_t> _expectedExecutionsPerDeployment
): ):
DataFlowAnalyzer(_dialect, std::move(_functionSideEffects)), DataFlowAnalyzer(_dialect, std::move(_functionSideEffects)),
m_optimizeMLoad(_optimizeMLoad), m_containsMSize(_containsMSize),
m_expectedExecutionsPerDeployment(std::move(_expectedExecutionsPerDeployment)) m_expectedExecutionsPerDeployment(std::move(_expectedExecutionsPerDeployment))
{} {}
@ -75,7 +75,8 @@ protected:
std::vector<Expression> const& _arguments std::vector<Expression> const& _arguments
); );
bool m_optimizeMLoad = false; /// If the AST contains `msize`, then we skip resolving `mload` and `keccak256`.
bool m_containsMSize = false;
/// The --optimize-runs parameter. Value `nullopt` represents creation code. /// The --optimize-runs parameter. Value `nullopt` represents creation code.
std::optional<size_t> m_expectedExecutionsPerDeployment; std::optional<size_t> m_expectedExecutionsPerDeployment;
}; };

View File

@ -0,0 +1,19 @@
{
sstore(0, msize())
mstore(0, 10)
// Keccak-256 should not be evaluated because of the msize.
// Even though, though the code is likely equivalent,
// we skip steps if `msize` is present
let val := keccak256(0, 32)
sstore(1, val)
}
// ----
// step: loadResolver
//
// {
// let _1 := msize()
// let _2 := 0
// sstore(_2, _1)
// mstore(_2, 10)
// sstore(1, keccak256(_2, 32))
// }