Introduce LiteralRematerializer and thus simplify StructuralSimplifier.

This commit is contained in:
chriseth
2019-09-11 19:50:24 +02:00
parent a064e0fc97
commit fcfe829534
10 changed files with 152 additions and 145 deletions
+16 -48
View File
@@ -61,29 +61,7 @@ OptionalStatements replaceConstArgSwitch(Switch& _switchStmt, u256 const& _const
void StructuralSimplifier::operator()(Block& _block)
{
pushScope(false);
simplify(_block.statements);
popScope();
}
boost::optional<dev::u256> StructuralSimplifier::hasLiteralValue(Expression const& _expression) const
{
Expression const* expr = &_expression;
if (expr->type() == typeid(Identifier))
{
Identifier const& ident = boost::get<Identifier>(*expr);
if (m_value.count(ident.name))
expr = m_value.at(ident.name);
}
if (expr && expr->type() == typeid(Literal))
{
Literal const& literal = boost::get<Literal>(*expr);
return valueOfLiteral(literal);
}
return boost::optional<u256>();
}
void StructuralSimplifier::simplify(std::vector<yul::Statement>& _statements)
@@ -124,34 +102,24 @@ void StructuralSimplifier::simplify(std::vector<yul::Statement>& _statements)
bool StructuralSimplifier::expressionAlwaysTrue(Expression const& _expression)
{
return boost::apply_visitor(GenericFallbackReturnsVisitor<bool, Identifier const, Literal const>(
[&](Identifier const& _identifier) -> bool {
if (auto expr = m_value[_identifier.name])
return expressionAlwaysTrue(*expr);
return false;
},
[](Literal const& _literal) -> bool {
return
(_literal.kind == LiteralKind::Boolean && _literal.value == "true"_yulstring) ||
(_literal.kind == LiteralKind::Number && valueOfNumberLiteral(_literal) != u256(0))
;
}
), _expression);
if (boost::optional<u256> value = hasLiteralValue(_expression))
return *value != 0;
else
return false;
}
bool StructuralSimplifier::expressionAlwaysFalse(Expression const& _expression)
{
return boost::apply_visitor(GenericFallbackReturnsVisitor<bool, Identifier const, Literal const>(
[&](Identifier const& _identifier) -> bool {
if (auto expr = m_value[_identifier.name])
return expressionAlwaysFalse(*expr);
return false;
},
[](Literal const& _literal) -> bool {
return
(_literal.kind == LiteralKind::Boolean && _literal.value == "false"_yulstring) ||
(_literal.kind == LiteralKind::Number && valueOfNumberLiteral(_literal) == u256(0))
;
}
), _expression);
if (boost::optional<u256> value = hasLiteralValue(_expression))
return *value == 0;
else
return false;
}
boost::optional<dev::u256> StructuralSimplifier::hasLiteralValue(Expression const& _expression) const
{
if (_expression.type() == typeid(Literal))
return valueOfLiteral(boost::get<Literal>(_expression));
else
return boost::optional<u256>();
}