Adding vardecl optimization for boolean types

This commit is contained in:
Djordje Mijovic 2020-02-18 16:11:21 +01:00 committed by chriseth
parent e75cace78d
commit a52c9af5b9
7 changed files with 52 additions and 7 deletions

View File

@ -23,6 +23,14 @@
using namespace solidity::yul;
using namespace std;
using namespace solidity::langutil;
Literal Dialect::zeroLiteralForType(solidity::yul::YulString _type) const
{
if (_type == boolType && _type != defaultType)
return {SourceLocation{}, LiteralKind::Boolean, "false"_yulstring, _type};
return {SourceLocation{}, LiteralKind::Number, "0"_yulstring, _type};
}
bool Dialect::validTypeForLiteral(LiteralKind _kind, YulString, YulString _type) const
{

View File

@ -34,6 +34,7 @@ namespace solidity::yul
class YulString;
using Type = YulString;
enum class LiteralKind;
struct Literal;
struct BuiltinFunction
{
@ -66,6 +67,8 @@ struct Dialect: boost::noncopyable
/// Should only be called if the type exists in the dialect at all.
virtual bool validTypeForLiteral(LiteralKind _kind, YulString _value, YulString _type) const;
virtual Literal zeroLiteralForType(YulString _type) const;
virtual std::set<YulString> fixedFunctionNames() const { return {}; }
Dialect() = default;

View File

@ -100,10 +100,10 @@ void UnusedPruner::operator()(Block& _block)
subtractReferences(ReferencesCounter::countReferences(*varDecl.value));
statement = Block{std::move(varDecl.location), {}};
}
else if (varDecl.variables.size() == 1 && m_dialect.discardFunction())
else if (varDecl.variables.size() == 1 && m_dialect.discardFunction(varDecl.variables.front().type))
statement = ExpressionStatement{varDecl.location, FunctionCall{
varDecl.location,
{varDecl.location, m_dialect.discardFunction()->name},
{varDecl.location, m_dialect.discardFunction(varDecl.variables.front().type)->name},
{*std::move(varDecl.value)}
}};
}

View File

@ -20,6 +20,7 @@
#include <libsolutil/CommonData.h>
#include <libsolutil/Visitor.h>
#include <libyul/Dialect.h>
using namespace std;
using namespace solidity;
@ -32,14 +33,14 @@ void VarDeclInitializer::operator()(Block& _block)
using OptionalStatements = std::optional<vector<Statement>>;
util::GenericVisitor visitor{
util::VisitorFallback<OptionalStatements>{},
[](VariableDeclaration& _varDecl) -> OptionalStatements
[this](VariableDeclaration& _varDecl) -> OptionalStatements
{
if (_varDecl.value)
return {};
Literal zero{{}, LiteralKind::Number, YulString{"0"}, {}};
if (_varDecl.variables.size() == 1)
{
_varDecl.value = make_unique<Expression>(std::move(zero));
_varDecl.value = make_unique<Expression>(m_dialect.zeroLiteralForType(_varDecl.variables.front().type));
return {};
}
else
@ -47,7 +48,10 @@ void VarDeclInitializer::operator()(Block& _block)
OptionalStatements ret{vector<Statement>{}};
langutil::SourceLocation loc{std::move(_varDecl.location)};
for (auto& var: _varDecl.variables)
ret->emplace_back(VariableDeclaration{loc, {std::move(var)}, make_unique<Expression>(zero)});
{
unique_ptr<Expression> expr = make_unique<Expression >(m_dialect.zeroLiteralForType(var.type));
ret->emplace_back(VariableDeclaration{loc, {std::move(var)}, std::move(expr)});
}
return ret;
}
}

View File

@ -34,9 +34,14 @@ class VarDeclInitializer: public ASTModifier
{
public:
static constexpr char const* name{"VarDeclInitializer"};
static void run(OptimiserStepContext&, Block& _ast) { VarDeclInitializer{}(_ast); }
static void run(OptimiserStepContext& _ctx, Block& _ast) { VarDeclInitializer{_ctx.dialect}(_ast); }
void operator()(Block& _block) override;
private:
explicit VarDeclInitializer(Dialect const& _dialect): m_dialect(_dialect) {}
Dialect const& m_dialect;
};
}

View File

@ -111,6 +111,8 @@ YulOptimizerTest::YulOptimizerTest(string const& _filename)
m_dialect = &WasmDialect::instance();
else if (dialectName == "evm")
m_dialect = &EVMDialect::strictAssemblyForEVMObjects(solidity::test::CommonOptions::get().evmVersion());
else if (dialectName == "evmTyped")
m_dialect = &EVMDialectTyped::instance(solidity::test::CommonOptions::get().evmVersion());
else
BOOST_THROW_EXCEPTION(runtime_error("Invalid dialect " + dialectName));

View File

@ -0,0 +1,23 @@
{
let a1
let a2: bool
let b1, b2: bool
function f(a:u256, b:u256, c:bool) -> r:bool, t {
let x1: bool, x2
}
}
// ====
// dialect: evmTyped
// step: varDeclInitializer
// ----
// {
// let a1 := 0
// let a2:bool := false
// let b1 := 0
// let b2:bool := false
// function f(a, b, c:bool) -> r:bool, t
// {
// let x1:bool := false
// let x2 := 0
// }
// }