mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Adding vardecl optimization for boolean types
This commit is contained in:
parent
e75cace78d
commit
a52c9af5b9
@ -23,6 +23,14 @@
|
|||||||
|
|
||||||
using namespace solidity::yul;
|
using namespace solidity::yul;
|
||||||
using namespace std;
|
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
|
bool Dialect::validTypeForLiteral(LiteralKind _kind, YulString, YulString _type) const
|
||||||
{
|
{
|
||||||
|
@ -34,6 +34,7 @@ namespace solidity::yul
|
|||||||
class YulString;
|
class YulString;
|
||||||
using Type = YulString;
|
using Type = YulString;
|
||||||
enum class LiteralKind;
|
enum class LiteralKind;
|
||||||
|
struct Literal;
|
||||||
|
|
||||||
struct BuiltinFunction
|
struct BuiltinFunction
|
||||||
{
|
{
|
||||||
@ -66,6 +67,8 @@ struct Dialect: boost::noncopyable
|
|||||||
/// Should only be called if the type exists in the dialect at all.
|
/// Should only be called if the type exists in the dialect at all.
|
||||||
virtual bool validTypeForLiteral(LiteralKind _kind, YulString _value, YulString _type) const;
|
virtual bool validTypeForLiteral(LiteralKind _kind, YulString _value, YulString _type) const;
|
||||||
|
|
||||||
|
virtual Literal zeroLiteralForType(YulString _type) const;
|
||||||
|
|
||||||
virtual std::set<YulString> fixedFunctionNames() const { return {}; }
|
virtual std::set<YulString> fixedFunctionNames() const { return {}; }
|
||||||
|
|
||||||
Dialect() = default;
|
Dialect() = default;
|
||||||
|
@ -100,10 +100,10 @@ void UnusedPruner::operator()(Block& _block)
|
|||||||
subtractReferences(ReferencesCounter::countReferences(*varDecl.value));
|
subtractReferences(ReferencesCounter::countReferences(*varDecl.value));
|
||||||
statement = Block{std::move(varDecl.location), {}};
|
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{
|
statement = ExpressionStatement{varDecl.location, FunctionCall{
|
||||||
varDecl.location,
|
varDecl.location,
|
||||||
{varDecl.location, m_dialect.discardFunction()->name},
|
{varDecl.location, m_dialect.discardFunction(varDecl.variables.front().type)->name},
|
||||||
{*std::move(varDecl.value)}
|
{*std::move(varDecl.value)}
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
|
|
||||||
#include <libsolutil/CommonData.h>
|
#include <libsolutil/CommonData.h>
|
||||||
#include <libsolutil/Visitor.h>
|
#include <libsolutil/Visitor.h>
|
||||||
|
#include <libyul/Dialect.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace solidity;
|
using namespace solidity;
|
||||||
@ -32,14 +33,14 @@ void VarDeclInitializer::operator()(Block& _block)
|
|||||||
using OptionalStatements = std::optional<vector<Statement>>;
|
using OptionalStatements = std::optional<vector<Statement>>;
|
||||||
util::GenericVisitor visitor{
|
util::GenericVisitor visitor{
|
||||||
util::VisitorFallback<OptionalStatements>{},
|
util::VisitorFallback<OptionalStatements>{},
|
||||||
[](VariableDeclaration& _varDecl) -> OptionalStatements
|
[this](VariableDeclaration& _varDecl) -> OptionalStatements
|
||||||
{
|
{
|
||||||
if (_varDecl.value)
|
if (_varDecl.value)
|
||||||
return {};
|
return {};
|
||||||
Literal zero{{}, LiteralKind::Number, YulString{"0"}, {}};
|
|
||||||
if (_varDecl.variables.size() == 1)
|
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 {};
|
return {};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -47,7 +48,10 @@ void VarDeclInitializer::operator()(Block& _block)
|
|||||||
OptionalStatements ret{vector<Statement>{}};
|
OptionalStatements ret{vector<Statement>{}};
|
||||||
langutil::SourceLocation loc{std::move(_varDecl.location)};
|
langutil::SourceLocation loc{std::move(_varDecl.location)};
|
||||||
for (auto& var: _varDecl.variables)
|
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;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,9 +34,14 @@ class VarDeclInitializer: public ASTModifier
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static constexpr char const* name{"VarDeclInitializer"};
|
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;
|
void operator()(Block& _block) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
explicit VarDeclInitializer(Dialect const& _dialect): m_dialect(_dialect) {}
|
||||||
|
|
||||||
|
Dialect const& m_dialect;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -111,6 +111,8 @@ YulOptimizerTest::YulOptimizerTest(string const& _filename)
|
|||||||
m_dialect = &WasmDialect::instance();
|
m_dialect = &WasmDialect::instance();
|
||||||
else if (dialectName == "evm")
|
else if (dialectName == "evm")
|
||||||
m_dialect = &EVMDialect::strictAssemblyForEVMObjects(solidity::test::CommonOptions::get().evmVersion());
|
m_dialect = &EVMDialect::strictAssemblyForEVMObjects(solidity::test::CommonOptions::get().evmVersion());
|
||||||
|
else if (dialectName == "evmTyped")
|
||||||
|
m_dialect = &EVMDialectTyped::instance(solidity::test::CommonOptions::get().evmVersion());
|
||||||
else
|
else
|
||||||
BOOST_THROW_EXCEPTION(runtime_error("Invalid dialect " + dialectName));
|
BOOST_THROW_EXCEPTION(runtime_error("Invalid dialect " + dialectName));
|
||||||
|
|
||||||
|
23
test/libyul/yulOptimizerTests/varDeclInitializer/typed.yul
Normal file
23
test/libyul/yulOptimizerTests/varDeclInitializer/typed.yul
Normal 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
|
||||||
|
// }
|
||||||
|
// }
|
Loading…
Reference in New Issue
Block a user