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:
committed by
chriseth
parent
e75cace78d
commit
a52c9af5b9
@@ -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)}
|
||||
}};
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user